MySQL - primary key PK unique key,key PK index

primary key PK unique key 总结

  1. primary key = unique + not null  primary key can not duplicate each field value is null, UNIQUE can be empty, non-empty field will not be repeated
  2. unique  one or more defined fields, Primary Key  single primary key field or fields primary key
  3. primary key of a table can have only one, UNIQUE a table can have multiple
  4. Logically designed primary key used as a record identifier, unique to guarantee uniqueness, but when they will go to create the appropriate create a unique index, can be used to make sql optimization.

key PK index summary

When you create a primary key, automatically create a primary key index. Create a unique key when automatically create a unique index:

  1. There are two key functions: constraint index index +
  2. index is only one role: Auxiliary query

Key role of two

The key index mysql and somewhat confusing, and other key individual keyword combination key (primary key) represents the actual meaning different, which is actually observing and understanding of the architecture of the database.

key database is a physical structure , which comprises two layers significance and role,

  • One constraint (emphasis on the structural integrity of the database constraints and specifications)
  • Second index (assisted query used)

包括primary key, unique key, foreign key 等。

primary key 

primary key  has two functions, one binding effect (constraint), it is used to regulate a primary key storage and uniqueness , but this also creates a primary key index on Key;    

                          PRIMARY KEY constraints: uniquely identifies each record in the database table;

                                                                 Primary keys must contain unique values;

                                                                 Primary key column can not contain a NULL value;

                                                                 Each table should have a primary key, and each table can have only one primary key. (PRIMARY KEY has automatically defined UNIQUE constraint)

unique key

unique key  has two functions, one binding effect (constraint), standardized data uniqueness , but also on the establishment of a unique index key;

UNIQUE constraint: uniquely identifies each record in the database table.
                                                    UNIQUE and PRIMARY KEY constraints are column or set of columns provides uniqueness guaranteed.
                                                    (Each table can have multiple UNIQUE constraints, but each table can have only one PRIMARY KEY constraints)

foreign key

foreign key  also has two roles, one is referenced binding effect (constraint), standardized data integrity, but also established a index on the key;

 

Visible, mysql is of key significance constraint and index the same time, there may be the difference between this and other database performance.

(Oracle establishing at least on the foreign key, does not automatically index), thus creating key has the following ways:
(1) at the field level in a key established manner, as Table T Create (int Not null Primary key ID);
( 2) in the table level in constraint established manner, such as Create table T (ID int, cONSTRAINT pk_t_id a PRIMARY key (ID));
(. 3) at the table level in a key established manner, such as create table t (id int, primary key (id ));

other key to create a similar, but regardless of that way, not only the establishment of a constraint, but also the establishment of index, but the index is the key constraint or use.

 

Index role

index is the physical structure of the database, it is only secondary query, the structure will be stored in another table space (innodb table space in mysql) when it is created in a similar directory. Index to be classified, it is divided into prefix indexes, full-text indexing;
therefore, the index is only an index, it will not be bound to conduct field index (that is key to do). As, Create Table T (int ID, index inx_tx_id (ID) );

 

Index Classification

Index classification , divided into:

  1. Primary key index (must be specified as "PRIMARY KEY", no PRIMARY Index),
  2. The only index (unique index, usually written as unique key),
  3. General index (index, only this kind is pure index)

The most important is, no matter how described, need to understand the index is pure index, or be used as key (such as: unique index, unique key and primary key), then there will be two key significance as if from two or kind of role.

 

reference

Dandelion on snow  https://www.cnblogs.com/zjfjava/p/6922494.html

Guess you like

Origin www.cnblogs.com/frankcui/p/12006515.html