Professional Documents
Culture Documents
Final Lab Manual DBMS 2023
Final Lab Manual DBMS 2023
Practical No: 1
Aim: To create a of database of your own name and delete (drop) using
Postgres SQL.
Objective:
PostgreSQL is a powerful, open source object-relational database system. The
main purpose is to make students familiar with its reliability and stability as it is
mostly common with the companies.
Theory:
PostgreSQL (pronounced "post-gress-Q-L") is an open source relational
database management system (DBMS) developed by a worldwide team of
volunteers. PostgreSQL is not controlled by any corporation or other private
entity and the source code is available free of charge.
Key Features:
PostgreSQL runs on all major operating systems, including Linux, UNIX (AIX,
BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows. It supports
text, images, sounds, and video, and includes programming interfaces for C /
C++, Java, Perl, Python, Ruby, Tcl and Open Database Connectivity (ODBC).
• Complex SQL queries
• SQL Sub-selects
• Foreign keys
• Trigger
• Views
• Transactions
• Multi-version concurrency control (MVCC)
• Streaming Replication (as of 9.0)
• Hot Standby (as of 9.0)
Creating a database:
This command will create a database from PostgreSQL shell prompt.
Syntax:
The basic syntax of CREATE DATABASE statement is as follows −
CREATE DATABASE dbname;
where dbname is the name of a database to create.
Example
The following is a simple example, which will create abc in your PostgreSQL
schema
postgres=# CREATE DATABASE abc;
Page | 1
Database Management Systems Lab Manual 2022-23
Connectivity:
Syntax:
postgres=# \c dbname;
Example:
Since the created dbname is abc, the query will be:
postgres=# \c abc;
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page)
Example:
abc-# CREATE TABLE company(id integer, Name varchar(20), Salary integer,
Department varchar(20));
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page)
Syntax:
abc=# \d table_name;
Page | 2
Database Management Systems Lab Manual 2022-23
Example:
abc=# \d company;
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page)
Viva Questions:
1. Difference between MySql and PostgreSQL.
2. How do you login to psql shell in your system?
Page | 3
Database Management Systems Lab Manual 2022-23
Practical No: 2
Theory:
The PostgreSQL CREATE TABLE statement is used to create a new table in
any of the given database.
Syntax
Basic syntax of CREATE TABLE statement is as follows −
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype);
Example:
abc-# CREATE TABLE company(id integer, Name varchar(20), Salary integer,
Department varchar(20));
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page)
Page | 4
Database Management Systems Lab Manual 2022-23
abc-# \d
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page).
To Drop Database:
abc-# DROP DATABASE abc;
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page).
abc=# \c postgres;
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page).
postgres=# \c abc;
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page).
Alter Table:
Syntax:-
Page | 5
Database Management Systems Lab Manual 2022-23
abc-# alter table company add column dname varchar(5) not null;
Page | 6
Database Management Systems Lab Manual 2022-23
Practical No: 3
Objective:
It's a relational algebra calculator which calculates any relational algebra
statement like ( σ a > 42 ( A ) ) ⋈ ( π a, b ( B ) ) on a set of relations. Therefore its
necessary to make the students aware about this tool.
Theory:
Relational algebra is a procedural query language, which takes instances of
relations as input and yields instances of relations as output. It uses operators to
perform queries. An operator can be either unary or binary. They accept
relations as their input and yield relations as their output. Relational algebra is
performed recursively on a relation and intermediate results are also considered
relations.
Key Features:
The fundamental operations of relational algebra are as follows −
• Select
• Project
• Union
• Set different
• Cartesian product
• Rename
Page | 7
Database Management Systems Lab Manual 2022-23
Practical No: 4
Aim: To perform SQL query for creating relation Student or Emp and perform
DML operation on it. ( Select , Insert , Update, Delete).
Objective:
DML statements serve for manipulation with data in database. Therefore it is
essential that students get acquainted with these commands.
Theory:
INSERT:
The PostgreSQL INSERT INTO statement allows one to insert new rows into a
table. One can insert a single row at a time or several rows as a result of a query.
Syntax
Basic syntax of INSERT INTO statement is as follows −
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
• Here, column1, column2,...columnN are the names of the columns in the
table into which you want to insert data.
• The target column names can be listed in any order. The values supplied
by the VALUES clause or query are associated with the explicit or
implicit column list left-to-right.
Example:
CREATE TABLE company(Id integer primary key not null, Name varchar(20)
not null, Salary integer, Dept varchar(20));
Once the table is created now insert values in the table. The table will be created
in the database that you have created.
If you see the second query, the column salary is not written therefore it will
take default value.
SELECT:
Page | 8
Database Management Systems Lab Manual 2022-23
PostgreSQL SELECT statement is used to fetch the data from a database table,
which returns data in the form of result table. These result tables are called
result-sets.
After performing the above queries, you need to view your table and check
whether the records are entered or not.
Syntax:
SELECT * from table_name;
Example:
SELECT * from company;
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page).
Now if you want to fetch only Id, Name fields from the table:
Syntax:
SELECT Id, Name from company;
UPDATE:
The PostgreSQL UPDATE Query is used to modify the existing records in a
table.
With UPDATE query you can use WHERE clause to update the required row.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
Example:
UPDATE company SET Name = ‘Kanchan’, Salary = 30000 WHERE Id=3;
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page).
**Note- The update query mentioned above may differ depending on what
fields you have in your table and which record you need to update.**
DELETE:
Page | 9
Database Management Systems Lab Manual 2022-23
The PostgreSQL DELETE Query is used to delete the existing records from a
table. You can use WHERE clause with DELETE query to delete the selected
rows.
Syntax:
DELETE from table_name WHERE [condition];
Example:
DELETE from company WHERE Id = 2;
Output: (After Performance it is expected to take a screenshot of the
output and stick it on the left hand page).
Result: The SELECT, INSERT, UPDATE and DELETE queries are studied
and performed successfully.
Page | 10
Database Management Systems Lab Manual 2022-23
Practical No: 5
Theory:
Aggregate Functions:
Aggregate functions are used to compute against a "returned column of numeric data" from
your SELECT statement. They basically summarize the results of a particular column of
selected data. We are covering these here since they are required by the next topic, "GROUP
BY". Although they are required for the "GROUP BY" clause, these functions can be used
without the "GROUP BY" clause.
For example:
Another example:
Example:
Page | 11
Database Management Systems Lab Manual 2022-23
This particular statement is slightly different from the other aggregate functions since there
isn't a column supplied to the count function. This statement will return the number of rows
in the employees table.
Page | 12
Database Management Systems Lab Manual 2022-23
Practical No: 6
OBJECTIVE:
Student will able to:
⚫ Understand to perform join of columns of two or more tables.
BASIC CONCEPTS:
Join in SQL
SQL Join is used to fetch data from two or more tables, which is joined to appear as single set
of data. SQL Join is used for combining column from two or more tables by using values
common to both tables. Join Keyword is used in SQL queries for joining two or more tables.
Minimum required condition for joining table, is (n-1) where n, is number of tables. A table
can also join to itself known as, Self Join.
Types of Join
The following are the types of JOIN that we can use in SQL.
• Inner
• Outer
• Left
• Right
Page | 13
Database Management Systems Lab Manual 2022-23
ID NAME
1 abhi
2 adam
4 alex
1 DELHI
2 MUMBAI
3 CHENNAI
1 abhi 1 DELHI
2 adam 1 DELHI
4 alex 1 DELHI
1 abhi 2 MUMBAI
2 adam 2 MUMBAI
4 alex 2 MUMBAI
1 abhi 3 CHENNAI
2 adam 3 CHENNAI
Page | 14
Database Management Systems Lab Manual 2022-23
4 alex 3 CHENNAI
1 abhi
2 adam
3 alex
4 anu
1 DELHI
2 MUMBAI
3 CHENNAI
Page | 15
Database Management Systems Lab Manual 2022-23
ID NAME ID Address
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
Natural JOIN
Natural Join is a type of Inner join which is based on column having same name and same
datatype present in both the tables to be joined.
Natural Join Syntax is,
SELECT *
from table-name1
NATURAL JOIN
table-name2;
Example of Natural JOIN
The class table,
ID NAME
1 abhi
2 adam
3 alex
4 anu
1 DELHI
2 MUMBAI
3 CHENNAI
Page | 16
Database Management Systems Lab Manual 2022-23
SELECT * from class NATURAL JOIN class_info;
The result table will look like,
ID NAME Address
1 abhi DELHI
2 adam MUMBAI
3 alex CHENNAI
In the above example, both the tables being joined have ID column(same name and same
datatype), hence the records for which value of ID matches in both the tables will be the
result of Natural Join of these two tables.
Outer JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,
Page | 17
Database Management Systems Lab Manual 2022-23
Example of Left Outer Join
The class table,
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
Page | 18
Database Management Systems Lab Manual 2022-23
1 abhi
2 adam
3 alex
4 anu
5 ashish
Page | 19
Database Management Systems Lab Manual 2022-23
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
Page | 20
Database Management Systems Lab Manual 2022-23
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
1 abhi 1 DELHI
2 adam 2 MUMBAI
3 alex 3 CHENNAI
Page | 21
Database Management Systems Lab Manual 2022-23
Page | 22
Database Management Systems Lab Manual 2022-23
Practical No: 7
AIM: To perform trigger operations in PostgreSQL.
OBJECTIVE:
Student will able to:
⚫ Understand how to perform trigger based operations.
BASIC CONCEPTS:
PostgreSQL Triggers are database callback functions, which are automatically
performed/invoked when a specified database event occurs.
The following are important points about PostgreSQL triggers −
• PostgreSQL trigger can be specified to fire
o Before the operation is attempted on a row (before constraints
are checked and the INSERT, UPDATE or DELETE is
attempted)
o After the operation has completed (after constraints are checked
and the INSERT, UPDATE, or DELETE has completed)
o Instead of the operation (in the case of inserts, updates or deletes
on a view)
Syntax
Example :
Page | 23
Database Management Systems Lab Manual 2022-23
CREATE TRIGGER example_trigger AFTER INSERT ON COMPANY
FOR EACH ROW EXECUTE PROCEDURE auditlogfunc()
Page | 24
Database Management Systems Lab Manual 2022-23
Practical No: 8
AIM : To perform transaction control (TCL) using PostgreSQL commands.
OBJECTIVE:
Student will able to:
⚫ Understand to manage the transaction in databases when changes made by DML
statements.
BASIC CONCEPTS:
TCL command
Transaction Control Language (TCL) commands are used to manage transactions in
database.These are used to manage the changes made by DML statements. It also allows
statements to be grouped together into logical transactions.
Commit command
Commit command is used to permanently save any transaction into database.
Following is Commit command's syntax,
commit;
Rollback command
This command restores the database to last commited state. It is also use with savepoint
command to jump to a savepoint in a transaction.
Following is Rollback command's syntax,
rollback to savepoint-name;
Savepoint command
savepoint command is used to temporarily save a transaction so that you can rollback to that
point whenever necessary.
Following is savepoint command's syntax,
savepoint savepoint-name;
Example of Savepoint and Rollback
Page | 25
Database Management Systems Lab Manual 2022-23
Following is the class table,
ID NAME
1 abhi
2 adam
4 alex
Lets use some SQL queries on the above table and see the results.
INSERT into class values(5,'Rahul');
commit;
UPDATE class set name='abhijit' where id='5';
savepoint A;
INSERT into class values(6,'Chris');
savepoint B;
INSERT into class values(7,'Bravo');
savepoint C;
SELECT * from class;
The resultant table will look like,
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
6 chris
7 bravo
rollback to B;
Page | 26
Database Management Systems Lab Manual 2022-23
SELECT * from class;
The resultant table will look like
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
6 chris
rollback to A;
SELECT * from class;
The result table will look like
ID NAME
1 abhi
2 adam
4 alex
5 abhijit
Page | 27
Database Management Systems Lab Manual 2022-23
Page | 28
Database Management Systems Lab Manual 2022-23
Practical No: 9
Objective:
To learn procedural language designed specifically to embrace SQL statements within its
syntax. PL/SQL is a combination of SQL along with the procedural features of programming
languages.
Theory:
PostgreSQL functions, also known as Stored Procedures, allow you to carry out operations
that would normally take several queries and round trips in a single function within the
database. Functions allow database reuse as other applications can interact directly with your
stored procedures instead of a middle-tier or duplicating code.
Functions can be created in a language of your choice like SQL, PL/pgSQL, C, Python, etc.
Key Features:
The basic syntax to create a function is as follows:
Where,
• function-name specifies the name of the function. [OR REPLACE] option allows
modifying an existing function. The function must contain a return statement.
• RETURN clause specifies that data type you are going to return from the function.
The return_datatype can be a base, composite, or domain type, or can reference the
type of a table column.
• Function-body contains the executable part. The AS keyword is used for creating a
standalone function.
• plpgsql is the name of the language that the function is implemented in. Here, we use
this option for PostgreSQL, it Can be SQL, C, internal, or the name of a user-defined
procedural language. For backward compatibility, the name can be enclosed by single
quotes.
Example :
Page | 29
Database Management Systems Lab Manual 2022-23
Output: (After Performance it is expected to take a screenshot of the output along with
query and stick it on the left hand page)
Page | 30