lab1.SQL
lab1.SQL
Database Management
Indications for Postgres installation are in the Moodle. With Postgres, you can use the psql
client in order to interact with the DBMS. To run the client nd the related icon in the
Postgres directory and launch it by double clicking on the icon.
The client will allow for interactions via a prompt (e.g., postgres=# ) allowing you for writing
and executing SQL commands. Observe that there is a ‘by-default’ database called
postgres to which you are initially connected.
Here is a list of useful psql commands, including \c to change the current database, after
you have created it with CREATE DATABASE dbname; .
• \q to quit pgsql;
• \l to obtain the list of available databases;
• \c to connect to a particular database;
• \d to have the list of tables in the current database;
• \d to describe a table (the table name must provided as the argument of the command,
e.g. \d egg for describing the egg table);
• \i le-adresss, to execute SQL commands stored in a separate le.
fi
fi
fi
needed to make the expression meaningful:
(1) R1 ∪R2,
MSC Data Science (2) R1 ∩R2, (3) R1 − R2, (4) R1 × R2, (5) σ
for Business a=5 (R1),2023
Jannuary (6) πa (R1),
and (7) R1/R2
Answer 4.2 See Figure 4.1.
Exercice 1. SQL
ConsiderExercise Consider
4.3relational
the following the following schema:
schema.
1. 42part.
Find the names of suppliers who supply some red
2. Find the names of suppliers who supply some red or green part.
3. Find the sids of suppliers who supply some red part or are at Illinois.
4. Find the sids of suppliers who supply some red part and some green part.
8. Find the sids of suppliers who supply every red or green part.
9. Find the sids of suppliers who supply every red part or supply every green part.
10. Find pairs of sids such that the supplier with the rst sid charges more for some part
than the supplier with the second sid.
11. Find the pids of parts supplied by at least two different suppliers.
12. Find the pids of the most expensive parts supplied by suppliers named Alien Aircaft
Inc.
13. Find for each supplier the most expensive part he/she supplies
fi
fi
fi
fi
MSC Data Science for Business Jannuary 2023
To create and ll the database please use the following commands in the next page.
\c tp;
And then execute the following commands one after the other :
create table suppliers(sid int primary key, sname text, address text);
create table parts(pid int primary key, pname text, color text);
create table catalog(sid int, pid int, cost real,
constraint pk_cat primary key(sid, pid),
constraint fk_sup foreign key(sid) references suppliers,
constraint fk_par foreign key(pid) references parts
);
fi
fi
MSC Data Science for Business Jannuary 2023
INSERT INTO parts (pid, pname, color) VALUES
(1,'Left Handed Bacon Stretcher Cover','Red'),
(2,'Smoke Shifter End','Black'),
(3,'Acme Widget Washer','Red'),
(4,'Acme Widget Washer','Silver'),
(5,'I Brake for Crop Circles Sticker','Translucent'),
(6,'Anti-Gravity Turbine Generator', 'Cyan'),
(7,'Anti-Gravity Turbine Generator',' Magenta'),
(8,'Fire Hydrant Cap','Red'),
(9,'7 Segment Display','Green');
sname
-----------------------
Perfunctory Parts
(3 rows)
2. Find the sids of suppliers who supply some red or green part.
sname
-----------------------
Perfunctory Parts
(3 rows)
3.Find the sids of suppliers who supply some red part or are at Illinois.
sid
-----
(3 rows)
MSC Data Science for Business Jannuary 2023
4.Find the sids of suppliers who supply some red part and some green part.
sid
-----
(1 row)
sid
-----
(1 row)
sid
-----
(0 rows)
sid
-----
(1 row)
MSC Data Science for Business Jannuary 2023
8. Find the sids of suppliers who supply every red or green part.
sid
-----
(0 rows)
9.Find the sids of suppliers who supply every red part or supply every green part.
sid
-----
(2 rows)
10. Find pairs of sids such that the supplier with the rst sid charges more for some part
than the supplier with the second sid.
sid_exp | sid_cheap
---------+-----------
2| 1
1| 2
3| 1
3| 2
(4 rows)
fi
MSC Data Science for Business Jannuary 2023
11. Find the pids of parts supplied by at least two different suppliers.
pid
-----
(2 rows)
12. Find the pids of the most expensive parts supplied by suppliers named Alien Aircaft
Inc.
pid
-----
(2 rows)
13. Find for each supplier the most expensive part he/she supplies.
-----------------------+-----+-------------
(5 rows)