The keys and indexes in mysql

A primary key index relationship
1. Primary: role of unique primary key uniquely identifies a data line is in the table. Into a single master key and the primary key:
a single main key: only one row can be uniquely identified.
The primary key: When using an already can not uniquely identify a line, we must use multiple columns that uniquely identifies a row, is the primary key. Co-fields-the same at the same time can not be

2. Index: Index of action is to increase the speed of data retrieval, and combined into a single index Index:
single index: except that a column of data as an index, the index index is the default, this column can contain duplicate data; If a column does not exist preferably duplicate data provided in the form of unique index, speed index faster than the index in the text data to be used fulltext index.
Joint index: In order to further improve the retrieval speed, each retrieval needs simultaneously with multiple columns, you can set this joint multi-column index, improve indexing speed, according to multiple columns is unique, and unique is also divided into index index index.
Care must be taken to use the joint index: You can not collapse field use, such as abc joint index only a | ab | abc owe the joint index field.

3. The primary key must be indexed, but the index is not necessarily the primary key. A table can have a primary key or a composite key, but can have multiple indexes.
Primary key field must not be empty, but the index field can be empty.

Second, the relationship between the primary key and the foreign key in
1. is the foreign key values in the table so that the field in a constraint in the reference table selected Flag field
2. Create conditions foreign key
is the primary key field must be reference
reference field and a Referring field must be the same data type
table storage engine must be innodb

Third, the syntax
1. Create a command format of the primary key

1.1 Create a primary key when creating a table
create table table name (field list, primary key (need to set the primary key field name) auto_increment); #auto_increment is the primary key field needs to be set to automatically increase keyword if the main health is not an integer type or no increase automatically when the key is not required; when you need to set the primary key, required to be provided a plurality of spaced field names with commas primary key, and can not use the automatic `growth
or

create table table name (primary key Field Type primary key auto_increment, field list); # this method when creating the joint primary key unavailable
1.2 to create the primary key (if data already exists in the table on the already created table. and the data violates the primary key constraint, you can not create a primary key, the data need to resolve the conflict and then create)
the ALTER the table table name add primary key (need to set the primary key field name) AUTO_INCREMENT;
2. create a foreign key command format

2.1 foreign key is created when creating the table (to satisfy the following condition creating foreign keys)
Create Table table name (field list, foreign key (need to create foreign key constraints field) references the reference table of the table name (the reference table is Referring field) on update cascade on delete cascade) engine = innodb;
if the foreign key requires a unique label when the outer key can also be combined with the primary key constraint
. 2.2 foreign key is created in the already existing table
alter table add foreign table key (field name) references the reference table (refer to field names) ON ON Update Casecade the Delete Cascade) Engine = InnoDB;
3. create an index command format

3.1. Create a table when creating an index (the index can have multiple, added in the following example two)
the Create the Table table name (field list, index (field name already exists), index (field name that already exists)) ; # if multiple joint index already exists with the field names, separated, remember not to set up a joint cross-field index
default index and field names the same
3.2 field to the table already exists in the index field.
the Create index index name on the table name (field name)
index name can be named himself general use field names as the index name
4. create a unique index and the general index format command format is similar only need to ensure the uniqueness of field of the record you can use unique keywords

Guess you like

Origin blog.51cto.com/14447527/2420968