2
Most read
3
Most read
9
Most read
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
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
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)
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
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);
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)
Mrs. Sunita M Dol, CSE Department
WIT, Solapur Page 7
CITY CHAR(20)
PINCODE NUMBER(6)
STATE CHAR(20)
MOBILE_NUMBER VARCHAR2(10)
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
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

More Related Content

PDF
Software engineering practical
PDF
4. DML.pdf
PPTX
Null values, insert, delete and update in database
PPT
Introduction to structured query language (sql)
PPTX
PDF
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
PPT
Aggregate functions
PPT
Introduction to sql
Software engineering practical
4. DML.pdf
Null values, insert, delete and update in database
Introduction to structured query language (sql)
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Aggregate functions
Introduction to sql

What's hot (20)

PPTX
introdution to SQL and SQL functions
PPTX
Database Testing
PPTX
Group By, Order By, and Aliases in SQL
PPTX
Data base testing
PPTX
advanced sql(database)
PPT
Review of SQL
PPTX
Sql queries presentation
PPTX
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
PDF
Assignment#01
PPTX
SQL Basics
PPTX
E-R diagram & SQL
PPTX
basic structure of SQL FINAL.pptx
PDF
Relational Database Management System (RDBMS) LAB - GROUP B
PPTX
DATABASE CONSTRAINTS
DOC
Database queries
PDF
Sql tutorial
PPTX
sql function(ppt)
PPT
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
PPTX
Database Management - Lecture 2 - SQL select, insert, update and delete
PPTX
Introduction to SQL
introdution to SQL and SQL functions
Database Testing
Group By, Order By, and Aliases in SQL
Data base testing
advanced sql(database)
Review of SQL
Sql queries presentation
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Assignment#01
SQL Basics
E-R diagram & SQL
basic structure of SQL FINAL.pptx
Relational Database Management System (RDBMS) LAB - GROUP B
DATABASE CONSTRAINTS
Database queries
Sql tutorial
sql function(ppt)
CHAPTER-7 C++ PROGRAMMING ( STRUCTURE IN C++)
Database Management - Lecture 2 - SQL select, insert, update and delete
Introduction to SQL
Ad

Similar to 3. DDL.pdf (20)

PPTX
Oraclesql
PPT
SQL : introduction
PPTX
data base programming chapter2 29 slides
PPTX
SQL PPT.pptx
PPT
UNIT 2 Structural Query Language UPDATED
PPTX
DDL(Data defination Language ) Using Oracle
PPTX
Sql commands
PPT
Db1 lecture4
PPTX
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
PDF
4 the sql_standard
DOCX
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
PPTX
Creating database using sql commands
PPTX
session_2_sqlpptxfhfhfhfdhfdhkkfdhfdhfdh
PPTX
Data Definition Language Commands in DBMS
PPT
Structure query language - Data definition language.ppt
PPT
DDL. data defination language for creating database
DOCX
SQL Tutorial for BCA-2
PPTX
Data base.ppt
PDF
Relational database management system
Oraclesql
SQL : introduction
data base programming chapter2 29 slides
SQL PPT.pptx
UNIT 2 Structural Query Language UPDATED
DDL(Data defination Language ) Using Oracle
Sql commands
Db1 lecture4
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
4 the sql_standard
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Creating database using sql commands
session_2_sqlpptxfhfhfhfdhfdhkkfdhfdhfdh
Data Definition Language Commands in DBMS
Structure query language - Data definition language.ppt
DDL. data defination language for creating database
SQL Tutorial for BCA-2
Data base.ppt
Relational database management system
Ad

More from Sunita Milind Dol (20)

PDF
Assignment No. 10 on Unit-IV Set Theory, Relations and Function
PDF
Assignment No. 9 on Unit-IV Set Theory, Relations and Function
PDF
Assignment No. 8 on Unit-IV Set Theory, Relations and Function
PDF
Assignment No. 7 on Unit-IV - Set Theory, Relations and Function
PDF
Assignment No. 6 on Representation of Expression
PDF
Assignment No. 5 on Unit-III Representation of Expression
PDF
Assignment No. 4 on Unit-II Mathematical Logic
PDF
Assignment No. 3 on Unit-II Mathematical Logic
PDF
Assignment No. 2 on Unit-I Mathematical Induction
PDF
Assignment No. 1 on Unit-I Fundamental Principles of Counting
PDF
Unit Number 5 - Research Ethics, IPR and Publishing
PDF
Unit Number 4 - Research reports and Thesis writing
PDF
Unit Number 3 - Data collection and Statistical Analysis
PDF
Unit Number 2 - Research Problem Formulation and Methods
PDF
Unit Number 1 - Introduction to Research
PDF
Unit Number 5 - Research Ethics, IPR and Publishing
PDF
Unit Number 5 - Research reports and Thesis writing
PDF
Unit Number 3 - Data collection and Statistical Analysis
PDF
Unit Number 2 - Research Problem Formulation and Methods
PDF
Unit Number 1 : Introduction to Research
Assignment No. 10 on Unit-IV Set Theory, Relations and Function
Assignment No. 9 on Unit-IV Set Theory, Relations and Function
Assignment No. 8 on Unit-IV Set Theory, Relations and Function
Assignment No. 7 on Unit-IV - Set Theory, Relations and Function
Assignment No. 6 on Representation of Expression
Assignment No. 5 on Unit-III Representation of Expression
Assignment No. 4 on Unit-II Mathematical Logic
Assignment No. 3 on Unit-II Mathematical Logic
Assignment No. 2 on Unit-I Mathematical Induction
Assignment No. 1 on Unit-I Fundamental Principles of Counting
Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 4 - Research reports and Thesis writing
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 1 - Introduction to Research
Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research reports and Thesis writing
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 1 : Introduction to Research

Recently uploaded (20)

PDF
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
PDF
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
PPTX
Core Concepts of Personalized Learning and Virtual Learning Environments
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PPTX
Climate Change and Its Global Impact.pptx
PPTX
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
PDF
Race Reva University – Shaping Future Leaders in Artificial Intelligence
PDF
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
PPTX
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PDF
Empowerment Technology for Senior High School Guide
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
Journal of Dental Science - UDMY (2022).pdf
PPTX
B.Sc. DS Unit 2 Software Engineering.pptx
PDF
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
PDF
My India Quiz Book_20210205121199924.pdf
PDF
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
LEARNERS WITH ADDITIONAL NEEDS ProfEd Topic
FOISHS ANNUAL IMPLEMENTATION PLAN 2025.pdf
Core Concepts of Personalized Learning and Virtual Learning Environments
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
Climate Change and Its Global Impact.pptx
DRUGS USED FOR HORMONAL DISORDER, SUPPLIMENTATION, CONTRACEPTION, & MEDICAL T...
Race Reva University – Shaping Future Leaders in Artificial Intelligence
Skin Care and Cosmetic Ingredients Dictionary ( PDFDrive ).pdf
ELIAS-SEZIURE AND EPilepsy semmioan session.pptx
Literature_Review_methods_ BRACU_MKT426 course material
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
Empowerment Technology for Senior High School Guide
Journal of Dental Science - UDMY (2021).pdf
Journal of Dental Science - UDMY (2022).pdf
B.Sc. DS Unit 2 Software Engineering.pptx
LIFE & LIVING TRILOGY - PART - (2) THE PURPOSE OF LIFE.pdf
My India Quiz Book_20210205121199924.pdf
LIFE & LIVING TRILOGY- PART (1) WHO ARE WE.pdf
Cambridge-Practice-Tests-for-IELTS-12.docx

3. DDL.pdf

  • 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