Professional Documents
Culture Documents
Batch No: C2
Experiment No: 02
Experiment No.:02
Experiment Name: Creating and Executing DDL Commandsin SQL & Apply
various integrity constarints on table
Course Outcome:
Theory:
DROP
ALTER
TRUNCATE
CREATE – is used to create the database or its objects (like table, index, function,
views, store procedure and triggers).
EXAMPLE
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),
);
EXAMPLE
DROP DATABASE student_data;
EXAMPLE
ALTER TABLE Student ADD (AGE number(3),COURSE varchar(40));
TRUNCATE–is used to remove all records from a table, including all spaces
allocated for the records are removed.
EXAMPLE
TRUNCATE TABLE Student_details;
Your output
CREATE COMMAND.
ALTER COMMAND.
DROP COMMAND.
TRUNCATE COMMAND
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy
and reliability of the data in the table. If there is any violation between the constraint and the
data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and
table level constraints apply to the whole table.
List of constraints
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
EXAMPLE
CREATE TABLE Persons
(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
EXAMPLE
CREATE TABLE Persons
(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
EXAMPLE
CREATE TABLE Persons
(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
FOREIGN KEY - Prevents actions that would destroy links between tables
EXAMPLE
CREATE TABLE Orders
(
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
EXAMPLE
CREATE TABLE Persons
(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
EXAMPLE
CREATE TABLE Persons
(
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
YOUR OUTPUT
NOT NULL.
UNIQUE KEY.
PRIMARY KEY.
CHECK.
Conclusion: