0% found this document useful (0 votes)
9 views148 pages

CS4416 Lectures 1-7

This document provides an introduction to database systems, defining a database as a structured collection of information managed by a Database Management System (DBMS). It outlines the evolution of DBMS, from early file systems to modern relational and NoSQL databases, and discusses the relational data model, including schemas and relational algebra. Additionally, it introduces SQL as a high-level query language for database management and includes examples of database schemas and data types.

Uploaded by

miriamoamohammed
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)
9 views148 pages

CS4416 Lectures 1-7

This document provides an introduction to database systems, defining a database as a structured collection of information managed by a Database Management System (DBMS). It outlines the evolution of DBMS, from early file systems to modern relational and NoSQL databases, and discusses the relational data model, including schemas and relational algebra. Additionally, it introduces SQL as a high-level query language for database management and includes examples of database schemas and data types.

Uploaded by

miriamoamohammed
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
You are on page 1/ 148

LECTURE 1

CS4416 Introduction to Database Systems


Definition of a Database (1)
2

Ullman and Widom:

 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

Connolly and Begg:

 A shared collection of logically related data and


its description, designed to meet the
informational needs of an organization.
 Minimum duplication
 Self-describing nature of a database (data + metadata)
 Data abstraction
 Information takes the form of entities, attributes and
relationships.
Database System
4

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

 A DBMS is expected to:


 Allow users to define the logical structure of a database
through a data-definition language (DDL).
 Give users the ability to insert, update, delete, and
retrieve data from the database through a data-
manipulation language (DML).
 Support the storage of very large amounts of data over a
long period of time, allowing efficient access to the data
for queries and database modifications.
 Enable durability, the recovery of database in the face of
failures, errors of many kinds, or intentional misuse.
 Control shared access to data from many users at once.
DBMS Evolution
7

 File Systems

 First important applications of DBMSs:


 Banking systems,
 Airline reservation systems,

 Corporate record keeping:


 Employment and tax records,
 Inventories,
 Sales records, etc.
DBMS Evolution (contd.)
8

 In early DBMSs (1960s) required the programmer to


visualize data as it is stored:
 Data structures were directly modelled after those created
for COBOL:
 Hierarchical data model
 Network data model

 Relational database systems


 Ted Codd, 1970: Database systems should present the user
with a view of data organised as tables called relations
 Queries could be expressed in a very high-level language
 Norm by 1990
DBMS Evolution (contd.)
9

 Object (Object-oriented) database systems and data


warehousing systems appeared in the 1990s.

 Web-database integration since mid 1990s.

 XML 1.0 ratified by W3C in 1998. Leads to native XML


databases. XML gets integrated with relational DBMSs.

 Current trends: Non-relational, distributed, open-source and


