Professional Documents
Culture Documents
DATABASE SYSTEMS
LAB 07
Creating Table
Data definition queries are used to create tables in database. Following constraints can be
applied while creating table.
(
P_Id int NOT NULL PRIMARY KEY CHECK (P_Id > 0 AND P_Id < 21),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'
)
// To name PK constraint and to set PK on multiple columns
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
)
//Create table with Foreign Key Constraint
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
)
Insert Statement:
INSERT INTO departments(P_ID, LastName, FirstName, Address, City) VALUES (1, 'Ali’
Khan', ‘xyz’, ‘Lhr’);
Update Statement:
All rows in the table are modified if you omit the WHERE clause:
All rows in the table are deleted if you omit the WHERE clause.
Truncate Statement:
Removes all rows from a table, leaving the table empty and the table structure intact
Drop Table:
Deletes the table from the database completely.