You are on page 1of 34

IES COLLEGE OF TECHNOLOGY, BHOPAL

Department of Computer Science & Engineering

PRACTICAL FILE
(CS-700 )

Name: Year:
Section: subject:
Semester:

Approved by:
CS5003
Database Management System
Practical file

LAB OBJECTIVE
Upon successful completion of this Lab the student will be able to:

 Creating database objects


 Modifying database objects
 Manipulating the data
 Retrieving the data from the database server
 Performing database operations in a procedural manner using pl/sql
 Performing database operations (create, update, modify, retrieve, etc.,) using front-end tools l
 Design and Develop applications like banking, reservation system, etc.,

Introduction
SQL is structure query language.
SQL contains different data types those are
 1. char(size)
 2. varchar(size)
 3. varchar2(size)
 4. date
5. number(p,s) //** P-PRECISION S-SCALE **//
 6. number(size)
 7. raw(size)
 8. raw/long raw(size)
Different types of commands in SQL:
 A).DDL commands: - To create a database objects
 B).DML commands: - To manipulate data of a database objects
 C).DQL command: - To retrieve the data from a database.
 D).DCL/DTL commands: - To control the data of a database…
EXPERIMENT NO. 1

DDL commands:

AIM:

To create a DDL to perform creation of table, alter, modify and drop column.

CREATION OF TABLE:

SYNTAX:

create table<tablename>(c olumn1 datatype,column2 datatype...);

EXAMPLE:

SQL>create table std(sno number(5),sname varchar(20),age number(5),sdob date,sm1


number(4,2),sm2 number(4,2),sm3 number(4,4));

Table created.

SQL>insert into std values(101,’AAA’,16,’03-jul-88’,80,90,98);

1 row created.

SQL>insert into std values(102,’BBB’,18,’04-aug-89’,88,98,90);

1 row created.

OUTPUT:

Select * from std;

SNO SNAME AGE SDOB SM1 SM2 SM3


101 AAA 16 03-jul-88 80 90 98
102 BBB 18 04-aug-89 88 98 90

ALTER TABLE WITH ADD:

SQL>create table student(id number(5),name varchar(10),game varchar(20));

Table created.
SQL>insert into student values(1,’mercy’,’cricket’);

1 row created.

SYNTAX:

alter table<tablename>add(col1 datatype,col2 datatype..);

EXAMPLE:

SQL>alter table student add(age number(4));

SQL>insert into student values(2,’sharmi’,’tennis’,19);

OUTPUT:

ALTER: select * from student;

ID NAME GAME
1 Mercy Cricket

ADD: select * from student;

ID NAME GAME AGE


1 Mercy cricket
2 Sharmi Tennis 19

ALTER TABLE WITH MODIFY:

SYNTAX:

Alter table<tablename>modify(col1 datatype,col2 datatype..);

EXAMPLE:

SQL>alter table student modify(id number(6),game varchar(25));

OUTPUT:

MODIFY

desc student;
NAME NULL? TYPE
Id Number(6)
Name Varchar(20)
Game Varchar(25)
Age Number(4)

DROP:

SYNTAX: drop table<tablename>;

EXAMPLE:

SQL>drop table student;

SQL>Table dropped.

RESULT:

Thus the DDL commands have been executed successfully.


EXPERIMENT NO. 2

DDL COMMANDS (SOME MORE EXAMPLES)

1. The Create Table Command: - 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> (<col1> <datatype>(<size>),<col2>
<datatype><size>));
Ex:
create table emp(empno number(4) primary key, ename char(10));
2. Modifying the structure of tables.
a) Add new columns
Syntax:
Alter table <tablename> 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 <tablename> drop column <col>;
Ex:
alter table emp drop column sal;

4. Modifying existing columns.


