Five common constraints

1. The primary key constraint (Primay Key Coustraint) uniqueness, Nonemptiness

2. foreign key constraint (Foreign Key Counstraint) the need to establish the relationship between the two main reference table and the table

3. The only constraint (Unique Counstraint) is unique, and can be empty, but only one

4. Default values ​​The default constraint (Default Counstraint) of the data

The check constraint (Check Counstraint) the range column data format restrictions (such as: age, sex, etc.)

1. Primary Key constraint

Often a combination of one or more columns in the table, the value that uniquely identifies each row in the table.

Such one or more columns in the primary key table (PrimaryKey). A table can have a primary key and a primary key constraint column can not be null. Only primary key columns can be created as a foreign key to another table.

Create a primary key constraint can right-click the table, choose the design.

A solution SQL-- daily ------ five common constraints

 

We want to create a primary key column, and then click the top of a small key.

A solution SQL-- daily ------ five common constraints

 

You can also right need to create a primary key, and then click the small key.

A solution SQL-- daily ------ five common constraints

 

2. Foreign Key Constraints

Foreign key constraint is used to strengthen the two tables (main table and the table) between one or more columns of the data connection. Sequentially create foreign key constraints that define the primary key in the primary table and the foreign key from the definition table. That is only the main table's primary key can be used as a foreign key from a table, the table being constrained from the column may not be the primary key, the primary table from the table update operation limits and insertions.

Right-click the foreign key column (in this case emerges from the table as a foreign key in the table), select the relationship.

A solution SQL-- daily ------ five common constraints

 

Then click Add -> Table and column specification.

A solution SQL-- daily ------ five common constraints

 

Primary table and select the main table in the primary key table primary key column.

A solution SQL-- daily ------ five common constraints

 

After setting save.

3. Unique Constraints

The only constraint ensures that a data table is not the same value. Similarly the primary key constraint, the only constraint is also the only mandatory, but a unique combination of non-primary key constraint for a plurality of rows or columns, and a plurality of tables can define a unique constraint.

Right-click the column selection index to set / key.

A solution SQL-- daily ------ five common constraints

 

Then click the Add button.

A solution SQL-- daily ------ five common constraints

 

Select the column to be set, it may be a combination of a plurality of columns.

A solution SQL-- daily ------ five common constraints

 

Close and save the settings.

4. Default constraints

If the constraint defines the default value in the table, users insert new rows of data, if the data line is not specified, the system will default value is assigned to the column, if we do not set a default value, the system defaults to NULL.

Student information table, for example, in the Table Designer, to fill in the column defaults sex sex male.

A solution SQL-- daily ------ five common constraints

 

5. Check Constraints

Check constraints to determine the validity of data by logical expressions, for limiting an input value of one or more columns of the range. When updating the data in a column, the content to be input conditions Check constraints that must be met, otherwise it will not be entered correctly.

Student information table in sex, for example, we want to limit sex column can only be male or female.

A solution SQL-- daily ------ five common constraints

 

A solution SQL-- daily ------ five common constraints

 

A solution SQL-- daily ------ five common constraints

 

A solution SQL-- daily ------ five common constraints

 

And save the design.

Column and table constraints

For the database, the constraint is divided into column constraints (Column Constraint) and table constraints (Table Constraint).

Column column constraint definition part only acts as the column itself thereto. As part of the table definition table constraint may be applied to

Multiple columns.

A solution SQL-- daily ------ five common constraints

 

 Seen from the FIG., 1, primary key, foreign key, unique, these four check, either create a column constraint can also create a table constraint. The non-null default and can only create column constraints.

Guess you like

Origin www.cnblogs.com/twelvezuo/p/11671202.html