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

DBMS Practical File

The document contains examples of SQL queries using DDL, DML, and DCL commands. In the DDL section, queries show how to create, alter, rename and drop database tables. The DML section contains queries demonstrating how to insert, update, delete and select data from tables. The DCL section shows use of GRANT and REVOKE commands to manage user privileges for database objects.

Uploaded by

OSCAR AHINAMPONG
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
261 views

DBMS Practical File

The document contains examples of SQL queries using DDL, DML, and DCL commands. In the DDL section, queries show how to create, alter, rename and drop database tables. The DML section contains queries demonstrating how to insert, update, delete and select data from tables. The DCL section shows use of GRANT and REVOKE commands to manage user privileges for database objects.

Uploaded by

OSCAR AHINAMPONG
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

PRACTICAL FILE

OF

DBMS

DEPARTMENT OF COMPUTER SCIENCE AND APPLICATION

Submitted to: Submitted by:

Ms. Prachi Jude


B40718003
BSc IT 3rd Sem
INDEX

Sr. no. Program Name Page No. Date Signature

1 Queries based on DDL

2 Queries based on DML

3 Queries based on DCL

4 Queries based on SQL Joins

5 Queries based on SQL


Functions

6 Queries to Create/Drop a view


in SQL

7 PL/SQL Program to find the


radius of a circle and result get
stored into the Database
8 PL/SQL Program to Create
Function & how it can be used
in our program
9 PL/SQL Program to Create
Cursor & how it can be used in
our program
10 PL/SQL Program to Create
Procedure & how it can used in
our program
11 PL/SQL Program to Create
Trigger & how it can be used in
our program
Program 1

Queries Based on DDL

DDL( Data definition Language):- DDL or Data Definition Language actually consists of
the SQL commands and can used to define the database schema, It simply deals with
descriptions of the database schema and is used to create and modify the structure of
database objects in database.

Example of DDL Commands:


 CREATE – It is used to create the database or its objects( like table, index, function,
view, store, procedure and triggers).
 DROP – It is used to delete objects from the database.
 ALTER – It is used to alter the structure of the database.
 TRUNCATE – It is used to remove all records from a table, including all spaces
allocated for the records are removed .
 COMMENT- It is used to add comments to the data dictionary.
 RENAME -It is used rename an object existing in the database.

Queries containing DDL Commands: -

SQL> create table record (id int primary key, name char(20), city varchar(30));

Table created.

SQL> desc record;

Name Null? Type


----------------------------------------------------------------------------------------------

ID NOT NULL NUM(38)


NAME CHAR(20)