Syntax:
Alter table <tablename> modify(<col><newdatatype>(<newsize>));
Ex:
alter table emp modify(ename varchar2(15));
5. Renaming the tables
Syntax:
Rename <oldtable> to <new table>;
Ex:
rename emp to emp1;
6. truncating the tables.
Syntax:
Truncate table <tablename>;
Ex:
trunc table emp1;

7. Destroying tables.
Syntax:
Drop table <tablename>;
Ex:
drop table emp;
EXPERIMENT NO. 3

DML COMMANDS

AIM:

To create a view for the purpose of display in order to hide the data.

VIEW:

A view is a logical data table that allows us to view data from other tables and values.

SYNTAX:

Create [or replace] view viewname as query;

TABLE:

SQL>create table std(sno number(5),sname varchar(20),sm1 number(3),sm2 number(3),sm3 number(3));

SQL>insert into std values(&sno,’&sname’,&sm1,&sm2,&sm3);

SQL>create table sport(spoid number(5),game varchar(20));

SQL>insert into sport values(&spoid,’&game’);

OUTPUT: select * from std;

SNO SNAME SM1 SM2 SM3


1 Mercy 90 89 90
2 Sharmi 90 89 90
3 Priya 78 89 70
4 Amri 67 78 90
5 Divya 98 78 50

select * from sport;

SPOID GAME
1 Cricket
2 Basketball
4 Tennis

QUERIES:
SQL>select std.sno,std.sname,sport.game from std,sport where std.sno=sport.spoid;

OUTPUT:

SNO SNAME GAME


1 Mercy Cricket
2 Sharmi Basketball

4 Amri Tennis

SQL>create or replace view college as select std.sno,std.sname,sport.spoid,

sport.game from std,sport where std.sno=sport.spoid;

View created.

OUTPUT:

select * from college;

SNO SNAME SPOID GAME


1 Mercy 1 Cricket
2 Sharmi 2 Basketball
4 Amri 4 Tennis

DROP VIEW:

To remove a view from the database, one has to use drop view.

drop view viewname;

drop view college;

view dropped;

SQL> select * from college;

Table or view does not exist.

RESULT:

Thus, the view has been created successfully.


EXPERIMENT NO. 4

DML commands (ADDITIONAL EXAMPLES):

1. Inserting Data into Tables: - once a table is created the most natural thing to do is load this table
with data to be manipulated later.

Syntax:
insert into <tablename> (<col1>,<col2>) values(<exp>,<exp>);

2. Delete operations.

a) Remove all rows


Syntax:
delete from <tablename>;

b) Removal of a specified row/s


Syntax:
delete from <tablename> where <condition>;

3. Updating the contents of a table.

a) Updating all rows


Syntax:
Update <tablename> set <col>=<exp>,<col>=<exp>;

b) Updating selected records.


Syntax:
Update <tablename> set <col>=<exp>,<col>=<exp>
where <condition>;

4. Types of data constrains.


a) Not null constraint at column level.
Syntax:
<col><datatype>(size)not null

b) unique constraint
Syntax:
Unique constraint at column level.
<col><datatype>(size)unique;
c) unique constraint at table level:
Syntax:
Create table tablename(col=format,col=format,unique(<col1>,<col2>);

d) Primary key constraint at column level


Syntax:
<col><datatype>(size)primary key;

e) Primary key constraint at table level.


Syntax:
Create table tablename(col=format,col=format
Primary key(col1>,<col2>);

f) Foreign key constraint at column level.


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

g) Foreign key constraint at table level


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

h) Check constraint
check constraint constraint at column level.
Syntax: <col><datatype>(size) check(<logical expression>)

i) Check constraint constraint at table level.


Syntax: check(<logical expression>)
EXPERIMENT NO. 5

DCL commands
Oracle provides extensive feature in order to safeguard information stored in its tables from unauthoraised
viewing and damage. The rights that allow the user of some or all oracle resources on the server are called
privileges.

a) Grant privileges using the GRANT statement

The grant statement provides various types of access to database objects such as tables, views and
sequences and so on.

