Uploaded by ss7913

SQL WORKSHEET-1 (DDL COMMANDS)

advertisement
Date: 09-11-2023
SQL WORKSHEET-1 (DDL COMMANDS)
1. CREATE TABLE COMMAND
● A table is a unit of storage which holds data in the form of rows and columns.
● In a table,
● A unique column name should be specified.
● Proper data type along with its width should be specified.
● ‘Not null’ condition can be included when needed, by default it is ‘Null’.
Syntax:create table <table name> (column definition1, column definition 2, …);
Example: create table student (regno number (11), name varchar2 (25), addr varchar2(25),
dept varchar2 (3));
Output:
Table created.
2. ALTER TABLE COMMAND
● This command is used to add a new column, modify the existing column definition,
include or drop integrity constraints.
Syntax:
alter table <table name> modify (column definition …);
alter table <table name> add (column definition …);
alter table <table name> add (column definition …);
Example:
I.
alter table student modify (name varchar2 (30));
Output:
Table altered.
II.
alter table student add (comments long);
Output:
Table altered.
3. TRUNCATE TABLE COMMAND
● The truncate command deletes all rows from the table. Only the structure of the table
remains.
Syntax:
Truncate table <table name>;
Example: Truncate table student;
Output:
Table truncated.
4. DESC COMMAND
● This command will display the structure of the table.
Syntax:
Desc <table name>;
Example: Desc student;
Output:
Name
Null? Type
---------------------------------- -------- ---------------------------REGNO
NUMBER(11)
NAME
VARCHAR2(30)
DEPT
VARCHAR2(3)
COMMENTS
LONG
5. DROP TABLE COMMAND
● The drop table command is used to delete the table permanently from the database.
Syntax:
Drop table <table name>;
Example: Drop table student;
Output:
Table dropped.
Questions to Solve
Q1. Create the tables DEPT and EMP as described below.
DEPT
Column Name
DNO
DNAME
LOC
DataType
Number
Varchar
Varchar
Description
Department
Department Name
Department Location
Column Name
ENO
ENAME
JOB
MGR
HIREDATE
DataType
Number
Varchar
Char
Number
Date
Description
Employee Number
Emp Name
Designation
Manager EMPno
Date of Joining
EMP
SAL
COMM
DEPTNO
Number
Number
Number
Basic Salary
Commission
Department Number
Q2. Confirm Table Creation
Q3. List names of tables created by user.
Q4. Modify the size of the column LOC by 15 in the DEPT table.
Q5.Add new columns COMNT and MISCEL in the DEPT table.
Q6. Drop the column COMNT from the table.
Q7.Rename the table DEPT to DEPT1
Q8. Delete the table from the database.
SOLUTIONS
Q1.
SQL> create table dept(deptno number,dname varchar2(25),loc varchar2(25));
Table created.
SQL> create table empl(empno number,ename varchar(25),job char(15),mgr number,hiredate
date, sal number(9,2),comm number(7,2),deptno number);
Table created.
Q2.
SQL>desc dept
Name
Null? Type
----------------------------------------- -------- ---------------------------DEPTNO
NUMBER
DNAME
VARCHAR2(25)
LOC
VARCHAR2(25)
Q3.
SQL>select * from tab;
Q4:
SQL> alter table dept modify(loc char(30));
Table altered.
Q5.
SQL> alter table dept add(comnt varchar(25),miscel varchar(25));
Table altered.
Q6.
SQL> alter table dept drop column comnt;
Table altered.
Q7.
SQL> rename dept to dept1;
Table renamed.
Q8.
SQL> drop table dept1;
Table dropped.
Download