SlideShare a Scribd company logo
2
Most read
Data Definition Language (DDL) Statements
Mohd Tousif
Data definition language (DDL) statements enable you to perform these tasks:
 Create, alter, and drop schema objects
 Grant and revoke privileges and roles
 Analyze information on a table, index, or cluster
 Establish auditing options
 Add comments to the data dictionary
The CREATE; ALTER and DROP commands require exclusive access to the specified
object.
For example, an ALTER TABLE statement fails if another user has an open transaction
on the specified table.
The GRANT, REVOKE, ANALYZE, AUDIT, and COMMENT commands do not require exclusive
access to the specified object.
For example, you can analyze a table while other users are updating the table.
Oracle implicitly commits the current transaction before and after every DDL
statement.
Many DDL statements may cause Oracle to recompile or reauthorize schema objects.
DDL Statements are
CREATE : Use to create objects like CREATE TABLE, CREATE FUNCTION,
CREATE SYNONYM, CREATE VIEW. Etc.
ALTER : Use to Alter Objects like ALTER TABLE, ALTER USER, ALTER
TABLESPACE, ALTER DATABASE. Etc.
DROP : Use to Drop Objects like DROP TABLE, DROP USER, DROP
TABLESPACE, DROP FUNCTION. Etc.
REPLACE : Use to rename table names.
TRUNCATE : Use to truncate (delete all rows) a table.
Create
To create tables, views, synonyms, sequences, functions, procedures, packages etc.
Example
To create a table, you can give the following statement
create table emp (empno number(5) primary key,
name varchar2(20),
sal number(10,2),
job varchar2(20),
mgr number(5),
Hiredate date,
comm number(10,2));
Now suppose you have emp table. now you want to create a TAX table with the
following structure and also insert rows of those employees whose salary is above
5000.
Tax
Empno
Tax
Number(5)
Number(10,2)
To do this we can first create TAX table by defining column names and datatypes and
then use INSERT into EMP SELECT …. statement to insert rows from emp table
like given below.
create table tax (empno number(5), tax number(10,2));
insert into tax select empno,(sal-5000)*0.40
from emp where sal > 5000;
Instead of executing the above two statements the same result can be achieved by
giving a single CREATE TABLE AS statement.
create table tax as select empno,(sal-5000)*0.4
as tax from emp where sal>5000
You can also use CREATE TABLE AS statement to create copies of tables. Like to
create a copy EMP table as EMP2 you can give the following statement.
create table emp2 as select * from emp;
To copy tables without rows i.e. to just copy the structure give the following statement
create table emp2 as select * from emp where 1=2;
Temporary Tables (From Oracle Ver. 8i)
It is also possible to create a temporary table. The definition of a temporary table is
visible to all sessions, but the data in a temporary table is visible only to the session
that inserts the data into the table. You use the CREATE GLOBAL TEMPORARY
TABLE statement to create a temporary table. The ON COMMIT keywords indicate if the
data in the table is transaction-specific(the default) or session-specific:
 ON COMMIT DELETE ROWS specifies that the temporary table is transaction specific and Oracle
truncates the table (delete all rows) after each commit.
 ON COMMIT PRESERVE ROWS specifies that the temporary table is session specific and Oracle
