You are on page 1of 5

DDL command s:

1. The Create Table Comman d: - it defines each column of the table uniquely. Each column
has minimum of
three attributes , a name, data type and size.

Syntax:
Create table <table name> (<coll> <datatype>(<size>),<col2> <datatype><size>));

Ex:
create table emp(emp no number(4 ) primary key, ename char(lO));

2. Modifying the structure of tables.


a)add new columns

Syntax:
Alter table <tablenam e> add(<new col><datatype(size),<new col>datatype(size));

Ex:
alter table emp add(sal number(7 ,2));

3. Dropping a column from a table.

Syntax:
Alter table <tablenam e> drop column <col>;

Ex:
alter table emp drop column sal;

4. Modifying existing columns.

Syntax:
Alter table <tablenam e> modify(<col><newdatatype>(<newsize>));

Ex:
alter table emp modify(en ame varchar2(15));

5. Renaming the tables

Syntax:
Rename <oldtable > to <new table>;

Ex:
rename emp to empl;

6. truncatin g the tables.

Syntax:
Truncate table <tablename>;

Ex:
trunc table empl;
7 Dest roying t ables

Synta x·
Drop table <table n ame> ,

Ex.
drop table emp;

DML co mman d s:

,s created th e mos t na tural thing to do• ~ load th1, tJblC' " th d Jt J


8 Inse rting Data into Tables : - once a table
to be man ipulat ed later .

Syntax :
p>);
insert into <table name > (<coll >,<col2> ) values(<exp>,<ex

9 . Delete opera tion s.

a) remov e all rows


Syntax :
delete from <table name >;

b ) remov al of a specified row/s


Syntax :
delete from <table name > where <co nd1t 1on>,

10. Upda ting th e conte nt s of a tabl e.

a) updat ing all rows


Syntax :
Updat e <table name > set <col> =< exp>, <col>=<e.xp>;

b) updat ing selete d record s.


Syntax :
Updat e <table name > set <col>=<exp> ,<col>=<exp>
where <cond ition> ;

11. Types of data constrains .


a) not null const raint at colum n level.
Syntax :
<col> <data type> (size) not null

b) uniqu e con str aint


Syntax :
Uniqu e const raint at colum n level.
<col><dat.atype>(size)unique;

c) uniqu e const rain t at table level :


Synta x:
<coll> , <col2 >);
Create table t ablen ame(c ol =format.,co l =form at,uni que(
- d) pnmary key const raint at col umn level
Syntax:
<col><datatype>(si ze )primary key;

e) primary key con stra int at tab le level.


Syn tax:
I},
Create tab le tablename {co l=format,co l=format
primary key(coll>,<col2>);

f) fore ign key constra int at column level.


Syntax :
<col><datatype> (size >) references <tablename>[<col> ];

g) foreign key constraint at table level


Syntax :
foreign key{ <col> l,<col>] )references <tablename>[{ <col>,<col>)

h) check constra int


check constraint constraint at column level.
Syntax: <col><datatype>(si ze) check(<l ogical expression>)

i) check constraint constraint at table level.


Syntax: check( <logical expression> )

name sql

CREATE TABLE classroom (building varchar (lS), room _number varchar(7), capacity numeri c(4,0),
classroom
primary key (bu ilding, room _number))

CREATE TABLE department (dept_name va rchar(20), build ing varchar(lS), budget numeric(l2,2)
department
check (budget> 0), primary ke y (dept_name) )

CREATE TABLE course (course_id varchar(8), title varchar(S0), dept_name varchar(20), cred its
course numeric(2,0) check (credits> 0), primary key (course_id), foreign key (dept_name) reference s
department on delete set null )

CREATE TABLE instructor (ID varchar(S), name varchar(20) not null, dept_name varchar(20),
instructor salary numeric(B,2) check (salary> 29000), primary key (ID), foreign key (dept_name) references
department on delete set null )

CREATE TABLE section (course_id varchar(B), sec_id varchar(B), semester varchar(6) check
(semester in ('Fall', 'Winter', 'Spring', 'Summer')), year numeric(4,0) check (year> 1701 and year
section < 2100), building varchar(lS), room_number varchar(7), time_s lot_id varchar(4), primary key
(course_id, sec_i d, semester, year), foreign key (course_id ) references course on delet e ca scad e,
foreign key (building, room _number) references classroom on delete set null )

CREATE TABLE teaches (ID varchar(S), course_id varchar(8), sec_id varchar(8), semester
varchar(6), year numeric(4,0), primary key (ID, course_i d, sec_id, semester, year), foreign key
teaches
(course_id,sec_id, semester, year) references section on delete cascade, foreign key (ID)
re ferences instructor on delete cascade )
name sql
CREATE TABLE studen t (ID varchar(S), name varchar(20) not null,
dept_name varchar(20),
studen t tot_cre d numeric(3,0) check (tot_cred >= 0), primary key (ID),
foreign key (dept_name)
references depart ment on delete set null )

CREATE TABLE takes (ID varchar(S), course_id varchar(8), sec_id


varchar(8), semester varchar(6),
year numeric(4,0), grade varchar(2), primar y key (ID, course_id,
takes sec_id, semester, year), foreign
key (course_id,sec_id, semester, year) references section on delete
cascade, foreign key (ID}
references studen t on delete cascade )

CREATE TABLE advisor (s_lD varchar(S), i_lD varchar(S), primar


y key (s_lD), foreign key (i_lD)
advisor references instruc tor (ID) on delete set null, foreign key (s_lD)
references studen t (ID) on delete
cascad e)

CREATE TABLE time_s lot (time_ slot_id varchar(4), day varcha


r(l), start_h r numeric(2) check
(start_ hr >= 0 and start_h r < 24), start_m in numeric(2) check
time_s lot (start_ min >= 0 and start_min <
60), end_h r numeric(2) check (end_hr >= O and end_hr < 24),
end_m in numeric(2) check
(end_m in >= 0 and end_m in < 60), primar y key (time_slot_id,
day, start_h r, start_m in))

CREATE TABLE prereq (course_id varchar(8), prereq_id varcha


r(8), primar y key (course_id,
prereq prereq _id), foreign key (course_id) references course on delete
cascade, foreign key (prereq _id)
referen ces course )
DQL Commands:

12. Viewing data in the tables: - once data has been inserted into a table, the next most logical operation
would be to view what has been inserted.

a) all rows and all columns


Syntax:
Select <col> to <col n> from tablename;

Select * from tablename;

13. Filtering table data: - while viewing data from a table, it is rare that all the data from table will be required
each time. Hence, sql must give us a method of filtering out data that is not required data.

a) Selected columns and all rows:


Syntax:
select <coll>,<col2> from <tablename>;

b) selected rows and all columns:


Syntax:
select * from <tablename> where <condition>;

c) selected columns and selected rows


Syntax:
select <coll>,<col2> from <tablename> where<condition>;

14. Sorting data in a table.


Syntax:
Select * from <tablename> order by <coll>,<co12> <[sortorder]>;

You might also like