Required SQL statement (8) integrity constraints of the database and SQL integrity

Source: Internet
Author: User

Required SQL statement (8) integrity constraints of the database and SQL integrity

Entity integrity
1. Define the primary key when creating a table

Create table Name
(
Sno int identity (1, 1 ),
Sname nvarchar (20 ),
-- Set the primary key
Primary key (Sno)
)
 
2. Add a primary key

Alter table name
Add constraint PK _ TABLE name_sno
Primary key (id)
Reference integrity 1. Define Foreign keys when creating a table

Create table Name
(
Sno int identity (1, 1) primary key,
Cno int not null,
Foreign key (cno) References
Table name 2 (Cno)
On Delete cascade -- cascade Delete
On update cascade -- cascade update
-- On delete on action delete Control
)
 
2. Add a foreign key
Alter table name
Add constraint FK _ TABLE name_table name 2
Foreign key (cid) references table name 2 (cid)
User-Defined integrity 1. Non-empty Constraints
Alter table name
Alter column name varchar (20) not null
 
2. unique constraints
Alter table name
Add constraint UQ _ TABLE name_column name unique (column)
 
3. Check Constraints
Alter table name
Add constraint CK _ TABLE name_column name check (age> 5)
 
4. Default Constraints
Alter table name
Add constraint DF _ TABLE name_column name default ('male ')
For gender
Delete constraint -- delete Constraint
Alter table Name drop constraint DF _ table name_column

Related Article

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.