truncates the table when you terminate the session.
This example creates a temporary table that is transaction specific:
CREATE GLOBAL TEMPORARY TABLE taxable_emp
(empno number(5),
ename varchar2(20),
sal number(10,2),
tax number(10,2))
ON COMMIT DELETE ROWS;
Indexes can be created on temporary tables. They are also temporary and the data in
the index has the same session or transaction scope as the data in the underlying table.
Alter
Use the ALTER TABLE statement to alter the structure of a table.
Examples:
To add new columns addr, city, pin, ph, fax to employee table you can give the
following statement
alter table emp add (addr varchar2(20), city
varchar2(20),
pin varchar2(10),ph varchar2(20));
To modify the datatype and width of a column. For example we you want to increase
the length of the column ename from varchar2(20) to varchar2(30) then give the
following command.
ALTER table emp modify (ename varchar2(30))
To decrease the width of a column the column can be decreased up to largest value it
holds.
ALTER table emp modify (ename varchar2(15));
The above is possible only if you are using Oracle ver 8i and above. In Oracle 8.0 and
7.3 you cannot decrease the column width directly unless the column is empty.
To change the datatype, column must be empty in All Oracle Versions.
To drop columns
From Oracle Ver. 8i you can drop columns directly it was not possible in previous
versions.
For example to drop PIN, CITY columns from emp table.
ALTER table emp drop column (pin, city);
Remember you cannot drop the column if the table is having only one column.
If the column you want to drop is having primary key constraint on it then you have to
give cascade constraint clause.
ALTER table emp2 drop column (empno) cascade constraints;
To drop columns in previous versions of Oracle8.0 and 7.3 and to change the column
name in all Oracle versions do the following.
For example we want to drop pin and city columns and to change SAL column name
to SALARY.
Step 1: Create a temporary table with desired columns using subquery.
CREATE TABLE temp as select empno, ename,
sal AS salary, addr, ph from emp;
Step 2: Drop the original table.
DROP table emp;
Step 3: Rename the temporary table to the original table.
RENAME temp to emp;
Rename
Use the RENAME statement to rename a table, view, sequence, or private synonym for a
table, view, or sequence.
 Oracle automatically transfers integrity constraints, indexes, and grants on the old object to the
new object.
 Oracle invalidates all objects that depend on the renamed object, such as views, synonyms, and
stored procedures and functions that refer to a renamed table.
Example
To rename table emp2 to employee2 you can give the following command.
RENAME emp2 to employee2
Drop
Use the drop statement to drop tables, functions, procedures, packages, views,
synonym, sequences, tablespaces etc.
Example
The following command drops table emp2
DROP table emp2;
If emp2 table is having primary key constraint, to which other tables refer to, then you
have to first drop referential integrity constraint and then drop the table. Or if you
want to drop table by dropping the referential constraints then give the following
command
DROP table emp2 cascade constraints;
Truncate
Use the Truncate statement to delete all the rows from table permanently. It is same as
“DELETE FROM <table_name>” except
 Truncate does not generate any rollback data hence, it cannot be roll backed.
 If any delete triggers are defined on the table. Then the triggers are not fired
 It de-allocates free extents from the table. So that the free space can be use by other tables.
Example
TRUNCATE table emp;
If you do not want free space and keep it with the table. Then specify the REUSE
storage clause like this
TRUNCATE table emp reuse storage;

More Related Content

PPTX
Oracle: PLSQL Commands
DataminingTools Inc
 
PDF
Proc sql tips
Naresh Kumar Gamidi
 
DOCX
Ddl commands
Vasudeva Rao
 
PPT
Les10
arnold 7490
 
PPT
Select To Order By
Krizia Capacio
 
PDF
Sql basics v2
Yousuf Akhtar Sultan
 
PPT
Les02
Sudharsan S
 
Oracle: PLSQL Commands
DataminingTools Inc
 
Proc sql tips
Naresh Kumar Gamidi
 
Ddl commands
Vasudeva Rao
 
Select To Order By
Krizia Capacio
 
Sql basics v2
Yousuf Akhtar Sultan
 

What's hot (18)

ODP
Babitha2.mysql
banubabitha
 
PPTX
SQL
Jerin John
 
PPTX
Oracle: DDL
DataminingTools Inc
 
PPTX
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
PDF
Database Systems - SQL - DDL Statements (Chapter 3/2)
Vidyasagar Mundroy
 
PPT
Les09
arnold 7490
 
PPSX
Oracle Training in Kochi | Trivandrum |Thrissur
IndiaOptions Softwares
 
PPT
MY SQL
sundar
 
PPTX
Oracle: DML
DataminingTools Inc
 
PPTX
Oracle: Basic SQL
DataminingTools Inc
 
PPTX
Sql
Aman Lalpuria
 
PPTX
Lab1 select statement
Balqees Al.Mubarak
 
PPTX
Commands of DML in SQL
Ashish Gaurkhede
 
