Mainly add? Restricted
The following constraints are listed in detail:
1. Primary Key constraints:
To add a primary key constraint to a column, this column must meet the condition that it is null.
Because of the primary key constraint: A column is restricted, and the constraint is (not empty, not repeated)
The following is the code ?? Add a primary key to a column. The column name is ID and the table name is EMP.
Format:
Alter table table name Add constraint name add? Constraint type (column name)
Example:
Alter table emp add constraint PPP primary key (ID)
--------------------------------
2. Check constraints:
Restrict the data in a column.
For example, data in the age column must be greater than 20
Table Name (EMP) column name (AGE)
Format:
Alter table table name Add constraint name add? Constraint type (column name)
Example:
Alter table emp add constraint XXX check (age> 20)
______________________________________________________________________
3. unique constraints:
This constraint is a type of constraint that does not repeatedly append data to columns.
Format:
Alter table table name Add constraint name constraint type (column name)
For example, you can add a unique to the ename column so that the data in the ename column is not repeated.
Example:
Alter table emp add constraint qwe unique (ename)
--------------------------------
4. Default constraints:
This makes the data in this column feel a certain amount of data.
Format:
Alter table table name Add constraint name constraint type default value) for column name
For example, the gongzi column in the EMP table feels 10000
Alter table emp add constraint jfsd default 10000 for gongzi
--------------------------------
5. Foreign key constraints:
This is hard to understand. The foreign key is actually a reference.
Because the primary key implements entity integrity,
The foreign key implements the integrity of the reference,
Application integrity requirements: The referenced data must exist!
It is actually a reference,
For example, a table named dept contains two columns of data, one is ID, and the other is ename.
ID: indicates the product ID.
Ename: indicates the product name.
Another table name is EMP with two columns of data. One column is ID and the other column is did.
ID: indicates the user ID.
Did: indicates the purchased product number.
Let the did column in the EMP Table reference the ID in the dept table
The following method can be used:
Format:
Alter table table name Add constraint name constraint type (column name) References referenced table name (column name)
Example:
Alter table emp add constraint jfkdsj foreign key (did) References dept (ID)