Constraints 7.MySQL table

1. Default constraint (DEFAULT)

Specify default values ​​for the data field in a table. To ensure that the properties of things there will be a value.

BLOB, TEXT data type is not supported by default constraint.

 1 CREATE DATABASE mahaiwuji;
 2 USE mahaiwuji;
 3 
 4 CREATE TABLE student1 (
 5     id INT,
 6     sname VARCHAR(30),
 7     sex VARCHAR(1) DEFAULT ''
 8 ) ENGINE = INNODB DEFAULT CHARSET = utf8;
 9 
10 INSERT INTO student1 VALUES (1, ' John Doe ' , ' M ' );
 . 11  the INSERT  the INTO student1 the VALUES ( 2 , ' John Doe ' , ' F ' );
 12 is  the INSERT  the INTO student1 (ID, sname) the VALUES ( . 3 , ' Wang Wu ' );

2. Non-empty constraint (NOT NULL)

Field value can not be NULL.

 1 CREATE TABLE student2 (
 2     id INT,
 3     sname VARCHAR(30) NOT NULL,
 4     sex VARCHAR(1)
 5 ) ENGINE = INNODB DEFAULT CHARSET = utf8;
 6 
 7 INSERT INTO student2 VALUES (1,'张三','');
 8 INSERT The INTO STUDENT2 the VALUES ( 2 , ' John Doe ' , NULL );
 . 9  - error, since the name can not be specified for the blank 
10  the INSERT  the INTO STUDENT2 (ID, Sex) the VALUES ( . 3 , ' F ' );

3. The only constraint (UNIQUE)

Guaranteed unique values, i.e., the data field in a table of columns in the table can not be repeated.

 1 CREATE TABLE student3 (
 2     id INT,
 3     sname VARCHAR(30) UNIQUE,
 4     sex VARCHAR(1)
 5 ) ENGINE = INNODB DEFAULT CHARSET = utf8;
 6 
 7 INSERT INTO student3 VALUES (1,'张三','');
 8 INSERT INTOstudent3 the VALUES ( 2 , ' John Doe ' , ' F ' );
 . 9  - error, since the name can not be specified repeating 
10  the INSERT  the INTO student3 the VALUES ( . 3 , ' John Doe ' , ' M ' );

4. The primary key constraint (PRIMARY KEY)

Role of the primary key : uniquely identify the record.

Corresponds to a unique combination of a primary key constraint constraint constraint and non-empty, is constrained required field must be unique, NULL values ​​are not allowed, Each table contains only one primary key.

Column-level constraint

Field Name Data Type PRIMARY KEY

 1 CREATE TABLE student4 (
 2     id INT PRIMARY KEY,
 3     sname VARCHAR(30),
 4     sex VARCHAR(1)
 5 ) ENGINE = INNODB DEFAULT CHARSET = utf8;
 6 
 7 INSERT INTO student4 VALUES (1,'张三','');
 8 INSERT INTOstudent4 the VALUES ( 2 , ' John Doe ' , ' F ' );
 . 9  - error, since id is the primary key, can not duplicate 
10  the INSERT  the INTO student4 the VALUES ( . 1 , ' Wang Wu ' , ' M ' );

Table-level constraints

PRIMARY KEY (field name 1, field name 2, ...)

 1 CREATE TABLE student5 (
 2     id INT,
 3     sname VARCHAR(30),
 4     sex VARCHAR(1),
 5     PRIMARY KEY (id,sname)
 6 ) ENGINE = INNODB DEFAULT CHARSET = utf8;
 7 
 8 INSERT INTO student5 VALUES (1,'张三','');
 9 The INSERT  the INTO student5 the VALUES ( . 1 , ' John Doe ' , ' F ' );
 10  - error, id as the primary key and sanme joint can not be duplicated 
. 11  the INSERT  the INTO student5 the VALUES ( . 1 , ' John Doe ' , ' M ' );

The foreign key constraint (FOREIGN KEY)

Foreign key refers to a reference one or more columns in a table of another table, the referenced columns should have unique primary key constraint or constraints, to ensure data integrity and consistency.

Referenced table is called the primary table .

Reference table foreign key is called from the table .

Create a class table

1 CREATE TABLE grade (
2     gid INT,
3     gname VARCHAR(30),
4     PRIMARY KEY (gid)
5 ) ENGINE = INNODB DEFAULT CHARSET = utf8;
6 
7 INSERT INTO grade VALUES (1,'1班');
8 INSERT INTO grade VALUES (2,'2班');

Students then create a table

1 CREATE TABLE student (
2     sid INT,
3     sname VARCHAR(30),
4     sex VARCHAR(1),
5     gid INT,
6     PRIMARY KEY (sid)
7 ) ENGINE = INNODB DEFAULT CHARSET = utf8;

Table for students to add foreign key constraint

ALTER TABLE name table entry (primary table primary key field name) field name from the table master from the table entry name REFERENCES ADD CONSTRAINT foreign key name FOREIGN KEY;

1 ALTER TABLE student ADD CONSTRAINT FK_ID FOREIGN KEY (gid) REFERENCES grade(gid);

Student table to add data

. 1  the INSERT  the INTO Student the VALUES ( . 1 , ' John Doe ' , ' M ' , . 1 );
 2  the INSERT  the INTO Student the VALUES ( 2 , ' John Doe ' , ' M ' , 2 );
 . 3  - error, because there is no grade gid 3, so the failure to add 
4  INSERT  iNTO Student VALUES ( 3 , ' Wang Wu ' , ' male ',3);

Guess you like

Origin www.cnblogs.com/mahaiwuji/p/12593735.html