You are on page 1of 37

USHA MITTAL INSTITUTE OF TECHNOLOGY

 SNDT WOMEN’S UNIVERSITY

 MUMBAI – 400049

Name: Shruti Manoj Chavan

Roll No.: 14
1
Subject: Database Management System lab

Branch: Computer Science and Technology

Year: 2021-22
2
INDEX

SR.
PG
NO EXPERIMENT LIST DATE
NO.
.

1 EXPERIMENT:1 07/09/2021 03

2 EXPERIMENT:2 14/09/2021 07

3 EXPERIMENT:3 21/09/2021 10

4 EXPERIMENT:4 28/09/2021 15

5 EXPERIMENT:5 05/10/2021 18

6 EXPERIMENT:6 19/10/2021 20

7 EXPERIMENT:7 26/10/2021 22

8 EXPERIMENT:8 16/11/2021 26

9 EXPERIMENT:9 16/11/2021 30

10 EXPERIMENT:10 23/11/2021 32

11 EXPERIMENT:11
3 30/11/2021 34

12 EXPERIMENT:12 07/12/2021 35
EXPERIMENT 1

AIM:
To study the basics of postgreSQL

DEFINATION:
PostgreSQL is a powerful, open source object-relational database system with over
30 years of active development that has earned it a strong reputation for reliability,
feature robustness, and performance.

PostgreSQL supports both SQL (relational) and JSON (non-relational) querying.


PostgreSQL is a highly stable database backed by more than 20 years of development by
the open-source community. PostgreSQL is used as a primary database for many web
applications as well as mobile and analytics applications.

HISTORY:

The origins of PostgreSQL date back to 1986 as part of the POSTGRES project at


the University of California at Berkeley and have more than 30 years of active
development on the core platform.

By 1996, it became clear that the name “Postgres95” would not stand the test of
time. The company chose a new name, PostgreSQL, to reflect the relationship between
the original POSTGRES and the more recent versions with SQL capability. At the same
time, they set the version numbering to start at 6.0, putting the numbers back into the
sequence originally begun by the Berkeley POSTGRES project.

Many people continue to refer to PostgreSQL as “Postgres” (now rarely in all


capital letters) because of tradition or because it is easier to pronounce. This usage is
widely accepted as a nickname or alias.

The emphasis during development of Postgres95 was on identifying and


understanding existing problems in the server code. With PostgreSQL, the emphasis has
shifted to augmenting features and capabilities, although work continues in all areas.

LANGUAGE SUPPORT: 4

PostgreSQL support most popular programming languages: Python, Java, C#, C/C+,
Ruby, JavaScript (Node.js), Perl, Go, Tcl
KEY FEATURES:
PostgreSQL has many advanced features that other enterprise-class database
management systems offer, such as:

 User-defined types
 Table inheritance
 Sophisticated locking mechanism
 Foreign key referential integrity
 Views, rules, sub-query
 Nested transactions (save-points)
 Multi-version concurrency control (MVCC)
 Asynchronous replication

The recent versions of PostgreSQL support the following features:

 Native Microsoft Windows Server version


 Table spaces
 Point-in-time recovery

PostgreSQL is designed to be extensible. PostgreSQL allows you to define your


own data types, index types, functional languages, etc.

BASIC COMMANDS PSQL:

Command Description Additional Information

psql -d database -U Connects to a database -d: used to state the


user -W under a specific user database name 
-U:used to state the
database user

psql -h host -d Connect to a database -h: used to state the host 


database -U user -W that resides on another -d: used to state the
host database name 
-U:used to state the
database user
5

psql -U user -h host Use SSL mode for the -h: used to state the host 
“dbname=db connection -U:used to state the
sslmode=require” database user
Command Description Additional Information

\c dbname Switch connection to a


new database

\l List available databases

\dt List available tables

\d table_name Describe a table such as


a column, type, modifiers
of columns, etc.

\dn List all schemes of the


currently connected
database

\df List available functions in


the current database

\dv List available views in the


current database

\du List all users and their


assign roles

SELECT version(); Retrieve the current


version of PostgreSQL
server

\g Execute the last


command again

\s Display command history

\s filename Save the command


6

history to a file

\i filename Execute psql commands


Command Description Additional Information

from a file

\? Know all available psql


commands

\h Get help Eg:to get detailed


information on ALTER
TABLE statement use
the \h ALTER TABLE

\e Edit command in your own


editor

\a Switch from aligned to


non-aligned column output

\H Switch the output to


HTML format

\q Exit psql shell

CONCLUSION:

Hence we learned about postgreSQL history, key features and basic commands
used in lab.

7
EXPERIMENT 2

AIM:
To study basic postgreSQL commands (create, database, create table, insert,
drop) and execute the queries.

Query 1:
Create database name employee

Syntax:

CREATE DATABASE <DATABASE NAME>;


\C <DATABASE NAME>

Query 2:
Using database employee create table emp with the attributes e_name, e_city,
e_number, e_ salary, e_address, dept_name.

Syntax:
8

CREATE TABLE <TABLE_NAME>( COL _1 NAME, COL_2 NAME, …);


SELECT *FROM <TABLE NAME>;
Query 3:
Insert values to the table

Syntax:

INSERT INTO <TABLE_NAME>


VALUES(‘INFO_1’, ‘INFO_2’,……);

9
Query 4:
Create another table company with attributes cname,ccity,empno.in database
employee
Syntax:

CREATE TABLE <TABLE_NAME>( COL _1 NAME, COL_2 NAME, …);


SELECT *FROM <TABLE NAME>;

Query 5:
Insert values into the table company

Syntax:

INSERT INTO <TABLE_NAME>


VALUES(‘INFO_1’, ‘INFO_2’,……);

10
EXPERIMENT 3

AIM :
To study, view and modify the structure of the table (select, update, alter, delete)
and execute the following queries using these commands,

Query 1:
Find the name of all employees who live in Delhi.

Syntax:

SELECT <COL 1 NAME> FROM <TABLE> WHERE <COL 2 NAME> =’INFO’;

Query 2:
Increase the salary of all employee by 5000

Syntax:

UPDATE <TABLE NAME>


11 SET <COL NAME> = <COL NAME> + 5000;
Query 3:
Find the company names where the number of employees is greater than 15000

Syntax:

SELECT <COL 1 NAME> WHERE <COL 2 NAME> > 15000;

12

Query 4:
Change the company city to Gurgaon where the company name is ‘TCS’

Syntax:

UPDATE <COL 1 NAME> WHERE <COL 2 NAME> > ‘INFO’;

Query 5:
Add an attribute name ‘designation’ to the table emp

Syntax:

ALTER TABLE <TABLE NAME> ADD <COL NAME>;

13
Query 6:
Modify the table emp, change the data type of salary attribute to float

Syntax:

ALTER TABLE <TABLE NAME> ALTER COLUMN <COL


NAME> TYPE FLOAT;

Query 7:
Drop the attribute deptname from the table emp

Syntax:

ALTER TABLE <TABLE NAME> DROP COLUMN <COL NAME>;

14
Query 8: Delete the entries from the table company where the number of employees
are less than 20000

Syntax:

DELETE FROM <TABLE NAME> WHERE <COL NAME> < 20000;

15
EXPERIMENT 4

AIM :
To study the commands that involve compound conditions (and, or, in, not in,
between, not between, like, not like) and execute the following queries using this
commands.

Query 1:
Find the names of all employees who live in Gurgaon and whose salary is between RS
50000 and RS 70000.

Syntax:

SELECT <COL NAME> FROM <TABLE NAME> WHERE <COL_1> =’INFO>


AND <COL_2> BETWEEN 50000 AND 80000;

Query 2:
Find the names of all employees whose name begin with letter S

Syntax:

SELECT <COLUMN NAME> FROM <TABLE NAME> WHERE


<COLUMN NAME> LIKE ‘S%’;
Query 3:
Find the company names where the company city is Delhi and the number of
employees is not between 10000 and 20000.

Syntax:

SELECT <COL_1> FROM <TABLE NAME> WHERE <COL_2>=’INFO’ AND


<COL_3> NOT BETWEEN 10000 AND 20000;
Query 4: Find the names of all companies that do not end with letter ‘A’.

Syntax:

SELECT <COL_1> FROM <TABLE NAME> WHERE <COL_1>


NOT LIKE ‘%A’;
EXPERIMENT 5

AIM:
To study the aggregate function (sum, count, max, mean, average) and execute the
following queries using this command.

Query 1:
Find the sum and average of salaries of all employees in finance department.

Syntax:

SELECT SUM(<COL_1>), AVG(<COL_1) FROM <TABLE NAME> WHERE


<COL_2>=’INFO’;

Query 2:
Find the number of all employees who live in Delhi.

Syntax:

SELECT SUM(C0L_1> FROM <TABLE NAME> WHERE <COL_2>=’INFO’;


Query 3:
Find the maximum and minimum salary in HR department.

Syntax:

SELECT MIN(<COL_1>),MAX(<COL_1>) FROM <TABLE NAME>


WHERE <COL_2>=’INFO’;
EXPERIMENT 6

AIM :
To study the grouping commands (group by, order by) and execute the following
queries using this commands.

Query 1:
List all the employee names in descending order.

Syntax:

SELECT *FROM<TABLE NAME> ORDER <COL_1> DESC;

Query 2:
Find the number of employees in each department where number of employee is
greater than 1.

Syntax:

SELECT <COL_1>, COUNT(*) FROM <TABLE NAME> GROUP BY


<COL_1> HAVING COUNT(*)>1;
Query 3:
List all the department names where average salary of department is Rs 75000.

Syntax:

SELECT <COL_1>, AVG(<COL_2>) FROM <TABLE NAME> GROUP BY


<COL_1> HAVING AVG(<COL_2>) >71000;
EXPERIMENT 7

AIM:
To study the command involving data constraints and execute the following queries
using this commands

Queries 1:
Alter the table ‘emp’ and make ‘enumber’ as the primary key.

Syntax:

ALTER TABLE <TABLE NAME> ADD PRIMARY KEY(<COL_1>);

Queries 2:
Alter a table company and add the foreign key constraint.

Syntax:

ALTER TABLE <TABLE_2> ADD FOREIGN KEY(<COL_1>)


REFERENCES <TABLE2>(<COL_1>);
Queries 3:
Add a check constraint in the table emp such that salary has the value between RS
0 and RS 100000.

Syntax:

ALTER TABLE <TABLE NAME> ADD CHECK(<COL_1> BETWEEN 0


AND 86000;
Queries 4:
Alter a table company and add unique constraint to column name

Syntax:

ALTER TABLE <TABLE NAME> ADD CONSTRAINT


<TABLENAME>_UNIQUE UNIQUE (COL_1);

(P.T.O)
Queries 5:
Add a default constraint to column emp_no of a table company with value ‘25000’.

Syntax:

ALTER TABLE <TABLE NAME> ALTER <COL_1> SET DEFAULT ‘25000’;


EXPERIMENT 8

AIM:
To study the commands for aliasing, joins(cross join, inner join, outer join,) and
renaming the various set operations, execute the following queries using this commands.

Queries 1:
Rename the name of database to employee 1.

Syntax:

ALTER DATABASE <DB NAME> RENAME TO <NEW DB NAME>;

Queries 2:
Rename the name of table of emp to emp1

Syntax:

ALTER TABLE <TABLE NAME> RENAME TO <NEW TABLE NAME>;


Queries 3:
Change the name of the attribute ename to eemp name

Syntax:

ALTER TABLE <TABLE NAME> RENAME <COL> TO <NEW COL>;

Queries 4:
Retrieve the complete record of an employee and it’s company from both the
tables using joins.

Syntax:

SELECT <TB_1 NME>.<COL1>,<TB_1 NME>.<COL2>,<TB_2 NME>.<COL1>,…


FROM <TB_1 NME>
INNER JOIN <TB_2 NME>
ON <TB_1 NME>.<COL2> = <TB_2 NME>.<COL1>;
Queries 5:
List all the employees working in the company TCS.

Syntax:

SELECT <COL1> FROM <TB_1 NME>


INNER JOIN <TB_2 NME>
ON <TB_1 NME>.<COL2> = <TB_2 NME>.<COL1> AND <TB_2
NME>.<COL1>=’INFO’;

Queries 6:
List the e_number of all employees whose residence is in Kalyan and whose
company is in Delhi or if both the conditions are true.

Syntax:

SELECT <COL1> FROM <TB_1 NME>


INNER JOIN <TB_2 NME>
ON <TB_1 NME>.<COL2> = <TB_2 NME>.<COL1> AND <TB_1
NME>.<COL4>=’INFO’ AND <TB_2 NME>.<COL1>=’INFO’;
Queries 7:
list the enumber of all employees whose residence is in Kalyan but whose company
is not Delhi.

Syntax:

SELECT <COL1> FROM <TB_1 NME>


INNER JOIN <TB_2 NME>
ON <TB_1 NME>.<COL2> = <TB_2 NME>.<COL1> AND <TB_1
NME>.<COL4>=’INFO’ AND <TB_2 NME>.<COL1>!=’INFO’;
EXPERIMENT 9

AIM:
To study the commands for views and execute the following queries using this
command.

Queries 1:
Create a view having ename and ecity

Syntax:

CREATE VIEW EMP_DETAILS AS SELECT EMP_NAME, EMP_CITY


FROM EMP_1;

Queries 2:
In the above view change the ecity to Delhi where ename is John.

Syntax:

UPDATE <CV NAME> SET <COL2>=’INFO’ WHERE <COL1>=’INFO’;


Queries 3:
Create a view having attributes from both the tables.

Syntax:

CREATE VIEW <CV NME> T1.<COL1>,…, T2.<COL1>,…


FROM <TABLE1> AS T1 AND <TABLE 2> AS T2
WHERE T1.COL7=T2.COL1;
EXPERIMENT 10

AIM:
To study the commands involving indexes and execute the following queries

Queries 1:
Create an index with attribute e_name on the table employee

Syntax:

CREATE INDEX <INDEX NME> ON <TABLE>(<COL1>);


SELECT INDEXNAME FROM PG_INDEXES WHERE TABLE=’<TABLE>’;

Queries 2:
Create a composite index with attributes c_name and c_city on table company.

Syntax:

CREATE INDEX <INDEX NME> ON <TABLE>(<COL1>,COL2>);


Queries 3:
Drop all indexes created on table company.4
Syntax:

DROP INDEX <INDEX NME>;


EXPERIMENT 11

AIM :
Draw an ER diagram by using dia(software) tools with the relational schemas, Find
out the problem statement, Identify the entities.
EXPERIMENT 12

AIM : Write a program to implement trigger in postgreSQL

You might also like