SQL> alter table record add (email varchar (40), mobile_no int;

Table altered

SQL> desc record;

Name Null? Type


----------------------------------------------------------------------------------------------

ID NOT NULL NUM(38)


NAME CHAR(20)
CITY VARCHAR2(30)
EMAIL VARCHAR2(30)
MOBILE_NO NUMBER(38)

SQL> --alter command to drop column of table

SQL> alter table record drop column email;

Table altered

SQL> desc record;

Name Null? Type


----------------------------------------------------------------------------------------------

ID NOT NULL NUM(38)


NAME CHAR(20)
CITY VARCHAR2(30)
MOBILE_NO NUMBER(30)

SQL> --alter command is used to modify the existing columns in a table

SQL> alter table record modify city varchar(20);

Table altered.

SQL> desc record;

Name Null? Type


----------------------------------------------------------------------------------------------

ID NOT NULL NUM(38)


NAME CHAR(20)
CITY VARCHAR2(20)
MOBILE_NO NUMBER(38)

SQL> alter table record rename to record_1;

Table altered.
SQL> desc record_1;

Name Null? Type


----------------------------------------------------------------------------------------------

ID NOT NULL NUM(38)


NAME CHAR(20)
CITY VARCHAR2(20)
MOBILE_NO NUMBER(38)

SQL>-- in above query, table name gets changed

SQL>-- rename the column name

SQL> insert into record_1 values( 1, ‘Jude’, ‘Bahadurgarh’, 8090228900);

1 row created.

SQL> select * from record_1;

Table truncated.

SQL> select * from record_1;

no rows selected.

SQL> drop table record_1;

Table dropped.

SQL> desc record_1;

ERROR:

ORA-04043: object record_1 does not exist.


Program 2

Queries Based on DML

DML( Data Manipulation Language):- The SQL commands that deals with the
manipulation of data present in database belong to DML or Data Manipulation Language
and this includes most of the SQL statements.

Examples of DML:
 SELECT – It is used to retrieve data from the database.
 INSERT – It is used to insert data into table.
 UPDATE – It is used to update existing data within a table.
 DELETE -It is used to delete records from database table.

Queries containing DML commands:

SQL> create table record(id int primary key, name char(20), city varchar(30),mobile_no
int);

Table created.

SQL> select * from record;

no rows selected.

SQL> --because no record exist at this time.

SQL> insert into record values(1, ‘Jude’, ‘Mumbai’, 8020930009);

1 row created.

SQL> -- the above insert query is used to insert a single record.

SQL> -- for multiple entries the below query is used

SQL> insert all

2 into record values(2, ‘Fred’, ‘Delhi’, 9029292009);

3 into record values(3, ‘Jones’, ‘New York’, 8210092922);

4 into record values(4, ‘Oscar’, ‘Japan’, 7899282910);

5 select * from dual;

3 rows created.
SQL> select * from record;

ID NAME CITY MOBILE_NO

1 Jude Mumbai 8020930009

2 Fred Delhi 9029292009

3 Jones New York 8210092922

4 Oscar Japan 7899282910

SQL> alter table record add pincode varchar(10);

Table altered.

SQL> update record set pincode =’124507’ where id = 1;

1 row updated.

SQL> update record set pincode =’124583’ where id = 2;

1 row updated.

SQL> update record set pincode =’457889’ where id = 3;

1 row updated.

SQL> update record set pincode =’75364’ where id = 4;

1 row updated.
SQL> select * from record;

ID NAME CITY MOBILE_NO PINCODE

1 Jude Mumbai 8020930009 124507

2 Fred Delhi 9029292009 124583

3 Jones New York 8210092922 457889

4 Oscar Japan 7899282910 75364

SQL> delete from record where name = ‘Jones’;

1 row deleted.

SQL> select * from record;

ID NAME CITY MOBILE_NO PINCODE

1 Jude Mumbai 8020930009 124507

2 Fred Delhi 9029292009 124583

4 Oscar Japan 7899282910 75364

SQL> -- for deleting the all record from table;

SQL> delete from record;

3 rows deleted.

SQL> select * from record;

no rows selected.
Program 3

Queries Based on DCL

DML( Data Control Language):- DCL includes commands such as GRANT and
REVOKE which mainly deals with the rights, permissions and other control of the database
system.

Examples of DCL commands:

 GRANT- gives user’s priviledges to database.

The Syntax for the REVOKE command is:

GRANT privilege_name
ON object_name
TO {user_name|PUBLIC|role_name}
[WITH GRANT OPTION];

 REVOKE – withdraw user’s access privileges given by using the GRANT


command.

The Syntax for the REVOKE command is

REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}

privilege_name is the access right or privilege granted to the user.


Some of the access rights are ALL, EXECUTE, and SELECT.

object_name is the name of an database object like TABLE, VIEW, STORED PROC and
SEQUENCE.

user_name is the name of the user to whom an access right is being granted.
PUBLIC is used to grant access rights to all users.

ROLES are a set of privileges grouped together.

WITH GRANT OPTION - allows a user to grant access rights to other users.

Queries containing DCL commands:-

SQL> create table record(id int,name char(10),age int);

Table created.
SQL> insert all

2 into record values(1,'Jude',18)


3 into record values(2,'Fred',19)
4 into record values(3,'Jones',20)
5 into record values(4,'Sam',18)

6 select * from dual;

4 rows created.

SQL> select * from record;

ID NAME AGE
---------- ---------- ----------
1 Jude 18
2 Fred 19
3 Jones 20
4 Sam 18

SQL> grant all on record to hr;

Grant succeeded.

SQL> /* all is privilege_name

SQL> record is object_name

SQL> hr is a another user*/

SQL> grant all on record to public;

Grant succeeded.

SQL> --now the grant had given to everyone

SQL> revoke all on record from public;

Revoke succeeded.

SQL> revoke all on record from hr;

Revoke succeeded.
Program 4

Queries Based on SQL JOINS

A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.

Different Types of SQL JOINS

Here are the different types of the JOINs in SQL:

 (INNER) JOIN: Returns records that have matching values in both tables
 LEFT (OUTER) JOIN: Return all records from the left table, and the matched
records from the right table
 RIGHT (OUTER) JOIN: Return all records from the right table, and the matched
