Several differences between the primary key and the index of SQL Server database

Source: Internet
Author: User
Tags one table

When we use SQL server databases , we often create primary keys and indexes , so what is the difference between a primary key and an index? In this paper, we mainly introduce the difference between primary key and index.

The difference between a primary key and an index is as follows:

The primary key is an index, but the index is not necessarily a primary key.

The primary key is unique, and only the uniqueness index is unique; the value of the primary key cannot be null and cannot be duplicated. The index can create deletes dynamically in the program. It can also be any ordered field.

If, in a table, columns A, B, and C are set as primary keys, when A,B,C is set to index, A,B,C is set as the primary key, the database is automatically indexed, no index is required, and when the column a,b,c,d,e is indexed, the d,e cannot be indexed only. All indexed columns must be included in the index: A,b,c,d,e.

Primary key, foreign key, index:

Index just to sort and speed up browsing! You can also testify on the table data! A table can have only one primary key, and the foreign key is almost the same as the primary key! Both can connect the table and define the rules!

The primary key, which must be available in each table, is unique and uniquely identifies each record.

Foreign key, the foreign key in one table can only be the primary key in the other table, in general, to delete the primary key in a table must first ensure that there is no same foreign key in the other table (that is, the primary key in the table does not have a foreign key associated with it)

Indexes only speed up queries, sorting speed, primary key and foreign key definitions can guarantee data integrity, eliminating the writing of certain triggers. The primary key is best defined on each table, and the small database can be used with a unique index.

Each table should have a primary key to ensure data integrity.   The foreign key of the table is the primary key of the other table, and the foreign key ties the two tables together. Indexes simply increase the speed of query ordering.

Several differences between the primary key and the index of SQL Server database

Related Article

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.