MySQL Others-- constraints (the Constraint) Example

ENUM constraints

- using ENUM to restrict user input 
the CREATE  TABLE Student 
( 
        StudentID the INT the AUTO_INCREMENT a PRIMARY  KEY , 
        the ClassID the INT , 
        StudentName VARCHAR ( 200 is ), 
        Sex ENUM ( ' for a Man ' , ' for Woman ' ) 
)

 

The default value constraints

- Specifies the time of table creation default value of 
the CREATE  TABLE Student 
( 
        StudentID the INT the AUTO_INCREMENT a PRIMARY  KEY , 
        the ClassID the INT , 
        StudentName VARCHAR ( 200 is ) the DEFAULT  '' 
) 


- after creating the table to create a default constraint 
the CREATE  TABLE Student 
( 
        StudentID the INT the AUTO_INCREMENT a PRIMARY  KEY , 
        the ClassID INT , 
        StudentName VARCHAR ( 200 ) 
)

The ALTER  TABLE Student
 the ALTER  the COLUMN StudentName the SET  the DEFAULT  '' 


- see default constraint 
SHOW the CREATE  TABLE Student;

 

Foreign key constraint

MYSQL foreign key constraint only supports instant checks (immediate check), the same as the SQL SERVER.

ORACLE support for foreign key constraints delay check (deferred check).

When you create a foreign key constraint, MYSQL default foreign key constraint column to add the index, in order to avoid deadlock caused by a foreign key constraint.

- Create a table referenced class 
the CREATE  TABLE Class 
( 
        ClassID INT AUTO_INCREMENT PRIMARY  KEY , 
        ClassName VARCHAR ( 200 ) 
); 

- When you create a foreign key reference class student table table 
the CREATE  TABLE Student 
( 
        StudentID INT AUTO_INCREMENT PRIMARY  KEY , 
        ClassID INT , 
        StudentName VARCHAR ( 200 is ),
         a FOREIGN  KEY (the ClassID) the REFERENCESClass (ClassID) 
) 


- Creating Student table, and then add the foreign key 
the CREATE  TABLE Student 
( 
        StudentID INT AUTO_INCREMENT PRIMARY  KEY , 
        ClassID INT , 
        StudentName VARCHAR ( 200 ), 
); 
the ALTER  TABLE Student the ADD  a FOREIGN  KEY (ClassID) the REFERENCES Class (ClassID ); 



- See foreign key 
the SELECT  *  
the FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
 the WHERE CONSTRAINT_SCHEMA = 'testdb1'
AND TABLE_NAME='Student' \G

*************************** 1. row ***************************
       CONSTRAINT_CATALOG: def
        CONSTRAINT_SCHEMA: testdb1
          CONSTRAINT_NAME: student_ibfk_1
UNIQUE_CONSTRAINT_CATALOG: def
UNIQUE_CONSTRAINT_SCHEMA: testdb1
   UNIQUE_CONSTRAINT_NAME: PRIMARY
             MATCH_OPTION: NONE
              UPDATE_RULE: RESTRICT
              DELETE_RULE: RESTRICT
               TABLE_NAME: student
    REFERENCED_TABLE_NAME: class

In the data lead process, to improve the introduction efficiency, you disable checking the foreign key, after their introduction opening.

- disable the foreign key checks 
the SET FOREIGN_KEY_CHECKS @@ = 0 ; 

- enable foreign key checks 
SET @@ foreign_key_checks = 1;

 

The only constraint

- When you create a foreign key reference class student table table 
the CREATE  TABLE Student 
( 
        StudentID INT AUTO_INCREMENT PRIMARY  KEY , 
        ClassID INT , 
        StudentName VARCHAR ( 200 ),
         UNIQUE  KEY (StudentName) 
) 


- and then add a unique constraint create table 
the CREATE  TABLE Student 
( 
        StudentID INT AUTO_INCREMENT PRIMARY  KEY , 
        ClassID INT , 
        StudentName VARCHAR (200)
)
ALTER TABLE Student ADD UNIQUE KEY(StudentName);


--查看约束
SELECT * 
FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_SCHEMA='testdb1'
AND TABLE_NAME='Student' \G

*************************** 1. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: testdb1
   CONSTRAINT_NAME: PRIMARY
      TABLE_SCHEMA: testdb1
        TABLE_NAME: student
   CONSTRAINT_TYPE: PRIMARY KEY
*************************** 2. row ***************************
CONSTRAINT_CATALOG: def
CONSTRAINT_SCHEMA: testdb1
   CONSTRAINT_NAME: StudentName
      TABLE_SCHEMA: testdb1
        TABLE_NAME: student
   CONSTRAINT_TYPE: UNIQUE

 

Guess you like

Origin www.cnblogs.com/gaogao67/p/11324397.html