Manually set data constraint 1 manually add [primary key constraint] constraint (EmpId) www.2cto. com2 manually add non-null constraint for EmpName altertableEmployeesaltercolumnEmpNamevarchar (50) notnull
Manually set data constraint 1 manually add [primary key constraint] PK_Employees_EmpId alter table Employees add constraint PK_Employees_EmpId primary key (EmpId) manually add non-empty constraint alter table Employees alter column EmpName varchar (50) not null for EmpName on www.2cto.com 2
Manually set data constraints for Databases
1 manually add the [primary key constraint] PK_Employees_EmpId
Alter table Employees add constraint PK_Employees_EmpId primary key (EmpId)
Www.2cto.com
2 manually add non-empty constraints for EmpName
Alter table Employees alter column EmpName varchar (50) not null
3 manually add a unique key constraint for EmpName
Alter table Employees add constraint UQ_Employees_EmpName unique (EmpName)
Go
Www.2cto.com
4. Delete the unique key constraint.
Alter table Employees drop constraint UQ_Employees_EmpName
5. Add a default constraint for gender so that the default value is "male"
Alter table Employees add constraint DF_Employees_EmpGender
Default (1) for EmpGender
6 Add detection constraints for age 0-1 20 containing 0 and 120
Alter table Employees add constraint CK_Employees_EmpAge
Check (EmpAge> = 0 and EmpAge <= 120)
7. Add check constraints for Gender: Non-male or female
Alter table Employees add constraint CK_Employees_EmpGender
Check (EmpGender = 'male' or EmpGender = 'femal ')
8 -- add a foreign key constraint to the employee table
-- First, set the DepId in the department table as the primary key, and the foreign key cannot be blank.
Alter table Department add constraint Pk_Deparment_DepId primary key (DepId)
Alter table Employee alter column DepId int not null
Alter table Employee add constraint FK_Employee_EmpDepId
Foreign key (DepId) references Department (DepId) on delete cascade
Www.2cto.com
9 Delete multiple constraints in one statement
Alter table Employees drop constraint FK_Employees_EmpDepId,
CK_Employees_EmpAge,
UQ_Employees_EmpName
One statement adds multiple constraints to the table.
Alter table Employees add constraint
UQ_Employees_EmpName
Unique (EmpName ),
Constraint
CKSSS
Check (EmpAge> = 0and EmpAge <= 120)