Syntax:
GRANT <object privileges>
ON <objectname>
TO<username>
[WITH GRANT OPTION];

b) Revoke permissions using the REVOKE statement:

The REVOKE statement is used to deny the Grant given on an object.

Syntax:
REVOKE<object privilege> ON FROM<user name>;

Result:
Thus the DCL commands have been successfully executed and the results are verified.
EXPERIMENT NO. 6

SINGLE ROW FUNCTIONS

AIM:

To create a single row functions.

QUERY:

CHARACTER FUNCTIONS:

ASCII: It returns ascii decimal equivalent to a character.

SYNTAX:

ascii (string)

Ex: SQL>select ascii(‘A’) from dual;

OUTPUT:

ASCII(‘A’)
65

INSTR:

It will return first position of the character x in the string.

SYNTAX:

i) instr(string/column name,x)

Ex: SQL>select instr(‘sample’,’a’) as position from dual;

OUTPUT:

POSITION
2

select instr(<colname>,’chr’) as position from <tablename>;

Ex: SQL>select instr(name,’e’) as position from student;


OUTPUT:

POSITION
2

LENGTH:

It will return the length of a given string.

SYNTAX:

i) length(string)

Ex: SQL>select length(‘INDIA’) from dual;

OUTPUT:

LENGTH(‘INDIA’)
5

ii) Select length(fieldvalue) from <tablename>;

Ex: SQL>select length(name) from std1;

OUTPUT:

LENGTH(NAME)
3
4
6

LPAD:

It pads the leading space to the left side of the column and fill with given character to the total
width of n.

SYNTAX:

i) lpad(char1,n,[,char2])
Ex: SQL>select lpad(‘abc’,10,’*’) from dual;

OUTPUT:

LPAD(‘ABC’)
*******ABC

ii) select lpad(char1,n,[,char2]) from <tablename>;

Ex: SQL>select lpad(deptname,10,’*’) fromn dept;

OUTPUT:

LPAD(DEPTNAME,10,’*’)
*****sales
***account
****manager

CONCAT:

It concatenates the two strings x1 and x2.

SYNTAX:

i) concat(x1,x2)

Ex: SQL>select concat(‘good’,’evening’) from dual;

OUTPUT:

CONCAT(‘GOOD’,’EVENING’)
Goodevening

ii) select concat(field1,field2) from <tablename>;

Ex: SQL>select concat(deptno,deptname) from dept;

OUTPUT:
CONCAT(DEPTNO,DEPTNAME)
101sales
102account
103manager

CHR:

It returns the character corresponding to the value x.

SYNTAX:

i) chr(x)

Ex: SQL>select chr(‘97’) from dual;

OUTPUT:

CHR(‘97’)
a

SQL> select chr(field) from <tablename>;

Ex: SQL>select chr(deptno) from dept;

OUTPUT:

CHR(DEPTNO)
e
f
g

RPAD:

It pads the leading space to the right side of the column and fills with blank space to the width
of n.
SYNTAX:

i) rpad(char1,n,[,char2])

Ex: SQL>select rpad(‘abc’,10,’*’) from dual;

OUTPUT:

RPAD(‘ABC’)
abc*******

ii) select rpad(colname,n,[,char2]) from <tablename>;

Ex: SQL>select rpad(deptname,10,’*’) from dept;

OUTPUT:

RPAD(DEPTN AME,10,’*’)
Sales*****
Account***
Manager****

LTRIM:

It removes the leading occurrences of that character from the string from left side position.

SYNTAX:

i) ltrim(string[,char(s)])

Ex: SQL>select ltrim(‘abc’,’a’) from dual;

OUTPUT:

LTRIM(‘ABC’,’A’)
bc
ii) select ltrim(colname,[,char(s)]) from <tablename>;

Ex: SQL>select ltrim(deptname,’a’) from dept;

OUTPUT:

