Manually set data constraints for Databases

Source: Internet
Author: User
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)

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.