SQL DDL
SQL DDL
Language
DDL COMMANDS
• SQL commands are SQL Commands are like instructions
to a table. It is used to interact with the database with
some operations. These commands are divided into
categories
• Data Definition Language (DDL),
• Data Manipulation Language (DML)
• Data Control Language (DCL)
• Data Query Language (DQL)
• Transaction Control Language (TCL).
Data Definition Language (DDL)
in SQL
• DDL or Data Definition Language actually consists
of the SQL commands that can be used
to defining, altering, and deleting database
structures such as tables, indexes, and schemas. DDL
Commands are
• CREATE
• DROP
• ALTER
• TRUNCATE
Data Definition, Constraints, and
Schema Changes
• Used to CREATE, DROP, and ALTER the descriptions of the tables
(relations) of a database
Slide 8-5
Create table
SQL CREATE TABLE Syntax
CREATE TABLE table_name CREATE TABLE Persons
( (
PersonID int,
column_name1 data_type(size),
LastName varchar(255),
column_name2 data_type(size), FirstName varchar(255),
column_name3 data_type(size), Address varchar(255),
.... City varchar(255)
); );
Slide 8-6
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
DESC
CREATE TABLE with Constraints
• Specifies a new base relation by giving it a name, and specifying each
of its attributes and their data types (INTEGER, FLOAT, DECIMAL(i,j),
CHAR(n), VARCHAR(n))
• A constraint NOT NULL may be specified on an attribute
CREATE TABLE DEPARTMENT
( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9) );
Slide 8-9
CREATE TABLE
• In SQL2, can use the CREATE TABLE command for specifying the primary key
attributes, secondary keys, and referential integrity constraints (foreign keys).
• Key attributes can be specified via the PRIMARY KEY and UNIQUE phrases
Slide 8-10
SQL Constraints
• SQL constraints are used to specify rules for the data in a table.
Slide 8-11
REFERENTIAL INTEGRITY OPTIONS
We can specify RESTRICT, CASCADE, SET NULL or SET DEFAULT on referential
integrity constraints (foreign keys)
Slide 8-14
REFERENTIAL INTEGRITY OPTIONS
We can specify RESTRICT, CASCADE, SET NULL or SET DEFAULT on referential
integrity constraints (foreign keys)
Slide 8-15
REFERENTIAL INTEGRITY OPTIONS
(continued)
CREATE TABLE EMP
( ENAME VARCHAR(30) NOT NULL,
ESSN CHAR(9),
BDATE DATE,
DNO INTEGER DEFAULT 1,
SUPERSSN CHAR(9),
PRIMARY KEY (ESSN),
FOREIGN KEY (DNO) REFERENCES DEPT
ON DELETE SET DEFAULT ON UPDATE CASCADE,
FOREIGN KEY (SUPERSSN) REFERENCES EMP
ON DELETE SET NULL ON UPDATE CASCADE );
Slide 8-16
SQL ALTER TABLE : ADD, DROP,
MODIFY, RENAME
SQL Add command
Syntax: Alter table tablename add(column name datatype(size));
Alter-add constaint
• ADD CONSTRAINT is a SQL command that is used together with ALTER
TABLE to add constraints (such as a primary key or foreign key) to an
existing table in a SQL database.
• ALTER TABLE table_name ADD CONSTRAINT PRIMARY KEY (col1, col2);
Alter- Rename
• ALTER TABLE table_name RENAME TO new_table_name;
Alter contin..
• ALTER TABLE table_name RENAME COLUMN column_name TO
new_column_name;
Alter Modify-used to modify or change the data type of an
existing column.
• Syntax
• TRUNCATE TABLE table_name;