Files
G4G0-1/Semester 1/Database Systems/Week 3/Week 3 Database Systems.md
2024-01-15 20:14:10 +00:00

5.5 KiB

Lecture 1 (13:00) - More SQL

Attribute Alias

SELECT driverID AS DriverNumber
FROM Drivers;

Distinct

SELECT address FROM Drivers WHERE DriverID IN (D020, D030)

Output:

address
Dundee
Dundee
SELECT DISTINCT address FROM Drivers WHERE DriverID IN (D020, D030)

Output:

address
Dundee

Aggregate Functions

  • sum() - Adds all inputs
  • avg() - Mean average of all inputs
  • count() - Count total records of input
  • max() - Get maximum value of inputs
  • min() - Get minimum value of inputs

Group By

SELECT address, COUNT(niNo)
FROM Employees
GROUP BY address;

Output:

Address count(niNo)
Aberdeen 2
Dundee 2
Stirling 1

Having Clause

  • Used with group by to filter groups / rows
SELECT <attributes>, <column functions>
FROM <tables>
WHERE <conditions> //Optional
GROUP BY <attributes>
HAVING <conditions>;

SELECT address, AVG(salary)
FROM Employees
GROUP BY address
HAVING AVG(salary) > 21000;

Output:

address avg(salary)
Dundee 22500

Having Vs. Where

  • WHERE removes rows before grouping
  • HAVING filters groups
  • Aggregate functions cannot be used with the WHERE clause.

Update Queries

  • Allows you to change the values of attributes of existing rows ( tuples ) of a table
UPDATE <table>
SET <attribute assignments>
WHERE <conditions>; //optional
UPDATE Drivers
SET points = 6
WHERE driverID = "D040";

Output:

UPDATE Drivers
SET address = "Salford"
WHERE driverID = "D020";

Delete Queries

  • Deletes whole rows ( tuples ), not individual values
DELETE FROM <table name>
WHERE <conditions>; //optional
DELETE FROM Drivers
WHERE points > 10;

Output

Insert Queries

  • Adds data to existing table without deleting it or any records
INSERT INTO <table name> <attribute list> //optional
SELECT <statement> | VALUES <value list>;
INSERT INTO Drivers
VALUES ("D060", "Betty", "Inverness", 6);

Output

INSERT INTO Drivers (driverID, name)
VALUES ("D070", "Jeannie")

Output

Lecture 2 (15:00) - More SQL

Data Types

CHARACTER(L)
  • Fixed length character string containing L letters. Fewer characters are used as padding.
CHARACTER VARYING(L)
  • Variable length character string that holds up to L characters. No padding.
INTEGER(L)
  • Signed whole number.
NUMERIC(P,S)
  • P = Precision. Total number of digits
  • S = Scale. Number of digits to correct decimal place.
  • Ex. NUMERIC(5,2) is -999.99 to 999.99
DATE YYYY-MM-DD
  • YYYY = 1 to 9999
  • MM = 1 to 12
  • DD = 1 to `31
TIME HH:MM:SS
  • HH = Hours
  • MM = Minutes
  • SS = Seconds
TIMESTAMP YYYY-MM-DD HH:MM:SS
  • Combination of DATE and TIME
INTERVAL
  • Refers to period of time. May have value such as 90 days. Also known as a span
BINARY LARGE OBJECT(L)
  • Large, variable length binary string that can hold up to L bytes. Also known as BLOB
BOOLEAN
  • Value of TRUE or FALSE
NULL
  • Value of unknown, valid for any data type.

Exercise

  1. CHAR
  2. DATE
  3. BOOLEAN
  4. INTEGER
  5. NUMERIC
  6. CHAR(10) = Bryant____
  7. VARCHAR(10) = Bryant
  8. INTEGER can have the value NULL

Exercise on Data Types and Keys

  1. Book
    1. ISBN - VARCHAR
    2. Title - VARCHAR
    3. Author - VARCHAR
  2. Members
    1. MemID - CHAR
    2. Name - VARCHAR
    3. Age - INTEGER
    4. Address - VARCHAR
    5. Phone - VARCHAR
  3. Loan
    1. LoanID - CHAR
    2. ISBN ( Foreign Key )
    3. MemID ( Foreign Key )
    4. LoanDate - DATE
    5. ReturnDate - DATE

Exercise on Adding, Deleting and Modifying Data

  1. Append tuple to pirates table
INSERT INTO Pirates
VALUES ("p5", "Pegleg", 1, 2, "Spain");
INSERT INTO Boats (boat-code, boat-name, origin)
VALUES ("b9", "Swift", "British");
INSERT INTO Booty
VALUES ("bt13", "Flour", 8);
  1. Change the name of Booty "bt8" from "nothing" to "infected animals"
UPDATE Booty
SET booty-name = "Infected Animals"
WHERE booty-code = "bt8";
  1. Remove b7 from the boats table
DELETE FROM Boats
WHERE boat-code = "b7";

Exercise on Aggregating Results

Exercise

  1. Calculate total value of all booty in booty table
SELECT SUM(value)
FROM booty;
  1. List each type of boat, with the number of boats with that type
SELECT boat-type, COUNT (boat-code)
FROM boats;
  • Remember comma between select and aggregate functions

Workshop 1 (10:00)

  1. List the unit price of the cheapest product.
  2. What is the total value of all the products?
  3. How many products have been discontinued?
  4. What is the average unit price of the products which have been discontinued?
  5. Display the name of every supplier and the total number of products supplied by that company.
  6. Display total number of orders for each employee.