Constraint in SQL is mainly about increasing constraints

Source: Internet
Author: User

This is mostly about adding constraints.
The following are some of the constraints and lists:
1. PRIMARY KEY constraint:

PRIMARY KEY constraint: A column is constrained to (non-null, not duplicate) to a column with a PRIMARY KEY constraint, the column must satisfy the condition is not NULL, so that you can constrain the following is the codeTo 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 constraint name increased constraint type (column name)
Example: ALTER TABLE EMP ADD constraint PPP primary key (ID)
————————————————————————————————
2.check constraint: Check constraint: Limits the data for a column for example, the data for the Age column is greater than 20 of the table name (EMP) column name
Format: ALTER TABLE name ADD CONSTRAINT constraint name increased constraint type (column name)
Example: ALTER TABLE EMP add constraint xxx check (age>20)
______________________________________________________________________
3.unique constraint: Unique constraint: The non-repeating constraint type appended to the data for the column
Format: ALTER TABLE name ADD CONSTRAINT constraint name constraint type (column name) Let's say you can add a unique to the ename column, so that the data for the ename column is not repeated example: ALTER TABLE EMP add constraint qwe Unique (ename)
————————————————————————————————
4. Default constraint: Default constraint: Let the data in this column default to certain data
Format: ALTER TABLE name ADD CONSTRAINT constraint name constraint type default value) for column name
For example: The Gongzi column in the EMP table defaults to 10000
ALTER TABLE EMP ADD constraint JFSD default10000 for Gongzi
————————————————————————————————
5. FOREIGN KEY constraint: foreign key is actually a reference because the primary key realizes the entity integrity, the foreign key realizes the referential integrity, the application integrity stipulation, the reference data must exist!
is actually a reference, say a table name dept inside there are 2 columns of data one column is the ID column is the ename ID: Indicates the product's number ename: The name of the product
Another table name is the EMP contains 2 columns of data, one column is the ID column is the Did ID: Indicates the user number did: Indicates the product number of the purchase
To make the Did column in the EMP table refer to the ID in the Dept table
You can use the following method
Format: ALTER TABLE name add CONSTRAINT 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)

Constraint in SQL is mainly about increasing constraints

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.