Home Database Mysql Tutorial 中文Access2000速成教程--1.6 定义“主键”

中文Access2000速成教程--1.6 定义“主键”

Jun 07, 2016 pm 06:07 PM

中文Access2000速成教程--1.6 定义“主键”


第六讲 定义“主键” 在中文Access 2000中,可以建立一个庞大的数据信息库,而要将这些分布于不同表中的数据作为一个“库”来使用,就需要为各表建立好“主键”,从而建立起一个关系型数据库系统。这种系统的特点是可以使用查询、窗体和报表快速查找数据,并能组合保存来自各个不同表中信息。如果要做到这一点,每一张表就应该包含相同的一个或一组字段,它些都是保存在表中的、每一条记录的唯一标识,即表的“主键”,通常需要在建立数据表时一并制定,以便节省时间,因此本章在这里插入了这一小节。


中文Access 2000允许定义三种类型的主键:自动编号、单字段及多字段,它们的特点如下所述。

1.自动编号主键


在前面的操作中,您一定注意到了当向表中添加一行记录时,中文Access 2000总是会自动设置编号,即使您指定了不同的编号,这个软件也会让您将自动编号字段设置为自动输入连续数字的编号,从而将自动编号字段指定为表的主键,这是创建主键的最简单方法,相信您一定还记得前面就是这样做。


其实,如果在保存新建的表之前没有设置主键,那么中文Access 2000将询问是否要创建主键。如果回答为“是”,就将创建“自动编号主键”。这种建立主键的方法可以应用于任何中文Access 2000的表中,而在中文Visual FoxPro 6中不会如此便利,初学者掌握起来是比较困难的。

注:指定了表的主键之后,为确保其唯一性,中文Access 2000将防止在主键字段中输入重复值或 Null。Null表示字段中没有值,或者是未知值。

2.单字段主键


如果某些信息相关的表中拥有相同的字段,而且所包含的都是唯一的值,如ID 号或零件编号,那么就可以将该字段指定为主键。如果选择的字段有重复值或 Null 值,Access 2000将不会设置其主键,为此可运行“查找重复项”查询可以找出包含重复数据的记录,然后编辑修改它。


注意:如果通过编辑数据仍然不容易消除这些重复项,可以添加一个自动编号字段并将它设置为主键,或定义多字段主键,这是一个很有用的操作技巧。

3.多字段主键


在不能保证任何单字段都包含唯一值时,可以将两个或更多的字段指定为主键。这种情况最常出现在用于多对多关系中关联另外两个表的表。“多对多关系”是关系数据库中较难理解的概念,但却非常实用,它说明如A 表中的记录能与 B 表中的许多行记录匹配,并且B表中的记录也能与A表中的许多行记录匹配。此关系的类型仅能通过定义第三张表(称作“联结表”)的方法来实现,其主键包含二个字段,即来源于A和B两张表的外部键。多对多关系实际上是使用第三张表的两个一对多关系。例如,“订单”表和“产品”表就可能有一个多对多的关系,它是通过“订单明细”表中两个一对多关系来创建的。

注:“一对多关系”是最常用的关系类型。在这种关系中,A表中的一行记录能与B表中的许多行记录匹配,但是在B表中的一行记录仅能与A表中的一行记录匹配。若A表中的一行记录只与B表中的另一行记录匹配,这就是“一对一关系”。因此可将“一对一关系”看成是“一对多关系”的特例。多对多关系、一对一关系、一对多关系,是应用关系数据库的基本概念。

若要指定或者更改主键,可以在“设计”视图中打开相应的表,然后从“行选定器”中选择所要定义为主键的那一个或多个字段,接着单击“设计视图”工具栏中的“主键”按钮即可,参见图29。若要删除一个主键的话,也只需要在“行选定器”中选定它,接着单击“设计视图”工具栏中的“主键”按钮,参见图32。





图29单击这个按钮

如果您没有在屏幕上看到“设计视图”工具栏,那么就不能使用“主键”按钮。为了让它显示出来,需要单击“数据库视图”工具栏中的“视图”下拉按钮,然后从下拉菜单中选择“设计视图”命令,它就会显示出来,参见图32与9-33。





图30单击这个下拉按钮





图31选择这一条命令






图32“行选定器”的位置
注:选定一个字段时,只需要单击“行选定器”中的相应方框即可。若要选定多个字段,可请按Ctrl键后,在“行选定器”中分别单击它们的方框,参见图34
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reduce the use of MySQL memory in Docker Reduce the use of MySQL memory in Docker Mar 04, 2025 pm 03:52 PM

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

How to solve the problem of mysql cannot open shared library How to solve the problem of mysql cannot open shared library Mar 04, 2025 pm 04:01 PM

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

Run MySQl in Linux (with/without podman container with phpmyadmin) Run MySQl in Linux (with/without podman container with phpmyadmin) Mar 04, 2025 pm 03:54 PM

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

What is SQLite? Comprehensive overview What is SQLite? Comprehensive overview Mar 04, 2025 pm 03:55 PM

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

Running multiple MySQL versions on MacOS: A step-by-step guide Running multiple MySQL versions on MacOS: A step-by-step guide Mar 04, 2025 pm 03:49 PM

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

See all articles