Below are the List of Adding Constraints in a table
To add referential Integrity between tables
--foreign key constraint
alter table sales.orders
add constraint FK_orderrs
Foreign key(empid)
references dbo.employee(empid)
To Check all the values of a column are unique it can take one NULL Values
--unique constraint
alter table dbo.employee
add constraint UQ_Emp_SN
unique (ssn)
To Set Default value to a column if no value will be inserted or updated it will take default values
--default constraint
alter table orders
add constraint df_dt
default(getdate()) for orders
To Check Value of a column every time with insert and update it will check for the value
--check constaint
ALTER TABLE dbo.Employees
ADD CONSTRAINT CHK_Employees_salary
CHECK(salary > 0);
To add Primary key Constraint in a table it gaurntee uniqueness without taking any Null Value
--primary key constraint
ALTER TABLE dbo.Employees
ADD CONSTRAINT PK_Employees
PRIMARY KEY(empid);
To add Primary Key Constraint while creating a table
--Primary key constraints while creating table
CREATE TABLE dbo.Orders ( orderid INT NOT NULL, empid INT NOT NULL,
custid VARCHAR(10) NOT NULL, orderts DATETIME NOT NULL, qty INT NOT NULL,
CONSTRAINT PK_Orders PRIMARY KEY(OrderID) );
No comments:
Post a Comment