1, data integrity (Data Integrity) refers to the accuracy of the data (the Accuracy) and reliability (Reliability).
-
-
Domain integrity (Domain Integrity): For example: 0-120 age range, gender scope of "male / female"
-
Referential integrity (Referential Integrity): For example: employee's department, in the department table to be able to find this department
-
User-defined integrity (User-defined Integrity): For example: the only user name, password can not be waited in vain
2, constraint
-
-
Not NULL constraints: non-null constraint
-
Check Constraint: check constraints
-
Default constraint: the default value constraints
-
Increment constraint
3, constraints (CONSTRAINTS) and the index (INDEX)
Constraints are used for data traffic and data integrity rules for implementation and maintenance. The scope is limited constraint in the current database, logical constraints, and does not set constraints and the extra space.
Can greatly improve query speed. Should be in the key column, or other frequently queried, sorted, indexed by the column to find the range.
In which the primary key to delete, the corresponding index is also deleted
The only way is to delete the key by deleting the corresponding index to achieve
Remove the foreign key index on the foreign key column still, if you need to remove the need to remove the index alone
SELECT * FROM information_schema.table_constraints WHERE table_name = '表名称'; 或 SHOW CREATE TABLE 表名;
SHOW INDEX the FROM table name;
The primary key and the primary key is divided into single composite primary key
Primary key features :
(1) unique and not null (2) a table can have only one primary key constraint (3) primary key constraint name is called PRIMARY (4) create a primary key will automatically create a corresponding index, also deletes the primary key corresponding to the index will be deleted.
Specifying a primary key constraint to build the table
Create Table [data name.] table ( Field Name Data Type 1 Primary Key , .... ); or Create Table [data name.] table ( Field Name Data Type 1, ...., Primary Key (Field name 1) ); or Create table [.] name data table ( field name data type 1, field name data type 2, ...., primary key (composite primary key field list) # If the primary key is a composite, then we need using this form specified in the field list of all the back, can not be applied directly in the field behind the Primary Key );
After the construction of the table specify the primary key constraint
ALTER Table table name the Add Primary Key (primary key field list);
Delete the primary key constraint
ALTER Table Table Name drop Primary Key ;
Unique key constraint features :
Specify a unique key constraint when construction of the table
Create Table [data name.] table ( Field Name Data Type 1 Primary Key , Field Name Data Type 2 UNIQUE Key , .... ); Create Table [data name.] table ( Field Name Data Type 1 Primary Key , 2 types of data field names, field names 3 data types, ...., uNIQUE key (unique complex field list) # If is a composite unique key, then you need to specify the use of this form at all behind the field list, not in the back field added directly UNIQUE Key );
After the construction of the table to add a unique key constraint
ALTER Table Table Name add [ constraint constraint name] unique [ key ] (a list of field names);
# If you do not specify a constraint name, (list of field names) in only one field, the default is the field name, if the default is more than one field is a field name in the list of a field name.
To delete a unique key constraint
The ALTER TABLE table name DROP INDEX unique key constraint name;
Foreign key features:
-
-
When you create a foreign key constraint, if not to the foreign key constraint name, the default name is not a column name, but automatically generates a foreign key name (for example student_ibfk_1;), you can also specify the foreign key constraint name.
-
When you create a foreign key constraint, the system will create a corresponding general index on the column resides. But the index name is the column name, not the name of the foreign key constraint.
-
When you delete a foreign key, the general index on the foreign key column needs to be removed separately.
-
Establishing a foreign key in the table, and the first primary table exists.
-
A table can create multiple foreign key constraints
-
From foreign key columns of the table, the main table referenced in the key column only (primary key, unique keys, foreign keys), it is recommended to reference the primary key of the primary table.
-
Referring to the columns may be different from the name of the foreign key column of the primary table the table, but must be the same data type
Constraints : the constraint is for both sides
After adding a foreign key constraint, add and modify the table bound
5 constraint level
-
-
Set null mode: update / delete records in the parent table, the child will match record on the table columns set to null, but pay attention to the child table foreign key column can not not null
-
No action: If there are matching records in the child table is not allowed to the parent table corresponds to a candidate key update / delete operations
-
Restrict way: with no action, foreign key constraints are checked immediately
-
Set default mode (may display a blank in the visualizer SQLyog): the parent table has changed, the sub-table as a foreign key column provided default values, but can not identify Innodb
If you do not specify a level equivalent to Restrict way
Specify the foreign key constraint when construction of the table
Create Table [data name.] from the table ( field name Data type Primary Key , Field Name Data Type [2 UNIQUE Key ], ...., [ constraint foreign key constraint name] Foreign Key (Table fields) References main table (the primary table fields) [ ON Update foreign key constraint level] [ ON Delete foreign key constraint level] );
# Foreign key can only be specified separately in all behind the field list
# if you want to name their foreign key constraint name, the proposed main table name _ _ from the table name associated with the field name _fk
After the construction of the table specifies the foreign key constraint
ALTER Table Table Name add { constraint foreign key constraint name] Foreign Key (from Table Field Name) References main table (the primary table is with reference to a field name) [ ON Update XX] [ ON Delete XX];
Delete the foreign key constraint
The ALTER TABLE table name DROP a FOREIGN KEY foreign key constraint name;
# Delete foreign key constraint does not delete the corresponding index
Delete Index
The ALTER TABLE table name DROP INDEX index name;
The provisions of a field can not be empty
Specify a non-null constraints when construction of the table
Create Table [data name.] table ( Field Name Data Type 1 Primary Key , Field Name Data Type [2 UNIQUE Key ] [ Not null ], Field Name Data Type [2 Not null ], ...., );
After the construction of the table specifies a non-null constraint
The ALTER TABLE Table Field Name Data Type Name MODIFY the NOT NULL [ default Default];
# If the field was set a default value constraints to follow to write again together, otherwise the default value constraints will be lost
Delete non-empty constraint
The ALTER TABLE table MODIFY name Field Name Data Type [ default default value];
# If the field was set a default value constraints to follow to write again together, otherwise the default value constraints will be lost
Specify the default constraint when construction of the table
Create Table [data name.] table ( field name 1 Data Type Primary Key , field name 2 Data Type [ UNIQUE Key ] [ Not null ] [ default Default], field name 3 Data Type [ Not null ] [ default Default ], ...., );
After the construction of the table specify a default value constraints
The ALTER TABLE Table Field Name Data Type Name [MODIFY default Default] [ the NOT NULL ];
# If the field was set up non-empty constraint to follow to write again together, otherwise non-empty constraint will be lost
Delete the default value constraints
The ALTER TABLE table MODIFY Name Field Name Data Type [ the NOT NULL ];
# If the field was set up non-empty constraint to follow to write again together, otherwise non-empty constraint will be lost
Check constraints , mysql does not support
Features:
-
-
Since growth column must be key columns (primary key columns, columns unique key, foreign key columns), and requires a non-empty.
-
Auto-increment must be an integer type
-
Automatic growth column InnoDB tables can be manually inserted, but inserted it is empty or if the value of 0, the actual value will be inserted automatically increase.
Specify a custom built table when growth
Create Table [data name.] table ( Field Name Data Type 1 Primary Key AUTO_INCREMENT, Field Name Data Type [2 UNIQUE Key ] [ Not null ] [ default Default], .... ); or Create Table [data name .] table ( field name data type 1 Primary Key , field name data type [2 UNIQUE Key Not null ] AUTO_INCREMENT, .... );
After the construction of the table from the specified growth
ALTER Table [data name.] table modify self-energizing Field Name Data Type AUTO_INCREMENT;
Delete increment constraint
ALTER Table [data name.] table modify the self-energizing type data field name;
Data is added, if there is a column increment constraints, how to add the value of the field
INSERT INTO [Database Name] table name. values (value list); # value list, the corresponding auto-increment can be assigned to 0 and null
INSERT INTO [Database Name] table name (field list section). values (value list) ; # auto-increment in (part of the field list) can not write
Data is added, if a column has a default constraint, how to add, modify the value of the field
INSERT INTO [Database Name] table name. values (value list); # in the list of values corresponding to the default value of the column, if you want to use the default values, with the default INSERT INTO [Database Name] table name (partial field list). values ( list of values); default values corresponding to columns # if you want to use the default value, the (part of the field list) can not write
change the data
update [database name.] table name set field name 1 = value 1, 2 field name = value 2. . . [ Where conditions]; # corresponds to the default value for the column, if you want to use the default values, write field name = default can