Dbms Practical File
Dbms Practical File
Dbms Practical File
Aim: Introduction of DBMS (with basic commands DDL, DML, DCL, TCL)
Theory:
Database Management System (DBMS) refers to the technology solution used to optimize
and manage the storage and retrieval of data from databases. DBMS offers a systematic
approach to manage databases via an interface for users as well as workloads accessing the
databases via apps.
DBMS has the following key components:
Software. DBMS is primarily a software system that can be considered as a management
console or an interface to interact with and manage databases.
Data. DBMS contains operational data, access to database records and metadata as a resource
to perform the necessary functionality.
Database languages. These are components of the DBMS used to access, modify, store, and
retrieve data items from databases; specify database schema.
Query processor. As a fundamental component of the DBMS, the query processor acts as an
intermediary between users and the DBMS data engine in order to communicate query
requests.
Runtime database manager. A centralized management component of DBMS that handles
functionality associated with runtime data, which is commonly used for context-based
database access.
Database manager. Unlike the runtime database manager that handles queries and data at
runtime, the database manager performs DBMS functionality associated with the data within
databases.
Database engine. This is the core software component within the DBMS solution that
performs the core functions associated with data storage and retrieval.
Database language
Structured Query Language(SQL) as we all know is the database language by the use of
which we can perform certain operations on the existing database and also we can use this
language to create a database. SQL uses certain commands like Create, Drop, Insert etc. to
carry out the required tasks.
These SQL commands are mainly categorized into four categories as:
1. DDL – Data Definition Language
2. DML – Data Manipulation Language
3. DCL – Data Control Language
4. TCL – Transaction Control Language
DML(Data Manipulation Language) : The SQL commands that deals with the
manipulation of data present in the database belong to DML or Data Manipulation Language
and this includes most of the SQL statements.
Examples of DML:
INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a table.
DELETE – is used to delete records from a database table.
DCL(Data Control Language) : DCL includes commands such as GRANT and REVOKE
which mainly deals with the rights, permissions and other controls of the database system.
Examples of DCL commands:
GRANT-gives user’s access privileges to database.
REVOKE-withdraw user’s access privileges given by using the GRANT command
Benefits of DBMS
Introducing DBMS software to manage databases results in the following benefits:
Aim: To implement Data Definition language - Create, alter, drop, truncate - To implement
Constraints. (a). Primary key, (b).Foreign Key, (c). Check, (d). Unique, (e). Null, (f). Not null
, (g) . Default, (h). Enable Constraints, (i). Disable Constraints (j). Drop Constraints
Implementation
Creating Table
CREATE TABLE students (
studentID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Alter table
ALTER TABLE students
ADD Email varchar(255);
Dropping table
DROP Table students;
Aim: To implementation on DML, TCL and DRL (a) Insert, (b) Select, (c) Update, (d)
Delete, (e) commit, (f) rollback, (g) save point, (i) Like'%', (j)Relational Operator.
Implementation
Inserting into table
INSERT INTO students (studentID, FirstName,LastName, Address,City,emailid,age)
VALUES ('2213', 'John','Doe','23/1 New Delhi', 'Delhi', 'John@gmail.com',20),
('2214','Raj','Kumar','11/2 New Delhi','Delhi','raj@gmail.com',21),
('2215','Ravi','Kumar','13/2 Borivli','Mumbai','ravi@gmail.com',21),
('2216','Ajay','aditya','122 South Mumbai','Mumbai','aditya@gmail.com',22);
Select statement
select * from students;
Updating table
UPDATE students
SET FirstName = 'aditya', LastName = ‘Singh’
WHERE studentID = 2216;
select * from students where studentID = 2216;
Deleting table
DELETE FROM students WHERE studentID=2216;
Commit
set autocommit = 0;
INSERT INTO students (studentID, FirstName,LastName, Address,City,emailid,age)
VALUES ('2216', 'Aditya','Singh','23/1 New Delhi', 'Delhi', 'aditya@gmail.com',20);
In new connection
Select * from students;
In old connection
Commit;
In new connection
Select * from students;
Rollback
set autocommit = 0;
INSERT INTO students (studentID, FirstName,LastName, Address,City,emailid,age)
VALUES ('2217', 'Jai','Singh','23/1 New Delhi', 'Delhi', 'jai@gmail.com',20);
Select * from students;
rollback;
select * from students;
Savepoint
START TRANSACTION;
SAVEPOINT initial_save;
UPDATE students
SET firstName = 'Harry'
where studentID = 2216;
select * from students;
ROLLBACK TO initial_save;
select * from students;
Like operator
SELECT * FROM students
WHERE FirstName LIKE 'a%';
Relational operator
SELECT * FROM students
WHERE age > 20;
SELECT * FROM students
WHERE age <> 21;
Aim: To implement Nested Queries & Join Queries - To implementation of Nested Queries -
(a) Inner join, (b) Left join, (c) Right join (d) Full join
Implementation
Nested Queries
SELECT * FROM students
WHERE studentID IN (SELECT studentId
FROM students
WHERE age > 20) ;
UPDATE students
SET age = age + 1
WHERE age IN (SELECT age FROM students2
WHERE age >= 20 );
select * from students;
DELETE FROM students2
WHERE age IN (SELECT age FROM students
WHERE age = 21 );
select * from students2;
Inner Join
SELECT students.FirstName, students.address, marks.courseID,marks.score
FROM students
INNER JOIN marks
ON students.studentID = marks.studentID;
Left join
SELECT students.FirstName, students.address, marks.courseID,marks.score
FROM students
LEFT JOIN marks
ON students.studentID = marks.studentID;
Right Join
SELECT students.FirstName, students.address, marks.courseID,marks.score
FROM students
RIGHT JOIN marks
ON students.studentID = marks.studentID;
Full Join
LEFT JOIN marks
ON students.studentID = marks.studentID
union all
SELECT students.FirstName , students.address, marks.courseID,marks.score
FROM students
RIGHT JOIN marks
ON students.studentID = marks.studentID;
Experiment NO – 5
Aim: To implement Views - (a) View, (b) joint view, (c) force view, (d) View with check
option
Implementation
Creating View
CREATE VIEW student_address AS SELECT FirstName,address FROM students;
select * from student_address;
The selection structure tests a condition, then executes one sequence of statements instead of
another, depending on whether the condition is true or false. A condition is any variable or
expression that returns a Boolean value (TRUE or FALSE). The iteration structure executes a
sequence of statements repeatedly as long as a condition holds true. The sequence structure
simply executes a sequence of statements in the order in which they occur.
Conditional Control: IF Statements
Often, it is necessary to take alternative actions depending on circumstances.
The IF statement lets you execute a sequence of statements conditionally. That is, whether the
sequence is executed or not depends on the value of a condition. There are three forms
of IF statements: IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF.
IF-THEN
The simplest form of IF statement associates a condition with a sequence of statements
enclosed by the keywords THEN and END IF (not ENDIF), as follows:
IF condition THEN
sequence_of_statements;
END IF;
The sequence of statements is executed only if the condition yields TRUE. If the condition
yields FALSE or NULL, the IF statement does nothing. In either case, control passes to the
next statement. An example follows:
IF sales > quota THEN
compute_bonus(empid);
UPDATE payroll SET pay = pay + bonus WHERE empno = emp_id;
END IF;
Implementation
declare
x number(5);
y number(5);
z number(7);
begin
x:=13;
y:=27;
z:=x+y;
dbms_output.put_line('Sum is '||z);
end;
Experiment NO – 8
Implementation
DECLARE n_sales
NUMBER := 3000000;
BEGIN
IF n_sales > 100000 THEN
DBMS_OUTPUT.PUT_LINE( 'Sales revenue is greater than 100K ' );
END IF;
END;
Experiment NO – 9
Implementation
DECLARE
a NUMBER:=14;
BEGIN
dbms_output.put_line ('Program started');
IF( mod(a,2)=0) THEN
dbms_output.put_line('a is even number' );
ELSE
dbms_output.put_line('a is odd number1');
END IF;
dbms_output.put_line ('Program completed');
END;
Experiment NO – 10
Aim: To write a PL/SQL block for greatest of three numbers using IF AND ELSEIF
Implementation
DECLARE
a NUMBER := 26;
b NUMBER := 18;
c NUMBER := 11;
BEGIN
IF a > b
AND a > c THEN
dbms_output.Put_line('Greatest number is '||a);
ELSIF b > a AND b > c THEN
dbms_output.Put_line('Greatest number is '||b);
ELSE
dbms_output.Put_line('Greatest number is '||c);
END IF;
END;
Experiment NO – 11
Aim: To write a PL/SQL block for summation of odd numbers using for LOOP
Implementation
DECLARE
x NUMBER(3) := 1;
oddsum NUMBER(4) := 0;
BEGIN
dbms_output.Put_line(x);
oddsum := oddsum + x;
x := x + 2;
END LOOP;
END;
Experiment NO – 12
Procedure is a subprogram unit that consists of a group of PL/SQL statements. Each procedure in
Oracle has its own unique name by which it can be referred. This subprogram unit is stored as a
database object. Below are the characteristics of this subprogram unit.
Note: Subprogram is nothing but a procedure, and it needs to be created manually as per the
requirement. Once created they will be stored as database objects.
Procedures are standalone blocks of a program that can be stored in the database.
Call to these procedures can be made by referring to their name, to execute the PL/SQL
statements.
It is mainly used to execute a process in PL/SQL.
It can have nested blocks, or it can be defined and nested inside the other blocks or
packages.
It contains declaration part (optional), execution part, exception handling part (optional).
The values can be passed into the procedure or fetched from the procedure through
parameters.
These parameters should be included in the calling statement.
Procedure can have a RETURN statement to return the control to the calling block, but it
cannot return any values through the RETURN statement.
Procedures cannot be called directly from SELECT statements. They can be called from
another block or through EXEC keyword.
Syntax
CREATE PROCEDURE instructs the compiler to create new procedure. Keyword 'OR REPLACE'
instructs the compile to replace the existing procedure (if any) with the current one.
Keyword 'IS' will be used, when the procedure is nested into some other blocks. If the
procedure is standalone then 'AS' will be used. Other than this coding standard, both have
the same meaning.
Example;
IS
BEGIN
END;
Implementation
Implementation
BEGIN
insertuser(id => 104,'Virat');
dbms_output.put_line('record inserted successfully');
END;
Select * from users2;
Experiment NO – 15
Implementation
DECLARE
num1 INTEGER;
num2 INTEGER;
t INTEGER;
BEGIN
num1 := 18;
num2 := 36;
WHILE MOD(num2, num1) != 0 LOOP
t := MOD(num2, num1);
num2 := num1;
num1 := t;
END LOOP;
dbms_output.Put_line('GCD of '||num1 ||' and '||num2 ||' is '||num1);
END;
Experiment NO – 16
Implementation
Address varchar(255) ,
);
INSERT All
VALUES
VALUES
VALUES
('2215','Ravi','Kumar','13/2 Borivli','Mumbai','ravi@gmail.com',21)
VALUES
c_studentid students.studentid%type;
c_FirstName students.FirstName%type;
c_addr students.address%type;
CURSOR c_students is
BEGIN
OPEN c_students;
LOOP
END LOOP;
CLOSE c_students;
END;