records from the left table
 FULL (OUTER) JOIN: Return all records when there is a match in either left or
right table

Queries containing SQL Joins:-

SQL> create table record(id int,name char(10),age int);

Table created.

SQL> insert all

2 into record values(1,'Jude',18)

3 into record values(2,'Fred',19)

4 into record values(3,'Jones',20)

5 into record values(4,'Sam',18)

6 select * from dual;

4 rows created.
SQL> select * from record;

ID NAME AGE -
--------- ---------- ----------
1 Jude 18
2 Fred 19
3 Jones 20
4 Sam 18

SQL> create table record_1(gov_id varchar(20),occupation char(20),age int);

Table created.

SQL> insert all

2 into record_1 values('b40518006','student',18)

3 into record_1 values('b40518007','part-time student',21)

4 into record_1 values('b40518008','full-time student',19)

5 into record_1 values('b40518031','worker',22)

6 select * from dual;

4 rows created.

SQL> select * from record_1;

GOV_ID OCCUPATION AGE


-------------------- -------------------- ----------
b40518006 student 18

b40518007 part-time student 21

b40518008 full-time student 19

b40518031 worker 22
SQL> select record.name,record_1.occupation

2 from record

3 inner join record_1 on record.age=record_1.age;

NAME OCCUPATION
---------- --------------------

sam student

shivam student

shiv full-time student

SQL> select record.name,record_1.occupation

2 from record

3 left join record_1 on record.age=record_1.age;

NAME OCCUPATION
---------- --------------------
Sam student

Jude student

Fred full-time student

Jones

SQL> select record.name,record_1.occupation

2 from record

3 right join record_1 on record.age=record_1.age;

NAME OCCUPATION
---------- --------------------
shivam student

shiv full-time student

sam student
worker

part-time student

SQL> select record.name,record_1.occupation

2 from record

3 full join record_1 on record.age=record_1.age;

NAME OCCUPATION
---------- --------------------
sam student

shivam student

shiv full-time student

shiva worker

part-time student
Program 5

Queries Based on SQL Functions

SQL has many built-in functions for performing processing on string or numeric data.
Following is the list of all useful SQL built-in functions −

 SQL COUNT Function - The SQL COUNT aggregate function is used to count the
number of rows in a database table.

 SQL MAX Function - The SQL MAX aggregate function allows us to select the
highest (maximum) value for a certain column.

 SQL MIN Function - The SQL MIN aggregate function allows us to select the
lowest (minimum) value for a certain column.

 SQL AVG Function - The SQL AVG aggregate function selects the average value
for certain table column.

 SQL SUM Function - The SQL SUM aggregate function allows selecting the total
for a numeric column.

 SQL SQRT Functions - This is used to generate a square root of a given number.

 SQL RAND Function - This is used to generate a random number using SQL
command.

 SQL CONCAT Function - This is used to concatenate any string inside any SQL
command.

Queries containing SQL functions:-

SQL> create table record(id int,name char(20),daily_earning int);


Table created.

SQL> insert all

2 into record values(1,'Jude',500)


3 into record values(2,'Fred',400)
4 into record values(3,'Jones',600)
5 into record values(4,'Oscar',5000)
6 into record values(5,'Sam',700)

7 select * from dual;

5 rows created.

SQL> select count(*) from record;

COUNT(*)

SQL> select count(*) from record where name='shiv';

COUNT(*)

----------
1

SQL> select max(daily_earning) from record;

MAX(DAILY_EARNING)
------------------
5000

SQL> select min(daily_earning) from record;

MIN(DAILY_EARNING)
------------------
400

SQL> select min(daily_earning) least,max(daily_earning) max from record;

LEAST MAX

---------- ----------
400 5000

SQL> select avg(daily_earning) average from record;


AVERAGE
----------
1440

SQL> select name, avg(daily_earning)

2 from record group by name;

NAME AVG(DAILY_EARNING)
-------------------- ------------------
Jones 600
Oscar 5000
Sam 700
Jude 500
Fred 400

SQL> select sum(daily_earning) sum from record;

SUM
----------
7200

SQL> select name, SQRT(daily_earning)

2 from record;

NAME SQRT(DAILY_EARNING)
-------------------- -------------------
Jude 22.3606798
Fred 20
Jones 24.4948974
Oscar 70.7106781
Sam 26.4575131

SQL> select concat(id,name)

2 from record;

CONCAT(ID,NAME)
------------------------------------------------------------
1Jude
2Fred

