1. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 1
3. Data Definition Language (DDL)
Data Definition Language (DDL) allows the specification of not only a set of relations but also information
about each relation, including:
The schema for each relation.
The domain of values associated with each attribute.
Integrity constraints
The set of indices to be maintained for each relations.
Security and authorization information for each relation.
The physical storage structure of each relation on disk.
Following are the DDL commands
CREATE
ALTER
DROP
CREATE Command
An SQL relation is defined using the create table command:
create table r (A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))
r is the name of the relation
each Ai is an attribute name in the schema of relation r
Di is the data type of values in the domain of attribute Ai
Constraints on relations
primary key (Aj1 , Aj2, . . . , Ajm ): The primary-key specification says that attributes Aj1 ,
Aj2, . . . , Ajm form the primary key for the relation. The primary key attributes are required to
be nonnull and unique; that is, no tuple can have a null value for a primary-key attribute, and
no two tuples in the relation can be equal on all the primary-key attributes. Although the
primary-key specification is optional, it is generally a good idea to specify a primary key for
each relation.
foreign key (Ak1 , Ak2, . . . , Akn ) references s: The foreign key specification says that the
values of attributes (Ak1 , Ak2, . . . , Akn ) for any tuple in the relation must correspond to
values of the primary key attributes of some tuple in relation s. The definition of the account
table has a declaration “foreign key (branch_name) references branch”. This foreign-key
declaration specifies that for each account tuple, the branch_name specified in the tuple must
exist in the primary key attribute (brach_name) of the branch relation. Without this constraint,
it is possible for a account relation to specify a nonexistent branch name.
not null: The not null constraint on an attribute specifies that the null value is not allowed for
that attribute; in other words, the constraint excludes the null value from the domain of that
2. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 2
attribute. For example, the not null constraint on the customer_city attribute of the customer
relation ensures that the name of an instructor cannot be null.
check(P): The check clause specifies a predicate P that must be satisfied by every tuple in the
relation.
The department relation
SQL> create table department(dept_name varchar(20),building varchar(20),budget numeric(12,2),primary
key(dept_name));
Table created.
SQL> desc department
Name Null? Type
----------------------------------------- -------- ---------------------
DEPT_NAME NOT NULL VARCHAR2(20)
BUILDING VARCHAR2(20)
BUDGET NUMBER(12,2)
The course relation
SQL> create table course(course_id varchar(7),title varchar(50),dept_name varchar(20),credits numeri
c(2,0),primary key(course_id),foreign key(dept_name) references department);
Table created.
SQL> desc course
Name Null? Type
----------------------------------------- -------- -----------------
COURSE_ID NOT NULL VARCHAR2(7)
TITLE VARCHAR2(50)
DEPT_NAME VARCHAR2(20)
CREDITS NUMBER(2)
The instructor relation
SQL> create table instructor(ID varchar(5),name varchar(20) not null,dept_name varchar(20),salary nu
meric(8,2), primary key(ID),foreign key(dept_name)references department);
Table created.
SQL> desc instructor
Name Null? Type
3. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 3
----------------------------------------- -------- ----------------
ID NOT NULL VARCHAR2(5)
NAME NOT NULL VARCHAR2(20)
DEPT_NAME VARCHAR2(20)
SALARY NUMBER(8,2)
The section relation
SQL> create table section(course_id varchar(8),sec_id varchar(8),semester varchar(6),year numeric(4,
0),building varchar(15),room_number varchar(7),time_slot_id varchar(4),primary key(course_id,sec_id,
semester,year),foreign key(course_id) references course);
Table created.
SQL> desc section
Name Null? Type
----------------------------------------- -------- --------------------
COURSE_ID NOT NULL VARCHAR2(8)
SEC_ID NOT NULL VARCHAR2(8)
SEMESTER NOT NULL VARCHAR2(6)
YEAR NOT NULL NUMBER(4)
BUILDING VARCHAR2(15)
ROOM_NUMBER VARCHAR2(7)
TIME_SLOT_ID VARCHAR2(4)
The teaches relation
SQL> create table teaches(ID varchar(5),course_id varchar(8),sec_id varchar(8),semester varchar(6),y
ear numeric(4,0),primary key(ID,course_id,sec_id,semester,year),foreign key(course_id,sec_id,semeste
r,year) references section,foreign key(ID) references instructor);
Table created.
SQL> desc teaches
Name Null? Type
----------------------------------------- -------- ------------
ID NOT NULL VARCHAR2(5)
COURSE_ID NOT NULL VARCHAR2(8)
SEC_ID NOT NULL VARCHAR2(8)
SEMESTER NOT NULL VARCHAR2(6)
YEAR NOT NULL NUMBER(4)
4. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 4
ALTER Command
The Oracle ALTER TABLE statement is used to add, modify, or drop/delete columns in a table. The Oracle
ALTER TABLE statement is also used to rename a table.
Add column in table
To ADD A COLUMN in a table, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
ADD column_name column-definition;
Example: Let's look at an example that shows how to add a column in an Oracle table using the
ALTER TABLE statement.
ALTER TABLE customers
ADD customer_name varchar2(45);
This Oracle ALTER TABLE example will add a column called customer_name to the customers
table.
Add multiple columns in table
To ADD MULTIPLE COLUMNS to an existing table, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
ADD (column_1 column-definition,
column_2 column-definition,
...
column_n column_definition);
Example: Let's look at an example that shows how to add multiple columns in an Oracle table using
the ALTER TABLE statement.
ALTER TABLE customers
ADD (customer_name varchar2(45),
city varchar2(40));
This Oracle ALTER TABLE example will add two columns, customer_name as a varchar2(45) field
and city as a varchar2(40) field to the customers table.
Modify column in table
To MODIFY A COLUMN in an existing table, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
MODIFY column_name column_type;
Example: Let's look at an example that shows how to modify a column in an Oracle table using the
ALTER TABLE statement.
ALTER TABLE customers
MODIFY customer_name varchar2(100) not null;
This Oracle ALTER TABLE example will modify the column called customer_name to be a data
type of varchar2(100) and force the column to not allow null values.
Modify Multiple columns in table
To MODIFY MULTIPLE COLUMNS in an existing table, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
5. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 5
MODIFY (column_1 column_type,
column_2 column_type,
...
column_n column_type);
Example: Let's look at an example that shows how to modify multiple columns in an Oracle table
using the ALTER TABLE statement.
ALTER TABLE customers
MODIFY (customer_name varchar2(100) not null,
city varchar2(75));
This Oracle ALTER TABLE example will modify both the customer_name and city columns.
Drop column in table
To DROP A COLUMN in an existing table, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
DROP COLUMN column_name;
Example: Let's look at an example that shows how to drop a column in an Oracle table using the
ALTER TABLE statement.
ALTER TABLE customers
DROP COLUMN customer_name;
This Oracle ALTER TABLE example will drop the column called customer_name from the table
called customers.
Rename column in table
To RENAME A COLUMN in an existing table, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
Example: Let's look at an example that shows how to rename a column in an Oracle table using
the ALTER TABLE statement.
ALTER TABLE customers
RENAME COLUMN customer_name to cname;
This Oracle ALTER TABLE example will rename the column called customer_name to cname.
Rename table
To RENAME A TABLE, the Oracle ALTER TABLE syntax is:
ALTER TABLE table_name
RENAME TO new_table_name;
Example: Let's look at an example that shows how to rename a table in Oracle using the ALTER
TABLE statement.
ALTER TABLE customers
RENAME TO contacts;
This Oracle ALTER TABLE example will rename the customers table to contacts.
SQL> create table student(roll_no char(10),first_name char(30),middle_name char(30),last_name char(3
0),address char(50),city char(20),pincode numeric(6,0),state char(20),mobile_number integer);
6. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 6
Table created.
SQL> desc student;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO CHAR(10)
FIRST_NAME CHAR(30)
MIDDLE_NAME CHAR(30)
LAST_NAME CHAR(30)
ADDRESS CHAR(50)
CITY CHAR(20)
PINCODE NUMBER(6)
STATE CHAR(20)
MOBILE_NUMBER NUMBER(38)
SQL> alter table student drop column mobile_number;
Table altered.
SQL> desc student;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO CHAR(10)
FIRST_NAME CHAR(30)
MIDDLE_NAME CHAR(30)
LAST_NAME CHAR(30)
ADDRESS CHAR(50)
CITY CHAR(20)
PINCODE NUMBER(6)
STATE CHAR(20)
SQL> alter table student add mobile_number varchar(10);
Table altered.
SQL> desc student;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO CHAR(10)
FIRST_NAME CHAR(30)
MIDDLE_NAME CHAR(30)
LAST_NAME CHAR(30)
ADDRESS CHAR(50)
7. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 7
CITY CHAR(20)
PINCODE NUMBER(6)
STATE CHAR(20)
MOBILE_NUMBER VARCHAR2(10)
8. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 8
DROP Command
To remove a relation from an SQL database, we use the drop table command. The drop table
command deletes all information about the dropped relation from the database. The command
drop table r
is a more drastic action than
delete from r
The latter retains relation r, but deletes all tuples in r. The former deletes not only all tuples of r, but
also the schema for r. After r is dropped, no tuples can be inserted into r unless it is re-created with
the create table command.
SQL> desc student;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLL_NO CHAR(10)
FIRST_NAME CHAR(30)
MIDDLE_NAME CHAR(30)
LAST_NAME CHAR(30)
ADDRESS CHAR(50)
CITY CHAR(20)
PINCODE NUMBER(6)
STATE CHAR(20)
MOBILE_NUMBER VARCHAR2(10)
SQL> drop table student;
Table dropped.
SQL> desc student;
ERROR:
ORA-04043: object student does not exist
9. Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 9
Practice Problem Statement:
Schema for Hospital Management Example
admitted(patient_id, department_num ,date_admission, date_discharge, doctor_id, doctor_grade,
prescription)
patient (patient_id, patient_name, dt_birth, patient_address)
doctors (employee_id, name, address, contact_number , qualifications, grade)
department (department_num, department_name, total_worker_count, floor)
workers(employee_id, name, address, type)
nurses(nurse_id, name, address)
works_for (department_num, employee_id, schedule)
outdoor(patient_id, department_num, doctor_id, prescription, date_checkup)
emergency(doctor_id, nurse_id, date)
References:
Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill
International Edition) sixth edition.
Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill
International Edition) fifth edition.
http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/
http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/
http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/
MOOCs: Database Management System:
https://onlinecourses.nptel.ac.in/noc18_cs15