PPTX
Lecture 3 sql {basics ddl commands}
Shubham Shukla
 
PPTX
Lecture 4 sql {basics keys and constraints}
Shubham Shukla
 
PPTX
Commands
Ayushi Goyal
 
PDF
Nested Queries Lecture
Felipe Costa
 
Babitha2.mysql
banubabitha
 
Oracle: DDL
DataminingTools Inc
 
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
Database Systems - SQL - DDL Statements (Chapter 3/2)
Vidyasagar Mundroy
 
Oracle Training in Kochi | Trivandrum |Thrissur
IndiaOptions Softwares
 
MY SQL
sundar
 
Oracle: DML
DataminingTools Inc
 
Oracle: Basic SQL
DataminingTools Inc
 
Lab1 select statement
Balqees Al.Mubarak
 
Commands of DML in SQL
Ashish Gaurkhede
 
Lecture 3 sql {basics ddl commands}
Shubham Shukla
 
Lecture 4 sql {basics keys and constraints}
Shubham Shukla
 
Commands
Ayushi Goyal
 
Nested Queries Lecture
Felipe Costa
 
Ad

Similar to Data Definition Language (DDL) (20)

PPT
SQL WORKSHOP::Lecture 10
Umair Amjad
 
PPT
Les10[1]Creating and Managing Tables
siavosh kaviani
 
DOC
SQL
Tuhin_Das
 
PDF
DBMS_ddlVFSBFSBS22222222222222222222222222222222222
227567
 
PPT
DDL. data defination language for creating database
SHAKIR325211
 
PPT
SQL DDL
Vikas Gupta
 
PPT
database management system lessonchapter
MohammedNouh7
 
PPT
Sql intro & ddl 1
Dr. C.V. Suresh Babu
 
PPT
Sql intro & ddl 1
Dr. C.V. Suresh Babu
 
PPTX
8. sql
khoahuy82
 
PPTX
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
universalcomputer1
 
PPT
Structure query language - Data definition language.ppt
munmunitjusl
 
PPTX
data base programming chapter2 29 slides
nights1988
 
PPTX
Oraclesql
Priya Goyal
 
PPTX
DDL Commands in Database Management System.pptx
chintanraval26
 
PPTX
My SQL.pptx
KieveBarreto1
 
PDF
Sql tutorial
amitabros
 
DOC
Oracle SQL AND PL/SQL
suriyae1
 
PPT
Sql dml & tcl 2
Dr. C.V. Suresh Babu
 
PDF
3. DDL.pdf
Sunita Milind Dol
 
SQL WORKSHOP::Lecture 10
Umair Amjad
 
Les10[1]Creating and Managing Tables
siavosh kaviani
 
DBMS_ddlVFSBFSBS22222222222222222222222222222222222
227567
 
DDL. data defination language for creating database
SHAKIR325211
 
SQL DDL
Vikas Gupta
 
database management system lessonchapter
MohammedNouh7
 
Sql intro & ddl 1
Dr. C.V. Suresh Babu
 
Sql intro & ddl 1
Dr. C.V. Suresh Babu
 
8. sql
khoahuy82
 
DBMS week 2 hjghg hvgfhgf,3 BSCS 6th.pptx
universalcomputer1
 
Structure query language - Data definition language.ppt
munmunitjusl
 
data base programming chapter2 29 slides
nights1988
 
Oraclesql
Priya Goyal
 
DDL Commands in Database Management System.pptx
chintanraval26
 
My SQL.pptx
KieveBarreto1
 
Sql tutorial
amitabros
 
Oracle SQL AND PL/SQL
suriyae1
 
Sql dml & tcl 2
Dr. C.V. Suresh Babu
 
3. DDL.pdf
Sunita Milind Dol
 
Ad

More from Mohd Tousif (20)

PDF
Sql commands
Mohd Tousif
 
PPT
Sql basics and DDL statements
Mohd Tousif
 
PDF
SQL practice questions set
Mohd Tousif
 
PPT
Introduction to Databases
Mohd Tousif
 
PPS
Entity Relationship Model - An Example
Mohd Tousif
 