3Jones

4Oscar

5Sam

Program 6

Queries to Create/Drop a View in SQL

SQL view:- A view is a result set of a stored query on the data.

The SQL view is a table which does not physically exist. It is only a virtual table.

SQL view can be created by an SQL query by joining one or more table.

Queries containing SQL view:-

SQL> create table record(id int,name char(20),daily_earning int);

Table created.

SQL> insert all

2 into record values(1,'Jude',500)


3 into record values(2,'Fred',400)
4 into record values(3,'Jones',600)
5 into record values(4,'Oscar',5000)
6 into record values(5,'Sam',700)

7 select * from dual;

5 rows created.

SQL> create table record_1(id_1 int,name char(20));

Table created.

SQL> insert all


2 into record_1 values(1,'Jude')
3 into record_1 values(2,'Fred')
4 into record_1 values(3,'Jones')
5 into record_1 values(4,'Sam')

6 select * from dual;

4 rows created.

SQL> create view view_1 as

2 select record.id,record_1.id_1,record.name,record_1.name

3 fromrecord,record_1
4 where record.id=record_1.id_1;

select record.id,record_1.id_1,record.name,record_1.name
*

ERROR at line 2:

ORA-00957: duplicate column name

SQL>--in a sql view duplicate column name can’t exist

SQL> alter table record_1 rename column name to name_1;

Table altered.

SQL> create view view_1 as

2 select record.id,record_1.id_1,record.name,record_1.name_1

3 from record,record_1

4 where record.id=record_1.id_1;

View created.

SQL> select * from view_1;

ID ID_1 NAME NAME_1


---------- ---------- -------------------- --------------------
1 1 Jude Jude
2 2 Fred Fred
3 3 Jones Jones
4 4 Sam Oscar

SQL> drop view view_1;


View dropped.

Program 7

PL/SQL Program to find radius of a circle and result get stored into the
Database

SQL> Set serveroutput on

SQL> declare

2 r float;

3 d float;

4 a foat;

5 c float;

6 pi constant number :=3.1;

7 begin

8 r:=&r;

9 d:=2*r;

10 a:=pi*r*r;

11 c:=2*pi*r

12 dbms_output.put_line('diameter = '||d);

13 dbms_output.put_line('area = '||a);
14 dbms_output.put_line('circumference = '||c);

15 end;

16 /

Output:-
Enter value for r: 4.4
old 8: r:=&r;
new 8: r:=4.4;
diameter = 8.8
area = 60.016
circumference = 27.28

PL/SQL procedure successfully completed.


Program 8

PL/SQL Program to Create Function & how it can be used in our Program

The PL/SQL Function is very similar to PL/SQL Procedure. The main difference between
procedure and a function is, a function must always return a value, and on the other hand a
procedure may or may not return a value. Except this, all the other things of PL/SQL
procedure are true for PL/SQL function too.

SQL> set serveroutput on

SQL> create or replace function adder(n1 in number, n2 in number)


2 return number

3 is

4 n3 number(8);

5 begin

6 n3 := n1+n2;

7 return n3;

8 end;

9/

Function created.

SQL> declare
2 n3 number(2);

3 begin

4 n3 := adder(11,22);

5 dbms_output.put_line('addition of (11,22) is : '||n3);

6 end;

7/

Output:-
addition of (11,22) is : 33

PL/SQL procedure successfully completed.


Program 9

PL/SQL Program to Create Cursor & how it can be used in our Program

When an SQL statement is processed, Oracle creates a memory area known as context area.
A cursor is a pointer to this context area. It contains all information needed for processing
the statement. In PL/SQL, the context area is controlled by Cursor. A cursor contains
information on a select statement and the rows of data accessed by it.

A cursor is used to referred to a program to fetch and process the rows returned by the SQL
statement, one at a time. There are two types of cursors:

Implicit Cursors
Explicit Cursors

Implicit Cursors:- The implicit cursors are automatically generated by Oracle while an
SQL statement is executed, if you don?t use an explicit cursor for the statement.

These are created by default to process the statements when DML statements like INSERT,
UPDATE, DELETE etc. are executed.

Explicit Cursors:- The Explicit cursors are defined by the programmers to gain more
control over the context area. These cursors should be defined in the declaration section of
the PL/SQL block. It is created on a SELECT statement which returns more than one row.

SQL> select * from record;

ID NAME DAILY_EARNING
---------- -------------------- -------------
1 shiv 5500
2 shiva 5400
3 harsh 5600

