SlideShare ist ein Scribd-Unternehmen logo
1 von 61
Downloaden Sie, um offline zu lesen
2 December 2005
Introduction to Databases
Structured Query Language
Prof. Beat Signer
Department of Computer Science
Vrije Universiteit Brussel
http://www.beatsigner.com
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 2March 17, 2017
Context of Today's Lecture
Access
Methods
System
Buffers
Authorisation
Control
Integrity
Checker
Command
Processor
Program
Object Code
DDL
Compiler
File
Manager
Buffer
Manager
Recovery
Manager
Scheduler
Query
Optimiser
Transaction
Manager
Query
Compiler
Queries
Catalogue
Manager
DML
Preprocessor
Database
Schema
Application
Programs
Database and
System Catalogue
Database
Manager
Data
Manager
DBMS
Programmers Users DB Admins
Based on 'Components of a DBMS', Database Systems,
T. Connolly and C. Begg, Addison-Wesley 2010
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 3March 17, 2017
Structured Query Language (SQL)
 Declarative query language to create database schemas,
insert, update, delete and query information based on a
data definition and data manipulation language
 Data definition language (DDL)
 definition of database structure (relation schemas)
 data access control
 Data manipulation language (DML)
 query language to create, read, update and delete tuples
(CRUD operations)
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 4March 17, 2017
Structured Query Language (SQL) ...
 The SQL language further deals with the following issues
 transaction control
 integrity constraints (DDL)
 auhorisation (DDL)
 views (DDL)
 embedded SQL and dynamic SQL
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 5March 17, 2017
 SEQUEL (70's)
 structured english query language
 developed by Raymond F. Boyce
and Donald D. Chamberlin
 access data stored in IBM's
System R relational database
 SQL-86
 first ANSI standard version
 SQL-89 / SQL 1
 SQL-92 / SQL 2
 we will mainly discuss features of the SQL-92 standard
History of SQL
Donald D. Chamberlin Raymond F. Boyce
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 6March 17, 2017
History of SQL ...
 SQL:1999 / SQL 3
 recursive queries, triggers, object-oriented features, ...
 SQL:2003
 window functions, XML-related features, ...
 SQL:2006
 XML Query Language (XQuery) support, ...
 SQL:2008
 SQL:2011
 improved support for temporal databases
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 7March 17, 2017
SQL "Standard"
 Each specific SQL implementation by a database vendor
is called a dialect
 The vendors implement parts of the SQL standard
(e.g. most implement SQL-92) but add their vendor-
specific extensions
 Most relational database vendors conform to a set of
Core SQL features but portability might still be limited
due to missing or additional features
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 8March 17, 2017
Data Definition Language (DDL)
 The data definition language (DDL) is used to specify the
relation schemas as well as other information about the
relations
 relation schemas
 attribute domain types
 integrity constraints
 relation indexes
 access information
 physical storage structure of relations
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 9March 17, 2017
Database Creation
 The concrete process of creating a new database might
differ for different relational database products
 According to the SQL standard, an SQL environment
contains one or more catalogues
 Each catalogue manages various metadata
 set of schemas consisting of
- relations/tables
- views
- assertions
- indexes
 SET SCHEMA name can be used to
set the current schema
 users and user groups
environment
catalogue catalogue
schema
schema schema
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 10March 17, 2017
Database Creation ...
 The creation of catalogues is not covered by the SQL
standard and therefore implementation specific
 Schemas can be created and deleted via the CREATE and
DROP statements
 The default parameter of the DROP SCHEMA statement is
RESTRICT
 only empty schema can be deleted
 If CASCADE is specified, all objects associated with the
schema will be dropped
createSchema = "CREATE SCHEMA" , name , "AUTHORIZATION" , creator ,
[ ddlStatements ];
dropSchema = "DROP SCHEMA" , name , [ "RESTRICT" | "CASCADE" ];
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 11March 17, 2017
Extended Backus-Naur Form (EBNF)
 Notation to describe computer program-
ming languages (context-free grammars)
 developed by Niklaus Wirth
Notation Meaning
= Definition
, Sequence
; Termination
| Choice
[...] Option
{...} Repetition
(...) Grouping
"..." Terminal String
Niklaus Wirth
 We use the EBNF
to describe different
SQL concepts
http://en.wikipedia.org/wiki/Extended_Backus-Naur_Form
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 12March 17, 2017
Relational Database Example
customerID name street postcode city
1 Max Frisch Bahnhofstrasse 7 8001 Zurich
2 Eddy Merckx Pleinlaan 25 1050 Brussels
5 Claude Debussy 12 Rue Louise 75008 Paris
53 Albert Einstein Bergstrasse 18 8037 Zurich
8 Max Frisch ETH Zentrum 8092 Zurich
cdID name duration price year
1 Falling into Place 2007 17.90 2007
2 Carcassonne 3156 15.50 1993
3 Chromatic 3012 16.50 1993
customer
cd
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 13March 17, 2017
Relational Database Example ...
supplierID name postcode city
5 Max Frisch 8037 Zurich
2 Mario Botta 6901 Lugano
orderID customerID cdID date amount status
1 53 2 13.02.2010 2 open
2 2 1 15.02.2010 1 delivered
order
supplier
Customer (customerID, name, street, postcode, city)
CD (cdID, name, duration, price, year)
Order (orderId, customerID, cdID, date, amount, status)
Supplier (supplierID, name, postcode, city)
relational database schema
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 14March 17, 2017
Table Definition Example
CREATE TABLE Customer (
customerID INTEGER CHECK (customerID > 0) PRIMARY KEY,
name VARCHAR(30) NOT NULL,
street VARCHAR(30) NOT NULL,
postcode SMALLINT CHECK (postcode > 0),
city VARCHAR(20)
);
CREATE TABLE CD (
cdID INTEGER PRIMARY KEY,
name VARCHAR(30) NOT NULL,
duration TIME,
price NUMERIC(6,2),
year SMALLINT
);
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 15March 17, 2017
Table Definition Example ...
CREATE TABLE Supplier (
supplierID INTEGER PRIMARY KEY,
name VARCHAR(30) NOT NULL,
postcode SMALLINT CHECK (postcode > 0),
city VARCHAR(20)
);
CREATE TABLE Order (
orderID INTEGER CHECK (orderID > 0) PRIMARY KEY,
customerID INTEGER,
cdID INTEGER ,
date DATE,
amount INTEGER,
Status VARCHAR(20) NOT NULL DEFAULT 'open',
UNIQUE (customerID, cdID, date),
FOREIGN KEY (customerID) REFERENCES Customer(customerID)
ON UPDATE CASCADE ON DELETE SET NULL,
FOREIGN KEY (cdID) REFERENCES CD(cdID)
ON UPDATE CASCADE
);
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 16March 17, 2017
Table Constraints
 We can have only one PRIMARY KEY constraint but
multiple UNIQUE constraints
 if no primary key is defined, duplicates are allowed (bag)
 Referential integrity
 a foreign key always has to have a matching value in the
referenced table (or it can be null)
 different referential actions can be defined for update (ON UPDATE)
and delete (ON DELETE) operations on the referenced candidate
key
- CASCADE: propagate operations to the foreign keys which might lead to further
cascaded operations
- SET DEFAULT: set the foreign keys to their default value
- SET NULL: set the foreign keys to NULL
- NO ACTION: the operation on the candidate key will be rejected (default)
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 17March 17, 2017
Table Definition
createTable = "CREATE TABLE" , table , "(" ,
( columnElement | tableConstraint ) ,
{ "," , ( columnElement | tableConstraint ) } , ")";
columnElement = column , datatype ,
[ "DEFAULT" , ( value | "NULL" ) ] , { columnConstraint };
columnConstraint = "NOT NULL" | "UNIQUE" | "PRIMARY KEY" |
( "REFERENCES" , table , [ "(" , column , ")" ] ,
{ referentialAction } ) |
( "CHECK (" , searchCondition , ")" );
tableConstraint = ( ( "UNIQUE" | "PRIMARY KEY ) , "(" , column ,
{ "," , column } , ")" ) |
( "FOREIGN KEY (" , column , { "," , column } , ")" ,
"REFERENCES" , table , [ "(" , column , { "," , column } , ")" ] ,
{ referentialAction } ) |
( "CHECK (" , searchCondition , ")" );
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 18March 17, 2017
Table Definition ...
referentialAction = ( "ON UPDATE" | "ON DELETE" ) ,
( "CASCADE" | "SET DEFAULT" | "SET NULL" | "NO ACTION" );
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 19March 17, 2017
SQL Datatypes
 Character data
 fixed-length or variable-length sequence of characters
 optional multibyte character sets (e.g. for Japanese etc.)
 Large character data or binary data
 often a so-called locator is returned to access a large object in
pieces instead of loading the entire object into memory
char = fixedChar | varyingChar [charSet];
fixedChar = "CHAR" , [ "(" , length , ")" ];
varyingChar = "VARCHAR" , [ "(" , maxLength , ")" ];
charSet = "CHARACTER SET" charSetName;
lob = clob | blob;
clob = "CLOB" , [ "(" , size , ")" ];
blob = "BLOB" , [ "(" , size , ")" ];
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 20March 17, 2017
SQL Datatypes ...
 Numeric data
 The DECIMAL datatype is sometimes used as a synonym
for the NUMERIC datatype
numeric = decimal | int | smallInt | float | real | double;
decimal = "DECIMAL" , [ "(" , precision , [ "," , scale ] , ")" ];
int = "INTEGER";
smallInt = "SMALLINT";
float = "FLOAT" , [ "(" , precision , ")" ];
real = "REAL";
double = "DOUBLE PRECISION";
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 21March 17, 2017
SQL Datatypes ...
 Datetime data
 Format of the datetime values
 date: YYYY-MM-DD
 time: hh:mm:ss.p ± hh:mm
 timestamp: YYYY-MM-DD hh:mm:ss.p ± hh:mm
datetime = date | time | timestamp;
date = "DATE";
time = "TIME" , [ "(" , precision , ")" ] ,
[ "WITH TIME ZONE" , timezone ];
timestamp = "TIMESTAMP" , [ "(" , precision , ")" ] ,
[ "WITH TIME ZONE" , timezone ];
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 22March 17, 2017
SQL Datatypes ...
 Boolean
 the domain of boolean values consist of the two truth values TRUE
and FALSE
 a thrid UNKNOWN truth value is used to represent NULL values
 introduced in SQL:1999
 Bit data
 fixed or varying sequence of binary digits (0 or 1)
boolean = "BOOLEAN";
bit = fixedBit | varyingBit;
fixedBit = "BIT" , [ "(" , length , ")" ];
varyingBit = "BIT VARYING" , [ "(" , maxLength , ")" ];
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 23March 17, 2017
SQL Datatypes ...
 For further details about the presented datatypes as well
as information about vendor-specific datatypes one has
to consult the specific database manuals
datatype = char | lob | numeric | datetime | boolean | bit;
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 24March 17, 2017
Data Manipulation
 After a table has been created, we can use the INSERT
command to add tuples
 unspecified attribute values are set to the default value or NULL
 attribute order can be changed via optional column names
 "bulk loader" utilities to insert large amounts of tuples
 Example
INSERT INTO Customer VALUES(8,'Max Frisch','ETH Zentrum', 8001, 'Zurich');
insert = "INSERT INTO" , table ,
[ "(" , column , { "," , column } , ")" ] ,
( "VALUES (" , expr , { "," , expr } , ")" ) | ( "(" , query , ")" );
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 25March 17, 2017
Expressions
expr = exprElement { ( "+" | "-" | "*" | "/" ) , exprElement };
exprElement = column | value |
"COUNT" , "(" ( "*" | ( [ "ALL" | "DISTINCT" ] , column ) , ")" |
( "MIN" | "MAX" ) , "(" , expr , ")" |
( "SUM" | "AVG" ) , "(" , [ "DISTINCT" ] , expr , ")";
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 26March 17, 2017
Data Manipulation ...
 The DELETE statement can be used to delete tuples
 Tuples can be updated via the UPDATE statement
 Example
UPDATE Customer SET name = 'Walter Faber' WHERE customerID = 8;
update = "UPDATE" , table , "SET" ,
column , "=" , ( "NULL" | expr | "(" , query , ")" ) ,
{ "," , column , "=" , ("NULL" | expr | "(" , query , ")" ) } ,
[ "WHERE" , searchCondition ];
delete = "DELETE FROM" , table [ "WHERE" , searchCondition ];
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 27March 17, 2017
Data Manipulation ...
 The DROP TABLE statement can be used to delete a
relation from the database
 A relation schema can be modified via the ALTER TABLE
command
 existing tuples are assigned a NULL value for the new attribute
 Example
alterTable = "ALTER TABLE" , table , "ADD" ,
( columnElement | columnConstraint );
ALTER TABLE Customer ADD birthdate DATE;
dropTable = "DROP TABLE" , table;
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 28March 17, 2017
Basic SQL Query Structure
 A basic SQL query consists of a SELECT, a FROM and a
WHERE clause
 SELECT
- specifies the columns to appear in the result (projection in relational algebra)
 FROM
- specifies the relations to be used (cartesian product in relational algebra)
 WHERE
- filters the tuples (selection in relational algebra)
- join conditions are explicitly specified in the WHERE clause
 GROUP BY
- groups rows with the same column values
- the HAVING construct can be used to further filter the groups
 ORDER BY
- defines the order of the resulting tuples
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 29March 17, 2017
Basic SQL Query Structure ...
 In general, the SELECT FROM WHERE parts are evaluated as
follows
1. generate a cartesian product of the relations listed in the FROM
clause
2. apply the predicates specified in the WHERE clause on the result
of the first step
3. for each tuple in the result of the second step output the attri-
butes (or results of expressions) specified in the SELECT clause
 The evaluation is normally optimised by a query optimiser
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 30March 17, 2017
Basic SQL Query Structure ...
 The order of clauses in an SQL query cannot be
changed
 Note that the SELECT is equivalent to a relational algebra
projection
 In contrast to the relational algebra, SQL does not
eliminate duplicates automatically
 the automatic elimination of duplicates would be time consuming
 user has to eliminate duplicates explicitly via DISTINCT keyword
SELECT A1, A2,..., An
FROM r1, r2,..., rm
WHERE P
pA1,A2,...,An
(sP(r1  r2  ...  rm)
is equivalent to
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 31March 17, 2017
SELECT Clause
 A '*' can be used in the SELECT clause as a shortcut to
get all tuple attributes
SELECT *
FROM Customer;
customerID name street postcode city
1 Max Frisch Bahnhofstrasse 7 8001 Zurich
2 Eddy Merckx Pleinlaan 25 1050 Brussels
5 Claude Debussy 12 Rue Louise 75008 Paris
53 Albert Einstein Bergstrasse 18 8037 Zurich
8 Max Frisch ETH Zentrum 8092 Zurich
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 32March 17, 2017
SELECT Clause ...
 Duplicate tuples resulting from a projection to specific
attributes are not eliminated by default
SELECT name
FROM Customer;
name
Max Frisch
Eddy Merckx
Claude Debussy
Albert Einstein
Max Frisch
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 33March 17, 2017
SELECT Clause ...
 The DISTINCT keyword can be used to eliminate
duplicates
SELECT DISTINCT name
FROM Customer;
name
Max Frisch
Eddy Merckx
Claude Debussy
Albert Einstein
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 34March 17, 2017
Computed Attributes and Rename
 Computations can be performed in the SELECT clause
 multiple numeric attributes can be used in a computation
 The rename operation (AS) is used to rename relations
as well as attributes
 computed columns have no name by default
 also used when multiple relations have the same attribute names
SELECT name, price * 1.5 AS newPrice
FROM CD;
name newPrice
Falling into Place 26.85
Carcassonne 23.20
Chromatic 24.75
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 35March 17, 2017
WHERE Clause
 In the WHERE clause we can use five basic predicates
(search conditions)
 comparison
- compare two expressions
 range
- check whether the value is within a specified range of values (BETWEEN)
 set membership
- check whether the value is equal to a value of a given set (IN)
 pattern matching
- test whether the expression matches a specifies string pattern (LIKE)
 check for NULL values
- check whether the expression is a NULL value (IS NULL)
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 36March 17, 2017
WHERE Clause ...
SELECT name, postcode
FROM Customer
WHERE city = 'Zurich' AND postcode >= 8040;
name postcode
Max Frisch 8092
SELECT name, price
FROM CD
WHERE price BETWEEN 15.0 AND 17.0;
name price
Carcassonne 15.50
Chromatic 16.50
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 37March 17, 2017
WHERE Clause ...
 Check for set membership with the IN construct
SELECT *
FROM Customer
WHERE city IN ('Zurich', 'Brussels');
customerID name street postcode city
1 Max Frisch Bahnhofstrasse 7 8001 Zurich
2 Eddy Merckx Pleinlaan 25 1050 Brussels
53 Albert Einstein Bergstrasse 18 8037 Zurich
8 Max Frisch ETH Zentrum 8092 Zurich
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 38March 17, 2017
Pattern Matching
 Strings are enclosed in single quotes
 use a double single quote for escaping
 The LIKE operator is used for pattern matching
 the underscore (_) is a placeholder for a single character
 the percent sign (%) is a placeholder for any substring
 e.g. LIKE '_e%'
name
Albert Einstein
SELECT DISTINCT name
FROM Customer
WHERE name LIKE '%Ein%';
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 39March 17, 2017
Null Values
 Missing (unknown) info is represented by NULL values
 result of any comparison involving a NULL value is Unknown
 three-valued logic (3VL) based on True, False and Unknown
True False Unknown
True True False Unknown
False False False False
Unknown Unknown False Unknown
AND
True False Unknown
True True True True
False True False Unknown
Unknown True Unknown Unknown
OR
=
True False Unknown
True True False Unknown
False False True Unknown
Unknown Unknown Unknown Unknown
NOT
True False Unknown
False True Unknown
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 40March 17, 2017
Null Values ...
 The NULL keyword can also be used in predicates to
check for null values
 Note that a check for NULL is not the same as a check for
the empty String ''
SELECT *
FROM CD
WHERE price IS NOT NULL;
cdID name duration price year
1 Falling into Place 2007 17.90 2007
2 Carcassonne 3156 15.50 1993
3 Chromatic 3012 16.50 1993
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 41March 17, 2017
FROM Clause
 The FROM clause creates a cartesian product of multiple
relations and can be used to specify join operations
 In a previous lecture we have seen the following
relational algebra expression
- "list the name and street of customers whose order is still open"
- pname, street(sstatus="open"(order ⋈ customer))
- the same can be achieved in SQL by explicitly specifying the matching attributes
SELECT name, street
FROM Customer, Order
WHERE Order.customerID = Customer.customerID AND status = 'open';
name street
Albert Einstein Bergstrasse 18
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 42March 17, 2017
Inner and Outer Joins
 Note that there exist SQL extensions to perform join
operations between two relations R and S in the FROM
clause
 Inner Joins
 Outer Joins
SELECT * FROM R NATURAL JOIN S;
SELECT * FROM R CROSS JOIN S;
SELECT * FROM R JOIN S ON R.A > S.B;
SELECT * FROM R LEFT OUTER JOIN S ON R.A = S.B;
SELECT * FROM R RIGHT OUTER JOIN S ON R.A = S.B;
SELECT * FROM R FULL OUTER JOIN S ON R.A = S.B;
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 43March 17, 2017
Correlation Variable
 A correlation variable can be used as an alias for a table
 Example
 "Find all pairs of CDs that were produced in the same year"
SELECT c1.name AS name1, c2.name AS name2
FROM CD c1, CD c2
WHERE c1.year = c2.year AND c1.cdID < c2.cdID;
name1 name2
Carcassonne Chromatic
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 44March 17, 2017
Sorting
 The ORDER BY clause can be used to arrange the result
tuples in acending (ASC) or descending (DESC) order
 multiple sort keys can be specified; highest priority first
 tuples with NULL values are either before or after non-NULL tuples
SELECT name, street, city
FROM Customer
ORDER BY city ASC, name DESC;
name street city
Eddy Merckx Pleinlaan 25 Brussels
Claude Debussy 12 Rue Louise Paris
Max Frisch ETH Zentrum Zurich
Max Frisch Bahnhofstrasse 7 Zurich
Albert Einstein Bergstrasse 18 Zurich
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 45March 17, 2017
Set Operations
 The UNION, INTERSECT and EXCEPT operations correspond
to the , and - relational algebra operations
 the relations have to be compatible (same attributes)
 these operations remove duplicates by default
- the ALL keyword has to be used to retain duplicates
(SELECT name
FROM Customer)
INTERSECT
(SELECT name
FROM Supplier);
name
Max Frisch
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 46March 17, 2017
Aggregate Functions and Grouping
 In SQL there are five aggregate functions (MIN, MAX, AVG,
SUM and COUNT) that take a set or multiset of values as
input and return a single value
 Example
 "Find the number of customers in each city"
 Aggregate functions (except COUNT(*)) ignore NULL
values in the input set
 input set might be empty in which case NULL is returned
SELECT city, COUNT(customerID) AS number
FROM Customer
GROUP BY city;
city number
Zurich 3
Brussels 1
Paris 1
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 47March 17, 2017
Subqueries
 A subquery is a SELECT FROM WHERE expression that is
nested within another query
 e.g. via check for set membership (IN or NOT IN)
 Example
 "Find all the suppliers who are no customers"
SELECT DISTINCT name
FROM Supplier
WHERE name NOT IN (SELECT name
FROM Customer);
name
Mario Botta
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 48March 17, 2017
Nested Subqueries ...
 Example
 "Find all CDs with a price smaller than average"
SELECT *
FROM CD
WHERE price < (SELECT AVG(price)
FROM CD);
cdID name duration price year
2 Carcassonne 3156 15.50 1993
3 Chromatic 3012 16.50 1993
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 49March 17, 2017
Set Comparison
 For nested queries with conditions like "greater than at
least one" we can use these set comparison operators
 > SOME, >= SOME, < SOME, <= SOME, = SOME, <> SOME as well as the
same combination with ALL
 Example
 "Find the customers with a postcode greater than all supplier postcodes"
SELECT name ,postcode
FROM Customer
WHERE postcode > ALL (SELECT postcode
FROM Supplier);
name postcode
Claude Debussy 75008
Max Frisch 8092
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 50March 17, 2017
Existence Test
 The EXISTS operator can be used to check if a tuple
exists in a subquery
 Example
SELECT name
FROM Customer
WHERE EXISTS (SELECT *
FROM Supplier
WHERE Supplier.name = Customer.name);
name
Max Frisch
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 51March 17, 2017
Derived Relations
 A subquery expression can also be used in the FROM
clause
 in this case, a name has to be given to the relation
 Example
 "Find the number of customers in the city with the most
customers"
SELECT MAX(noCustomers) AS max
FROM (SELECT city, COUNT(customerID)
FROM Customer
GROUP BY city) AS CityTotal(city, noCustomers);
max
3
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 52March 17, 2017
Basic SQL Query Structure
 The query statement can be used to retrieve information
from one or multiple database tables
 can perform the relational algebra's selection, projection and join
operation in a single SELECT FROM WHERE command
query = select { ("UNION" | "INTERSECT" | "EXCEPT") , [ "ALL" ] , select};
select = "SELECT" [ "ALL" | "DISTINCT" ] ,
("*" | ( expr , [ "AS" , newName ] ,
{ "," , expr , [ "AS" , newName ] } ) ,
"FROM" , table , [ correlationVar ] ,
{ "," , table , [ correlationVar ] } ,
[ "WHERE" , searchCondition ] ,
[ "GROUP BY" , column , { "," , column } ,
[ "HAVING" , searchCondition ] ];
orderedQuery = query , "ORDER BY" , column , [ "ASC" | "DESC" ] ,
{ "," , column , [ "ASC" | "DESC" ] };
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 53March 17, 2017
Basic SQL Query Structure ...
searchCondition = [ "NOT" ] , search ,
{ ( "AND" | "OR" ) , [ "NOT" ] , search };
search = ( expr , [ "NOT" ] , "BETWEEN" , expr , "AND" , expr ) |
( expr , [ "NOT" ] , "LIKE" , "'" , ( string | "_" | "%" ) ,
{ string | "_" | "%" } , "'" ) |
( column | ( "(" , expr , ")" ) , "IS" , [ "NOT" ] , "NULL" ) |
( expr , ( "=" | "<>" | ">" | ">=" | "<" | "<=" ) , ( expr |
( [ "SOME" | "ALL" ] , "(" , query , ")" ) ) ) |
( expr , [ "NOT" ] , "IN (" ,
( ( value , { "," , value } ) | query ) , ")" |
( "EXISTS (" , query , ")";
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 54March 17, 2017
WITH Clause
 The WITH clause can be used to improve the readability
by introducing temporary new relations
 introduced only in SQL:1999 and not supported by all databases
 Example
 "Find all customers who bought one of the most expensive CDs"
WITH Expensive(price) AS
SELECT MAX(price)
FROM CD
SELECT Customer.name
FROM Customer, CD, Order
WHERE CD.price = Expensive.price AND CD.cdID = Order.cdID AND
Order.customerID = Customer.customerID;
name
Albert Einstein
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 55March 17, 2017
Views
 New virtual relations (views) can be defined on top of an
existing logical model
 simplify queries
 provide access to only parts of the logical model (security)
 computed by executing the query whenever the view is used
 Some DBMS allow views to be stored (materialised
views)
 materialised views have to be updated when its relations change
(view maintenance)
createView = "CREATE VIEW" , table ,
[ "(" , column , { "," , column } , ")" ] ,
"AS" , query;
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 56March 17, 2017
Views
 Example
 Note that a view can be used like any other relation
 Views are useful for queries but they present a serious
problem for UPDATE, INSERT and DELETE operations
 modifications are difficult to be propagated to the actual relations
 modifications on views are therefore generally not permitted
CREATE VIEW CustomerCD AS
SELECT Customer.customerID, Customer.name, CD.cdID, CD.name AS cdName
FROM Customer, Order, CD
WHERE Customer.customerID = Order.customerID AND
Order.cdID = CD.cdID;
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 57March 17, 2017
Transactions
 A transaction consists of a sequence of query and/or
update statements
 atomic set of statements
 A transaction explicitly starts when an SQL statement is
executed and is ended by
 a COMMIT statement
 a ROLLBACK statement
 In many SQL implementations each SQL statement is a
transaction on its own (automatic commit)
 this default behaviour can be disabled
 SQL:1999 introduced BEGIN ATOMIC ... END blocks
 Transactions will be discussed in detail later
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 58March 17, 2017
Homework
 Study the following chapters of the
Database System Concepts book
 chapter 3
- sections 3.1-3.10
- Introduction to SQL
 chapter 4
- sections 4.1-4.5 and section 4.7
- Intermediate SQL
Beat Signer - Department of Computer Science - bsigner@vub.ac.be 59March 17, 2017
Exercise 5
 Structured Query Language (SQL)

Beat Signer - Department of Computer Science - bsigner@vub.ac.be 60March 17, 2017
References
 A. Silberschatz, H. Korth and S. Sudarshan,
Database System Concepts (Sixth Edition),
McGraw-Hill, 2010
 Donald D. Chamberlin and Raymond F. Boyce,
SEQUEL: A Structured English Query Language,
Proceedings of the 1974 ACM SIGFIDET Workshop on
Data Description, Access and Control (SIGFIDET 1974),
Michigan, USA, May 1974
2 December 2005
Next Lecture
Advanced SQL

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Mysql
MysqlMysql
Mysql
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
SQL commands
SQL commandsSQL commands
SQL commands
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)ppt
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
Introduction to database & sql
Introduction to database & sqlIntroduction to database & sql
Introduction to database & sql
 
Creating a database
Creating a databaseCreating a database
Creating a database
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)Presentation slides of Sequence Query Language (SQL)
Presentation slides of Sequence Query Language (SQL)
 
An Introduction To Oracle Database
An Introduction To Oracle DatabaseAn Introduction To Oracle Database
An Introduction To Oracle Database
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
Types Of Keys in DBMS
Types Of Keys in DBMSTypes Of Keys in DBMS
Types Of Keys in DBMS
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
 
Integrity Constraints
Integrity ConstraintsIntegrity Constraints
Integrity Constraints
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
DBMS Notes: DDL DML DCL
DBMS Notes: DDL DML DCLDBMS Notes: DDL DML DCL
DBMS Notes: DDL DML DCL
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 

Andere mochten auch

Andere mochten auch (9)

DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using Oracle
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
 
Sql a practical_introduction
Sql a practical_introductionSql a practical_introduction
Sql a practical_introduction
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
SQL : introduction
SQL : introductionSQL : introduction
SQL : introduction
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 

Ähnlich wie Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007156ANR)

Lecture05sql 110406195130-phpapp02
Lecture05sql 110406195130-phpapp02Lecture05sql 110406195130-phpapp02
Lecture05sql 110406195130-phpapp02Lalit009kumar
 
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)Beat Signer
 
Build a better UI component library with Styled System
Build a better UI component library with Styled SystemBuild a better UI component library with Styled System
Build a better UI component library with Styled SystemHsin-Hao Tang
 
Structuring Spark: DataFrames, Datasets, and Streaming
Structuring Spark: DataFrames, Datasets, and StreamingStructuring Spark: DataFrames, Datasets, and Streaming
Structuring Spark: DataFrames, Datasets, and StreamingDatabricks
 
ABD304-R-Best Practices for Data Warehousing with Amazon Redshift & Spectrum
ABD304-R-Best Practices for Data Warehousing with Amazon Redshift & SpectrumABD304-R-Best Practices for Data Warehousing with Amazon Redshift & Spectrum
ABD304-R-Best Practices for Data Warehousing with Amazon Redshift & SpectrumAmazon Web Services
 
Squirrel – Enabling Accessible Analytics for All
Squirrel – Enabling Accessible Analytics for AllSquirrel – Enabling Accessible Analytics for All
Squirrel – Enabling Accessible Analytics for AllSudipta Mukherjee
 
NoSQL Databases - Lecture 12 - Introduction to Databases (1007156ANR)
NoSQL Databases - Lecture 12 - Introduction to Databases (1007156ANR)NoSQL Databases - Lecture 12 - Introduction to Databases (1007156ANR)
NoSQL Databases - Lecture 12 - Introduction to Databases (1007156ANR)Beat Signer
 
A Rusty introduction to Apache Arrow and how it applies to a time series dat...
A Rusty introduction to Apache Arrow and how it applies to a  time series dat...A Rusty introduction to Apache Arrow and how it applies to a  time series dat...
A Rusty introduction to Apache Arrow and how it applies to a time series dat...Andrew Lamb
 
SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developersukdpe
 
Structuring Apache Spark 2.0: SQL, DataFrames, Datasets And Streaming - by Mi...
Structuring Apache Spark 2.0: SQL, DataFrames, Datasets And Streaming - by Mi...Structuring Apache Spark 2.0: SQL, DataFrames, Datasets And Streaming - by Mi...
Structuring Apache Spark 2.0: SQL, DataFrames, Datasets And Streaming - by Mi...Databricks
 
2021 04-20 apache arrow and its impact on the database industry.pptx
2021 04-20  apache arrow and its impact on the database industry.pptx2021 04-20  apache arrow and its impact on the database industry.pptx
2021 04-20 apache arrow and its impact on the database industry.pptxAndrew Lamb
 
No SQL, No Problem: Use Azure DocumentDB
No SQL, No Problem: Use Azure DocumentDBNo SQL, No Problem: Use Azure DocumentDB
No SQL, No Problem: Use Azure DocumentDBKen Cenerelli
 
What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?ukdpe
 
SQL Server 2008 Overview
SQL Server 2008 OverviewSQL Server 2008 Overview
SQL Server 2008 OverviewEric Nelson
 
Mimsy XG Discussion Group: Planning for Integrations
Mimsy XG Discussion Group: Planning for IntegrationsMimsy XG Discussion Group: Planning for Integrations
Mimsy XG Discussion Group: Planning for IntegrationsAxiell ALM
 
ICDM2019 table tutorial
ICDM2019 table tutorialICDM2019 table tutorial
ICDM2019 table tutorialNancy Wang
 
Creating Stunning Maps in GeoServer: mastering SLD and CSS styles
Creating Stunning Maps in GeoServer: mastering SLD and CSS stylesCreating Stunning Maps in GeoServer: mastering SLD and CSS styles
Creating Stunning Maps in GeoServer: mastering SLD and CSS stylesGeoSolutions
 

Ähnlich wie Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007156ANR) (20)

Lecture05sql 110406195130-phpapp02
Lecture05sql 110406195130-phpapp02Lecture05sql 110406195130-phpapp02
Lecture05sql 110406195130-phpapp02
 
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
Advanced SQL - Lecture 6 - Introduction to Databases (1007156ANR)
 
Build a better UI component library with Styled System
Build a better UI component library with Styled SystemBuild a better UI component library with Styled System
Build a better UI component library with Styled System
 
Physical Design and Development
Physical Design and DevelopmentPhysical Design and Development
Physical Design and Development
 
Structuring Spark: DataFrames, Datasets, and Streaming
Structuring Spark: DataFrames, Datasets, and StreamingStructuring Spark: DataFrames, Datasets, and Streaming
Structuring Spark: DataFrames, Datasets, and Streaming
 
ABD304-R-Best Practices for Data Warehousing with Amazon Redshift & Spectrum
ABD304-R-Best Practices for Data Warehousing with Amazon Redshift & SpectrumABD304-R-Best Practices for Data Warehousing with Amazon Redshift & Spectrum
ABD304-R-Best Practices for Data Warehousing with Amazon Redshift & Spectrum
 
Squirrel – Enabling Accessible Analytics for All
Squirrel – Enabling Accessible Analytics for AllSquirrel – Enabling Accessible Analytics for All
Squirrel – Enabling Accessible Analytics for All
 
NoSQL Databases - Lecture 12 - Introduction to Databases (1007156ANR)
NoSQL Databases - Lecture 12 - Introduction to Databases (1007156ANR)NoSQL Databases - Lecture 12 - Introduction to Databases (1007156ANR)
NoSQL Databases - Lecture 12 - Introduction to Databases (1007156ANR)
 
A Rusty introduction to Apache Arrow and how it applies to a time series dat...
A Rusty introduction to Apache Arrow and how it applies to a  time series dat...A Rusty introduction to Apache Arrow and how it applies to a  time series dat...
A Rusty introduction to Apache Arrow and how it applies to a time series dat...
 
SQL Server 2008 for Developers
SQL Server 2008 for DevelopersSQL Server 2008 for Developers
SQL Server 2008 for Developers
 
Structuring Apache Spark 2.0: SQL, DataFrames, Datasets And Streaming - by Mi...
Structuring Apache Spark 2.0: SQL, DataFrames, Datasets And Streaming - by Mi...Structuring Apache Spark 2.0: SQL, DataFrames, Datasets And Streaming - by Mi...
Structuring Apache Spark 2.0: SQL, DataFrames, Datasets And Streaming - by Mi...
 
2021 04-20 apache arrow and its impact on the database industry.pptx
2021 04-20  apache arrow and its impact on the database industry.pptx2021 04-20  apache arrow and its impact on the database industry.pptx
2021 04-20 apache arrow and its impact on the database industry.pptx
 
"If I knew then what I know now"
"If I knew then what I know now""If I knew then what I know now"
"If I knew then what I know now"
 
No SQL, No Problem: Use Azure DocumentDB
No SQL, No Problem: Use Azure DocumentDBNo SQL, No Problem: Use Azure DocumentDB
No SQL, No Problem: Use Azure DocumentDB
 
Presentation
PresentationPresentation
Presentation
 
What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?What's New for Developers in SQL Server 2008?
What's New for Developers in SQL Server 2008?
 
SQL Server 2008 Overview
SQL Server 2008 OverviewSQL Server 2008 Overview
SQL Server 2008 Overview
 
Mimsy XG Discussion Group: Planning for Integrations
Mimsy XG Discussion Group: Planning for IntegrationsMimsy XG Discussion Group: Planning for Integrations
Mimsy XG Discussion Group: Planning for Integrations
 
ICDM2019 table tutorial
ICDM2019 table tutorialICDM2019 table tutorial
ICDM2019 table tutorial
 
Creating Stunning Maps in GeoServer: mastering SLD and CSS styles
Creating Stunning Maps in GeoServer: mastering SLD and CSS stylesCreating Stunning Maps in GeoServer: mastering SLD and CSS styles
Creating Stunning Maps in GeoServer: mastering SLD and CSS styles
 

Mehr von Beat Signer

Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)
Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)
Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)Beat Signer
 
