0% found this document useful (0 votes)
27 views1 page

PostgresSQL ER Diagram and Queries

BCA slip 16

Uploaded by

sanchet237
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

PostgresSQL ER Diagram and Queries

BCA slip 16

Uploaded by

sanchet237
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q1) Practical Questions on PostgresSQL

STUDENT (sreg_no, name , class)


COMPETITION (c_no , name , C_type)
The relationship is as follows:
STUDENT-COMPETITION: M-M with described attributes rank and year. Class
should be ‘FYBCA’ , ‘SYBCA’ and ‘TYBCA’
Assume appropriate data types for all the attributes.
a) Draw the ER diagram for above relational schema and normalize it in 3NF.

b) Create the above database in 3NF form in PostgresSQL using constraints.

CREATE TABLE Student (sreg_no INT PRIMARY KEY, name VARCHAR(50) NOT NULL, class VARCHAR(10)
CHECK (class IN ('FYBCA', 'SYBCA', 'TYBCA')));

CREATE TABLE Competition (c_no INT PRIMARY KEY, name VARCHAR(50) NOT NULL, C_type
VARCHAR(50) NOT NULL);

CREATE TABLE Participates_In (sreg_no INT NOT NULL, c_no INT NOT NULL, rank INT, year INT, PRIMARY
KEY (sreg_no, c_no), FOREIGN KEY (sreg_no) REFERENCES Student(sreg_no), FOREIGN KEY (c_no)
REFERENCES Competition(c_no));

INSERT INTO Student (sreg_no, name, class) VALUES (1001, 'Alice Smith', 'FYBCA'), (1002, 'Bob Brown',
'SYBCA'), (1003, 'Charlie Chen', 'TYBCA'), (1004, 'David Davis', 'FYBCA'), (1005, 'Emily Evans', 'SYBCA');

INSERT INTO Competition (c_no, name, C_type) VALUES (2001, 'Essay Competition', 'Writing'), (2002, 'Debate
Competition', 'Public Speaking'), (2003, 'Poster Competition', 'Visual Arts'), (2004, 'Quiz Competition', 'Knowledge');

INSERT INTO Participates_In (sreg_no, c_no, rank, year) VALUES (1001, 2001, 1, 2023), (1001, 2003, 2, 2023),
(1002, 2002, 1, 2022), (1003, 2004, 3, 2022), (1004, 2002, 2, 2023), (1004, 2004, 1, 2023), (1005, 2001, 3, 2023);

Q2) Using above database, solve the following queries:


a) List the names of all students studying in FYBCA.
SELECT name FROM Student WHERE class = 'FYBCA';

b) Find the count of students participated in Poster competition class wise.


SELECT class, COUNT(DISTINCT s.sreg_no) AS Participated_In FROM Student s INNER JOIN
Participates_In p ON s.sreg_no = p.sreg_no INNER JOIN Competition c ON p.c_no = c.c_no WHERE c.C_type
= 'Poster' GROUP BY class;
c) List the names of students scoring 1st rank in all different competition.
SELECT [Link] FROM Student S INNER JOIN Participates_In PI ON S.sreg_no = PI.sreg_no WHERE [Link]
= 1 GROUP BY [Link] HAVING COUNT(DISTINCT PI.c_no) = (SELECT COUNT(DISTINCT c_no) FROM
Competition);
d) Delete all students of class FYBCA participated in Quiz of competition in year 2018.
DELETE FROM Student S USING Participates_In PI, Competition C WHERE S.sreg_no = PI.sreg_no AND
PI.c_no = C.c_no AND [Link] = 'FYBCA' AND C.C_type = 'Quiz' AND [Link] = 2018;

You might also like