LTRIM(DEPTNAME,’A’)
Sales
ccount
Manager

RTRIM:

It removes the leading occurrences of the character from the string from right side position.

SYNTAX:

i) rtrim(string,[,char(s)])

Ex: SQL>select rtrim(‘abc’,’a’) from dual;

OUTPUT:

RTRIM(‘ABC’,’A’)
ab

ii) select rtrim(fieldname,[,char(s)]) from <tablename>;

Ex: SQL>select rtrim (deptname,’t’) from dept;

OUTPUT:

RTRIM(DEPTNAME,’T’)
Sales
Accoun
Manager

REPLACE:

It replaces the character c2 with a given string c3 and returns the character c1.

SYNTAX:
i) replace(<c1>,<c2>[<c3>])

Ex: SQL>select replace(‘Madurai’,’Ma’,’ij’) from dual;

OUTPUT:

REPLACE(‘MADURAI’)
ijdurai

ii) select replace(fieldname,<c1>,<c2>) from <tablename>;

Ex: SQL>select replace(deptname,’a’,’c’) from dept;

OUTPUT:

REPLACE(DEPTNAME,’A’,’C’)
Scles
ccount
Mcncger

SUBSTRING:

It returns the substring from string z, the length equal to y starting at position x.

SYNTAX:

i) substr(z,x[,y])

Ex: SQL>select substr(‘Madurai’,2,4) from dual;

OUTPUT:

SUBSTR(MADURAI)
adur

ii) select substr(colname,x,y) from <tablename>;


Ex: SQL>select substr(deptname,3,2) from dept;

OUTPUT:

SUBSTR(DEPTNAME)
le
co
na
EXPERIMENT NO. 7

NUMERIC FUNCTIONS:

ABS:

It returns the absolute value.

SYNTAX:

abs(x)

Ex: SQL>select abs(-8) from dual;

OUTPUT:

ABS(-8)
8

select abs(colname) from <tablename>;

Ex: SQL>select abs(deptno) “absolute” from dept;

OUTPUT: ABS(DEPTNO)
101
102
103

SIN:

It returns the sine value.

SYNTAX:

sin(x)

Ex: SQL>select sin(45) from dual;

OUTPUT:

SIN(45)
.850903525

select sin(colname) from <tablename>;


Ex: SQL>select sin(deptno) from dept;

OUTPUT:

SIN(DEPTNO)
.452025787
.994826791
.622988631

COS:

It returns the cosine value.

SYNTAX:

cos(x)

Ex: SQL>select cos(60) from dual;

OUTPUT:

COS(60)
-.95241298

select cos(colname) from <tablename>;

Ex: SQL>select cos(deptno) from dept;

OUTPUT:
COS(DEPTNO)
.89200487
.101585704
-.78223089

MOD:

It returns mod value of a division operator.

SYNTAX:

mod(x,y)

Ex: SQL>select mod(10,3) as first,mod(3,4) as second from dual;

OUTPUT:

MOD(10,3): FIRST SECOND


2 3

select mod(colname,y) from <tablename>;

Ex: SQL>select mod(deptno,2) from dept;

OUTPUT:

MOD
1
0
1

POWER:

It returns a number raised to an arbitrary power.

SYNTAX:

power(x,y)

Ex: SQL>select power(3,5)”power” from dual;

OUTPUT:
POWER(3,5)
243

select power(colname,y) from <tablename>;

Ex: SQL>select power(deptno,2)”power” from dept;

OUTPUT:

POWER
10201
10404
10609

EXP:

Returns the base of natural logarithm raised to a power.

SYNTAX:

exp(x)

Ex: SQL>select exp(5) from dual;

OUTPUT:

EXP(5)
148.413159

select exp(colname) from <tablename>;

Ex: SQL>select exp(deptno) from dept;

OUTPUT:

EXP(DEPTNO)
7.3071E+43
1.9863E+44
5.3992E+44

SQRT:

