CS4416 Lectures 1-7
CS4416 Lectures 1-7
Collection of information:
Exists over a long period of time
Stored on secondary storage in a structured way.
Managed by a computer program called Database
Management System (DBMS)
Definition of a Database (2)
3
Database
Application
Database
Database
Application Database
Some Popular DBMSs
5
Oracle
MySQL
IBM DB2
SAP Hana
Microsoft SQL Server
Microsoft Access
PostgreSQL
SQLite
Apache Hbase
MongoDB
Neo4j
DBMS
6
File Systems
Normal forms
Database
Application
Database
Database
Application Database
Database System
3
Database
What is a Data Model?
4
Constraints
Data Models
5
ID Date of birth
Students
Relation name
Schemas
7
Database
SQL: General Information
3
To delete a relation:
DROP TABLE <name>;
SQL (PART 2)
CS4416 Lecture 4
2. SQL as a Data Manipulation Language
2.1. Introduction to the SELECT statement
Select-From-Where Statements
3
optional clause
Example
4
SELECT lastname
FROM Customers
WHERE city = 'Limerick';
Result of Query
5
lastname
SELECT lastname
Jones FROM Customers
Doe WHERE city = 'Limerick';
WHERE clause.
If so, compute the attributes or expressions
SELECT *
FROM Customers
WHERE city = 'Limerick';
Renaming Attributes
9
Using PCs:
SELECT model,
price*1.4 AS price,
'USD' AS currency
FROM PCs;
Result of Query
12
… … …
SELECT model
FROM PCs
WHERE speed >= 2.0 AND
price < 1000.0;
Patterns
15
SELECT *
FROM Sales
WHERE day LIKE '____-12%';
NULL Values
18
AND T F U OR T F U NOT
T T F U T T T T T F
F F F F F T F U F T
U U F U U T U U U U
2-Valued Laws != 3-Valued Laws
21
p OR NOT p = TRUE
Using relations
Customers(customer_id, firstname, lastname, city, address, email)
Sales(customer_id, model, quantity, day, paid, type_of_payment)
find the names and the addresses of all customers from
Limerick who have made a purchase on the 20th of
December 2015.
What if the same customer has bought multiple models on this date?
Joins in SQL:
JOIN, INNER JOIN, CROSS JOIN – equivalent to the comma operator
Outer joins: LEFT JOIN, RIGHT JOIN, FULL JOIN
NATURAL JOIN
Since the attribute we join the two tables on has the same name in both
tables, i.e. customer_id, we can alternatively write:
Q: What will be the difference between the results of the two queries
above?
Explicit Tuple-Variables
12
or alternatively:
SELECT model
FROM Products JOIN
(SELECT model FROM PCs WHERE speed >= 2.0) atleast2
USING(model)
WHERE maker = 'B';
Subqueries in WHERE
18
The lastname
of customer
1111111111
The IN Operator
21
SELECT model
FROM PCs
WHERE hd <= 250 AND
speed >= ALL(SELECT speed
FROM PCs
WHERE hd <= 250);
SQL (PART 4)
CS4416 Lecture 6
2. SQL as a Data Manipulation Language
2.4. Aggregations and Grouping
Aggregate Functions
4
SELECT AVG(speed)
FROM PCs
WHERE hd <= 250;
Example: Illegal Use of Aggregation
6
SELECT model
FROM PCs
WHERE hd <= 250 AND
speed = MAX(speed);
Example: Illegal Use of Aggregation
7
SELECT model
FROM PCs
WHERE hd <= 250 AND
speed =(SELECT MAX(speed)
FROM PCs
WHERE hd <= 250);
Eliminating Duplicates in Aggregations
9
Using Products, PCs and Laptops, find the makers who make at
least one PC model with price above 500 and at least one laptop
model also with price above 500.
SELECT maker
FROM Products NATURAL JOIN PCs
WHERE price > 500
INTERSECT
SELECT maker
FROM Products NATURAL JOIN Laptops
WHERE price > 500;
Resulting tuple:
maker model type
NULL 1014 pc
Inserting Many Tuples
21
DELETE
FROM Products
WHERE type = 'printer' AND
maker = 'A';
Example: Delete all Tuples
25
UPDATE PCs
SET price = 1999.0
WHERE model = '1001';
Example: Update Several Tuples
31
UPDATE PCs
SET price = 1999.0
WHERE price > 1999.0;
3 Views
Views
33
Declare by: