DBMS Practical File
DBMS Practical File
OF
DBMS
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.
SQL> create table record (id int primary key, name char(20), city varchar(30));
Table created.
SQL> alter table record add (email varchar (40), mobile_no int;
Table altered
Table altered
Table altered.
Table altered.
SQL> desc record_1;
1 row created.
Table truncated.
no rows selected.
Table dropped.
ERROR:
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.
SQL> create table record(id int primary key, name char(20), city varchar(30),mobile_no
int);
Table created.
no rows selected.
1 row created.
3 rows created.
SQL> select * from record;
Table altered.
1 row updated.
1 row updated.
1 row updated.
1 row updated.
SQL> select * from record;
1 row deleted.
3 rows deleted.
no rows selected.
Program 3
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.
GRANT privilege_name
ON object_name
TO {user_name|PUBLIC|role_name}
[WITH GRANT OPTION];
REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}
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.
WITH GRANT OPTION - allows a user to grant access rights to other users.
Table created.
SQL> insert all
4 rows created.
ID NAME AGE
---------- ---------- ----------
1 Jude 18
2 Fred 19
3 Jones 20
4 Sam 18
Grant succeeded.
Grant succeeded.
Revoke succeeded.
Revoke succeeded.
Program 4
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
(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
Table created.
4 rows created.
SQL> select * from record;
ID NAME AGE -
--------- ---------- ----------
1 Jude 18
2 Fred 19
3 Jones 20
4 Sam 18
Table created.
4 rows created.
b40518031 worker 22
SQL> select record.name,record_1.occupation
2 from record
NAME OCCUPATION
---------- --------------------
sam student
shivam student
2 from record
NAME OCCUPATION
---------- --------------------
Sam student
Jude student
Jones
2 from record
NAME OCCUPATION
---------- --------------------
shivam student
sam student
worker
part-time student
2 from record
NAME OCCUPATION
---------- --------------------
sam student
shivam student
shiva worker
part-time student
Program 5
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.
5 rows created.
COUNT(*)
COUNT(*)
----------
1
MAX(DAILY_EARNING)
------------------
5000
MIN(DAILY_EARNING)
------------------
400
LEAST MAX
---------- ----------
400 5000
NAME AVG(DAILY_EARNING)
-------------------- ------------------
Jones 600
Oscar 5000
Sam 700
Jude 500
Fred 400
SUM
----------
7200
2 from record;
NAME SQRT(DAILY_EARNING)
-------------------- -------------------
Jude 22.3606798
Fred 20
Jones 24.4948974
Oscar 70.7106781
Sam 26.4575131
2 from record;
CONCAT(ID,NAME)
------------------------------------------------------------
1Jude
2Fred
3Jones
4Oscar
5Sam
Program 6
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.
Table created.
5 rows created.
Table created.
4 rows created.
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:
Table altered.
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.
Program 7
PL/SQL Program to find radius of a circle and result get stored into the
Database
SQL> declare
2 r float;
3 d float;
4 a foat;
5 c float;
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 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.
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);
6 end;
7/
Output:-
addition of (11,22) is : 33
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.
ID NAME DAILY_EARNING
---------- -------------------- -------------
1 shiv 5500
2 shiva 5400
3 harsh 5600
SQL> DECLARE
2 total_rows number(2);
3 BEGIN
4 UPDATE record
6 IF sql%notfound THEN
9 total_rows := sql%rowcount;
11 END IF;
12 END;
13 /
5 customers updated
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.