Dbms Lab Mannual
Dbms Lab Mannual
– 1
Aim : Case study about SQL and its command languages .
Theory : SQL is Structured Query Language, which is a computer language for storing, manipulating and
retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the Relational Database Management
Systems (RDMS) like MySQL, MS Access, Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their
standard database language.
Advantages -
• Allows users to access data in the relational database management systems.
• Allows users to describe the data.
• Allows users to define the data in a database and manipulate that data.
• Allows to embed within other languages using SQL modules, libraries & pre-compilers.
• Allows users to create and drop databases and tables.
• Allows users to create view, stored procedure, functions in a database.
• Allows users to set permissions on tables, procedures and views.
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax:
d. TRUNCATE: It is used to delete all the rows from the table and free the space containing the table.
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a table.
b. UPDATE: This command is used to update or modify the value of a column in the table.
b. Rollback: Rollback command is used to undo transactions that have not already been saved to the database.
Syntax: ROLLBACK;
c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back the entire transaction.
Output:
Experiment No. – 3
Aim : Write a SQL statements for rollback, commit and save points.
Theory:
The ROLLBACK Command
The ROLLBACK command is the transactional command used to undo transactions that have not already been
saved to the database.
The syntax −ROLLBACK;
Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example, which would delete those records from the table which have the age = 25 and then
ROLLBACK the changes in the database.
Thus, the delete operation would not impact the table and the SELECT statement would produce the following
result.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
The COMMIT Command
The COMMIT command is the transactional command used to save changes invoked by a transaction to the
database.
The syntax -COMMIT;
Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is an example which would delete those records from the table which have age = 25 and then COMMIT
the changes in the database.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
The following code block contains the series of operations.
Syntax:
UPDATE TABLE_NAME
SET COLUMN_VALUE
= CASE COLUMN_NAME
WHEN 'COLUMN_NAME1' THEN COLUMN_VALUE1
WHEN 'COLUMN_NAME2' THEN COLUMN_VALUE2
ELSE COLUMN_VALUE
END
WHERE BAND_NAME IN('COLUMN_NAME1', 'COLUMN_NAME2');
Query:
CREATE DATABASE GeeksForGeeks
USE GeeksForGeeks
CREATE TABLE BANDS(
BAND_NAME VARCHAR(20),
PERFORMING_COST INT,
NUMBER_OF_MEMBERS INT);
INSERT INTO BANDS VALUES('INDIAN OCEAN',10000,5);
INSERT INTO BANDS VALUES('BTS',20000,6);
INSERT INTO BANDS VALUES('METALLICA',30000,10);
INSERT INTO BANDS VALUES('BEATLES',40000,4);
INSERT INTO BANDS VALUES('EAGLES',50000,4);
SELECT * FROM BANDS;
UPDATE BANDS
SET PERFORMING_COST = CASE BAND_NAME
WHEN 'METALLICA' THEN 90000
WHEN 'BTS' THEN 200000
ELSE PERFORMING_COST
END
WHERE BAND_NAME IN('METALLICA', 'BTS');
SELECT * FROM BANDS;
Output:
Before update-
After update-
Experiment No. – 5
Aim : Find the third highest paid and third lowest paid salary
Query:
• FOR THE HIGHEST PAID-
As this query is nested query lets understand each part step by step:
Step 1: First this part of the query will get executed then the outer part of the query will act on the result p
roduced by this query :
As you can see that few employees are getting the same salary(for example Bhaskar, Parul, and Chandan,
Garima are getting the same salary, therefore we have used dense_rank(), order by salary desc will arrang
e salary in descending order.
Output of : select ename, salary, dense_rank() over(order by salary desc)rank from Emp
ENAME SALARY RANK
------------ ---------- ----------
sonoo 40000 1
ravi 37000 2
rohit 32000 3
bhaskar 30000 4
parul 30000 4
akshita 28000 5
durgesh 28000 5
garima 25000 6
chandan 25000 6
amit 20000 7
select * from(select ename, salary, dense_rank() over(order by salary desc)r from Emp) where r=3
Output for n = 3 will be:
ENAME SALARY R
------------ ---------- ----------
rohit 32000 3
• FOR THE LOWEST PAID-
Output of : select ename, salary, dense_rank() over(order by salary )rank from Emp
ENAME SALARY RANK
------------ ---------- ----------
amit 20000 1
chandan 25000 2
garima 25000 2
durgesh 28000 3
akshita 28000 3
parul 30000 4
bhaskar 30000 4
rohit 32000 5
ravi 37000 6
sonoo 40000 7
select * from(select ename, salary, dense_rank() over(order by salary desc)r from Emp) where r=2
Output for n = 3 will be:
ENAME SALARY R
------------ ---------- ----------
durgesh 28000 3
•
Experiment No. – 6
Aim : Display the 3rd, 4th, 9th rows from table.
Syntax :
SELECT * FROM <table_name> LIMIT N-1,1;
Theory : The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. Here we
have to find more the one alphabet starting names so we will use ‘OR’ function , to get all those letter names
together.
Syntax: SELECT * FROM Table_name
WHERE column_name LIKE 'a%';
Query:
create the table –
CustomerID CustomerNam ContactNamE
17 Drachenblut Delikatessen Aachen
38 Island Tradin Cowes
39 Königlich Esse Brandenburg
19 Eastern Connectio London
Output:
Experiment No. – 8
Aim : Show all employees who were hired the first half of the month.
Syntax:
select * from table_name where to_char (column_name, ‘DD’)<‘16’;
Theory :
here below is given the table-
EMPLOYEES
Query:
select * from employees where to_char (HIRE_DATE, ‘DD’)<‘16’;
Output:
Employee_id First_name Last_name email Phone- Hire-date
number
100 Steven King SKING 515.123.4567 1987/06/17
Experiment No. – 9
Aim : inner and full joint
Theory :
• INNER JOIN
The INNER JOIN keyword selects records that have matching values in both tables.
Syntax-
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table
records.
Syntax-
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Output-
Experiment No. – 10
Aim : Solving the case study of Library Management system using ER data model.
Theory : ER Diagram is known as Entity-Relationship Diagram, it is used to analyze the structure of the
Database. It shows relationships between entities and their attributes. An ER Model provides a means of
communication.
The Library Management System database keeps track of readers with the following considerations –
• The system keeps track of the staff with a single point authentication system comprising login Id and
password.
• Staff maintains the book catalog with its ISBN, Book title, price(in INR), category(novel, general, story),
edition, author Number and details.
• A publisher has publisher Id, Year when the book was published, and name of the book.
• Readers are registered with their user_id, email, name (first name, last name), Phone no (multiple
entries allowed), communication address. The staff keeps track of readers.
• Readers can return/reserve books that stamps with issue date and return date. If not returned within
the prescribed time period, it may have a due date too.
• Staff also generate reports that has readers id, registration no of report, book no and return/issue info.