SQL Server (chapter I) Create a table Delete table create a PRIMARY KEY constraint, a unique constraint, a foreign key constraint, a check constraint, a DEFAULT constraint

Source: Internet
Author: User


1.Employees Employee table
/** Create employees employee table **/ UseTSQL2012IF object_id('dbo. Employees','U') is  not NULLDROP TABLEdbo. EmployeesCREATE TABLEdbo. Employees (EmpidINT     not NULL, FirstNameVARCHAR( -) not NULL, LastNameVARCHAR( -) not NULL, HireDate DATE not NULL, MgridINT    NULL, SSNVARCHAR( -) not NULL, Salary Money     not NULL)/** Add PRIMARY KEY constraint **/ALTER TABLEDbo. EmployeesADD CONSTRAINTpk_employeesPRIMARY KEY(empid);/** Add UNIQUE Constraint **/ALTER TABLEDbo. EmployeesADD CONSTRAINTUNQ_EMPLOYEES_SSNUNIQUE(SSN);/** Add foreign KEY constraint **/ALTER TABLEDbo. EmployeesADD CONSTRAINTfk_employees_employeesFOREIGN KEY(Mgrid)REFERENCESdbo. Employees (empid);/** Add CHECK constraint **/ALTER TABLEDbo. EmployeesADD CONSTRAINTchk_employees_salaryCHECK(Salary>0.00)/** Delete Check constraint **/ALTER TABLEDbo. EmployeesDROP CONSTRAINTChk_employees_salary

2.Orders Order Form

/** Create orders order form **/ UseTSQL2012IF object_id('dbo. Orders','U') is  not NULL DROP TABLEdbo. Orders;CREATE TABLEdbo. Orders (OrderIDINT     not NULL, EmpidINT  not NULL, CustIDVARCHAR(Ten) not NULL, Orderts DATETIME2 not NULL, QtyINT     not NULL,    CONSTRAINTPk_ordersPRIMARY KEY(OrderID));/** Add PRIMARY KEY constraint **/ALTER TABLEDbo. OrdersADD CONSTRAINTFk_orders_employeesFOREIGN KEY(Empid)REFERENCESdbo. Employees (empid);/** Add default constraint **/ALTER TABLEDbo. OrdersADD CONSTRAINTDft_orders_ordertsDEFAULT(Sysdatetime ()) forOrderts;/** Delete Check constraint **/ALTER TABLEDbo. EmployeesDROP CONSTRAINTDft_orders_orderts

SQL Server (chapter I) Create a table Delete table create a PRIMARY KEY constraint, a unique constraint, a foreign key constraint, a check constraint, a DEFAULT constraint

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.