SlideShare a Scribd company logo
1 of 16
Download to read offline
5. DDL Statements of SQL
 Create & Drop View Statements
 Create & Drop Table Statements
 Create & Drop View Statements
 Create & Drop Index Statement
 Alter Table Statement
SQL DDL Statements
Used to create relational structure in databases.
List of DDL Statements:
Create statements Alter statements Drop statements
CREATE SCHEMA DROP SCHEMA
CREATE DOMAIN ALTER DOMAIN DROP DOMAIN
CREATE TABLE ALTER TABLE DROP TABLE
CREATE VIEW DROP VIEW
CREATE INDEX DROP INDEX
Note:-
The list is not exhaustive. There are some DDL statements to create objects such
as triggers, roles and so on but they are not covered in this course.
CREATE SCHEMA Statement
CREATE SCHEMA statement creates a schema with the given
name in the current database.
Syntax: CREATE SCHEMA name [ AUTHORIZATION
user-id ]
name: Name of the schema.
user-id: User identifier used to login into the database.
Examples:
CREATE SCHEMA courses;
CREATE SCHEMA payroll AUTHORIZATION jimson;
DROP SCHEMA Statement
DROP SCHEMA statement drops specified schema.
Syntax: DROP SCHEMA name [ RESTRICT | CASCADE }
name: Name of the schema.
RESTRICT: Schema is not dropped if the schema has any
objects. This is default.
CASCADE: Drops all objects inside the schema before
dropping the schema itself
Examples:
DROP SCHEMA courses
CREATE TABLE Statement
CREATE TABLE statement creates a relation.
Syntax: CREATE TABLE table-name (
{ column-name data-type [ NOT NULL ] [ UNIQUE ]
[ PRIMARY KEY ] [ DEFAULT default-value ]
[ CHECK (search-condition) ]
} [ , … ]
[ , [ CONSTRAINT pk-cn ] PRIMARY KEY ( pk-column-list ) ]
[ , [ CONSTRAINT uq-cn ] UNIQUE ( uq-columns-list ) ] [ …
]
[ , [ CONSTRAINT fk-cn ] FOREIGN KEY ( rfg-column-list )
REFERENCES rfd-table-name [ ( rfd-column-list ) ]
[ … ]
[ , [ CONSTRAINT ck-cn ] CHECK ( search-condition ) ]
[ … ] )
CREATE TABLE
Simple Examples
CREATE TABLE books (
book_id INTEGER NOT NULL PRIMARY KEY,
book_title VARCHAR(100),
author VARCHAR(200),
publisher VARCHAR(200)
)
CREATE TABLE employees(
eno INTEGER NOT NULL PRIMARY KEY,
ename VARCHAR (50) NOT NULL,
dob DATE,
salary DECIMAL(10, 2),
join_date TIMESTAMP,
mno INTEGER NOT NULL,
dno INTEGER NOT NULL
)
CREATE TABLE
Examples with FOREIGN KEY Constraints
CREATE TABLE chapters (
book_id INTEGER NOT NULL,
chapter_no INTEGER NOT NULL,
chapter_title VARCHAR(100),
CONSTRAINT pk_ch PRIMARY KEY (book_id, chapter_no),
CONSTRAINT fk_ch1 FOREIGN KEY (book_id)
REFERENCES books(book_id)
)
CREATE TABLE
Examples with CHECK Constraints
CREATE TABLE contract_employees(
c_eno INTEGER NOT NULL PRIMARY KEY,
c_ename VARCHAR(30),
c_age INTEGER NOT NULL
CHECK (VALUE > 18 AND VALUE < 60)
)
CREATE TABLE new_books(
book_id INTEGER NOT NULL PRIMARY KEY,
book_title VARCHAR(30),
pub_date DATE,
CONSTRAINT cc_pubdate
CHECK (pub_date > ’2013-01-01’)
)
DROP TABLE Statement
DROP TABLE statement removes the specified table from the
database.
Syntax: DROP TABLE table-name [ RESTRICT | CASCADE ]
Default option is RESTRICT.
Example:
DROP TABLE contract_employees;
CREATE VIEW Statement
CREATE VIEW statement creates a view from one or more
tables..
Syntax: CREATE VIEW view-name [ ( column-list ) ]
AS subselect [ WITH CHECK OPTION ]
subselect: A SELECT statement that provides data for the view.
Example:
CREATE VIEW old_employees (old_eno, old_ename)
AS SELECT eno, ename FROM employees
WHERE join_date < ’2013-01-01’;
DROP VIEW Statement
DROP VIEW statement removes the specified view from the
database.
Syntax: DROP VIEW view-name [ RESTRICT | CASCADE ]
Default option is RESTRICT.
Example:
DROP VIEW old_employees;
CREATE INDEX Statement
CREATE INDEX statement creates an index on the specified
columns of a table.
Syntax: CREATE [ UNIQUE ] INDEX index-name ON
table-name ( {column_name [ ASC | DESC ] }
[, {column_name [ ASC | DESC ] } ] …)
Example:
CREATE INDEX emp_dno_idx ON employees (dno);
DROP INDEX Statement
DROP INDEX statement drops an index.
Syntax: DROP INDEX index-name
Example:
DROP INDEX emp_dno_idx;
ALTER TABLE Statement
ALTER TABLE statement alters or modifies existing table.
Syntax: ALTER TABLE table-name {
{ ADD [ COLUMN ] column-defiition } |
{ DROP [ COLUMN ] column-name
[ RESTRICT | CASCADE ] } |
{ ALTER [ COLUMN ] column-name
{ SET DEFAULT default-value |
DROP DEFAULT } } |
{ ADD table-constraint-definition } |
{ DROP CONSTRAINT constraint-name
[ RESTRICT | CASCADE }
}
ALTER TABLE Examples
ALTER TABLE books ADD COLUMN edition INTEGER;
ALTER TABLE books ADD
CONSTRAINT cc_chapters
CHECK (chapter_no > 0 AND chapter_no < 100)
ALTER TABLE books DROP COLUMN edition;
What You Have Learnt!
 CREATE statements to create database objects such as
Schemas, Tables, Views and Indexes.
 DROP statements to remove the database objects from the
database.
 ALTER statement to add columns to an existing table and
drop them.

More Related Content

What's hot (18)

Commands of DML in SQL
Commands of DML in SQLCommands of DML in SQL
Commands of DML in SQL
 
Oracle: DDL
Oracle: DDLOracle: DDL
Oracle: DDL
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Oracle SQL DML Statements
Oracle SQL DML StatementsOracle SQL DML Statements
Oracle SQL DML Statements
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Ddl commands
Ddl commandsDdl commands
Ddl commands
 
DML Commands
DML CommandsDML Commands
DML Commands
 
Sql commands
Sql commandsSql commands
Sql commands
 
Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
View & index in SQL
View & index in SQLView & index in SQL
View & index in SQL
 
SQL
SQLSQL
SQL
 
Avinash database
Avinash databaseAvinash database
Avinash database
 
Les11 Including Constraints
Les11 Including ConstraintsLes11 Including Constraints
Les11 Including Constraints
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
Sql DML
Sql DMLSql DML
Sql DML
 
SQL
SQLSQL
SQL
 

Viewers also liked

predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functionsSwapnil Yadav
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQueryRemy Sharp
 
Sql DDL Lenguaje de definición de datos
Sql DDL Lenguaje de definición de datosSql DDL Lenguaje de definición de datos
Sql DDL Lenguaje de definición de datosjosecuartas
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in CHarendra Singh
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpen Gurukul
 

Viewers also liked (6)

predefined and user defined functions
predefined and user defined functionspredefined and user defined functions
predefined and user defined functions
 
Yearning jQuery
Yearning jQueryYearning jQuery
Yearning jQuery
 
Sql DDL Lenguaje de definición de datos
Sql DDL Lenguaje de definición de datosSql DDL Lenguaje de definición de datos
Sql DDL Lenguaje de definición de datos
 
User defined functions in C
User defined functions in CUser defined functions in C
User defined functions in C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
OpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ ProgrammingOpenGurukul : Language : C++ Programming
OpenGurukul : Language : C++ Programming
 

Similar to Database Systems - SQL - DDL Statements (Chapter 3/2)

Similar to Database Systems - SQL - DDL Statements (Chapter 3/2) (20)

Sql
SqlSql
Sql
 
Les10
Les10Les10
Les10
 
Les09
Les09Les09
Les09
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Chapter 07 ddl_sql
Chapter 07 ddl_sqlChapter 07 ddl_sql
Chapter 07 ddl_sql
 
Lab
LabLab
Lab
 
Linguagem sql
Linguagem sqlLinguagem sql
Linguagem sql
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
MySQL
MySQLMySQL
MySQL
 
Sqlbysandeep
SqlbysandeepSqlbysandeep
Sqlbysandeep
 
Introduction to Oracle Database.pptx
Introduction to Oracle Database.pptxIntroduction to Oracle Database.pptx
Introduction to Oracle Database.pptx
 
Database
Database Database
Database
 
Database queries
Database queriesDatabase queries
Database queries
 
Introducción rápida a SQL
Introducción rápida a SQLIntroducción rápida a SQL
Introducción rápida a SQL
 
Less08 Schema
Less08 SchemaLess08 Schema
Less08 Schema
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
Unidad 4 actividad 1
Unidad 4 actividad 1Unidad 4 actividad 1
Unidad 4 actividad 1
 
SQL
SQLSQL
SQL
 
MS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutionsMS SQL - Database Programming Concepts by RSolutions
MS SQL - Database Programming Concepts by RSolutions
 

More from Vidyasagar Mundroy

Database Systems - Normalization of Relations(Chapter 4/3)
Database Systems - Normalization of Relations(Chapter 4/3)Database Systems - Normalization of Relations(Chapter 4/3)
Database Systems - Normalization of Relations(Chapter 4/3)Vidyasagar Mundroy
 
Database Systems - Entity Relationship Modeling (Chapter 4/2)
Database Systems - Entity Relationship Modeling (Chapter 4/2)Database Systems - Entity Relationship Modeling (Chapter 4/2)
Database Systems - Entity Relationship Modeling (Chapter 4/2)Vidyasagar Mundroy
 
Database Systems - Introduction to Database Design (Chapter 4/1)
Database Systems - Introduction to Database Design (Chapter 4/1)Database Systems - Introduction to Database Design (Chapter 4/1)
Database Systems - Introduction to Database Design (Chapter 4/1)Vidyasagar Mundroy
 
Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)Vidyasagar Mundroy
 
Database Systems - Introduction to SQL (Chapter 3/1)
Database Systems - Introduction to SQL (Chapter 3/1)Database Systems - Introduction to SQL (Chapter 3/1)
Database Systems - Introduction to SQL (Chapter 3/1)Vidyasagar Mundroy
 
Database Systems - Relational Data Model (Chapter 2)
Database Systems - Relational Data Model (Chapter 2)Database Systems - Relational Data Model (Chapter 2)
Database Systems - Relational Data Model (Chapter 2)Vidyasagar Mundroy
 
Database Systems - Introduction (Chapter 1)
Database Systems - Introduction (Chapter 1)Database Systems - Introduction (Chapter 1)
Database Systems - Introduction (Chapter 1)Vidyasagar Mundroy
 

More from Vidyasagar Mundroy (7)

Database Systems - Normalization of Relations(Chapter 4/3)
Database Systems - Normalization of Relations(Chapter 4/3)Database Systems - Normalization of Relations(Chapter 4/3)
Database Systems - Normalization of Relations(Chapter 4/3)
 
Database Systems - Entity Relationship Modeling (Chapter 4/2)
Database Systems - Entity Relationship Modeling (Chapter 4/2)Database Systems - Entity Relationship Modeling (Chapter 4/2)
Database Systems - Entity Relationship Modeling (Chapter 4/2)
 
Database Systems - Introduction to Database Design (Chapter 4/1)
Database Systems - Introduction to Database Design (Chapter 4/1)Database Systems - Introduction to Database Design (Chapter 4/1)
Database Systems - Introduction to Database Design (Chapter 4/1)
 
Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)Database Systems - SQL - DCL Statements (Chapter 3/4)
Database Systems - SQL - DCL Statements (Chapter 3/4)
 
