Consideration on primary keys and Foreign keys of SQL Server databases

Source: Internet
Author: User
What is a primary key: a primary key refers to a combination of columns or columns in a table, which uniquely identifies each row in the table. Such a column or multiple columns can be the primary key of the table, through which the Object Integrity of the table can be forced. When creating or changing a table, you can create a primary key by defining the PRIMARYKEY constraint. A table can only have one primary key constraint, and

What is a primary key: a primary key refers to a combination of columns or columns in a table, which uniquely identifies each row in the table. Such a column or multiple columns can be the primary key of the table, through which the Object Integrity of the table can be forced. When creating or changing a table, you can create a primary key by defining the primary key constraint. A table can only have one primary key constraint.

What is a primary key:

A primary key is a combination of columns or columns in a table. Its values can uniquely identify each row in the table. Such a column or multiple columns can be the primary key of the table, through which the Object Integrity of the table can be forced. When creating or changing a table, you can create a primary key by defining the primary key constraint. A table can only have one primary key constraint, and the column in the primary key constraint cannot be null. The primary key constraint ensures unique data, once the column is defined.

Do each table have to define a primary key?

I personally think that this problem depends on the functions of our table, which are used to store a large amount of data and need to be frequently queried. Setting up a primary key can speed up the query efficiency and reduce the burden on the server, however, if you do not have this requirement, creating a primary key consumes resources. In this case, you do not need to create a primary key.

Some people think that although the primary key is not mandatory in some databases, it is best to set a primary key for each table. Whether it is a primary key or a composite primary key, it represents the integrity of the table structure, the table record must have a unique field. The primary key is mainly used for foreign key Association of other tables and modification and deletion of this record.

Primary key function:

1. From the above definition, we can see that the primary key uniquely identifies a row of data in the database table.

2. As an object that can be effectively referenced by a foreign key.

Primary Key design principles:

1. The primary key should be meaningless to the user, for example, the login id when the user logs on to a system. This situation is impossible: the "Student table" needs to support such a requirement. "After a student logs out, he can reactivate his/her information and keep his/her number consistent with the original one ", in this case, the primary key cannot meet the requirements.

2. The uniqueness principle does not need to be discussed.

3. non-null: the value of the primary key cannot be repeated or empty.

What is a foreign key:

Foreign keys are established between tables to facilitate programming.

Foreign key features:

1. Foreign keys ensure data integrity

2. The use of foreign keys is simple and intuitive, and can be directly reflected in the data model, whether it is design, maintenance, and so on.

3. Foreign keys may cause some trouble when debugging the program.

How to insert a foreign key:

As shown in, there are three tables in the database. Next we will set the relationship between the news and category tables.


In the email, click "database relationship diagram" and select "new database relationship diagram"

The above "Add Table" appears"

Select the name of the table to be added and add all three tables to the table, as shown in)

We set the categoryId of news to the foreign key of the category Id.


Click Add


Select the three dots on the left of "table and column specification" above, as shown in.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.