PDF
Entity Relationship (ER) Model Questions
Mohd Tousif
 
PPTX
Entity Relationship (ER) Model
Mohd Tousif
 
PDF
SQL Practice Question set
Mohd Tousif
 
PDF
Introduction to Databases - Assignment_1
Mohd Tousif
 
PDF
Data Warehouse Concepts and Architecture
Mohd Tousif
 
DOC
SQL practice questions set - 2
Mohd Tousif
 
DOC
SQL practice questions - set 3
Mohd Tousif
 
DOC
SQL practice questions for beginners
Mohd Tousif
 
PDF
Oracle sql tutorial
Mohd Tousif
 
PDF
Sql (Introduction to Structured Query language)
Mohd Tousif
 
PDF
Sql commands
Mohd Tousif
 
PDF
Virtual box
Mohd Tousif
 
PDF
Deadlock
Mohd Tousif
 
PDF
Algorithm o.s.
Mohd Tousif
 
PPTX
System components of windows xp
Mohd Tousif
 
Sql commands
Mohd Tousif
 
Sql basics and DDL statements
Mohd Tousif
 
SQL practice questions set
Mohd Tousif
 
Introduction to Databases
Mohd Tousif
 
Entity Relationship Model - An Example
Mohd Tousif
 
Entity Relationship (ER) Model Questions
Mohd Tousif
 
Entity Relationship (ER) Model
Mohd Tousif
 
SQL Practice Question set
Mohd Tousif
 
Introduction to Databases - Assignment_1
Mohd Tousif
 
Data Warehouse Concepts and Architecture
Mohd Tousif
 
SQL practice questions set - 2
Mohd Tousif
 
SQL practice questions - set 3
Mohd Tousif
 
SQL practice questions for beginners
Mohd Tousif
 
Oracle sql tutorial
Mohd Tousif
 
Sql (Introduction to Structured Query language)
Mohd Tousif
 
Sql commands
Mohd Tousif
 
Virtual box
Mohd Tousif
 
Deadlock
Mohd Tousif
 
Algorithm o.s.
Mohd Tousif
 
System components of windows xp
Mohd Tousif
 

Recently uploaded (20)

PPTX
Introduction-to-Python-Programming-Language (1).pptx
dhyeysapariya
 
PDF
202501214233242351219 QASS Session 2.pdf
lauramejiamillan
 
