Alter table *** add constraint *** usage

Source: Internet
Author: User

1. Primary Key constraints:
To add a primary key constraint to a column, this column must meet the condition that it is left empty.
Because of the primary key constraint: A column is restricted, and the constraint is (not empty, not repeated)
The following code adds 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 added 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, the data in the age column must be greater than 20.
Table Name (EMP) column name (AGE)
Format:
Alter table table name Add constraint name added constraint type (column name)
Example:
Alter table emp add constraint XXX check (age> 20)
______________________________________________________________________
3. unique constraints:
This constraint is the non-repeated constraint type appended to the column data.
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 duplicated.
Example:
Alter table emp add constraint qwe unique (ename)
--------------------------------
4. Default constraints:
It means that the data in this column is set to a certain value by default.
Format:
Alter table table name Add constraint name constraint type default value) for column name
For example, the gongzi column in the EMP table is 10000 by default.
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 column is ID, and the other column 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
You can use the following method
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.