Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Basic SQL
Fall 2014
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Announcements
Assignment 1
Due Monday October 6th, 2014
Solutions will be posted on midnight
Lecture 4 Exercise
Solutions posted LEARN
Content
Lecture 4
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Review
Partial Schema
Example of a partial schema:
can be read as
A Students relation consists of the columns (attributes)
named: sid, name, login, age, and gpa
The column (or attribute) named sid has a domain named
string. The set of values associated with domain string is
the set of all character strings
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Review
Rows or Tuples
Rows (or tuples):
Each row (tuple) has the same number of columns (or attributes) as the relation
schema
Review
relational database state/schemas
What is a relational database state?
a collection of relation states with distinct relation names
m is the number of relations
DB = {r1, r2, ..., rm}
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Review
Preserving quality of database
A database is only good as the data stored in it
DBMS must prevent the entry of incorrect data to preserve
the quality of the information stored
How?
Using integrity constraints (ICs):
Review
Referential Constraints
What is
Sometimes data stored in one relation is linked to data
stored in another relation
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Review
Terminologies
A relation is a table (logical) with columns and rows.
An attribute is a named column of a relation.
A domain is a set of allowable values for one or more
attributes.
A tuple is a row of a relation.
Degree is a number of attributes in a relation.
Cardinality is a number of tuples in a relation.
Relational Database is a collection of relations.
Basic SQL
10
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Basic SQL
Ideally, database language should allow users to:
create the database and relation structures;
perform insertion, modification, deletion of data from relations;
perform simple and complex queries
It must be portable
11
Basic SQL
Structured Query Language (SQL)
SQL is a comprehensive database language
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
12
Basic SQL
SQL is easy to learn
it is non-procedural
you specify what information you require, rather than how to get it
In other words, SQL does not require you to specify the access methods to
the data
it is essentially free-format
Basic SQL
SQL consists of standard English words
CREATE TABLE staff(
);
id
lname
ssn
VARCHAR(5),
VARCHAR(15),
NUMBER(7,2)
14
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Basic SQL
SQL standards are mainly divided into two parts:
core specification
core SQL: a set of features that a vendor must implement to
claim conformance with the SQL standard
specialized extensions
can be implemented as optional modules to be purchased
independently for specific database applications (i.e. data
mining, multimedia data, etc)
15
Basic SQL
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Basic SQL
Basic SQL
18
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
Example
CREATE SCHEMA statement
CREATE SCHEMA COMPANY AUTHORIZATION Jsmith;
schema name
authorization identifier
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
20
10
21
Attributes
Initial constraints
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
22
11
Example
CREATE TABLE COMPANY.EMPLOYEE ...
23
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
12
25
26
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
13
Or because they refer to a table that has not yet been created
Example: Foreign key Dno in EMPLOYEE refers to
DEPARTMENT table which has not been created yet
Solution
Omit these initial constraints and add them
later using ALTER TABLE statement
27
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
14
29
30
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
15
32
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
16
33
Attribute Defaults
It is possible to specify a default value for an attribute by
appending the clause DEFAULT <value>
34
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
17
35
36
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
18
UNIQUE clause
Specifies alternate (secondary) keys
Example: Dname VARCHAR(15) UNIQUE;
37
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
19
ON DELETE NO ACTION
Specifies that if an attempt is made to delete a row with a key
referenced by foreign keys in existing rows in other tables, an error is
raised and the DELETE statement is rolled back
ON UPDATE NO ACTION
Specifies that if an attempt is made to update a key value in a row
whose key is referenced by foreign keys in existing rows in other
tables, an error is raised and the UPDATE statement is rolled back
39
ON DELETE CASCADE
Specifies that if an attempt is made to delete a row with a key
referenced by foreign keys in existing rows in other tables, all rows
that contain those foreign keys are also deleted
ON UPDATE CASCADE
Specifies that if an attempt is made to update a key value in a row,
where the key value is referenced by foreign keys in existing rows in
other tables, all the values that make up the foreign key are also
updated to the new value specified for the key
40
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
20
ON UPDATE CASCADE
When an AlbumID is updated in Albums, the all matching rows for
AlbumID in Tracks will also have the updated AlbumID
41
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
21
Practice Question(s)
Do exercise 4.8 on page 112 (6th Edition)
44
1992-2014 by Addison Wesley & Pearson Education, Inc., McGraw Hill, Cengage Learning
Slides adapted and modified from Fundamentals of Database Systems (5/6) (Elmasri et al.), Database System
Concepts (5/6) (Silberschatz et al.), Database Systems (Coronel et al.), Database Systems (4/5) (Connolly et al. )
22