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