❖ Week 03 Monday |
❖ Context (Reminder) |
Before typing commands, think about the context:
Context | ||
$ % | in Linux shell
run Unix commands, e.g. ls cd | |
db=# | in psql run SQL commands, e.g. select update | |
db> | in sqlite3 run SQL commands, e.g. select update | |
in vim but try :q ZZ |
❖ Assignment 1 |
Beer Database, with info on
Due: 11:59pm Friday 13 October (end week 5)
Supplied: ass1.sql
ass1.dump
Submit: ass1.sql
❖ Exercise: Exploring the Assignment 1 Database |
Explore the ass1
psql
❖ Modifying a database |
SQL commands for changing data in a database
insert into Table values ( Val1, Val2, ... )
delete from Table where Condition
update Table set Field = Value where Condition
create table Table ( Attributes/Constraints )
drop table Table
copy Table from ... -- PostgreSQL specific
PostgreSQL commands for manipulating databases
dropdb DatabaseName createdb DatabaseName psql DatabaseName -f DumpFile pg_dump DatabaseName > NewDumpFile
❖ Modifying a database (cont) |
Examples of making changes to a database:
-- Student(id,name,degree), Course(id,title,term) -- Enrolment(stuID,crsID,grade,mark) insert into Student(id,name,degree) values (5654321,'John',3778); insert into Student(name,degree) values ('John',3778) returning id; insert into Student(id,name,degree) values (default,'John',3778); insert into Course values (987, 'COMP3311', '23T1); insert into Enrolment(stuID,crsID) values(5654321, 987); -- update mark for one student update Enrolment set mark=75, grade='DN' where stuID = 5654321; -- adjust marks for all students in the course update Enrolment set mark=mark*1.1 where crsID = 987; -- a student drops the course delete from Enrolment where stuID = 5554321 and crsID = 987;
❖ Another Example Database: Beers/Bars/Drinkers |
Consider the following ER model:
❖ Exercise: Delete/Update Examples |
Carry out the following actions on the beer database:
❖ SQL Query Language |
Basic querying mechanism in SQL:
SELECT expressions FROM tables WHERE condition ORDER BY attributes
expressions
tables
JOIN
condition
❖ SQL Queries |
Functionality provided by SQL ...
Filtering: extract attributes from tuples, extract tuples from tables
SELECT b,c FROM R(a,b,c,d) WHERE a > 5
Combining: merging related tuples from different tables
... FROM R(x,y,z) JOIN S(a,b,c) ON R.y = S.a
Summarising: aggregating values in a single column
SELECT max(mark) FROM Enrolments ...
Set operations: union, intersection, difference
❖ SQL Queries (cont) |
More functionality provided by SQL ...
Grouping: forming subsets of tuples sharing some property
... GROUP BY R.a
(forms groups of tuples from R
a
Group Filtering: selecting only groups satisfying a condition
... GROUP BY R.a HAVING max(R.b) < 75
Renaming: assign a name to a component of a query
SELECT a as name FROM Employee(a,b,c) e WHERE e.b > 50000
❖ Semantics of SELECT..WHERE |
The query
SELECT * FROM R WHERE Condition
can be treated as
Results = {} foreach tuple T in R { if T satisfies Condition add T to Results }
❖ Semantics of JOIN |
The query
SELECT * FROM R JOIN S ON R.x = S.y WHERE Condition
can be treated as
Results = {}
foreach tuple TR in R {
foreach tuple TS in S {
if TR.x == TS.y {
Tjoined = TR+TS # combined
if Tjoined satisfies Condition
add Tjoined to Results
}
}
}
❖ Exercise: Queries on Simple Beer DB |
Answer these queries on the Beer database:
Produced: 25 Sep 2023