0% found this document useful (0 votes)
2 views

SQL DDL

Uploaded by

archana20059
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL DDL

Uploaded by

archana20059
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

SQL- Structured Query

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

CREATE TABLE Dept


( Dname VARCHAR(10) NOT NULL,
Dnum INTEGER NOT NULL,
Mgrssn CHAR(9),
Mgrstartdate CHAR(9),
PRIMARY KEY (Dnum),
UNIQUE (Dname),
FOREIGN KEY (Mgrssn) REFERENCES Emp );

Slide 8-10
SQL Constraints
• SQL constraints are used to specify rules for the data in a table.

The following constraints are commonly used in SQL:


•NOT NULL - Ensures that a column cannot have a NULL value
•PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each
row in a table
•FOREIGN KEY - Prevents actions that would destroy links between tables
•CHECK - Ensures that the values in a column satisfies a specific condition
•DEFAULT - Sets a default value for a column if no value is specified
•UNIQUE - Ensures that all values in a column are different

Slide 8-11
REFERENTIAL INTEGRITY OPTIONS
We can specify RESTRICT, CASCADE, SET NULL or SET DEFAULT on referential
integrity constraints (foreign keys)

CREATE TABLE Dept


( Dname varchar(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP
ON DELETE CASCADE );
DELETE CASCADE : We use ON DELETE cascade when we want that all
referenced entities will automatically delete if we delete any parent
entity.
Slide 8-12
REFERENTIAL INTEGRITY OPTIONS
We can specify RESTRICT, CASCADE, SET NULL or SET DEFAULT on referential
integrity constraints (foreign keys)

CREATE TABLE DEPT


( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP
ON UPDATE CASCADE );
UPDATE CASCADE : This is the same as ON DELETE cascade. The
difference is that instead of delete referenced table data it will
update the data.
Slide 8-13
• ON DELETE CASCADE
• It specifies that the child data is deleted when the parent data is deleted.
• ON UPDATE
• Optional. It specifies what to do with the child data when the parent data is updated.
You have the options of NO ACTION, CASCADE, SET NULL, or SET DEFAULT.

Slide 8-14
REFERENTIAL INTEGRITY OPTIONS
We can specify RESTRICT, CASCADE, SET NULL or SET DEFAULT on referential
integrity constraints (foreign keys)

CREATE TABLE DEPT


( DNAME VARCHAR(10) NOT NULL,
DNUMBER INTEGER NOT NULL,
MGRSSN CHAR(9),
MGRSTARTDATE CHAR(9),
PRIMARY KEY (DNUMBER),
UNIQUE (DNAME),
FOREIGN KEY (MGRSSN) REFERENCES EMP
ON DELETE SET DEFAULT ON UPDATE CASCADE );

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.

ALTER TABLE table_name MODIFY COLUMN column_name data_type;


Alter-drop(removes the column in a table using the
drop clause in SQL.)
• ALTER TABLE table_name DROP COLUMN column_name;
DROP TABLE -statement is used to drop an
existing table in a database.

• DROP TABLE table_name;


TRUNCATE TABLE

• The TRUNCATE TABLE statement is used to delete the data inside a


table, but not the table itself.

• Syntax
• TRUNCATE TABLE table_name;

You might also like