This document discusses various data definition language (DDL) statements in SQL. It describes DDL statements to create, modify, and delete database objects like schemas, tables, views, and indexes. Specifically, it covers the CREATE, ALTER, and DROP statements for schemas, tables, views, indexes, and constraints. Examples are provided for each statement type.
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
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.