SQL> --implicit cursor

SQL> DECLARE

2 total_rows number(2);

3 BEGIN

4 UPDATE record

5 SET daily_earning = daily_earning + 5000;

6 IF sql%notfound THEN

7 dbms_output.put_line('no customers updated'); 8 ELSIF sql%found THEN

9 total_rows := sql%rowcount;

10 dbms_output.put_line( total_rows || ' customers updated ');

11 END IF;

12 END;

13 /

5 customers updated

PL/SQL procedure successfully completed.

SQL> select * from record;

ID NAME DAILY_EARNING
---------- -------------------- -------------
1 shiv
2 shiva
3 harsh
4 dipanshu 5 dilip
SQL> --explicit cursor SQL> DECLARE
10500
10400
10600
15000
10700
2 3 4 5 6 7 8 9 10
r_id record.id%type; r_name record.name%type;
CURSOR r_record is
SELECT id, name FROM record;
BEGIN
OPEN r_record;
LOOP
FETCH r_record into r_id, r_name; EXIT WHEN r_record%notfound;
 Shivam/06
BCA 2nd SEM.
21
   
                11 12 13 14 15
dbms_output.put_line(r_id || ' ' || r_name); END LOOP;
CLOSE r_record;
END;
/
Output:-
1 shiv
2 shiva
3 harsh
4 dipanshu
5 dilip
PL/SQL procedure successfully completed.
 Shivam/06
BCA 2nd SEM.
22
   
               Program 10
PL/SQL Program to Create Procedure & how it can be used in our Program
   The PL/SQL stored procedure or simply a procedure is a PL/SQL block which
performs one or more
 specific tasks. It is just like procedures in other programming languages. The
procedure contains a header and a body.
 o Header:- The header contains the name of the procedure and the parameters or
variables passed to the procedure.
o Body:- The body contains a declaration section, execution section and exception
section similar to a general PL/SQL block.
 SQL> create table user_1(id number(10) primary key,name varchar2(100)); Table
created.
SQL> create or replace procedure "INSERTUSER"
2 (id IN NUMBER,
3 name IN VARCHAR2)
4 is
5 begin
6 insert into user_1 values(id,name); 7 end;
8/
Procedure created. SQL> BEGIN
2 Insertuser(8006,'shivam');
3 dbms_output.put_line('record inserted successfully'); 4 END;
5/
record inserted successfully
PL/SQL procedure successfully completed. SQL> select * from user_1;
ID NAME
---------- -------------------------------------------------------------
 Shivam/06
BCA 2nd SEM.
23
   
                8006 shivam
SQL> drop procedure insertuser; Procedure dropped.
 Shivam/06
BCA 2nd SEM.
24
   
               Program 11
PL/SQL Program to Create Trigger & how it can be used in our Program
SQL> CREATE OR REPLACE TRIGGER daily_earning_changes
   Trigger is invoked by Oracle engine automatically whenever a specified event
occurs.Trigger is
 stored into database and invoked repeatedly, when specific condition match.Triggers
are stored programs, which are automatically executed or fired when some event
occurs.
 2 3 4 5 6 7 8 9
BEFORE DELETE OR INSERT OR UPDATE ON record FOR EACH ROW
WHEN (NEW.id > 0)
DECLARE
daily_earning_diff number;
begin
daily_earning_diff := (:new.daily_earning - :old.daily_earning);
dbms_output.put_line('Old earning: '|| :old.daily_earning);
10 dbms_output.put_line('new earning: '|| :new.daily_earning);
11 dbms_output.put_line('Earning difference: '|| daily_earning_diff); 12 end;
13 /
Trigger created. SQL> DECLARE
2 3 4 5 6 7 8 9 10
total_rows number(2); BEGIN
UPDATE record
SET daily_earning = daily_earning + 5000; IF sql%notfound THEN
dbms_output.put_line('no record updated'); ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' records updated ');
 Shivam/06
BCA 2nd SEM.
25
   
                11 END IF; 12 END;
13 /
Output:-
Old earning: 500
new earning: 5500
Earning difference: 5000
Old earning: 400
new earning: 5400
Earning difference: 5000
Old earning: 600
new earning: 5600
Earning difference: 5000
Old earning: 5000
new earning: 10000
Earning difference: 5000
Old earning: 700
new earning: 5700
Earning difference: 5000
5 records updated
PL/SQL procedure successfully completed.

You might also like