Indoor Positioning Using the OpenHPS Framework
Indoor Positioning Using the OpenHPS FrameworkIndoor Positioning Using the OpenHPS Framework
Indoor Positioning Using the OpenHPS FrameworkBeat Signer
 
Personalised Learning Environments Based on Knowledge Graphs and the Zone of ...
Personalised Learning Environments Based on Knowledge Graphs and the Zone of ...Personalised Learning Environments Based on Knowledge Graphs and the Zone of ...
Personalised Learning Environments Based on Knowledge Graphs and the Zone of ...Beat Signer
 
Cross-Media Technologies and Applications - Future Directions for Personal In...
Cross-Media Technologies and Applications - Future Directions for Personal In...Cross-Media Technologies and Applications - Future Directions for Personal In...
Cross-Media Technologies and Applications - Future Directions for Personal In...Beat Signer
 
Bridging the Gap: Managing and Interacting with Information Across Media Boun...
Bridging the Gap: Managing and Interacting with Information Across Media Boun...Bridging the Gap: Managing and Interacting with Information Across Media Boun...
Bridging the Gap: Managing and Interacting with Information Across Media Boun...Beat Signer
 
Codeschool in a Box: A Low-Barrier Approach to Packaging Programming Curricula
Codeschool in a Box: A Low-Barrier Approach to Packaging Programming CurriculaCodeschool in a Box: A Low-Barrier Approach to Packaging Programming Curricula
Codeschool in a Box: A Low-Barrier Approach to Packaging Programming CurriculaBeat Signer
 
