MySQL- SQL statement is the basis of Title III (2) database constraints

1, database constraints.

    Constraint is enforced on the table data validation rules, constraints mainly used to ensure the integrity of data in databases.

    MySQL database using information_schema of TABLE_CONSTRAINTS table to hold all constraints information in this database instance.

2, common database integrity constraints:

   1 "NOT NULL: non-empty constraint.

   2 "UNIQUE: The only constraint specify which columns (one or more), or a combination of columns can not be repeated.

   3 "PRIMARY KEY: primary key constraint specifies a column value uniquely identifies this record.

   4 "FOREIGN KEY: foreign key constraint specifies the rows belonging to a record in the primary table (e.g., a field value of the field must be present in a field master table value), is mainly used to ensure referential integrity.

   5 "CHECK: Check specifying a Boolean expression, corresponding to specify the value of the column must satisfy the expression. (MySQL does not support this constraint, even if MySQL statement can use a CHECK constraint, but the constraint will not take effect)

3, is constrained database object, the data is stored in the dictionary (table system) is stored. The restrictions imposed on data columns, constraints are divided into two categories:

   1 "single constraint: Each constraint is only binding one.

   2 "Multi-column constraints: Constraints can constrain each of the plurality of data columns.

4, specify constraints on two occasions:

   1 "while the construction of the table designated constrained to the corresponding data column.

   2 "built after the table is created, to modify the list of ways to increase constraints.

   Most constraints can employ column-level constraint syntax or grammar table-level constraints.

5, five kinds of common constraints Detailed

   1 "NOT NULL constraints

      Use only as column-level constraints, can only use column-level constraint syntax definitions.

      SQL is not case-sensitive null, all data types may be null, empty string is not equal to null, 0 is not equal to null, null is not equal to null.

   2 "UNIQUE constraint

      Specify which columns (one or more), or a combination of columns can not be repeated (but more null values ​​may occur, because it does not mean null null).

      When creating a unique constraint for a column, MySQL will be listed accordingly create a unique index for that. If you do not give unique constraint named, the only constraint default the same as the column name.

      The only constraint can either use the column-level constraint grammar is established, you can use table-level constraint syntax established. If you need a combination of multi-column unique constraint for the construction, or the need for the unique constraints specified constraint names, you can only use table-level constraint syntax.

      Table-level constraint syntax: The constraint syntax can be placed either create table statement with the column definitions tied, you can be placed alter table statements using add keywords to add constraints.

[ Constraint A constraint name ] constraint definition

      example:

Create  Table EMP 
( 
  The e_name VARCHAR ( 255 ); 
  e_pass VARCHAR ( 255 );
   - using the table-level constraints create a unique constraint grammar 
  UNIQUE (The e_name),
   - table-level constraint syntax establishing a unique constraint, and the constraint name specified 
  constraint un_emp_pass UNIQUE ( e_pass) 
);

 

Guess you like

Origin www.cnblogs.com/ZeroMZ/p/11367552.html