It returns the square root of the given value.

SYNTAX:

sqrt(x)

Ex: select sqrt(8) as square_root from dual;

OUTPUT:

square_root
8

select sqrt(colname) as square_root from <tablename>;

Ex: select sqrt(deptno) as square_root from dept;

OUTPUT:

SQUARE_ROOT(DEPTNO)
10.0498756
10.0995049
10.1488916

TRUNC:

It truncates the given value by the width of n.

SYNTAX:
trunc(x,n)

Ex: SQL>select trunc(23.4567,3),trunc(35.678),trunc(78.453) from dual;

OUTPUT:

TRUNC(23.4567) TRUNC(35.678) TRUNC(78.453)


23.456 35 70

select trunc(colname,x) from <tablename>;

Ex: SQL>select trunc(deptno,3) from dept;

OUTPUT:

TRUNC(DEPTNO,3)
101
102
103

RESULT:

Thus, the single row functions have been executed successfully.

EXPERIMENT NO. 8
PL/SQL

1. CHECK THE GIVEN NO IS PRIME OR NOT

Aim: To check whether the given no is prime or not using PL/SQL

PROGRAM:

set serveroutput on;

declare

n number(10);

i number(10);

m number(10);

f number(10):=0;

begin

n:=&n;

m:=n;

for i in 2..m/2

loop

if(n mod i=0)then

f:=f+1;

end if;

end loop;

if(f=0) then

dbms_output.put_line('the given number is prime');

else
dbms_output.put_line('the given number is not

prime');

end if;

end;

OUTPUT:

Enter value for n: 5

The given number is prime

PL/SQL procedure successfully completed.

RESULT:

Thus the PL/SQL program has been executed successfully.

EXPERINMENT NO. 9
2.CHECK PALINDROME OF A GIVEN NUMBER

Aim: To check whether the given no is palindrome or not using PL/SQL

PROGRAM:

set serveroutput on;

declare

n number(10);

m number(10);

s number(10);

r number(10);

begin

n:=&n;

m:=n;

s:=0;

while(n>0)

loop

r:=n mod 10;

s:=(s*10)+r;

n:=floor(n/10);

end loop;

if(m=s) then

dbms_output.put_line('The given number is a

palindrome');
else

dbms_output.put_line('The given number is not a

palindrome');

end if;

end;

OUTPUT:

Enter value for n: 121

The given number is a palindrome

PL/SQL procedure successfully completed.

RESULT:

Thus the PL/SQL program has been executed successfully.

EXPERIMENT NO. 10
NO_DATA_FOUND EXCEPTION

Aim: To check ascertain NO_DATA_FOUND exception using PL/SQL

TABLE:

SQL>create table emp5(empno number(3),ename varchar(10),deptno number(3));

Table created.

SQL>insert into emp5 values(&empno,'&ename',&deptno);

SQL>select * from emp5;

EMPNO ENAME DEPTNO


101 aaa 1
102 bbb 2

PROGRAM:

set serveroutput on;

declare

veno emp5.empno%type:=&empno;

vename emp5.ename%type:='&ename';

vdeptno emp5.deptno%type;

begin

select empno,ename,deptno into veno,vename,vdeptno from

emp5 where empno=veno and ename=vename;


dbms_output.put_line('deptno is '||vdeptno);

exception

when no_data_found then

dbms_output.put_line('no records found');

end;

OUTPUT:

Enter value for eno: 101

Enter value for ename: aaa

deptno is1

PL/SQL procedure successfully completed.

Enter value for eno: 103

Enter value for ename: sss

no records found

PL/SQL procedure successfully completed.

RESULT:

Thus the PL/SQL program has been executed successfully.


IES COLLEGE OF TECHNOLOGY, BHOPAL

Department of Computer Science & Engineering

PRACTICAL FILE

CGMM (CS-5004)

Name: Year:
Section: subject:
Semester:

Approved by:

You might also like