horizontally scalable databases – NoSQL movement as a
response to challenges of the so-called big data explosion.
(see https://hostingdata.co.uk/nosql-database/)
Module Outline
10

 The relational data model


 SQL: data definition, data manipulation, views
 Design theory of relational databases

 Normal forms

 SQL: constraints, triggers, stored procedures

 SQL/host language interface: JDBC, PHP & MySQL

 Database Transaction Management


 Intro to NoSQL Databases
 Labs: MySQL
LECTURE 2
CS4416 The Relational Data Model
Database System
2

Database
Application
Database

Database
Application Database
Database System
3

Conceptual Physical Database


Data DBMS Data
Database Model Model
Application

Database
What is a Data Model?
4

Notation for describing data or information:

 Structure of the data: conceptual data model

 Operations on data: queries, modifications

 Constraints
Data Models
5

 Conceptual data models  Physical data models


◼ Entity-relationship  Unifying model
◼ Relational  Frame model
◼ Hierarchical
◼ Network
◼ Graph
◼ Key-value
◼ Object-based
◼ Document-based:
◼ XML
◼ JSON
Relational Data Model
6

 Based on the mathematical concept of a relation


 Both entities and relationships are represented as
tables
 A relation is a table:
Attributes (column headers)

ID Date of birth

Tuples 100 1990-01-20

(rows) 101 1980-12-12

Students
Relation name
Schemas
7

 Relation schema = relation name and attribute list.


 Optionally: types of attributes.
 Example:
◼ students(ID, Date of birth)
◼ students(ID : string, Date of birth : date)

 Database schema = set of all relation schemas in


the database.
 Database = collection of relations.
Why Relations?
8

 Very simple model.


 Often matches how we think about data.
 Conceptual data model that underlies SQL, the
most popular database language today.
 Relations (plus the operations with relations) form
an algebra – the relational algebra which has been
very well studied by database theorists.
What is an “Algebra”
9

 Mathematical system consisting of:


 Operands - variables or values from which new values
can be constructed.
 Operators - symbols denoting procedures that
construct new values from given values.
What is Relational Algebra?
10

 An algebra whose operands are relations or


variables that represent relations.
 Operators are designed to do the most common
things that we need to do with relations in a
database.
 The result is an algebra that can be used as a query
language for relations.
Core Relational Algebra Operators
11

 Union, intersection, and difference.


 Usual set operations, but both operands must have
the same relation schema.
 Selection: picking certain rows.
 Projection: picking certain columns.
 Product: Cartesian product of two relations
(considered as sets of tuples).
 Renaming of relations and attributes.
SQL (PART 1)
CS4416 Lecture 3
Relational Database System
2

Conceptual Physical Database


Data DBMS Data
Database Model Model
Application

Database
SQL: General Information
3

 SQL is a very-high-level language.


 Say “what to do” rather than “how to do it.”
 Avoid a lot of data-manipulation details needed in
procedural languages like C++ or Java.
 Database management system figures out “best” way
to execute query (called query optimization).

 Latest version of the standard: SQL:2023 or ISO/IEC


9075-16:2023
https://www.iso.org/standard/79473.html
Our Running Example
4

 All our SQL queries will be based on the following


database schema.
 Underline indicates primary key attributes.

Products(maker, model, type)


PCs(model, speed, ram, hd, price)
Laptops(model, speed, ram, hd, screen, price)
Printers(model, color, type, price)

Customers(customer_id, firstname, lastname, city, address, email)


Sales(customer_id, model, quantity, day, paid, type_of_payment)
Example Database
5

Products PCs Customers


maker model type model speed ram hd price customer_id firstname lastname city address email
A 1001 pc 1001 2.66 1024 250 2114.00 9999999999 Norah Jones Limerick 2 Thomas St. [email protected]
A 1002 pc 1002 2.10 512 250 995.00 1234567890 Paul Murphy Cork 20 O'Connell St. NULL
A 1003 pc 1003 1.42 512 80 478.00 1122334455 Ann O'Brien Dublin 1 Jervis St. [email protected]
A 2004 laptop 1004 2.80 1024 250 649.00 1231231231 John Doe Limerick NULL NULL
A 2005 laptop 1005 3.20 512 250 630.00 9876543210 Jack Murphy Galway 101 O'Connell St. [email protected]
A 2006 laptop 1006 3.20 1024 320 1049.00
B 1004 pc 1007 2.20 1024 200 510.00 Sales
B 1005 pc 1008 2.20 2048 250 770.00 customer_id model quantity day paid type_of_payment
B 1006 pc 1009 2.00 1024 250 650.00 9999999999 1007 1 2013-12-20 459.00 visa credit
B 2007 laptop 1010 2.80 2048 300 770.00 1234567890 1001 1 2013-12-20 1902.60 mastercard credit
C 1007 pc 1011 1.86 2048 160 959.00 9999999999 3007 2 2013-12-20 360.00 visa credit
D 1008 pc 1012 2.80 1024 160 649.00 9876543210 1007 3 2013-12-19 1530.00 visa debit
D 1009 pc 1013 3.06 512 80 529.00 1122334455 2010 1 2013-12-19 2300.00 mastercard credit
D 1010 pc 1231231231 2002 2 2013-12-19 1898.00 visa credit
D 3004 printer Printers 1122334455 3001 1 2013-12-18 99.00 cash
D 3005 printer model color type price 1231231231 3002 1 2013-12-18 239.00 cash
E 1011 pc 3001 TRUE ink-jet 99.00 9876543210 1007 1 2013-12-17 510.00 visa debit
E 1012 pc 3002 FALSE laser 239.00 9876543210 2002 1 2013-12-17 949.00 visa debit
E 1013 pc 3003 TRUE laser 899.00
E 2001 laptop 3004 TRUE ink-jet 120.00 Laptops
E 2002 laptop 3005 FALSE laser 120.00 model speed ram hd screen price
E 2003 laptop 3006 TRUE ink-jet 100.00 2001 2.00 2048 240 20.1 3673.00
E 3001 printer 3007 TRUE laser 200.00 2002 1.73 1024 80 17.0 949.00
E 3002 printer 2003 1.80 512 60 15.4 549.00
E 3003 printer Attributes in red and also underlined are [part of] 2004 2.00 512 60 13.3 1150.00
F 2008 laptop the primary key in each table. The attribute email 2005 2.16 1024 120 17.0 2500.00
F 2009 laptop in table Customers is unique. We assume that 2006 2.00 2048 80 15.4 1700.00
G 2010 laptop each customer has a unique email address. We 2007 1.83 1024 120 13.3 1429.00
H 3006 printer also assume that for each model X purchased by 2008 1.60 1024 100 15.4 900.00
H 3007 printer customer Y there is one entry per day in table 2009 1.60 512 80 14.1 680.00
Sales. 2010 2.00 2048 160 15.4 2300.00
1. SQL as a Data Definition Language
SQL as a Data-Definition Language
7

 SQL is primarily a query language, for


getting information from a database.
 But SQL also includes a data-definition
component for describing database
schemas.
Schemas
8

 Relation schema = relation name and attribute list.


 Optionally: types of attributes.
 Database schema = set of all relation schemas in
the database.
 Database = collection of relations.
Creating (Declaring) a Relation
9

 Simplest form is:


CREATE TABLE <name> (
<list of elements>
);

 Most basic element: the name of an attribute and its type.

 SQL names/identifiers must be


 no longer than 128 characters
 must start with a letter
 cannot contain spaces
ISO SQL Data Types
10

 SQL data types:


 Boolean: BOOLEAN
 Character:
◼ CHAR(n ) = fixed-length string of n characters.
◼ VARCHAR(n ) = variable-length string of up to n characters.
 Numeric: NUMERIC, DECIMAL, INTEGER, SMALLINT, FLOAT, REAL,
DOUBLE PRECISION
 Datetime: DATE, TIME, TIMESTAMP
 Standard data types:
http://www.dummies.com/how-to/content/sql-data-
types0.html
 Data types in various DBMSs:
http://www.w3schools.com/sql/sql_datatypes.asp
Example: Create Table
11

CREATE TABLE Products (


maker CHAR(1),
model CHAR(4),
type VARCHAR(8)
);
Example: Create Table (contd.)
12

CREATE TABLE Customers (


customer_id CHAR(10),
firstname VARCHAR(32),
lastname VARCHAR(32),
city VARCHAR(32),
address VARCHAR(128),
email VARCHAR(128)
);
Example: Create Table (contd.)
13

CREATE TABLE Sales (


customer_id CHAR(10),
model CHAR(4),
quantity INTEGER,
day DATE,
paid REAL,
type_of_payment VARCHAR(32)
);
SQL Values
14

 Integers and reals (i.e. floating point numbers) are


represented as you would expect.
 Strings are too, except they require single quotes.
 Two single quotes = real quote, e.g., 'O''Brien'.
 Any value can be NULL
SQL Values (contd.)
15

 Dates and Times


 DATE
◼ form of a date value yyyy-mm-dd
 TIME
◼ form of a time value hh:mm:ss
 TIMESTAMP
◼ Date and time together yyyy-mm-dd hh:mm:ss
Declaring Keys
16

 An attribute or list of attributes may be


declared PRIMARY KEY or UNIQUE.
 Either says that no two tuples (i.e. data
rows) of the relation may have the same
values for the attribute(s) on the list.
 There are a few distinctions to be
mentioned later.
Declaring Single-Attribute Keys
17

 Place PRIMARY KEY or UNIQUE after the type in


the declaration of the attribute.
 Example:

CREATE TABLE Products (


maker CHAR(1),
model CHAR(4) PRIMARY KEY,
type VARCHAR(8)
);
Declaring Single-Attribute Keys (2)
18

 Example of two alternative keys:

CREATE TABLE Customers (


customer_id CHAR(10) PRIMARY KEY,
firstname VARCHAR(32),
lastname VARCHAR(32),
city VARCHAR(32),
address VARCHAR(128),
email VARCHAR(128) UNIQUE
);
Declaring Multiattribute Keys
19

 A key declaration can also be another


element in the list of elements of a
CREATE TABLE statement.
 This form is essential if the key consists
of more than one attribute.
 May be used even for one-attribute keys.
Example: Multiattribute Key
20

 We assume that for each model X purchased by customer Y there is


one entry per day in table Sales. Thus:

CREATE TABLE Sales (


customer_id CHAR(10),
model CHAR(4),
quantity INTEGER,
day DATE,
paid REAL,
type_of_payment VARCHAR(32),
PRIMARY KEY(customer_id, model, day)
);
PRIMARY KEY vs. UNIQUE
21

1. There can be only one PRIMARY KEY for


a relation, but several UNIQUE
attributes.
2. No attribute of a PRIMARY KEY can ever
be NULL in any tuple. But attributes
declared UNIQUE may have NULL
values, and there may be several tuples
with NULL.
Altering and Deleting Relations
22

 To alter the schema of a relation:


 ALTER TABLE Customers
ADD COLUMN dob DATE;
 ALTER TABLE Customers
DROP COLUMN dob;

 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

SELECT desired attributes


FROM one or more tables
WHERE condition about tuples of the tables

optional clause
Example
4

 Using relation Customers, find the last names of the


customers who live in Limerick:

SELECT lastname
FROM Customers
WHERE city = 'Limerick';
Result of Query
5

lastname
SELECT lastname
Jones FROM Customers
Doe WHERE city = 'Limerick';

The answer is a relation with a single attribute,


lastname, and tuples with the lastname of each
customer from Limerick.
Meaning of Single-Relation Query
6

 Begin with the relation in the FROM


clause.
 Apply the selection indicated by the
WHERE clause.
 Apply the projection indicated by the
SELECT clause.
Operational Semantics --- General
7

 Think of a tuple variable visiting each tuple


of the relation mentioned in FROM.
 Check if the “current” tuple satisfies the

WHERE clause.
 If so, compute the attributes or expressions

of the SELECT clause using the components


of this tuple.
* In SELECT clauses
8

 When there is one relation in the FROM clause, * in


the SELECT clause stands for “all attributes of this
relation.”
 Example: Using Customers:

SELECT *
FROM Customers
WHERE city = 'Limerick';
Renaming Attributes
9

 If you want the result to have different attribute


names, use AS <new name> to rename an attribute.
 Example: Using Customers:

SELECT lastname AS surname surname


FROM customers Jones
WHERE city = 'Limerick'; Doe
Expressions in SELECT Clauses
10

 Any expression that makes sense can appear as an


element of a SELECT clause.
 Example: Using PCs:

SELECT model, price*1.4 AS priceUSD


FROM PCs;
Constants as Expressions
11

 Using PCs:

SELECT model,
price*1.4 AS price,
'USD' AS currency
FROM PCs;
Result of Query
12

model price currency

1001 2959.60 USD

1002 1393.00 USD

… … …

Useful for integrating data from different sources.


Complex Conditions in WHERE Clause
13

 Boolean operators AND, OR, NOT.


 Comparisons =, <>, <, >, <=, >=.
 And many other operators that produce Boolean-valued
results.
Example: Complex Condition
14

 Using PCs, find the models with speed greater than


or equal to 2.00 and price less than 1000.00:

SELECT model
FROM PCs
WHERE speed >= 2.0 AND
price < 1000.0;
Patterns
15

 A condition can compare a string to a pattern


by:
 <Attribute> LIKE <pattern> or
 <Attribute> NOT LIKE <pattern>

 Pattern is a quoted string with


% any string
_ any character
Example: LIKE
16

 Using Customers find the names of all customers


who live in Dublin and have 'Jervis St.' their
address:

SELECT firstname, lastname


FROM Customers
WHERE city = 'Dublin' AND
address LIKE '%Jervis St.%';
Example: LIKE
17

 Using Sales find all details about sales made in


December (any year):

SELECT *
FROM Sales
WHERE day LIKE '____-12%';
NULL Values
18

 Tuples in SQL relations can have NULL as a value


for one or more components.
 Meaning depends on context. Two common
cases:
 Missing value : e.g., we know a customer has some
address, but we don’t know what it is.
 Inapplicable : e.g., the value of attribute name of
spouse for an unmarried person.
Comparing NULL’s to Values
19

 The logic of conditions in SQL is really 3-valued


logic: TRUE, FALSE, UNKNOWN.
 Comparing any value (including NULL itself) with
NULL yields UNKNOWN.
 A tuple is in a query answer iff the WHERE clause is
TRUE (not FALSE or UNKNOWN).
Three-Valued Logic
20

T = TRUE, F = FALSE, U = UNKNOWN

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

 Some common laws hold in 3-valued logic.

 Example: commutativity of AND, i.e.

p AND q is the same as q AND p

 But not others, e.g. the law of the excluded middle:

p OR NOT p = TRUE

When p = UNKNOWN, the left side is UNKNOWN!


Multirelation Queries
22

 Interesting queries often combine data from more


than one relation.
 We can address several relations in one query by
listing them all in the FROM clause.
 Distinguish attributes of the same name by
<relation>.<attribute>
SQL (PART 3)
CS4416 Lecture 5
2. SQL as a Data Manipulation Language
2.2. Multirelational Queries
Example: Joining Two Relations
4

 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.

SELECT firstname, lastname, address


FROM Customers, Sales
WHERE Customers.customer_id=Sales.customer_id AND
city = 'Limerick' AND day = '2015-12-20';
Example: Joining Two Relations
5

SELECT firstname, lastname, address


FROM Customers, Sales
WHERE Customers.customer_id = Sales.customer_id AND
city = 'Limerick' AND day = '2015-12-20';

 What if the same customer has bought multiple models on this date?

SELECT DISTINCT firstname, lastname, address


FROM Customers, Sales
WHERE Customers.customer_id = Sales.customer_id AND
city = 'Limerick' AND day = '2015-12-20';
Formal Semantics
6

 Almost the same as for single-relation queries:


1. Start with the product of all the relations in the
FROM clause.
2. Apply the selection condition from the WHERE
clause.
3. Project onto the list of attributes and expressions in
the SELECT clause.
Operational Semantics
7

 Imagine one tuple-variable for each relation in the


FROM clause.
 These tuple-variables visit each combination of tuples,
one from each relation.
 If the tuple-variables are pointing to tuples that
satisfy the WHERE clause, send these tuples to the
SELECT clause.
Alternative [better] Syntax
8

SELECT DISTINCT firstname, lastname, address


FROM Customers JOIN Sales
ON Customers.customer_id = Sales.customer_id
WHERE city = 'Limerick' AND day = '2015-12-20';

 Joins in SQL:
 JOIN, INNER JOIN, CROSS JOIN – equivalent to the comma operator
 Outer joins: LEFT JOIN, RIGHT JOIN, FULL JOIN
 NATURAL JOIN

 It might be better to use JOIN … ON instead of the comma operator because


it provides a more information to the DBMS about what we intend to do.
Thus, the DBMs may optimise the query better (in general).
Inner and Outer Joins
9

 table1 JOIN table2 ON condition


 Combines data from two tables by forming pairs of related
rows based on the provided condition.
 table1 LEFT JOIN table2 ON condition
 Similar to JOIN but if a row in table1 has no match in table2
then it is matched with a row containing NULL values.
 table1 RIGHT JOIN table2 ON condition
 Same as table2 RIGHT JOIN table1 ON condition
 table1 FULL JOIN table2 ON condition
 Union of the result from the left and the right join of table1
and table2 on condition.
Natural Joins
10

 table1 JOIN table2 USING (a1,a2,…,an)


 Both tables contain attributes with names a1,a2,…,an
 A tuple from table1 is combines with a tuple from table2 if both
tuples have the same values for attributes a1,a2,…,an.
 Only one copy of each of the attributes a1,a2,…,an is present
in the result.

 table1 NATURAL JOIN table2


 Same as
table1 JOIN table2 USING (a1,a2,…,an)
where a1,a2,…,an are all attributes with the same name in
table1 and table2.
Natural Joins - Example
11

 Consider the last example:


SELECT DISTINCT firstname, lastname, address
FROM Customers JOIN Sales
ON Customers.customer_id = Sales.customer_id
WHERE city = 'Limerick' AND day = '2015-12-20';

 Since the attribute we join the two tables on has the same name in both
tables, i.e. customer_id, we can alternatively write:

SELECT DISTINCT firstname, lastname, address


FROM Customers NATURAL JOIN Sales
WHERE city = 'Limerick' AND day = '2015-12-20';

Q: What will be the difference between the results of the two queries
above?
Explicit Tuple-Variables
12

 Sometimes, a query needs to use two copies of


the same relation.
 Distinguish copies by following the relation
name by the name of a tuple-variable, in the
FROM clause.
 It’s always an option to rename relations this
way, even when not essential.
Example: Self-Join
13

 From Customers(customer_id, firstname, lastname, city,


address, email) find all pairs of customer IDs which
belong to customers from the same city.
 Do not produce pairs like (9999999999, 9999999999).
 Produce pairs in ascending (or alphabetic) order,
e.g. (1231231231, 99999999999),
not (99999999999, 1231231231).

SELECT c1.customer_id, c2.customer_id


FROM Customers c1, Customers c2
WHERE c1.city = c2.city AND
c1.customer_id < c2.customer_id;
2.3. Subqueries
Subqueries
15

 A parenthesized SELECT-FROM-WHERE statement


(subquery ) can be used as a value in a number of
places, including FROM and WHERE clauses.
Subquery in FROM
16

 In place of a relation in the FROM clause, we can


use a subquery and then query its result.
 Must use a tuple-variable to name tuples of the result.
Example: Subquery in FROM
17

 Example: From Products and PCs find the PC models made


by maker B and with speed at least 2.0.
SELECT model
FROM Products JOIN PCs USING(model)
WHERE maker = 'B' AND speed >= 2.0;

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

 If a subquery is guaranteed to produce one tuple,


then the subquery can be used as a value.
 Usually,the tuple has one component.
 A run-time error occurs if there is no tuple or more
than one tuple.
Example: Single-Tuple Subquery
19

 Using Customers, find the names of all


customers from Limerick who have the same
lastname as the customer with ID 1111111111.
 Two queries would surely work:
1. Find the lastname of customer 1111111111 (say it is
Ryan)
2. Find the names of those customers from Limerick
who have the same lastname (i.e. Ryan).
Query + Subquery Solution
20

SELECT firstname, lastname


FROM customers
WHERE city = 'Limerick' AND
lastname = (SELECT lastname
FROM customers
WHERE customer_id='1111111111');

The lastname
of customer
1111111111
The IN Operator
21

 <tuple> IN (<subquery>) is true if and only if the


tuple is a member of the relation produced by the
subquery.
 Opposite: <tuple> NOT IN (<subquery>).
 IN-expressions can appear in WHERE clauses.
Example: IN
22

 Example: From Products and PCs find the PC models


made by maker B and with speed at least 2.0 and at
most 2.8.
speed BETWEEN 2.0 AND 2.8
SELECT model alternative syntax
FROM Products for the same
WHERE maker = 'B' AND condition

model IN (SELECT model


FROM PCs
The set of PC models
with speed at least
WHERE speed >= 2.0 AND
2.0 and at most 2.8 speed <= 2.8);
The Exists Operator
23

 EXISTS(<subquery>) is true if and only if the


subquery result is not empty.
 Example: From Customers, find the IDs of those
customers whose first names are unique, i.e. no
other customer has the same first name.
Example: EXISTS
24

SELECT customer_id Notice scope rule: firstname


refers to the closest nested
FROM Customers c1 FROM with a relation having
that attribute.
WHERE NOT EXISTS (
SELECT *
Set of FROM Customers
customers
with the
WHERE firstname = c1.firstname AND
same customer_id <> c1.customer_id);
firstname
as c1, but
not the same
customer_id
The Operators ANY and ALL
25

 x = ANY(<subquery>) is a Boolean condition that is


true if and only if x equals at least one tuple in the
subquery result.
= could be any comparison operator.
 x <> ALL(<subquery>) is true if and only if x is
different from all tuples in the subquery result.
<> can be any comparison operator.
Example: ALL
26

 From PCs find the fastest PC model(s) with hd less


than or equal to 250.

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

 The aggregate functions include SUM, AVG,


COUNT, MIN, and MAX.
 They can be applied to a single attribute/column in
a SELECT clause to produce that aggregation on
the attribute.
 Also, COUNT(*) counts the number of tuples.
 Aggregations are NOT allowed in the WHERE
clause (outside subqueries)!!!
Example: Aggregation
5

 From PCs find the average speed of PC models


with hd less than or equal to 250.

SELECT AVG(speed)
FROM PCs
WHERE hd <= 250;
Example: Illegal Use of Aggregation
6

 From PCs find the fastest PC model(s) with hd less


than or equal to 250.

SELECT model
FROM PCs
WHERE hd <= 250 AND
speed = MAX(speed);
Example: Illegal Use of Aggregation
7

 From PCs find the fastest PC model(s) with hd less


than or equal to 250.

SELECT model, MAX(speed)


FROM PCs
WHERE hd <= 250;
Correct Solution
8

 From PCs find the fastest PC model(s) with hd less


than or equal to 250.

SELECT model
FROM PCs
WHERE hd <= 250 AND
speed =(SELECT MAX(speed)
FROM PCs
WHERE hd <= 250);
Eliminating Duplicates in Aggregations
9

 Use DISTINCT inside an aggregation.


 Example: find the number of different RAM sizes of
PC models:

SELECT COUNT(DISTINCT ram)


FROM PCs;
NULLs Ignored in Aggregation
10

 NULL never contributes to a sum, average, or


count, and can never be the minimum or
maximum of a column.

 But if there are no non-NULL values in a column,


then the result of the aggregation is NULL.
 Exception: COUNT of an empty set is 0.
Example: Effect of NULL
11

The number of rows


SELECT COUNT(*) in table Customers.
FROM Customers

The number of rows


SELECT COUNT(email) in table Customers with
FROM Customers; non-NULL email.
Grouping
12

 We may follow a SELECT-FROM-WHERE expression


by GROUP BY and a list of attributes.

 The relation that results from the SELECT-FROM-


WHERE is grouped according to the values of all
those attributes, and any aggregation is applied
only within each group.
Example: Grouping
13

 From Products(maker, model, type) find the total


number of laptop models per maker:

SELECT maker, COUNT(*)


FROM Products
WHERE type = 'laptop'
GROUP BY maker;
Grouping: Restriction on SELECT
14

 If grouping is used, then each element of the


SELECT list must be either:
1. Aggregated or
2. An attribute on the GROUP BY list.
HAVING Clauses
15

 HAVING <condition> may follow a GROUP BY


clause.
 If so, the condition applies to each group, and
groups not satisfying the condition are eliminated.
Requirements on HAVING Conditions
16

 Anything goes in a subquery.


 Outside subqueries, they may refer to attributes
only if they are either:
1. A grouping attribute, or
2. Aggregated
(same condition as for SELECT clauses with
aggregation).
Example: HAVING
17

 From Sales find the customers who have spent


more than 2000 in total. List the ID and the total
sum paid for each of them.
Solution
18

SELECT customer_id, SUM(paid)


FROM Sales
GROUP BY customer_id
HAVING SUM(paid) > 2000;
Example: HAVING
19

 Lets make it more complicated:

 From Sales and Customers find the customers


from Limerick who have spent more than 2000 in
total. List the ID and the total sum paid for each
of them.
Solution (1)
20

SELECT customer_id, SUM(paid)


FROM Sales
GROUP BY customer_id
HAVING SUM(paid) > 2000 AND
customer_id IN
(SELECT customer_id
FROM Customers
WHERE city = 'Limerick');
Solution (2)
21

SELECT customer_id, SUM(paid)


FROM Sales JOIN Customers USING(customer_id)
WHERE city = 'Limerick'
GROUP BY customer_id
HAVING SUM(paid) > 2000;
SQL (PART 5)
CS4416 Lecture 7
2. SQL as a Data Manipulation Language
2.5. Set Operators

UNION, UNION ALL, INTERSECT and MINUS


Set Operators
4

 It makes sense to unite, intersect or subtract only


the results of subqueries with the same schema!
That is, the same number of attributes in the same
order and with the same data types.
 Set operators in SQL remove duplicated rows in the
result by default. Use UNION ALL if you want to
suppress the deletion of duplications.
Example: UNION
5

 Using tables PCs, Laptops and Printers, find the list of


prices of all products. List the model number and price
for each product.

SELECT model, price FROM PCs


UNION
SELECT model, price FROM Laptops
UNION
SELECT model, price FROM Printers;

 Note that the three subqueries above have the same


schema!
Example: MINUS
6

 Using Products(maker, model, type), find the


makers who make at least one laptop model but
no PC models.

SELECT maker FROM Products WHERE type='laptop'


MINUS
SELECT maker FROM Products WHERE type='pc';

 Note that MINUS is not implemented in MySQL.


Example: Instead of MINUS
7

 Using Products(maker, model, type), find the


makers who make at least one laptop model but
no PC models.

SELECT DISTINCT maker


FROM Products
WHERE type='laptop' AND maker NOT IN
(SELECT maker FROM Products WHERE type='pc');
Example: INTERSECT
8

 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;

 Note that INTERSECT is not implemented in MySQL either.


Example: Instead of INTERSECT
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 DISTINCT maker


FROM Products NATURAL JOIN PCs
WHERE price > 500 AND
maker IN (SELECT maker
FROM Products NATURAL JOIN Laptops
WHERE price > 500);

 Note that INTERSECT is not implemented in MySQL either.


2.6. Built-In Functions
Built-In Functions
11

 The SQL standard defines a set of standard SQL


built-in functions.
 Only some aggregate functions (SUM, AVG, MIN,
MAX, COUNT) are covered in this module but you
are highly encouraged to use a wider range of
built-in functions in your project.
Some References
12

 Standard built-In functions in SQL92:


http://db.apache.org/derby/docs/10.4/ref/rrefsqlj29026.html
 Built-in functions in MySQL:
http://dev.mysql.com/doc/refman/5.7/en/functions.html
 Built-in functions in Oracle:
http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions001.
htm#SQLRF51171
 Built-in functions in the Microsoft SQL Server:
http://db.apache.org/derby/docs/10.4/ref/rrefsqlj29026.html
2.7. Data Modification Statements

INSERT, DELETE and UPDATE


Database Modifications
14

 A modification command does not return a


result (as a query does), but changes the
database in some way.
 Three kinds of modifications:
1. Insert a tuple or tuples.
2. Delete a tuple or tuples.
3. Update the value(s) of an existing tuple or tuples.
Insertion
15

 To insert a single tuple:


INSERT INTO <relation>
VALUES ( <list of values> );

 Example: add to Products(maker, model, type) the


fact that maker B makes PC model 1014.

INSERT INTO Products


VALUES('B', '1014', 'pc');
Specifying Attributes in INSERT
16

 We may add to the relation name a list of


attributes.
 Two reasons to do so:
1. We forget the standard order of attributes for the
relation.
2. We don’t have values for all attributes, and we want
the system to fill in missing components with NULL
or a default value.
Example: Specifying Attributes
17

 Another way to add the fact that maker B makes


PC model 1014 to Products(maker, model, type).

INSERT INTO Products(model, type, maker)


VALUES('1014', 'pc', 'B');
Adding Default Values
18

 In a CREATE TABLE statement, we can follow an


attribute by DEFAULT and a value.
 When an inserted tuple has no value for that
attribute, the default will be used.
Example: Default Values
19

CREATE TABLE Products (


maker CHAR(1),
model CHAR(4) PRIMARY KEY,
type VARCHAR(8) DEFAULT 'pc'
);
Example: Default Values
20

INSERT INTO Products(model)


VALUES('1014');

Resulting tuple:
maker model type
NULL 1014 pc
Inserting Many Tuples
21

 We may insert the entire result of a query into a


relation, using the form:
INSERT INTO <relation>
( <subquery> );
Example: Insert a Subquery
22

 Create a copy of Products(maker, model, type), called


Products_copy.

CREATE TABLE Products_copy (


maker CHAR(1),
model CHAR(4) PRIMARY KEY,
type VARCHAR(8) DEFAULT 'pc'
);
INSERT INTO Products_copy
(SELECT * FROM Products);
Deletion
23

 To delete tuples satisfying a condition from some


relation:
DELETE FROM <relation>
WHERE <condition>;
Example: Deletion
24

 Delete from Products(maker, model, type) all


printers made by maker A:

DELETE
FROM Products
WHERE type = 'printer' AND
maker = 'A';
Example: Delete all Tuples
25

 Make the relation Customers empty:

DELETE FROM Customers;

 Note no WHERE clause needed.


Example: Delete Some Tuples
26

 Delete from Products(maker, model, type) all


models for which there is another model by the
same maker.
Products with the
same maker and
DELETE FROM Products p a different model
WHERE EXISTS ( number from the
model number
SELECT model FROM Products represented by
tuple p.
WHERE maker = p.maker AND
model <> p.model);
Semantics of Deletion --- (1)
27

 Suppose maker H makes only products 3006 and


3007.
 Suppose we come to the tuple p for 3006 first.
 The subquery is nonempty, because of the 3007
tuple, so we delete 3006.
 Now, when b is the tuple for 3007, do we delete
that tuple too?
Semantics of Deletion --- (2)
28

 Answer: we do delete 3007 as well.


 The reason is that deletion proceeds in two
stages:
1. Mark all tuples for which the WHERE condition is
satisfied.
2. Delete the marked tuples.
Updates
29

 To change certain attributes in certain tuples of a


relation:
UPDATE <relation>
SET <list of attribute assignments>
WHERE <condition on tuples>;
Example: Update
30

 Change the price of PC model 1001 to 1999.0:

UPDATE PCs
SET price = 1999.0
WHERE model = '1001';
Example: Update Several Tuples
31

 Make 1999.0 the maximum price for PC:

UPDATE PCs
SET price = 1999.0
WHERE price > 1999.0;
3 Views
Views
33

 A view is a relation defined in terms of stored


tables (called base tables ) and other views.
 Two kinds:
1. Virtual = not stored in the database; just a query
for constructing the relation.
2. Materialized = actually constructed and stored
(available in Oracle but not in MySQL)
Declaring Views
34

 Declare by:

CREATE [MATERIALIZED] VIEW <name>


AS <query>;

 Default is virtual (and the only option in MySQL)


Example: View Definition
35

 PC_A_PriceList (model, price) is a view “containing” the


price of all PC made by maker A:

CREATE VIEW PC_A_PriceList AS


SELECT model, price
FROM PCs
WHERE model IN (SELECT model
FROM Products
WHERE maker = 'A');
Example: Accessing a View
36

 Query a view as if it were a base table.


 Also: a limited ability to modify views if it makes sense
as a modification of one underlying base table.
 Using views in queries can simplify the expression
of complex queries:
 Example query: Find the average price of a PC
made by maker A:
SELECT AVG(price)
FROM PC_A_PriceList;

You might also like