ADBMS LabFile Final
ADBMS LabFile Final
(ADBMS) Lab
ETCS 457
List of Experiments
RUCHIR BISHT 05914802717 7-C-4
1. (a) Compare different Databases such as PostgreSQL, MySQL and Maria DB.
(b) Installation of PostgreSQL.
2. Consider Schema: Student (Student Id, Student name, age, student Percentage). Write
SQL queries on Student Info Table:
d. Built-in Functions (e.g. Sum, min, max, avg, count, lower, upper, etc.)
Experiment-01(a)
RUCHIR BISHT 05914802717 7-C-4
Aim: Compare different Databases such as PostgreSQL, MySQL and Maria DB.
Name MariaDB MySQL PostgreSQL
MySQL application Widely used Widely used open
Description compatible open open source RDBMS
source RDBMS, source RDBMS
Primary
database model Relational DBMS Relational DBMS Relational DBMS
Document store Document store Document store
Secondary Graph DBMS
database
models
Website mariadb.com www.mysql.com www.postgresql.org
MariaDB Oracle PostgreSQL Global
Developer Corporation Ab Development Group
(MariaDB
Enterprise)
Initial release 2009 1995 1989
Current release 10.4.12, Jan 2020 8.0.21, Feb 2020 12.4, Aug 2020
License Open Source Open Source Open Source
Experiment-01(b)
RUCHIR BISHT 05914802717 7-C-4
Step 2: Change the Installation directory if required, else leave it to default. Click
Next.
RUCHIR BISHT 05914802717 7-C-4
Step 3: You may choose the components you want to install in your system. You may
uncheck Stack Builder. Click Next.
Step 5: Enter root user password. Make a note of it. Click Next.
Step 7: Select the nearest geographic location for the new database cluster.
Step 9: Click the next button. And the installation process with start and wait for few
minutes.
Step 10: Once install is complete you will see the Stack Builder prompt
Uncheck that option. We will use Stack Builder in more advance tutorials
Click Finish.
Step 11: To launch PostgreSQL go to Start Menu and search PostgreSQL. And then
open pgAdmin 4 for Graphical user interface(GUI) and SQL SHELL(psql) for
command line Interface(CLI).
RUCHIR BISHT 05914802717 7-C-4
Step 12: You will see pgAdmin homepage and SQL Command interface(psql).
RUCHIR BISHT 05914802717 7-C-4
Experiment-02
AIM: Consider Schema: Student (Student Id, Student name, age, student Percentage).
Write SQL queries on Student Info Table:
a. DDL (e.g. create, alter, drop, rename, truncate),
b. DML (e.g. Insert, update, delete etc.),
c. DCL (e.g. Grant, revoke etc.)
d. Built-in Functions (e.g. Sum, min, max, avg, count, lower, upper, etc.)
e. Views: Create and Drop View
Syntax:
CREATE SCHEMA Schema_Name;
2. ALTER:
The ALTER TABLE statement is used to either add, modify or delete
constraints and columns from a table.
Syntax:
ALTER TABLE table_name
ADD column_name datatype;
RUCHIR BISHT 05914802717 7-C-4
3. DROP:
The DROP TABLE statement is used to drop the entire table with all its
values.
Syntax:
DROP TABLE table_name;
4. TRUNCATE:
The TRUNCATE statement is used to delete the data which is present
inside a table, but the table doesn’t get deleted.
Syntax:
TRUNCATE TABLE table_name;
RUCHIR BISHT 05914802717 7-C-4
1. INSERT:
The INSERT statement is used to insert new records in a table.
Syntax:
The INSERT INTO statement can be written in the following two ways:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Or
INSERT INTO table_name VALUES (value1, value2, value3, ...);
2. UPDATE:
The UPDATE statement is used to modify the existing records in a table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
RUCHIR BISHT 05914802717 7-C-4
3. DELETE:
The DELETE statement is used to delete existing records in a table.
Syntax:
4. SELECT:
The SELECT statement is used to select data from a database and the data
returned is stored in a result table, called the result-set.
Syntax:
1. GRANT:
The GRANT command is used to provide user access privileges or other
privileges for the schema.
Syntax:
2. REVOKE:
The REVOKE command is used to withdraw user’s access privileges
given by using the GRANT command.
Syntax:
1. MIN:
The MIN function returns the smallest value of the selected column in a
table.
RUCHIR BISHT 05914802717 7-C-4
Syntax:
SELECT MIN(column_name)
FROM table_name
WHERE condition;
2. MAX:
The MAX function returns the largest value of the selected column in a
table.
Syntax:
3. COUNT:
The COUNT function returns the number of rows that match the
specified criteria.
Syntax:
4. AVG:
The AVG function returns the average value of a numeric column that
you choose.
Syntax:
5. SUM:
The SUM function returns the total sum of a numeric column that you
choose.
Syntax:
SELECT SUM(column_name)
FROM table_name
WHERE condition;
1. CREATE:
The CREATE VIEW statement is used to create a view from an existing
table.
Syntax
2. DROP VIEW:
The DROP VIEW statement is used to delete a view.
Syntax
DROP VIEW view_name;
RUCHIR BISHT 05914802717 7-C-4
EXPERIMENT - 03
AIM: Perform Nested Queries in PostgreSQL.
1. SELECT
To display all the schema and data in the database.
a. student table.
b. course table.
c. student_course table
RUCHIR BISHT 05914802717 7-C-4
2. IN
The IN operator allows you to specify multiple values in a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
3. NOT IN
The NOT IN operator allows you to exclude multiple values in a WHERE
clause.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name NOT IN (SELECT STATEMENT);
RUCHIR BISHT 05914802717 7-C-4
4. EXISTS
The EXISTS operator is used to test for the existence of any record in a
subquery.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
5. ANY
The ANY operator returns true if any of the subquery values meet the
condition.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name FROM table_name WHERE condition);
RUCHIR BISHT 05914802717 7-C-4
6. ALL
The ALL operator returns true if all of the subquery values meet the condition.
Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name FROM table_name WHERE condition);
7. 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;
8. LEFT JOIN
The LEFT JOIN keyword returns all records from the left table (table1), and
the matched records from the right table (table2). The result is NULL from the
right side, if there is no match.
RUCHIR BISHT 05914802717 7-C-4
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
9. RIGHT JOIN
The RIGHT JOIN keyword returns all records from the right table (table2), and
the matched records from the left table (table1). The result is NULL from the
left side, when there is no match.
Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name;
Syntax:
SELECT column_name(s)
FROM table1
CROSS JOIN table2;
RUCHIR BISHT 05914802717 7-C-4
Experiment-04
AIM: Create a PLSQL Function to:
1. Add 2 numbers (if single argument then increments by 1 otherwise return the
sum)
2. Find the number of course in the college
3. Count (*) from the company table.
PostgreSQL functions
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.
Syntax:
CREATE [OR REPLACE] FUNCTION function_name (arguments)
RETURNS return_datatype AS $variable_name$
DECLARE
declaration;
BEGIN
< function_body >
RETURN { variable_name | value }
END;
LANGUAGE plpgsql | sql | etc;
1. Function to Add 2 numbers (if single argument then increments by 1
otherwise return the sum)
Using CLI:
RUCHIR BISHT 05914802717 7-C-4
Using GUI:
Add a new function , in the general section enter the function name and
comment field is optional so as to describe the function logic or working.
Then in the Definition section select the return type , Language and the
function parameters or variables and their data type , default value , variable
name and Mode (IN , OUT , INOUT).
RUCHIR BISHT 05914802717 7-C-4
Then write the Code logic within the Begin and End keywords.
Output
RUCHIR BISHT 05914802717 7-C-4
Using GUI:
Add a new function , in the general section enter the function name and
comment field is optional so as to describe the function logic or working.
RUCHIR BISHT 05914802717 7-C-4
Then in the Definition section select the return type , Language and the
function parameters or variables and their data type , default value , variable
name and Mode (IN , OUT , INOUT).
Then write the Code logic within the Begin and End keywords
RUCHIR BISHT 05914802717 7-C-4
Output
RUCHIR BISHT 05914802717 7-C-4
Using GUI:
Add a new function , in the general section enter the function name and
comment field is optional so as to describe the function logic or working.
RUCHIR BISHT 05914802717 7-C-4
Then in the Definition section select the return type , Language and the
function parameters or variables and their data type , default value , variable
name and Mode (IN , OUT , INOUT).
Then write the Code logic within the Begin and End keywords
RUCHIR BISHT 05914802717 7-C-4
Output
RUCHIR BISHT 05914802717 7-C-4
RUCHIR BISHT 05914802717 7-C-4
Experiment-05
AIM : Create a PLSQL Function to:
1. Function to compare two numbers
2. Find the greatest of Three numbers
3. Check whether the number is Even-Odd.
PostgreSQL functions
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.
Syntax:
CREATE [OR REPLACE] FUNCTION function_name (arguments)
RETURNS return_datatype AS $variable_name$
DECLARE
declaration;
BEGIN
< function_body >
RETURN { variable_name | value }
END;
LANGUAGE plpgsql | sql | etc;
Using GUI:
Add a new function , in the general section enter the function name
and comment field is optional so as to describe the function logic or
working.
Then in the Definition section select the return type , Language and
the function parameters or variables and their data type , default
value , variable name and Mode (IN , OUT , INOUT).
RUCHIR BISHT 05914802717 7-C-4
Then write the Code logic within the Begin and End keywords.
Output
Using GUI:
Add a new function , in the general section enter the function name
and comment field is optional so as to describe the function logic or
working.
RUCHIR BISHT 05914802717 7-C-4
Then in the Definition section select the return type , Language and
the function parameters or variables and their data type , default
value , variable name and Mode (IN , OUT , INOUT).
Then write the Code logic within the Begin and End keywords
RUCHIR BISHT 05914802717 7-C-4
Output
RUCHIR BISHT 05914802717 7-C-4
Using GUI:
Add a new function , in the general section enter the function name
and comment field is optional so as to describe the function logic or
working.
RUCHIR BISHT 05914802717 7-C-4
Then in the Definition section select the return type , Language and
the function parameters or variables and their data type , default
value , variable name and Mode (IN , OUT , INOUT).
RUCHIR BISHT 05914802717 7-C-4
Then write the Code logic within the Begin and End keywords
Output
RUCHIR BISHT 05914802717 7-C-4
Experiment-06
AIM: Write the cursor to increase the marks of student by 10%.
PostgreSQL Cursor
A cursor is a pointer to this context area. PL/SQL controls the context area through a
cursor. A cursor holds the rows (one or more) returned by a SQL statement. The set
of rows the cursor holds is referred to as the active set.
You can name a cursor so that it could be referred to in 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
Syntax:
declare
cur_films cursor for
select *
from film;
cur_films2 cursor (year integer) for
select *
from film
where release_year = year;
returns void AS $$
declare
rec_student record;
cur_student cursor(sid integer)
for select marks
from student_marks
where student_marks.s_id = sid;
begin
-- open the cursor
open cur_student(s_id);
loop
fetch cur_student into rec_student;
exit when not found;
Update student_marks
Set marks = rec_student.marks + (rec_student.marks * 10/100)
where current of cur_student;
end loop;
-- close the cursor
close cur_student;
end; $$
language plpgsql;
BEGIN
FOR rec IN (SELECT * FROM student_marks) LOOP
perform modify_marks(rec.s_id);
RUCHIR BISHT 05914802717 7-C-4
END LOOP;
RETURN 'UPDATED';
END;
$$ LANGUAGE PLPGSQL;
select loop_students();
Before modification:
After modification:
Using GUI:
RUCHIR BISHT 05914802717 7-C-4
Before modification:
After modification:
RUCHIR BISHT 05914802717 7-C-4
Experiment-07
RUCHIR BISHT 05914802717 7-C-4
AIM: Write a program to create exceptions if enrollment no. is not issued to student
by the university and raise the exception explicitly by using raise command.
PostgreSQL Exceptions
When an error occurs in a block, PostgreSQL will abort the execution of the block and
also the surrounding transaction.
To recover from the error, you can use the exception clause in the begin...end block.
Syntax:
<<label>>
declare
begin
statements;
exception
when condition [or condition...] then
handle_exception;
[when condition [or condition...] then
handle_exception;]
[when others then
handle_other_exceptions;
]
end;
Write a program to create exceptions if enrollment no. is not issued to student by
the university and raise the exception explicitly by using raise command.
Using CLI:
do
$$
declare
rec record;
s_name text = 'Ruchir';
begin
select enrollno, students.name
RUCHIR BISHT 05914802717 7-C-4
Output:
RUCHIR BISHT 05914802717 7-C-4
Using GUI:
Student Found:
Experiment-08
AIM
a. Write the procedure to get the average marks of students for branch “CSE”.
b. Write a function that accepts branch and returns the total no. of students of the
branch.
PostgreSQL Procedures
A drawback of user-defined functions is that they cannot execute transactions. In other
words, inside a user-defined function, you cannot start a transaction, and commit or
rollback it. PostgreSQL 11 introduced stored procedures that support transactions.
To define a new stored procedure, you use the create procedure statement.
Syntax:
create [or replace] procedure procedure_name(parameter_list)
language plpgsql
as $$
declare
-- variable declaration
begin
-- stored procedure body
end; $$
PostgreSQL Function
The create function statement allows you to define a new user-defined function.
Syntax:
create [or replace] function function_name(param_list)
returns return_type
language plpgsql
as
$$
declare
-- variable declaration
begin
RUCHIR BISHT 05914802717 7-C-4
-- logic
end;
$$
(A) Write the procedure to get the average marks of students for
branch “CSE”.
Using CLI:
CREATE OR REPLACE PROCEDURE public.avg_marks_of_students(IN
student_branch text, INOUT avg_marks real)
LANGUAGE 'plpgsql'
AS $BODY$
begin
select avg(marks)
into avg_marks
from collegestudents
where branch = student_branch;
end;
$BODY$;
RUCHIR BISHT 05914802717 7-C-4
Using GUI:
(B) Write a function that accepts branch and returns the total no. of
students of the branch.
Using CLI:
RUCHIR BISHT 05914802717 7-C-4
Using GUI:
RUCHIR BISHT 05914802717 7-C-4
For CSE:
For ECE:
Experiment-09
RUCHIR BISHT 05914802717 7-C-4
AIM
a. Create a trigger on table after inserting a new student into table.
b. Write a row trigger to insert the existing values of the student table into a new
table when the marks of student are updated.
PostgreSQL Trigger
A trigger is a set of actions that are run automatically when a specified change
operation (SQL INSERT, UPDATE, DELETE or TRUNCATE statement) is
performed on a specified table.
Triggers are useful for tasks such as enforcing business rules, validating input data,
and keeping an audit trail.
Syntax:
CREATE [ CONSTRAINT ] TRIGGER name { BEFORE | AFTER | INSTEAD OF }
{ event [ OR ... ] }
ON table_name
[ FROM referenced_table_name ]
[ NOT DEFERRABLE | [ DEFERRABLE ] { INITIALLY IMMEDIATE |
INITIALLY DEFERRED } ]
[ FOR [ EACH ] { ROW | STATEMENT } ]
[ WHEN ( condition ) ]
EXECUTE PROCEDURE function_name ( arguments )
(A) Create a trigger on table after inserting a new student into table.
Using CLI:
Trigger:
CREATE TRIGGER ins_same_rec
AFTER INSERT
ON studentinfo
FOR EACH ROW
EXECUTE PROCEDURE rec_insert();
Trigger Function:
CREATE OR REPLACE FUNCTION rec_insert()
RETURNS trigger AS
RUCHIR BISHT 05914802717 7-C-4
$$
BEGIN
INSERT INTO student_added
VALUES(NEW.s_id,NEW.s_name);
RETURN NEW;
END;
$$
LANGUAGE 'plpgsql';
Output:
Before Insertion:
After Insertion:
Using GUI:
Before Insertion:
RUCHIR BISHT 05914802717 7-C-4
After Insertion:
(B) Write a row trigger to insert the existing values of the student table into a
new table when the marks of student are updated.
Using CLI:
RUCHIR BISHT 05914802717 7-C-4
Trigger:
CREATE TRIGGER upd_same_rec
AFTER UPDATE
ON student_marks
FOR EACH ROW
EXECUTE PROCEDURE rec_update();
Trigger Function:
CREATE OR REPLACE FUNCTION rec_update()
RETURNS trigger
LANGUAGE 'plpgsql'
BEGIN
INSERT INTO student_modify
VALUES(Old.s_id,Old.name,Old.marks);
RETURN NEW;
END;
Output:
Before Update:
After Update:
RUCHIR BISHT 05914802717 7-C-4
Using GUI:
Before Update:
After Update:
RUCHIR BISHT 05914802717 7-C-4
***********