DDL Commands
•FIRST COMMANDS TO DEAL WITH DATABASE
•CREATE, ALTER, RENAME, DROP, TRUNCATE
ALTER TABLE Statement
• The ALTER TABLE statement is used to add,
delete, or modify columns in an existing table.
• The ALTER TABLE statement is also used to
add and drop various constraints on an existing
table.
ALTER TABLE Statement
►To add a column in a table, use the
following syntax:
► ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE Statement
►To delete a column in a table, use
the following syntax :
►ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE Statement
►To change the data type of a
column in a table, use the following
syntax:
►ALTER TABLE table_name
MODIFY column_name datatype;
EXAMPLES :
►ALTER TABLE Persons
ADD DateOfBirth date;
►ALTER TABLE Persons
MODIFY name varchar2(30);
►ALTER TABLE Persons
DROP COLUMN DateOfBirth;
TRUNCATE COMMAND
► The Truncate SQL command is used to delete all
records from an existing oracle table.
► Removing rows with the TRUNCATE TABLE
statement can be faster than removing all rows
with the DELETE statement, especially if the
table has numerous triggers, indexes, and other
dependencies.
TRUNCATE
COMMAND
►Truncate drops the table first and then
recreates only the table structure which is
much faster than deleting the records one by
one.
► Hence in case a table contains large number
of records, it works much faster than delete
statement.
TRUNCATE
COMMAND
► Syntax :
TRUNCATE TABLE TABLE_NAME;
► Example:
1. TRUNCATE TABLE EMP;
2. TRUNCATE TABLE DEPARTMENT;
DROP COMMAND
► The DROP command is used to remove an object
from the database.
► If u drop a table, all the rows in the table is deleted
and the table structure is removed from the
database.
► Once a table is dropped we cannot get it back, so be
careful while using drop command.
► When a table is dropped all the references to the
DROP COMMAND
► Syntax:
DROP TABLE TABLE_NAME;
► Example:
DROP TABLE EMP;
DROP TABLE DEPARTMENT;
Difference
TRUNCATE DROP
Truncate command removes all Drop command removes all records
records from the specified table, but from the table along with the table
keeps structure of the table intact. structure and puts it in recycle bin.
The records once deleted with The table removed with the drop
truncate command cannot be command can be recovered from the
recovered with the help of rollback. recycle bin with the help of flashback
command.
Difference
TRUNCATE DELETE
Truncate command removes all Delete command can delete one
records from the specified table. or more records from the table.
The records once deleted with The records once deleted with
truncate command cannot be delete command can be
recovered with the help of recovered with the help of
rollback. rollback.