Primary key
a database primary key is a combination of one column or column in a table whose value uniquely identifies each row in the table. Such a column or columns is called the table's primary key, which enforces the entity integrity of the table. When you create or change a table, you can Create a primary key by defining the PRIMARY key constraint. A table can have only one PRIMARY key constraint, and PRIMARY key columns in the constraint cannot accept null values. Because the PRIMARY KEY constraint ensures unique data, it is often used to define identity columns.
the role of the primary key
The main functions of the primary key are as follows:
(1) Ensure the integrity of the entity;
(2) speed up the operation of the database;
(3) when a new record is added to a table, the database automatically checks the primary key value of the new record and does not allow the value to be duplicated with the primary key value of other records;
(4) The database automatically displays the records in the table in the order of the primary key values. If no primary key is defined, records in the table are displayed in the order in which they are entered.
The primary key features: Uniqueness, non-nullability.
Example of setting a primary KEY statement:
Code int PRIMARY key, primary key cannot be empty, cannot be duplicated, ensures uniqueness
Example of setting a self-growing primary KEY statement:
The code int primary Key identity ( 1) starts at 1 each time , adding values you do not need to add this column
To set a foreign key:
Right-click on the table where you want to set the foreign key, select design, and right-click the column name where you want to set the foreign key, such as:
Select Relationship Click, the dialog box appears, click Add, click the ellipsis following the table and column specification, such as:
In the interface that appears, do the following:
Click OK, then click OK, the operation is successful.
Database development Basics-Lesson Plans-7 primary key foreign key