The RSL Hypermedia Metamodel and Its Application in Cross-Media Solutions
The RSL Hypermedia Metamodel and Its Application in Cross-Media Solutions The RSL Hypermedia Metamodel and Its Application in Cross-Media Solutions
The RSL Hypermedia Metamodel and Its Application in Cross-Media Solutions Beat Signer
 
Case Studies and Course Review - Lecture 12 - Information Visualisation (4019...
Case Studies and Course Review - Lecture 12 - Information Visualisation (4019...Case Studies and Course Review - Lecture 12 - Information Visualisation (4019...
Case Studies and Course Review - Lecture 12 - Information Visualisation (4019...Beat Signer
 
Dashboards - Lecture 11 - Information Visualisation (4019538FNR)
Dashboards - Lecture 11 - Information Visualisation (4019538FNR)Dashboards - Lecture 11 - Information Visualisation (4019538FNR)
Dashboards - Lecture 11 - Information Visualisation (4019538FNR)Beat Signer
 
Interaction - Lecture 10 - Information Visualisation (4019538FNR)
Interaction - Lecture 10 - Information Visualisation (4019538FNR)Interaction - Lecture 10 - Information Visualisation (4019538FNR)
Interaction - Lecture 10 - Information Visualisation (4019538FNR)Beat Signer
 
View Manipulation and Reduction - Lecture 9 - Information Visualisation (4019...
View Manipulation and Reduction - Lecture 9 - Information Visualisation (4019...View Manipulation and Reduction - Lecture 9 - Information Visualisation (4019...
View Manipulation and Reduction - Lecture 9 - Information Visualisation (4019...Beat Signer
 
Visualisation Techniques - Lecture 8 - Information Visualisation (4019538FNR)
Visualisation Techniques - Lecture 8 - Information Visualisation (4019538FNR)Visualisation Techniques - Lecture 8 - Information Visualisation (4019538FNR)
Visualisation Techniques - Lecture 8 - Information Visualisation (4019538FNR)Beat Signer
 
Design Guidelines and Principles - Lecture 7 - Information Visualisation (401...
Design Guidelines and Principles - Lecture 7 - Information Visualisation (401...Design Guidelines and Principles - Lecture 7 - Information Visualisation (401...
Design Guidelines and Principles - Lecture 7 - Information Visualisation (401...Beat Signer
 
Data Processing and Visualisation Frameworks - Lecture 6 - Information Visual...
Data Processing and Visualisation Frameworks - Lecture 6 - Information Visual...Data Processing and Visualisation Frameworks - Lecture 6 - Information Visual...
Data Processing and Visualisation Frameworks - Lecture 6 - Information Visual...Beat Signer
 
Data Presentation - Lecture 5 - Information Visualisation (4019538FNR)
Data Presentation - Lecture 5 - Information Visualisation (4019538FNR)Data Presentation - Lecture 5 - Information Visualisation (4019538FNR)
Data Presentation - Lecture 5 - Information Visualisation (4019538FNR)Beat Signer
 
Analysis and Validation - Lecture 4 - Information Visualisation (4019538FNR)
Analysis and Validation - Lecture 4 - Information Visualisation (4019538FNR)Analysis and Validation - Lecture 4 - Information Visualisation (4019538FNR)
Analysis and Validation - Lecture 4 - Information Visualisation (4019538FNR)Beat Signer
 
Data Representation - Lecture 3 - Information Visualisation (4019538FNR)
Data Representation - Lecture 3 - Information Visualisation (4019538FNR)Data Representation - Lecture 3 - Information Visualisation (4019538FNR)
Data Representation - Lecture 3 - Information Visualisation (4019538FNR)Beat Signer
 
Human Perception and Colour Theory - Lecture 2 - Information Visualisation (4...
Human Perception and Colour Theory - Lecture 2 - Information Visualisation (4...Human Perception and Colour Theory - Lecture 2 - Information Visualisation (4...
Human Perception and Colour Theory - Lecture 2 - Information Visualisation (4...Beat Signer
 
Introduction - Lecture 1 - Information Visualisation (4019538FNR)
Introduction - Lecture 1 - Information Visualisation (4019538FNR)Introduction - Lecture 1 - Information Visualisation (4019538FNR)
Introduction - Lecture 1 - Information Visualisation (4019538FNR)Beat Signer
 
Towards a Framework for Dynamic Data Physicalisation
Towards a Framework for Dynamic Data PhysicalisationTowards a Framework for Dynamic Data Physicalisation
Towards a Framework for Dynamic Data PhysicalisationBeat Signer
 

Mehr von Beat Signer (20)

Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)
Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)
Introduction - Lecture 1 - Human-Computer Interaction (1023841ANR)
 
Indoor Positioning Using the OpenHPS Framework
Indoor Positioning Using the OpenHPS FrameworkIndoor Positioning Using the OpenHPS Framework
Indoor Positioning Using the OpenHPS Framework
 
Personalised Learning Environments Based on Knowledge Graphs and the Zone of ...
Personalised Learning Environments Based on Knowledge Graphs and the Zone of ...Personalised Learning Environments Based on Knowledge Graphs and the Zone of ...
Personalised Learning Environments Based on Knowledge Graphs and the Zone of ...
 
Cross-Media Technologies and Applications - Future Directions for Personal In...
Cross-Media Technologies and Applications - Future Directions for Personal In...Cross-Media Technologies and Applications - Future Directions for Personal In...
Cross-Media Technologies and Applications - Future Directions for Personal In...
 
Bridging the Gap: Managing and Interacting with Information Across Media Boun...
Bridging the Gap: Managing and Interacting with Information Across Media Boun...Bridging the Gap: Managing and Interacting with Information Across Media Boun...
Bridging the Gap: Managing and Interacting with Information Across Media Boun...
 
Codeschool in a Box: A Low-Barrier Approach to Packaging Programming Curricula
Codeschool in a Box: A Low-Barrier Approach to Packaging Programming CurriculaCodeschool in a Box: A Low-Barrier Approach to Packaging Programming Curricula
Codeschool in a Box: A Low-Barrier Approach to Packaging Programming Curricula
 
The RSL Hypermedia Metamodel and Its Application in Cross-Media Solutions
The RSL Hypermedia Metamodel and Its Application in Cross-Media Solutions The RSL Hypermedia Metamodel and Its Application in Cross-Media Solutions
The RSL Hypermedia Metamodel and Its Application in Cross-Media Solutions
 
Case Studies and Course Review - Lecture 12 - Information Visualisation (4019...
Case Studies and Course Review - Lecture 12 - Information Visualisation (4019...Case Studies and Course Review - Lecture 12 - Information Visualisation (4019...
Case Studies and Course Review - Lecture 12 - Information Visualisation (4019...
 
Dashboards - Lecture 11 - Information Visualisation (4019538FNR)
Dashboards - Lecture 11 - Information Visualisation (4019538FNR)Dashboards - Lecture 11 - Information Visualisation (4019538FNR)
Dashboards - Lecture 11 - Information Visualisation (4019538FNR)
 
Interaction - Lecture 10 - Information Visualisation (4019538FNR)
Interaction - Lecture 10 - Information Visualisation (4019538FNR)Interaction - Lecture 10 - Information Visualisation (4019538FNR)
Interaction - Lecture 10 - Information Visualisation (4019538FNR)
 
View Manipulation and Reduction - Lecture 9 - Information Visualisation (4019...
View Manipulation and Reduction - Lecture 9 - Information Visualisation (4019...View Manipulation and Reduction - Lecture 9 - Information Visualisation (4019...
View Manipulation and Reduction - Lecture 9 - Information Visualisation (4019...
 
Visualisation Techniques - Lecture 8 - Information Visualisation (4019538FNR)
Visualisation Techniques - Lecture 8 - Information Visualisation (4019538FNR)Visualisation Techniques - Lecture 8 - Information Visualisation (4019538FNR)
Visualisation Techniques - Lecture 8 - Information Visualisation (4019538FNR)
 
Design Guidelines and Principles - Lecture 7 - Information Visualisation (401...
Design Guidelines and Principles - Lecture 7 - Information Visualisation (401...Design Guidelines and Principles - Lecture 7 - Information Visualisation (401...
Design Guidelines and Principles - Lecture 7 - Information Visualisation (401...
 
Data Processing and Visualisation Frameworks - Lecture 6 - Information Visual...
Data Processing and Visualisation Frameworks - Lecture 6 - Information Visual...Data Processing and Visualisation Frameworks - Lecture 6 - Information Visual...
Data Processing and Visualisation Frameworks - Lecture 6 - Information Visual...
 
Data Presentation - Lecture 5 - Information Visualisation (4019538FNR)
Data Presentation - Lecture 5 - Information Visualisation (4019538FNR)Data Presentation - Lecture 5 - Information Visualisation (4019538FNR)
Data Presentation - Lecture 5 - Information Visualisation (4019538FNR)
 
Analysis and Validation - Lecture 4 - Information Visualisation (4019538FNR)
Analysis and Validation - Lecture 4 - Information Visualisation (4019538FNR)Analysis and Validation - Lecture 4 - Information Visualisation (4019538FNR)
Analysis and Validation - Lecture 4 - Information Visualisation (4019538FNR)
 
Data Representation - Lecture 3 - Information Visualisation (4019538FNR)
Data Representation - Lecture 3 - Information Visualisation (4019538FNR)Data Representation - Lecture 3 - Information Visualisation (4019538FNR)
Data Representation - Lecture 3 - Information Visualisation (4019538FNR)
 
Human Perception and Colour Theory - Lecture 2 - Information Visualisation (4...
Human Perception and Colour Theory - Lecture 2 - Information Visualisation (4...Human Perception and Colour Theory - Lecture 2 - Information Visualisation (4...
Human Perception and Colour Theory - Lecture 2 - Information Visualisation (4...
 
Introduction - Lecture 1 - Information Visualisation (4019538FNR)
Introduction - Lecture 1 - Information Visualisation (4019538FNR)Introduction - Lecture 1 - Information Visualisation (4019538FNR)
Introduction - Lecture 1 - Information Visualisation (4019538FNR)
 
Towards a Framework for Dynamic Data Physicalisation
Towards a Framework for Dynamic Data PhysicalisationTowards a Framework for Dynamic Data Physicalisation
Towards a Framework for Dynamic Data Physicalisation
 

Kürzlich hochgeladen

How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 

Kürzlich hochgeladen (20)

How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 

Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007156ANR)

  • 1. 2 December 2005 Introduction to Databases Structured Query Language Prof. Beat Signer Department of Computer Science Vrije Universiteit Brussel http://www.beatsigner.com
  • 2. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 2March 17, 2017 Context of Today's Lecture Access Methods System Buffers Authorisation Control Integrity Checker Command Processor Program Object Code DDL Compiler File Manager Buffer Manager Recovery Manager Scheduler Query Optimiser Transaction Manager Query Compiler Queries Catalogue Manager DML Preprocessor Database Schema Application Programs Database and System Catalogue Database Manager Data Manager DBMS Programmers Users DB Admins Based on 'Components of a DBMS', Database Systems, T. Connolly and C. Begg, Addison-Wesley 2010
  • 3. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 3March 17, 2017 Structured Query Language (SQL)  Declarative query language to create database schemas, insert, update, delete and query information based on a data definition and data manipulation language  Data definition language (DDL)  definition of database structure (relation schemas)  data access control  Data manipulation language (DML)  query language to create, read, update and delete tuples (CRUD operations)
  • 4. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 4March 17, 2017 Structured Query Language (SQL) ...  The SQL language further deals with the following issues  transaction control  integrity constraints (DDL)  auhorisation (DDL)  views (DDL)  embedded SQL and dynamic SQL
  • 5. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 5March 17, 2017  SEQUEL (70's)  structured english query language  developed by Raymond F. Boyce and Donald D. Chamberlin  access data stored in IBM's System R relational database  SQL-86  first ANSI standard version  SQL-89 / SQL 1  SQL-92 / SQL 2  we will mainly discuss features of the SQL-92 standard History of SQL Donald D. Chamberlin Raymond F. Boyce
  • 6. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 6March 17, 2017 History of SQL ...  SQL:1999 / SQL 3  recursive queries, triggers, object-oriented features, ...  SQL:2003  window functions, XML-related features, ...  SQL:2006  XML Query Language (XQuery) support, ...  SQL:2008  SQL:2011  improved support for temporal databases
  • 7. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 7March 17, 2017 SQL "Standard"  Each specific SQL implementation by a database vendor is called a dialect  The vendors implement parts of the SQL standard (e.g. most implement SQL-92) but add their vendor- specific extensions  Most relational database vendors conform to a set of Core SQL features but portability might still be limited due to missing or additional features
  • 8. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 8March 17, 2017 Data Definition Language (DDL)  The data definition language (DDL) is used to specify the relation schemas as well as other information about the relations  relation schemas  attribute domain types  integrity constraints  relation indexes  access information  physical storage structure of relations
  • 9. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 9March 17, 2017 Database Creation  The concrete process of creating a new database might differ for different relational database products  According to the SQL standard, an SQL environment contains one or more catalogues  Each catalogue manages various metadata  set of schemas consisting of - relations/tables - views - assertions - indexes  SET SCHEMA name can be used to set the current schema  users and user groups environment catalogue catalogue schema schema schema
  • 10. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 10March 17, 2017 Database Creation ...  The creation of catalogues is not covered by the SQL standard and therefore implementation specific  Schemas can be created and deleted via the CREATE and DROP statements  The default parameter of the DROP SCHEMA statement is RESTRICT  only empty schema can be deleted  If CASCADE is specified, all objects associated with the schema will be dropped createSchema = "CREATE SCHEMA" , name , "AUTHORIZATION" , creator , [ ddlStatements ]; dropSchema = "DROP SCHEMA" , name , [ "RESTRICT" | "CASCADE" ];
  • 11. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 11March 17, 2017 Extended Backus-Naur Form (EBNF)  Notation to describe computer program- ming languages (context-free grammars)  developed by Niklaus Wirth Notation Meaning = Definition , Sequence ; Termination | Choice [...] Option {...} Repetition (...) Grouping "..." Terminal String Niklaus Wirth  We use the EBNF to describe different SQL concepts http://en.wikipedia.org/wiki/Extended_Backus-Naur_Form
  • 12. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 12March 17, 2017 Relational Database Example customerID name street postcode city 1 Max Frisch Bahnhofstrasse 7 8001 Zurich 2 Eddy Merckx Pleinlaan 25 1050 Brussels 5 Claude Debussy 12 Rue Louise 75008 Paris 53 Albert Einstein Bergstrasse 18 8037 Zurich 8 Max Frisch ETH Zentrum 8092 Zurich cdID name duration price year 1 Falling into Place 2007 17.90 2007 2 Carcassonne 3156 15.50 1993 3 Chromatic 3012 16.50 1993 customer cd
  • 13. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 13March 17, 2017 Relational Database Example ... supplierID name postcode city 5 Max Frisch 8037 Zurich 2 Mario Botta 6901 Lugano orderID customerID cdID date amount status 1 53 2 13.02.2010 2 open 2 2 1 15.02.2010 1 delivered order supplier Customer (customerID, name, street, postcode, city) CD (cdID, name, duration, price, year) Order (orderId, customerID, cdID, date, amount, status) Supplier (supplierID, name, postcode, city) relational database schema
  • 14. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 14March 17, 2017 Table Definition Example CREATE TABLE Customer ( customerID INTEGER CHECK (customerID > 0) PRIMARY KEY, name VARCHAR(30) NOT NULL, street VARCHAR(30) NOT NULL, postcode SMALLINT CHECK (postcode > 0), city VARCHAR(20) ); CREATE TABLE CD ( cdID INTEGER PRIMARY KEY, name VARCHAR(30) NOT NULL, duration TIME, price NUMERIC(6,2), year SMALLINT );
  • 15. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 15March 17, 2017 Table Definition Example ... CREATE TABLE Supplier ( supplierID INTEGER PRIMARY KEY, name VARCHAR(30) NOT NULL, postcode SMALLINT CHECK (postcode > 0), city VARCHAR(20) ); CREATE TABLE Order ( orderID INTEGER CHECK (orderID > 0) PRIMARY KEY, customerID INTEGER, cdID INTEGER , date DATE, amount INTEGER, Status VARCHAR(20) NOT NULL DEFAULT 'open', UNIQUE (customerID, cdID, date), FOREIGN KEY (customerID) REFERENCES Customer(customerID) ON UPDATE CASCADE ON DELETE SET NULL, FOREIGN KEY (cdID) REFERENCES CD(cdID) ON UPDATE CASCADE );
  • 16. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 16March 17, 2017 Table Constraints  We can have only one PRIMARY KEY constraint but multiple UNIQUE constraints  if no primary key is defined, duplicates are allowed (bag)  Referential integrity  a foreign key always has to have a matching value in the referenced table (or it can be null)  different referential actions can be defined for update (ON UPDATE) and delete (ON DELETE) operations on the referenced candidate key - CASCADE: propagate operations to the foreign keys which might lead to further cascaded operations - SET DEFAULT: set the foreign keys to their default value - SET NULL: set the foreign keys to NULL - NO ACTION: the operation on the candidate key will be rejected (default)
  • 17. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 17March 17, 2017 Table Definition createTable = "CREATE TABLE" , table , "(" , ( columnElement | tableConstraint ) , { "," , ( columnElement | tableConstraint ) } , ")"; columnElement = column , datatype , [ "DEFAULT" , ( value | "NULL" ) ] , { columnConstraint }; columnConstraint = "NOT NULL" | "UNIQUE" | "PRIMARY KEY" | ( "REFERENCES" , table , [ "(" , column , ")" ] , { referentialAction } ) | ( "CHECK (" , searchCondition , ")" ); tableConstraint = ( ( "UNIQUE" | "PRIMARY KEY ) , "(" , column , { "," , column } , ")" ) | ( "FOREIGN KEY (" , column , { "," , column } , ")" , "REFERENCES" , table , [ "(" , column , { "," , column } , ")" ] , { referentialAction } ) | ( "CHECK (" , searchCondition , ")" );
  • 18. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 18March 17, 2017 Table Definition ... referentialAction = ( "ON UPDATE" | "ON DELETE" ) , ( "CASCADE" | "SET DEFAULT" | "SET NULL" | "NO ACTION" );
  • 19. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 19March 17, 2017 SQL Datatypes  Character data  fixed-length or variable-length sequence of characters  optional multibyte character sets (e.g. for Japanese etc.)  Large character data or binary data  often a so-called locator is returned to access a large object in pieces instead of loading the entire object into memory char = fixedChar | varyingChar [charSet]; fixedChar = "CHAR" , [ "(" , length , ")" ]; varyingChar = "VARCHAR" , [ "(" , maxLength , ")" ]; charSet = "CHARACTER SET" charSetName; lob = clob | blob; clob = "CLOB" , [ "(" , size , ")" ]; blob = "BLOB" , [ "(" , size , ")" ];
  • 20. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 20March 17, 2017 SQL Datatypes ...  Numeric data  The DECIMAL datatype is sometimes used as a synonym for the NUMERIC datatype numeric = decimal | int | smallInt | float | real | double; decimal = "DECIMAL" , [ "(" , precision , [ "," , scale ] , ")" ]; int = "INTEGER"; smallInt = "SMALLINT"; float = "FLOAT" , [ "(" , precision , ")" ]; real = "REAL"; double = "DOUBLE PRECISION";
  • 21. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 21March 17, 2017 SQL Datatypes ...  Datetime data  Format of the datetime values  date: YYYY-MM-DD  time: hh:mm:ss.p ± hh:mm  timestamp: YYYY-MM-DD hh:mm:ss.p ± hh:mm datetime = date | time | timestamp; date = "DATE"; time = "TIME" , [ "(" , precision , ")" ] , [ "WITH TIME ZONE" , timezone ]; timestamp = "TIMESTAMP" , [ "(" , precision , ")" ] , [ "WITH TIME ZONE" , timezone ];
  • 22. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 22March 17, 2017 SQL Datatypes ...  Boolean  the domain of boolean values consist of the two truth values TRUE and FALSE  a thrid UNKNOWN truth value is used to represent NULL values  introduced in SQL:1999  Bit data  fixed or varying sequence of binary digits (0 or 1) boolean = "BOOLEAN"; bit = fixedBit | varyingBit; fixedBit = "BIT" , [ "(" , length , ")" ]; varyingBit = "BIT VARYING" , [ "(" , maxLength , ")" ];
  • 23. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 23March 17, 2017 SQL Datatypes ...  For further details about the presented datatypes as well as information about vendor-specific datatypes one has to consult the specific database manuals datatype = char | lob | numeric | datetime | boolean | bit;
  • 24. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 24March 17, 2017 Data Manipulation  After a table has been created, we can use the INSERT command to add tuples  unspecified attribute values are set to the default value or NULL  attribute order can be changed via optional column names  "bulk loader" utilities to insert large amounts of tuples  Example INSERT INTO Customer VALUES(8,'Max Frisch','ETH Zentrum', 8001, 'Zurich'); insert = "INSERT INTO" , table , [ "(" , column , { "," , column } , ")" ] , ( "VALUES (" , expr , { "," , expr } , ")" ) | ( "(" , query , ")" );
  • 25. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 25March 17, 2017 Expressions expr = exprElement { ( "+" | "-" | "*" | "/" ) , exprElement }; exprElement = column | value | "COUNT" , "(" ( "*" | ( [ "ALL" | "DISTINCT" ] , column ) , ")" | ( "MIN" | "MAX" ) , "(" , expr , ")" | ( "SUM" | "AVG" ) , "(" , [ "DISTINCT" ] , expr , ")";
  • 26. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 26March 17, 2017 Data Manipulation ...  The DELETE statement can be used to delete tuples  Tuples can be updated via the UPDATE statement  Example UPDATE Customer SET name = 'Walter Faber' WHERE customerID = 8; update = "UPDATE" , table , "SET" , column , "=" , ( "NULL" | expr | "(" , query , ")" ) , { "," , column , "=" , ("NULL" | expr | "(" , query , ")" ) } , [ "WHERE" , searchCondition ]; delete = "DELETE FROM" , table [ "WHERE" , searchCondition ];
  • 27. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 27March 17, 2017 Data Manipulation ...  The DROP TABLE statement can be used to delete a relation from the database  A relation schema can be modified via the ALTER TABLE command  existing tuples are assigned a NULL value for the new attribute  Example alterTable = "ALTER TABLE" , table , "ADD" , ( columnElement | columnConstraint ); ALTER TABLE Customer ADD birthdate DATE; dropTable = "DROP TABLE" , table;
  • 28. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 28March 17, 2017 Basic SQL Query Structure  A basic SQL query consists of a SELECT, a FROM and a WHERE clause  SELECT - specifies the columns to appear in the result (projection in relational algebra)  FROM - specifies the relations to be used (cartesian product in relational algebra)  WHERE - filters the tuples (selection in relational algebra) - join conditions are explicitly specified in the WHERE clause  GROUP BY - groups rows with the same column values - the HAVING construct can be used to further filter the groups  ORDER BY - defines the order of the resulting tuples
  • 29. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 29March 17, 2017 Basic SQL Query Structure ...  In general, the SELECT FROM WHERE parts are evaluated as follows 1. generate a cartesian product of the relations listed in the FROM clause 2. apply the predicates specified in the WHERE clause on the result of the first step 3. for each tuple in the result of the second step output the attri- butes (or results of expressions) specified in the SELECT clause  The evaluation is normally optimised by a query optimiser
  • 30. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 30March 17, 2017 Basic SQL Query Structure ...  The order of clauses in an SQL query cannot be changed  Note that the SELECT is equivalent to a relational algebra projection  In contrast to the relational algebra, SQL does not eliminate duplicates automatically  the automatic elimination of duplicates would be time consuming  user has to eliminate duplicates explicitly via DISTINCT keyword SELECT A1, A2,..., An FROM r1, r2,..., rm WHERE P pA1,A2,...,An (sP(r1  r2  ...  rm) is equivalent to
  • 31. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 31March 17, 2017 SELECT Clause  A '*' can be used in the SELECT clause as a shortcut to get all tuple attributes SELECT * FROM Customer; customerID name street postcode city 1 Max Frisch Bahnhofstrasse 7 8001 Zurich 2 Eddy Merckx Pleinlaan 25 1050 Brussels 5 Claude Debussy 12 Rue Louise 75008 Paris 53 Albert Einstein Bergstrasse 18 8037 Zurich 8 Max Frisch ETH Zentrum 8092 Zurich
  • 32. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 32March 17, 2017 SELECT Clause ...  Duplicate tuples resulting from a projection to specific attributes are not eliminated by default SELECT name FROM Customer; name Max Frisch Eddy Merckx Claude Debussy Albert Einstein Max Frisch
  • 33. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 33March 17, 2017 SELECT Clause ...  The DISTINCT keyword can be used to eliminate duplicates SELECT DISTINCT name FROM Customer; name Max Frisch Eddy Merckx Claude Debussy Albert Einstein
  • 34. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 34March 17, 2017 Computed Attributes and Rename  Computations can be performed in the SELECT clause  multiple numeric attributes can be used in a computation  The rename operation (AS) is used to rename relations as well as attributes  computed columns have no name by default  also used when multiple relations have the same attribute names SELECT name, price * 1.5 AS newPrice FROM CD; name newPrice Falling into Place 26.85 Carcassonne 23.20 Chromatic 24.75
  • 35. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 35March 17, 2017 WHERE Clause  In the WHERE clause we can use five basic predicates (search conditions)  comparison - compare two expressions  range - check whether the value is within a specified range of values (BETWEEN)  set membership - check whether the value is equal to a value of a given set (IN)  pattern matching - test whether the expression matches a specifies string pattern (LIKE)  check for NULL values - check whether the expression is a NULL value (IS NULL)
  • 36. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 36March 17, 2017 WHERE Clause ... SELECT name, postcode FROM Customer WHERE city = 'Zurich' AND postcode >= 8040; name postcode Max Frisch 8092 SELECT name, price FROM CD WHERE price BETWEEN 15.0 AND 17.0; name price Carcassonne 15.50 Chromatic 16.50
  • 37. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 37March 17, 2017 WHERE Clause ...  Check for set membership with the IN construct SELECT * FROM Customer WHERE city IN ('Zurich', 'Brussels'); customerID name street postcode city 1 Max Frisch Bahnhofstrasse 7 8001 Zurich 2 Eddy Merckx Pleinlaan 25 1050 Brussels 53 Albert Einstein Bergstrasse 18 8037 Zurich 8 Max Frisch ETH Zentrum 8092 Zurich
  • 38. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 38March 17, 2017 Pattern Matching  Strings are enclosed in single quotes  use a double single quote for escaping  The LIKE operator is used for pattern matching  the underscore (_) is a placeholder for a single character  the percent sign (%) is a placeholder for any substring  e.g. LIKE '_e%' name Albert Einstein SELECT DISTINCT name FROM Customer WHERE name LIKE '%Ein%';
  • 39. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 39March 17, 2017 Null Values  Missing (unknown) info is represented by NULL values  result of any comparison involving a NULL value is Unknown  three-valued logic (3VL) based on True, False and Unknown True False Unknown True True False Unknown False False False False Unknown Unknown False Unknown AND True False Unknown True True True True False True False Unknown Unknown True Unknown Unknown OR = True False Unknown True True False Unknown False False True Unknown Unknown Unknown Unknown Unknown NOT True False Unknown False True Unknown
  • 40. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 40March 17, 2017 Null Values ...  The NULL keyword can also be used in predicates to check for null values  Note that a check for NULL is not the same as a check for the empty String '' SELECT * FROM CD WHERE price IS NOT NULL; cdID name duration price year 1 Falling into Place 2007 17.90 2007 2 Carcassonne 3156 15.50 1993 3 Chromatic 3012 16.50 1993
  • 41. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 41March 17, 2017 FROM Clause  The FROM clause creates a cartesian product of multiple relations and can be used to specify join operations  In a previous lecture we have seen the following relational algebra expression - "list the name and street of customers whose order is still open" - pname, street(sstatus="open"(order ⋈ customer)) - the same can be achieved in SQL by explicitly specifying the matching attributes SELECT name, street FROM Customer, Order WHERE Order.customerID = Customer.customerID AND status = 'open'; name street Albert Einstein Bergstrasse 18
  • 42. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 42March 17, 2017 Inner and Outer Joins  Note that there exist SQL extensions to perform join operations between two relations R and S in the FROM clause  Inner Joins  Outer Joins SELECT * FROM R NATURAL JOIN S; SELECT * FROM R CROSS JOIN S; SELECT * FROM R JOIN S ON R.A > S.B; SELECT * FROM R LEFT OUTER JOIN S ON R.A = S.B; SELECT * FROM R RIGHT OUTER JOIN S ON R.A = S.B; SELECT * FROM R FULL OUTER JOIN S ON R.A = S.B;
  • 43. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 43March 17, 2017 Correlation Variable  A correlation variable can be used as an alias for a table  Example  "Find all pairs of CDs that were produced in the same year" SELECT c1.name AS name1, c2.name AS name2 FROM CD c1, CD c2 WHERE c1.year = c2.year AND c1.cdID < c2.cdID; name1 name2 Carcassonne Chromatic
  • 44. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 44March 17, 2017 Sorting  The ORDER BY clause can be used to arrange the result tuples in acending (ASC) or descending (DESC) order  multiple sort keys can be specified; highest priority first  tuples with NULL values are either before or after non-NULL tuples SELECT name, street, city FROM Customer ORDER BY city ASC, name DESC; name street city Eddy Merckx Pleinlaan 25 Brussels Claude Debussy 12 Rue Louise Paris Max Frisch ETH Zentrum Zurich Max Frisch Bahnhofstrasse 7 Zurich Albert Einstein Bergstrasse 18 Zurich
  • 45. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 45March 17, 2017 Set Operations  The UNION, INTERSECT and EXCEPT operations correspond to the , and - relational algebra operations  the relations have to be compatible (same attributes)  these operations remove duplicates by default - the ALL keyword has to be used to retain duplicates (SELECT name FROM Customer) INTERSECT (SELECT name FROM Supplier); name Max Frisch
  • 46. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 46March 17, 2017 Aggregate Functions and Grouping  In SQL there are five aggregate functions (MIN, MAX, AVG, SUM and COUNT) that take a set or multiset of values as input and return a single value  Example  "Find the number of customers in each city"  Aggregate functions (except COUNT(*)) ignore NULL values in the input set  input set might be empty in which case NULL is returned SELECT city, COUNT(customerID) AS number FROM Customer GROUP BY city; city number Zurich 3 Brussels 1 Paris 1
  • 47. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 47March 17, 2017 Subqueries  A subquery is a SELECT FROM WHERE expression that is nested within another query  e.g. via check for set membership (IN or NOT IN)  Example  "Find all the suppliers who are no customers" SELECT DISTINCT name FROM Supplier WHERE name NOT IN (SELECT name FROM Customer); name Mario Botta
  • 48. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 48March 17, 2017 Nested Subqueries ...  Example  "Find all CDs with a price smaller than average" SELECT * FROM CD WHERE price < (SELECT AVG(price) FROM CD); cdID name duration price year 2 Carcassonne 3156 15.50 1993 3 Chromatic 3012 16.50 1993
  • 49. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 49March 17, 2017 Set Comparison  For nested queries with conditions like "greater than at least one" we can use these set comparison operators  > SOME, >= SOME, < SOME, <= SOME, = SOME, <> SOME as well as the same combination with ALL  Example  "Find the customers with a postcode greater than all supplier postcodes" SELECT name ,postcode FROM Customer WHERE postcode > ALL (SELECT postcode FROM Supplier); name postcode Claude Debussy 75008 Max Frisch 8092
  • 50. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 50March 17, 2017 Existence Test  The EXISTS operator can be used to check if a tuple exists in a subquery  Example SELECT name FROM Customer WHERE EXISTS (SELECT * FROM Supplier WHERE Supplier.name = Customer.name); name Max Frisch
  • 51. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 51March 17, 2017 Derived Relations  A subquery expression can also be used in the FROM clause  in this case, a name has to be given to the relation  Example  "Find the number of customers in the city with the most customers" SELECT MAX(noCustomers) AS max FROM (SELECT city, COUNT(customerID) FROM Customer GROUP BY city) AS CityTotal(city, noCustomers); max 3
  • 52. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 52March 17, 2017 Basic SQL Query Structure  The query statement can be used to retrieve information from one or multiple database tables  can perform the relational algebra's selection, projection and join operation in a single SELECT FROM WHERE command query = select { ("UNION" | "INTERSECT" | "EXCEPT") , [ "ALL" ] , select}; select = "SELECT" [ "ALL" | "DISTINCT" ] , ("*" | ( expr , [ "AS" , newName ] , { "," , expr , [ "AS" , newName ] } ) , "FROM" , table , [ correlationVar ] , { "," , table , [ correlationVar ] } , [ "WHERE" , searchCondition ] , [ "GROUP BY" , column , { "," , column } , [ "HAVING" , searchCondition ] ]; orderedQuery = query , "ORDER BY" , column , [ "ASC" | "DESC" ] , { "," , column , [ "ASC" | "DESC" ] };
  • 53. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 53March 17, 2017 Basic SQL Query Structure ... searchCondition = [ "NOT" ] , search , { ( "AND" | "OR" ) , [ "NOT" ] , search }; search = ( expr , [ "NOT" ] , "BETWEEN" , expr , "AND" , expr ) | ( expr , [ "NOT" ] , "LIKE" , "'" , ( string | "_" | "%" ) , { string | "_" | "%" } , "'" ) | ( column | ( "(" , expr , ")" ) , "IS" , [ "NOT" ] , "NULL" ) | ( expr , ( "=" | "<>" | ">" | ">=" | "<" | "<=" ) , ( expr | ( [ "SOME" | "ALL" ] , "(" , query , ")" ) ) ) | ( expr , [ "NOT" ] , "IN (" , ( ( value , { "," , value } ) | query ) , ")" | ( "EXISTS (" , query , ")";
  • 54. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 54March 17, 2017 WITH Clause  The WITH clause can be used to improve the readability by introducing temporary new relations  introduced only in SQL:1999 and not supported by all databases  Example  "Find all customers who bought one of the most expensive CDs" WITH Expensive(price) AS SELECT MAX(price) FROM CD SELECT Customer.name FROM Customer, CD, Order WHERE CD.price = Expensive.price AND CD.cdID = Order.cdID AND Order.customerID = Customer.customerID; name Albert Einstein
  • 55. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 55March 17, 2017 Views  New virtual relations (views) can be defined on top of an existing logical model  simplify queries  provide access to only parts of the logical model (security)  computed by executing the query whenever the view is used  Some DBMS allow views to be stored (materialised views)  materialised views have to be updated when its relations change (view maintenance) createView = "CREATE VIEW" , table , [ "(" , column , { "," , column } , ")" ] , "AS" , query;
  • 56. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 56March 17, 2017 Views  Example  Note that a view can be used like any other relation  Views are useful for queries but they present a serious problem for UPDATE, INSERT and DELETE operations  modifications are difficult to be propagated to the actual relations  modifications on views are therefore generally not permitted CREATE VIEW CustomerCD AS SELECT Customer.customerID, Customer.name, CD.cdID, CD.name AS cdName FROM Customer, Order, CD WHERE Customer.customerID = Order.customerID AND Order.cdID = CD.cdID;
  • 57. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 57March 17, 2017 Transactions  A transaction consists of a sequence of query and/or update statements  atomic set of statements  A transaction explicitly starts when an SQL statement is executed and is ended by  a COMMIT statement  a ROLLBACK statement  In many SQL implementations each SQL statement is a transaction on its own (automatic commit)  this default behaviour can be disabled  SQL:1999 introduced BEGIN ATOMIC ... END blocks  Transactions will be discussed in detail later
  • 58. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 58March 17, 2017 Homework  Study the following chapters of the Database System Concepts book  chapter 3 - sections 3.1-3.10 - Introduction to SQL  chapter 4 - sections 4.1-4.5 and section 4.7 - Intermediate SQL
  • 59. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 59March 17, 2017 Exercise 5  Structured Query Language (SQL) 
  • 60. Beat Signer - Department of Computer Science - bsigner@vub.ac.be 60March 17, 2017 References  A. Silberschatz, H. Korth and S. Sudarshan, Database System Concepts (Sixth Edition), McGraw-Hill, 2010  Donald D. Chamberlin and Raymond F. Boyce, SEQUEL: A Structured English Query Language, Proceedings of the 1974 ACM SIGFIDET Workshop on Data Description, Access and Control (SIGFIDET 1974), Michigan, USA, May 1974
  • 61. 2 December 2005 Next Lecture Advanced SQL