0% found this document useful (0 votes)
75 views3 pages

Chapter 4 Notes

The document describes various DDL commands in Oracle including CREATE TABLE, ALTER TABLE, and DROP TABLE. CREATE TABLE is used to create new tables and has options to create a table from an existing table by copying columns. ALTER TABLE allows modifying existing tables by adding, dropping, or modifying columns and renaming tables. DROP TABLE permanently removes a table while TRUNCATE deletes all table data but maintains the table structure.

Uploaded by

Bul Bul Aw Yusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views3 pages

Chapter 4 Notes

The document describes various DDL commands in Oracle including CREATE TABLE, ALTER TABLE, and DROP TABLE. CREATE TABLE is used to create new tables and has options to create a table from an existing table by copying columns. ALTER TABLE allows modifying existing tables by adding, dropping, or modifying columns and renaming tables. DROP TABLE permanently removes a table while TRUNCATE deletes all table data but maintains the table structure.

Uploaded by

Bul Bul Aw Yusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

DDL Commands

#############
(1)CREATE TABLE Statement
############################
a)create table
Syntax:
>create table <table_name>(colname datatype(size),colname datatype(size),...);
Example:
>create table Emp
(Empno number(4),Ename varchar2(50),jop varchar2(50),sal number(8,2));
>CREATE TABLE customers( customer_id number(10),customer_name varchar2(50),city
varchar2(50));

#DESCRIBE/DESC
It is used to display table structure.
A table structure contains column names,data types and size.
Syntax:
Desc <table_name>
Example:
>desc emp; or describe emp;
#HOW TO DISPLAY LIST OF TABLE NAMES
>select * from table_name;

b)CREATE TABLE AS Statement


...........................
You can also use the Oracle CREATE TABLE AS statement to create a table from
an existing table by copying the existing table's columns.
Syntax:
CREATE TABLE new_table
AS (SELECT * FROM old_table);
Exmple(1):Create TAble - By Copying all columns from another table.
>CREATE TABLE Emp_Backup
AS (SELECT * FROM emp);
Exmple(2):Create TAble - By Copying selected columns from another table.
CREATE TABLE new_table
AS (SELECT column_1, column2, ... column_n
FROM old_table);
>CREATE TABLE Emp_Backup2
AS (SELECT empno,ename FROM emp WHERE Empno=1);
Example(3):Create table - By Copying selected columns from multiple tables.
Sntax:CREATE TABLE new_table
AS (SELECT column_1, column2, ... column_n
FROM old_table_1, old_table_2, ... old_table_n);
>CREATE TABLE emp_cus_backup
AS (SELECT Empno, customer_id, customer_name FROM emp, customers);

Exmple(4):create an Oracle table from another table


without copying any values from the old table.
Syntax:CREATE TABLE new_table AS (SELECT * FROM old_table WHERE 1=2);
>CREATE TABLE Emp_backup2
AS (SELECT *
FROM emp WHERE 1=2);
................................................................................

(2)ALTER TABLE Statement


##########################
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.
a)Add column in table
Syntax:
ALTER TABLE table_name ADD column_name column-definition;
Example:
ALTER TABLE emp ADD address varchar2(45);

#Add multiple columns in table


Syntax:
ALTER TABLE table_name
ADD (column_1 column-definition,
column_2 column-definition,
...
column_n column_definition);
Example:ALTER TABLE customers
ADD (Edept varchar2(45),city varchar2(40));

b)Modify column in table


Syntax:ALTER TABLE table_name MODIFY column_name column_type;
Example:
ALTER TABLE emp MODIFY Ename varchar2(100) not null;

c)Drop column in table


syntax:ALTER TABLE table_name DROP COLUMN column_name;
Example:ALTER TABLE emp DROP COLUMN city;

d)Rename column in table


syntax:ALTER TABLE table_name RENAME COLUMN old_name to new_name;
Example:ALTER TABLE emp RENAME COLUMN ename to emp_name;

#Rename table
syntax:ALTER TABLE table_name RENAME TO new_table_name;
Example:ALTER TABLE emp RENAME TO Employee;
.........................................................
3)DROP and TRUNCATE TABLE Statement
#########################
The Oracle DROP TABLE statement allows you to remove or
delete a table from the Oracle database.
Syntax:DROP TABLE table_name
[ CASCADE CONSTRAINTS ]
[ PURGE ];
Note:
CASCADE CONSTRAINTS Optional. If specified, all referential integrity constraints
will be dropped as well.

PURGE Optional. If specified, the table and its dependent objects will be purged
from the recycle bin and you will not be able to recover the table.
If not specified, the table and its dependent objects are placed in the
recycle bin and can be recovered later, if needed.

Example:
DROP TABLE employee;

DROP TABLE employee PURGE;

#TRUNCATE
Truncate command release memory allocated for a table.
Truncate deletes all the data from a table permanently,Table structure is present

Syntax:TRUNCATE TABLE <TABLE NAME>


Example:
Truncate TABLE employee;
...............................................
###################################################################################
########
DML Commands
############

You might also like