SlideShare a Scribd company logo
1 of 9
Download to read offline
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

What's hot

Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusemailharmeet
 
Windows form application_in_vb(vb.net --3 year)
Windows form application_in_vb(vb.net --3 year)Windows form application_in_vb(vb.net --3 year)
Windows form application_in_vb(vb.net --3 year)Ankit Gupta
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScriptT11 Sessions
 
directory structure and file system mounting
directory structure and file system mountingdirectory structure and file system mounting
directory structure and file system mountingrajshreemuthiah
 
Data modeling using the entity relationship model
Data modeling using the entity relationship modelData modeling using the entity relationship model
Data modeling using the entity relationship modelJafar Nesargi
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functionsVikas Gupta
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in pythonLifna C.S
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Creating a database
Creating a databaseCreating a database
Creating a databaseRahul Gupta
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship DiagramShakila Mahjabin
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Microsoft Windows File System in Operating System
Microsoft Windows File System in Operating SystemMicrosoft Windows File System in Operating System
Microsoft Windows File System in Operating SystemMeghaj Mallick
 
Introduction to Oracle Database
Introduction to Oracle DatabaseIntroduction to Oracle Database
Introduction to Oracle Databasepuja_dhar
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programmingsrijavel
 
Decomposition using Functional Dependency
Decomposition using Functional DependencyDecomposition using Functional Dependency
Decomposition using Functional DependencyRaj Naik
 

What's hot (20)

File organization
File organizationFile organization
File organization
 
Lecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculusLecture 06 relational algebra and calculus
Lecture 06 relational algebra and calculus
 
Windows form application_in_vb(vb.net --3 year)
Windows form application_in_vb(vb.net --3 year)Windows form application_in_vb(vb.net --3 year)
Windows form application_in_vb(vb.net --3 year)
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
directory structure and file system mounting
directory structure and file system mountingdirectory structure and file system mounting
directory structure and file system mounting
 
Data modeling using the entity relationship model
Data modeling using the entity relationship modelData modeling using the entity relationship model
Data modeling using the entity relationship model
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
File and directories in python
File and directories in pythonFile and directories in python
File and directories in python
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Creating a database
Creating a databaseCreating a database
Creating a database
 
Unit 01 dbms
Unit 01 dbmsUnit 01 dbms
Unit 01 dbms
 
Entity Relationship Diagram
Entity Relationship DiagramEntity Relationship Diagram
Entity Relationship Diagram
 
File system structure
File system structureFile system structure
File system structure
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Microsoft Windows File System in Operating System
Microsoft Windows File System in Operating SystemMicrosoft Windows File System in Operating System
Microsoft Windows File System in Operating System
 
Introduction to Oracle Database
Introduction to Oracle DatabaseIntroduction to Oracle Database
Introduction to Oracle Database
 
Object and class
Object and classObject and class
Object and class
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
 
Decomposition using Functional Dependency
Decomposition using Functional DependencyDecomposition using Functional Dependency
Decomposition using Functional Dependency
 

Similar to 3. DDL.pdf

Similar to 3. DDL.pdf (20)

Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
 
Assignment#01
Assignment#01Assignment#01
Assignment#01
 
Assignment#07
Assignment#07Assignment#07
Assignment#07
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
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 wksht-2
Sql wksht-2Sql wksht-2
Sql wksht-2
 
Sql
SqlSql
Sql
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Unit04 dbms
Unit04 dbmsUnit04 dbms
Unit04 dbms
 
Les09
Les09Les09
Les09
 
Assignment#02
Assignment#02Assignment#02
Assignment#02
 
Integrity Constraints in Database Management System.ppt
Integrity Constraints in Database Management System.pptIntegrity Constraints in Database Management System.ppt
Integrity Constraints in Database Management System.ppt
 
Sql ch 12 - creating database
Sql ch 12 - creating databaseSql ch 12 - creating database
Sql ch 12 - creating database
 
SQL Query
SQL QuerySQL Query
SQL Query
 
Ankit
AnkitAnkit
Ankit
 
12 SQL
12 SQL12 SQL
12 SQL
 
12 SQL
12 SQL12 SQL
12 SQL
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Query
QueryQuery
Query
 
chapter9-SQL.pptx
chapter9-SQL.pptxchapter9-SQL.pptx
chapter9-SQL.pptx
 

More from Sunita Milind Dol (20)

9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
 
8.Views.pdf
8.Views.pdf8.Views.pdf
8.Views.pdf
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
 
6. Aggregate Functions.pdf
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
 
5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf
 
4. DML.pdf
4. DML.pdf4. DML.pdf
4. DML.pdf
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
 
Assignment12
Assignment12Assignment12
Assignment12
 
Assignment11
Assignment11Assignment11
Assignment11
 
Assignment10
Assignment10Assignment10
Assignment10
 
Assignment9
Assignment9Assignment9
Assignment9
 
Assignment8
Assignment8Assignment8
Assignment8
 
Assignment7
Assignment7Assignment7
Assignment7
 
Assignment6
Assignment6Assignment6
Assignment6
 
Assignment5
Assignment5Assignment5
Assignment5
 
Assignment4
Assignment4Assignment4
Assignment4
 
Assignment3
Assignment3Assignment3
Assignment3
 
Assignment2
Assignment2Assignment2
Assignment2
 
Assignment1
Assignment1Assignment1
Assignment1
 

Recently uploaded

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

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