Database Systems - Introduction to SQL (Chapter 3/1)
Database Systems - Introduction to SQL (Chapter 3/1)Database Systems - Introduction to SQL (Chapter 3/1)
Database Systems - Introduction to SQL (Chapter 3/1)
 
Database Systems - Relational Data Model (Chapter 2)
Database Systems - Relational Data Model (Chapter 2)Database Systems - Relational Data Model (Chapter 2)
Database Systems - Relational Data Model (Chapter 2)
 
Database Systems - Introduction (Chapter 1)
Database Systems - Introduction (Chapter 1)Database Systems - Introduction (Chapter 1)
Database Systems - Introduction (Chapter 1)
 

Recently uploaded

Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxJohnnyPlasten
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxolyaivanovalion
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxolyaivanovalion
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxolyaivanovalion
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxolyaivanovalion
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxfirstjob4
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxolyaivanovalion
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxolyaivanovalion
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Valters Lauzums
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Delhi Call girls
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...amitlee9823
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz1
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 

Recently uploaded (20)

Log Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptxLog Analysis using OSSEC sasoasasasas.pptx
Log Analysis using OSSEC sasoasasasas.pptx
 
Edukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFxEdukaciniai dropshipping via API with DroFx
Edukaciniai dropshipping via API with DroFx
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
BabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptxBabyOno dropshipping via API with DroFx.pptx
BabyOno dropshipping via API with DroFx.pptx
 
CebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptxCebaBaby dropshipping via API with DroFX.pptx
CebaBaby dropshipping via API with DroFX.pptx
 
Introduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptxIntroduction-to-Machine-Learning (1).pptx
Introduction-to-Machine-Learning (1).pptx
 
VidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptxVidaXL dropshipping via API with DroFx.pptx
VidaXL dropshipping via API with DroFx.pptx
 
Zuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptxZuja dropshipping via API with DroFx.pptx
Zuja dropshipping via API with DroFx.pptx
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Sampling (random) method and Non random.ppt
Sampling (random) method and Non random.pptSampling (random) method and Non random.ppt
Sampling (random) method and Non random.ppt
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 

Database Systems - SQL - DDL Statements (Chapter 3/2)

  • 1. 5. DDL Statements of SQL  Create & Drop View Statements  Create & Drop Table Statements  Create & Drop View Statements  Create & Drop Index Statement  Alter Table Statement
  • 2. SQL DDL Statements Used to create relational structure in databases. List of DDL Statements: Create statements Alter statements Drop statements CREATE SCHEMA DROP SCHEMA CREATE DOMAIN ALTER DOMAIN DROP DOMAIN CREATE TABLE ALTER TABLE DROP TABLE CREATE VIEW DROP VIEW CREATE INDEX DROP INDEX Note:- The list is not exhaustive. There are some DDL statements to create objects such as triggers, roles and so on but they are not covered in this course.
  • 3. CREATE SCHEMA Statement CREATE SCHEMA statement creates a schema with the given name in the current database. Syntax: CREATE SCHEMA name [ AUTHORIZATION user-id ] name: Name of the schema. user-id: User identifier used to login into the database. Examples: CREATE SCHEMA courses; CREATE SCHEMA payroll AUTHORIZATION jimson;
  • 4. DROP SCHEMA Statement DROP SCHEMA statement drops specified schema. Syntax: DROP SCHEMA name [ RESTRICT | CASCADE } name: Name of the schema. RESTRICT: Schema is not dropped if the schema has any objects. This is default. CASCADE: Drops all objects inside the schema before dropping the schema itself Examples: DROP SCHEMA courses
  • 5. CREATE TABLE Statement CREATE TABLE statement creates a relation. Syntax: CREATE TABLE table-name ( { column-name data-type [ NOT NULL ] [ UNIQUE ] [ PRIMARY KEY ] [ DEFAULT default-value ] [ CHECK (search-condition) ] } [ , … ] [ , [ CONSTRAINT pk-cn ] PRIMARY KEY ( pk-column-list ) ] [ , [ CONSTRAINT uq-cn ] UNIQUE ( uq-columns-list ) ] [ … ] [ , [ CONSTRAINT fk-cn ] FOREIGN KEY ( rfg-column-list ) REFERENCES rfd-table-name [ ( rfd-column-list ) ] [ … ] [ , [ CONSTRAINT ck-cn ] CHECK ( search-condition ) ] [ … ] )
  • 6. CREATE TABLE Simple Examples CREATE TABLE books ( book_id INTEGER NOT NULL PRIMARY KEY, book_title VARCHAR(100), author VARCHAR(200), publisher VARCHAR(200) ) CREATE TABLE employees( eno INTEGER NOT NULL PRIMARY KEY, ename VARCHAR (50) NOT NULL, dob DATE, salary DECIMAL(10, 2), join_date TIMESTAMP, mno INTEGER NOT NULL, dno INTEGER NOT NULL )
  • 7. CREATE TABLE Examples with FOREIGN KEY Constraints CREATE TABLE chapters ( book_id INTEGER NOT NULL, chapter_no INTEGER NOT NULL, chapter_title VARCHAR(100), CONSTRAINT pk_ch PRIMARY KEY (book_id, chapter_no), CONSTRAINT fk_ch1 FOREIGN KEY (book_id) REFERENCES books(book_id) )
  • 8. CREATE TABLE Examples with CHECK Constraints CREATE TABLE contract_employees( c_eno INTEGER NOT NULL PRIMARY KEY, c_ename VARCHAR(30), c_age INTEGER NOT NULL CHECK (VALUE > 18 AND VALUE < 60) ) CREATE TABLE new_books( book_id INTEGER NOT NULL PRIMARY KEY, book_title VARCHAR(30), pub_date DATE, CONSTRAINT cc_pubdate CHECK (pub_date > ’2013-01-01’) )
  • 9. DROP TABLE Statement DROP TABLE statement removes the specified table from the database. Syntax: DROP TABLE table-name [ RESTRICT | CASCADE ] Default option is RESTRICT. Example: DROP TABLE contract_employees;
  • 10. CREATE VIEW Statement CREATE VIEW statement creates a view from one or more tables.. Syntax: CREATE VIEW view-name [ ( column-list ) ] AS subselect [ WITH CHECK OPTION ] subselect: A SELECT statement that provides data for the view. Example: CREATE VIEW old_employees (old_eno, old_ename) AS SELECT eno, ename FROM employees WHERE join_date < ’2013-01-01’;
  • 11. DROP VIEW Statement DROP VIEW statement removes the specified view from the database. Syntax: DROP VIEW view-name [ RESTRICT | CASCADE ] Default option is RESTRICT. Example: DROP VIEW old_employees;
  • 12. CREATE INDEX Statement CREATE INDEX statement creates an index on the specified columns of a table. Syntax: CREATE [ UNIQUE ] INDEX index-name ON table-name ( {column_name [ ASC | DESC ] } [, {column_name [ ASC | DESC ] } ] …) Example: CREATE INDEX emp_dno_idx ON employees (dno);
  • 13. DROP INDEX Statement DROP INDEX statement drops an index. Syntax: DROP INDEX index-name Example: DROP INDEX emp_dno_idx;
  • 14. ALTER TABLE Statement ALTER TABLE statement alters or modifies existing table. Syntax: ALTER TABLE table-name { { ADD [ COLUMN ] column-defiition } | { DROP [ COLUMN ] column-name [ RESTRICT | CASCADE ] } | { ALTER [ COLUMN ] column-name { SET DEFAULT default-value | DROP DEFAULT } } | { ADD table-constraint-definition } | { DROP CONSTRAINT constraint-name [ RESTRICT | CASCADE } }
  • 15. ALTER TABLE Examples ALTER TABLE books ADD COLUMN edition INTEGER; ALTER TABLE books ADD CONSTRAINT cc_chapters CHECK (chapter_no > 0 AND chapter_no < 100) ALTER TABLE books DROP COLUMN edition;
  • 16. What You Have Learnt!  CREATE statements to create database objects such as Schemas, Tables, Views and Indexes.  DROP statements to remove the database objects from the database.  ALTER statement to add columns to an existing table and drop them.