0% found this document useful (0 votes)
419 views

Experiment-1 DDL Commands Create Table Alter Table Drop Table Rename TO Create Table

The document describes using various DDL commands in SQL such as CREATE TABLE, ALTER TABLE, DROP TABLE, and RENAME. It shows how to create tables, add/modify/drop columns, set primary keys and default values, truncate and rename tables. Examples include creating Department and Employee tables with primary keys and foreign keys, then altering Employee table by adding, modifying, and dropping columns, and renaming and dropping tables.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
419 views

Experiment-1 DDL Commands Create Table Alter Table Drop Table Rename TO Create Table

The document describes using various DDL commands in SQL such as CREATE TABLE, ALTER TABLE, DROP TABLE, and RENAME. It shows how to create tables, add/modify/drop columns, set primary keys and default values, truncate and rename tables. Examples include creating Department and Employee tables with primary keys and foreign keys, then altering Employee table by adding, modifying, and dropping columns, and renaming and dropping tables.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment-1

DDL COMMANDS
CREATE TABLE
ALTER TABLE
DROP TABLE
RENAME
TO create table:SQL> create table department (dno number(10),dname varchar2(10),primary key(dno));
Table created.
SQL> desc department;
Name
Null? Type
----------------------------------------- -------- ---------------------DNO
NOT NULL NUMBER (10)
DNAME
VARCHAR2 (10)
SQL> create table employee (eno number(10),ename varchar2(10),dno number(10),sal
number(10),jobid varchar2(10),mgrid varchar2(10),foreign key(dno) references
department(dno));
Table created.
SQL> desc employee;
Name
Null? Type
----------------------------------------- -------- ---------------------ENO
NUMBER(10)
ENAME
VARCHAR2(10)
DNO
NUMBER(10)
SAL
NUMBER(10)
JOBID
VARCHAR2(10)
MGRID
VARCHAR2(10)
TO Alter Table:SQL> alter table employee add constraint emp_pk_eno primary key(eno);
Table altered.

SQL> desc employee;


Name
Null?
Type
----------------------------------------- -------- ---------------------ENO
NOT NULL NUMBER(10)
ENAME
VARCHAR2(10)
DNO
NUMBER(10)
SAL
NUMBER(10)
JOBID
VARCHAR2(10)
MGRID
VARCHAR2(10)
Adding a column:SQL> alter table employee add(phno number(5),addr varchar2(10));
Table altered.
SQL> desc employee;
Name
Null? Type
----------------------------------------- -------- ---------------------ENO
NOT NULL NUMBER(10)
ENAME
VARCHAR2(10)
DNO
NUMBER(10)
SAL
NUMBER(10)
JOBID
VARCHAR2(10)
MGRID
VARCHAR2(10)
PHNO
NUMBER(5)
ADDR
VARCHAR2(10)
Modifying the column Data type:SQL> alter table employee modify(jobid char(5));
Table altered.
SQL> desc employee;
Name
Null?
Type
----------------------------------------- -------- ---------------------ENO
NOT NULL NUMBER(10)
ENAME
VARCHAR2(10)
DNO
NUMBER(10)
SAL
NUMBER(10)
JOBID
CHAR(5)
MGRID
VARCHAR2(10)
PHNO
NUMBER(5)
ADDR
VARCHAR2(10)

Assigning the default value to an existing column:SQL> alter table employee modify sal default 0;
Table altered.
Dropping a column:SQL> alter table employee drop column phno;
Table altered.
SQL> desc employee;
Name
Null?
Type
----------------------------------------- -------- ---------------------ENO
NOT NULL NUMBER(10)
ENAME
VARCHAR2(10)
DNO
NUMBER(10)
SAL
NUMBER(10)
JOBID
CHAR(5)
MGRID
VARCHAR2(10)
ADDR
VARCHAR2(10)
Truncate a Table:SQL> truncate table employee;
Table truncated.
SQL> select *from employee;
no rows selected
To rename table:SQL> rename employee to emp2;
Table renamed.
SQL> desc emp2;

Name
Null?
Type
----------------------------------------- -------- ---------------------ENO
NOT NULL NUMBER(10)
ENAME
VARCHAR2(10)
DNO
NUMBER(10)
SAL
NUMBER(10)
JOBID
CHAR(5)
MGRID
VARCHAR2(10)
ADDR
VARCHAR2(10)
SQL> desc employee;
ERROR:
ORA-04043: object employee does not exist
To drop the Table:SQL> drop table emp2;
Table dropped.
SQL> drop table department;
Table dropped.

You might also like