SQL Constraint document

Source: Internet
Author: User

CREATE TABLE Employees (EmpId int identity (empname), Empgender char (2), empage int,empemail varchar (100), empaddress varchar (+)) gocreate Table Department (depid int identity (), Depname varchar (50))
--Manually delete a column (delete empaddress column) ALTER TABLE Employees drop column empaddress--Manually add a column (add a column empaddr varchar) ALTER TABLE Employees add empaddr varchar (1000)--Manually modify the data type of empemail (varchar) ALTER TABLE employess ALTER COLUMN EMPADDR  VARCHAR (200)--Add a PRIMARY KEY constraint to EmpId ALTER TABLE employess add constraint Pk_employees_empid primary KEY (EMPID)--add yi8ge non-null constraint In fact, even if you modify the column ALTER TABLE employeesalter column EmpName varchar (a) Not null--adds a unique constraint to the EmpName ALTER TABLE Employees add Constrai Ntuq_employees_empname Unique (EmpName)--Add a DEFAULT constraint for gender ALTER TABLE Employees add Constraintdf_employees_empgender Default (' Male ') for empgender--a check constraint for age, age must be between 0-120 years old and 0 and 120 years old ALTER TABLE employess add Constraintck_employees_empage Check (empage>=0 and empage<=120)--Add FOREIGN KEY constraint, table employee has a column Empdeptid references tbldepartment table Deptidalter Add depid int not nullalter table Department add constraint Pk_department_depid primary KEY (DEPID) ALTER TABLE Employees AD D constraintfk_employees_department foreign KEY (depid) references departMnet (depid)-When adding additional constraints, set cascading updates, cascade delete--[on delete{noaction| SET null| DEFAULT}]--[on update{noaction| SET null| Default}]--delete the original foreign key ALTER TABLE Employees DROP constraint fk_employees_department--Set the cascade Delete when setting the foreign key Alte      R Table Employees Add constraintfk_employees_department foreign key (depid) references departmnet (depid) on DELETE cascade  --Delete a constraint on a name ALTER TABLE Employees DROP constraint fk_employees_department--A statement to delete multiple constraints, with the constraint name separated by a comma alter TABLE Employees Drop constraint fk_employees_department,ck_emplpoyees_empage,uq_employees_empname--A statement add multiple constraints ALTER TABLE Employees Add constraint uq_emplyees_empname unique (empname), constraint ck_emplyees_empage check (empage>=0 and empage<= 120)

  

SQL Constraint document

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.