Primary Key & Index

Source: Internet
Author: User

Primary key, Primarykey. Indexed, index. The primary key usually corresponds to a unique key. Primary key corresponds to a unique, which is essentially meaningless, but a constraint.
However, when establishing primary key and unique key, the corresponding index index will be established, which is the real meaning.
In a way, the primary key =unique+not null. The primary key corresponds to the primary key index, similar to the unique key corresponding to the unique index. The primary key differs from the unique index in nature. A primary key is a constraint, and a unique index is an index. Another important difference is that the primary key can be associated with other tables as foreign keys. And the index is not.
The primary key index is followed by a unique index. Primary key index Its value uniquely identifies each row in the table. A unique index whose indexed value cannot exist with the same value. A primary key index is a specific type of unique index. The index requires that each value in the primary key be unique.
In contrast, a federated primary key corresponds to a federated unique index. Analogy to the difference between a primary key and an index. A federated primary key, which consists of multiple column literals, will automatically create a primary key index.
The primary key, can avoid the repetition of the data more, so as to guarantee the uniqueness of the corresponding data, it becomes the unique identification of the data. A federated unique index that corresponds to a unique index.
The purpose of indexing is to speed up the search for data. But it takes up physical space. At the same time slow down the database and the efficiency of change.
Suitable for index-based scenarios:
1 frequently need to search the column, you can speed up the search. 2 The columns are often used on connected columns, which are mainly foreign keys that can speed up the connection
Duplicate entry is a key conflict, which can make a primary key or a unique key. Because the primary key and the unique key cannot be duplicated, the conflicting error is reported once repeated. In either case, the primary key or unique key-that is, the federated primary Key or the Federated unique index-will prompt duplicate to entry the error once the data is duplicated.




Unique index
A unique index does not allow two rows to have the same index value.
If duplicate key values exist in the existing data, most databases do not allow the newly created unique index to be saved with the table. The database also rejects this data when the new data duplicates the key values in the table. For example, if a unique index is created on the employee's last name (lname) column in the Employee table, all employees cannot have the same surname.


Primary key Index
A primary key index is a special type of unique index.
A database table typically has a column or combination of columns whose values are used to uniquely identify each row in the table. This column is called the primary key of the table.
Defining a primary key for a table in a database diagram automatically creates a primary key index, which is a special type of unique index. The primary key index requires that each value in the primary key be unique. When a primary key index is used in a query, it also allows for fast access to the data.


Some of their comparisons:
(1) for the main jian/unique constraint, Oracle/sql server/mysql and so on will automatically establish a unique index;
(2) The primary key does not necessarily contain only one field, so it is necessary if you Jianjian a unique index in one of the primary Key's characters;
(3) The primary health can be used as a foreign health, the only index is not;
(4) The main health cannot be empty, the unique index can be;
(5) The main health is also a combination of multiple fields;
(6) The primary key differs from the unique index:
A. There is not a null attribute;
B. There can only be one per table.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Primary Key & Index

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.