CREATE TABLE table_name
(
column_name1 datatype PRIMARY KEY,
column_name2 datatype,
….................
column_namen datatype
)
OR
CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
….................
column_namen datatype
CONSTRAINT constraint_name PRIMARY KEY(column1, column2, ....,column_n)
)
CREATE TABLE Students
( Stud_id INT NOT NULL PRIMARY KEY,
Last_name VARCHAR(50) ,
First_name VARCHAR(50)
)
CREATE TABLE Students
( Stud_id INT,
Last_name VARCHAR(50) ,
First_name VARCHAR(50)
CONSTRAINT students_pk PRIMARY KEY(Last_name, First_name)
)
ALTER TABLE table_name
ADD CONSTRAINT constraint_name PRIMARY KEY (column1, column2,...., Column_n)
ALTER TABLE Students
ADD CONSTRAINT students_pk PRIMARY KEY (Last_name, First_name)
ALTER TABLE table_name
DROP CONSTRAINT constraint_name
ALTER TABLE Students
DROP CONSTRAINT students_pk
ALTER INDEX constraint_name ON table_name
DISABLE
ALTER INDEX last_name_pk ON Students
DISABLE
ALTER INDEX constraint_name ON table_name
REBUILD
ALTER INDEX last_name_pk ON Students
REBUILD
CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
….........
column_name_n datatype
CONSTRAINT fk_name
FOREIGN KEY (column1, column2, …., column_n)
REFERENCES parent_table_name (column1, column2,..., column_n)
)
Step 1: Create PRIMARY KEY
CREATE TABLE Items
(
Item_Id int PRIMARY KEY,
Item_Name varchar(30),
Category varchar(20)
)
Step 2: Create FOREIGN KEY
CREATE TABLE Inventory
(
Inventory_id INT PRIMARY KEY,
Item_id INT NOT NULL,
quantity INT,
CONSTRAINT fk_inv_Items_id
FOREIGN KEY (Item_id)
REFERENCES Items(Item_id)
)
ALTER TABLE table_name
ADD CONSTRAINT fk_name
FOREIGN KEY(column1, column2, ….., column_n)
REFERENCES table_name ( column1, column2, ….., column_n)
ALTER TABLE Inventory
ADD CONSTRAINT fk_inv_product
FOREIGN KEY (Item_id)
REFERENCES Items(Item_id)
CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
….........
column_namen datatype
CONSTRAINT fk_name
FOREIGN KEY (column1, column2, …., column_n)
REFERENCES parent_table_name (column1, column2,..., column_n)
ON DELETE CASCADE
)
Step 1: Create PRIMARY KEY
CREATE TABLE Items
(
Item_Id int PRIMARY KEY,
Item_Name varchar(30),
Category varchar(20)
)
Step 2: Create FOREIGN KEY
CREATE TABLE Inventory
(
Inventory_id INT PRIMARY KEY,
Item_id INT NOT NULL,
quantity INT,
CONSTRAINT fk_inv_Items_id
FOREIGN KEY (Item_id)
REFERENCES Items(Item_id)
ON DELETE CASCADE
)
ALTER TABLE Inventory
ADD CONSTRAINT fk_inv_product
FOREIGN KEY (Item_id)
REFERENCES Items(Item_id)
ON DELETE CASCADE
CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
….........
column_namen datatype,
CONSTRAINT fk_name
FOREIGN KEY (column1, column2, …., column_n)
REFERENCES parent_table_name (column1, column2,..., column_n)
ON DELETE SET NULL
)
Step 1: Create PRIMARY KEY
CREATE TABLE Items
(
Item_Id int PRIMARY KEY,
Item_Name varchar(30),
Category varchar(20)
)
Step 2: Create FOREIGN KEY
CREATE TABLE Inventory
(
Inventory_id INT PRIMARY KEY,
Item_id INT NOT NULL,
quantity INT,
CONSTRAINT fk_inv_Items_id
FOREIGN KEY (Item_id)
REFERENCES Items(Item_id)
ON DELETE SET NULL
)
ALTER TABLE Inventory
ADD CONSTRAINT fk_inv_Items
FOREIGN KEY (Item_id)
REFERENCES Items(Item_id)
ON DELETE SET NULL
ALTER TABLE table_name
DROP CONSTRAINT fk_name
ALTER TABLE table_name
CHECK CONSTRAINT fk_name
ALTER TABLE table_name
NOCHECK CONSTRAINT fk_name
CREATE TABLE table_name
(
column1 datatype,
column2 datatype,
….................
column_namen datatype,
CONSTRAINT constraint_name
CHECK [NOT FOR REPLICATION] (column_name condition)
)
Check constraint using CREATE TABLE statement in SQL server on table titled 'Students'
CREATE TABLE Students
( Stud_id INT NOT NULL,
Last_name VARCHAR(50) NOT NULL,
First_name VARCHAR(50),
CONSTRAINT check_Stud_id
CHECK (Stud_id BETWEEN 1 and 100)
)
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
CHECK (column_name condition)
ALTER TABLE Students
ADD CONSTRAINT Check_First_name
CHECK (First_name IN ('SAM', 'JON'))
ALTER TABLE table_name
DROP CONSTRAINT constraint name
Check constraint using DROP TABLE statement in SQL server on table titled 'Students'.
ALTER TABLE Students
DROP CONSTRAINT First_name