PDF
Fundamentals and Techniques of Biophysics and Molecular Biology (Pranav Kumar...
RohitKumar868624
 
PDF
717629748-Databricks-Certified-Data-Engineer-Professional-Dumps-by-Ball-21-03...
pedelli41
 
PPTX
short term internship project on Data visualization
JMJCollegeComputerde
 
PPTX
M1-T1.pptxM1-T1.pptxM1-T1.pptxM1-T1.pptx
teodoroferiarevanojr
 
PPTX
Fuzzy_Membership_Functions_Presentation.pptx
pythoncrazy2024
 
PPTX
Introduction to Data Analytics and Data Science
KavithaCIT
 
PPTX
The whitetiger novel review for collegeassignment.pptx
DhruvPatel754154
 
PPTX
Multiscale Segmentation of Survey Respondents: Seeing the Trees and the Fores...
Sione Palu
 
PPTX
lecture 13 mind test academy it skills.pptx
ggesjmrasoolpark
 
PPTX
Probability systematic sampling methods.pptx
PrakashRajput19
 
PDF
202501214233242351219 QASS Session 2.pdf
lauramejiamillan
 
PPTX
Databricks-DE-Associate Certification Questions-june-2024.pptx
pedelli41
 
PPT
Real Life Application of Set theory, Relations and Functions
manavparmar205
 
PDF
blockchain123456789012345678901234567890
tanvikhunt1003
 
PDF
D9110.pdfdsfvsdfvsdfvsdfvfvfsvfsvffsdfvsdfvsd
minhn6673
 
PPTX
Pipeline Automatic Leak Detection for Water Distribution Systems
Sione Palu
 
PPTX
Introduction to Biostatistics Presentation.pptx
AtemJoshua
 
PDF
Blue Futuristic Cyber Security Presentation.pdf
tanvikhunt1003
 
Introduction-to-Python-Programming-Language (1).pptx
dhyeysapariya
 
202501214233242351219 QASS Session 2.pdf
lauramejiamillan
 
Fundamentals and Techniques of Biophysics and Molecular Biology (Pranav Kumar...
RohitKumar868624
 
717629748-Databricks-Certified-Data-Engineer-Professional-Dumps-by-Ball-21-03...
pedelli41
 
short term internship project on Data visualization
JMJCollegeComputerde
 
M1-T1.pptxM1-T1.pptxM1-T1.pptxM1-T1.pptx
teodoroferiarevanojr
 
Fuzzy_Membership_Functions_Presentation.pptx
pythoncrazy2024
 
Introduction to Data Analytics and Data Science
KavithaCIT
 
The whitetiger novel review for collegeassignment.pptx
DhruvPatel754154
 
Multiscale Segmentation of Survey Respondents: Seeing the Trees and the Fores...
Sione Palu
 
lecture 13 mind test academy it skills.pptx
ggesjmrasoolpark
 
Probability systematic sampling methods.pptx
PrakashRajput19
 
202501214233242351219 QASS Session 2.pdf
lauramejiamillan
 
Databricks-DE-Associate Certification Questions-june-2024.pptx
pedelli41
 
Real Life Application of Set theory, Relations and Functions
manavparmar205
 
blockchain123456789012345678901234567890
tanvikhunt1003
 
D9110.pdfdsfvsdfvsdfvsdfvfvfsvfsvffsdfvsdfvsd
minhn6673
 
Pipeline Automatic Leak Detection for Water Distribution Systems
Sione Palu
 
Introduction to Biostatistics Presentation.pptx
AtemJoshua
 
Blue Futuristic Cyber Security Presentation.pdf
tanvikhunt1003
 

Data Definition Language (DDL)

  • 1. Data Definition Language (DDL) Statements Mohd Tousif Data definition language (DDL) statements enable you to perform these tasks:  Create, alter, and drop schema objects  Grant and revoke privileges and roles  Analyze information on a table, index, or cluster  Establish auditing options  Add comments to the data dictionary The CREATE; ALTER and DROP commands require exclusive access to the specified object. For example, an ALTER TABLE statement fails if another user has an open transaction on the specified table. The GRANT, REVOKE, ANALYZE, AUDIT, and COMMENT commands do not require exclusive access to the specified object. For example, you can analyze a table while other users are updating the table. Oracle implicitly commits the current transaction before and after every DDL statement. Many DDL statements may cause Oracle to recompile or reauthorize schema objects. DDL Statements are CREATE : Use to create objects like CREATE TABLE, CREATE FUNCTION, CREATE SYNONYM, CREATE VIEW. Etc. ALTER : Use to Alter Objects like ALTER TABLE, ALTER USER, ALTER TABLESPACE, ALTER DATABASE. Etc. DROP : Use to Drop Objects like DROP TABLE, DROP USER, DROP TABLESPACE, DROP FUNCTION. Etc.
  • 2. REPLACE : Use to rename table names. TRUNCATE : Use to truncate (delete all rows) a table. Create To create tables, views, synonyms, sequences, functions, procedures, packages etc. Example To create a table, you can give the following statement create table emp (empno number(5) primary key, name varchar2(20), sal number(10,2), job varchar2(20), mgr number(5), Hiredate date, comm number(10,2)); Now suppose you have emp table. now you want to create a TAX table with the following structure and also insert rows of those employees whose salary is above 5000. Tax Empno Tax Number(5) Number(10,2) To do this we can first create TAX table by defining column names and datatypes and then use INSERT into EMP SELECT …. statement to insert rows from emp table like given below. create table tax (empno number(5), tax number(10,2)); insert into tax select empno,(sal-5000)*0.40 from emp where sal > 5000;
  • 3. Instead of executing the above two statements the same result can be achieved by giving a single CREATE TABLE AS statement. create table tax as select empno,(sal-5000)*0.4 as tax from emp where sal>5000 You can also use CREATE TABLE AS statement to create copies of tables. Like to create a copy EMP table as EMP2 you can give the following statement. create table emp2 as select * from emp; To copy tables without rows i.e. to just copy the structure give the following statement create table emp2 as select * from emp where 1=2; Temporary Tables (From Oracle Ver. 8i) It is also possible to create a temporary table. The definition of a temporary table is visible to all sessions, but the data in a temporary table is visible only to the session that inserts the data into the table. You use the CREATE GLOBAL TEMPORARY TABLE statement to create a temporary table. The ON COMMIT keywords indicate if the data in the table is transaction-specific(the default) or session-specific:  ON COMMIT DELETE ROWS specifies that the temporary table is transaction specific and Oracle truncates the table (delete all rows) after each commit.  ON COMMIT PRESERVE ROWS specifies that the temporary table is session specific and Oracle truncates the table when you terminate the session. This example creates a temporary table that is transaction specific: CREATE GLOBAL TEMPORARY TABLE taxable_emp (empno number(5), ename varchar2(20), sal number(10,2), tax number(10,2)) ON COMMIT DELETE ROWS; Indexes can be created on temporary tables. They are also temporary and the data in the index has the same session or transaction scope as the data in the underlying table. Alter Use the ALTER TABLE statement to alter the structure of a table. Examples:
  • 4. To add new columns addr, city, pin, ph, fax to employee table you can give the following statement alter table emp add (addr varchar2(20), city varchar2(20), pin varchar2(10),ph varchar2(20)); To modify the datatype and width of a column. For example we you want to increase the length of the column ename from varchar2(20) to varchar2(30) then give the following command. ALTER table emp modify (ename varchar2(30)) To decrease the width of a column the column can be decreased up to largest value it holds. ALTER table emp modify (ename varchar2(15)); The above is possible only if you are using Oracle ver 8i and above. In Oracle 8.0 and 7.3 you cannot decrease the column width directly unless the column is empty. To change the datatype, column must be empty in All Oracle Versions. To drop columns From Oracle Ver. 8i you can drop columns directly it was not possible in previous versions. For example to drop PIN, CITY columns from emp table. ALTER table emp drop column (pin, city); Remember you cannot drop the column if the table is having only one column. If the column you want to drop is having primary key constraint on it then you have to give cascade constraint clause. ALTER table emp2 drop column (empno) cascade constraints; To drop columns in previous versions of Oracle8.0 and 7.3 and to change the column name in all Oracle versions do the following.
  • 5. For example we want to drop pin and city columns and to change SAL column name to SALARY. Step 1: Create a temporary table with desired columns using subquery. CREATE TABLE temp as select empno, ename, sal AS salary, addr, ph from emp; Step 2: Drop the original table. DROP table emp; Step 3: Rename the temporary table to the original table. RENAME temp to emp; Rename Use the RENAME statement to rename a table, view, sequence, or private synonym for a table, view, or sequence.  Oracle automatically transfers integrity constraints, indexes, and grants on the old object to the new object.  Oracle invalidates all objects that depend on the renamed object, such as views, synonyms, and stored procedures and functions that refer to a renamed table. Example To rename table emp2 to employee2 you can give the following command. RENAME emp2 to employee2 Drop Use the drop statement to drop tables, functions, procedures, packages, views, synonym, sequences, tablespaces etc. Example The following command drops table emp2
  • 6. DROP table emp2; If emp2 table is having primary key constraint, to which other tables refer to, then you have to first drop referential integrity constraint and then drop the table. Or if you want to drop table by dropping the referential constraints then give the following command DROP table emp2 cascade constraints; Truncate Use the Truncate statement to delete all the rows from table permanently. It is same as “DELETE FROM <table_name>” except  Truncate does not generate any rollback data hence, it cannot be roll backed.  If any delete triggers are defined on the table. Then the triggers are not fired  It de-allocates free extents from the table. So that the free space can be use by other tables. Example TRUNCATE table emp; If you do not want free space and keep it with the table. Then specify the REUSE storage clause like this TRUNCATE table emp reuse storage;