CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying...

27
CS 338 The Relational Model 2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and DML • Views Textbook Chapter 3

Transcript of CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying...

Page 1: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-1

The Relational Model

Lecture Topics

• Overview of SQL

• Underlying relational model

• Relational database structure

• SQL DDL and DML

• Views

Textbook

• Chapter 3

Page 2: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-2

Overview of SQL

• Structured Query Language (SQL, sometimes pronounced “sequel”)

• ISO/IEC 9075, international standard for relational database systems

• the standard is evolving:– 1986: SQL1; initial version, 75 pages

– 1989: SQL89; most commercial products conform to this version, with extensions

– 1992: SQL2; three levels of conformity, ~600 pages

– 1999: SQL99 (sometimes called SQL3), ~1200 pages

– 2007: under revision again

Page 3: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-3

• powerful view definition language• integrity constraints in conceptual

schema• DML can be embedded in various

programming languages (called “embedded SQL”), or used via programming libraries– object/class libraries in OO environments

• transaction control• authorization sublanguage/model

...continued

Main features:

Page 4: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-4

DDBBMMSS

Application 1 Application 2 Application 3

View A View B

Conceptual Level

Physical Schema

Database

SQL

...continued

Page 5: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-5

Underlying relational model

Example relational database for a credit card company

VendorVno Vname City Vbal

1234

SearsKmartEssoEsso

TorontoOttawaMontrealWaterloo

200.00671.05 0.00 2.25

CustomerAccNum Cname Prov Cbal Climit

101102103

SmithJonesMartin

OntBCQue

25.152014.00 150.00

200025001000

TransactionTno Vno AccNum Tdate Amount10011002100310041005

22344

101103101102103

070115070116070115070120070125

13.2519.0025.0016.1333.12

Page 6: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-6

Structure of a relational database

Database: collection of uniquely named tables (relations)

Relation: set of rows (tuples)

Attribute: column

Domain: set of allowed values for an attribute

Attribute values must be atomic (single values): no tuples or sets or repetition

Row: distinguishable thing

Table: set of related things

Page 7: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-7

Diagrammatic conventions

VendorVno Vname City Vbal

or

Vendor

Vno

VnameCityVbal

Page 8: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-8

Pictorial schema

VnameCityVBal

Vno

Vendor

AccNumCnameProvCbalClimit

Customer

Tno

VnoAccNumTdateAmount

Transaction

Page 9: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-9

The SQL DDL

• used for defining– tables– views

• example of table definition (conceptual schema):

create table Vendor

(Vno INTEGER not null,

Vname VARCHAR(20),

City VARCHAR(10),

Vbal DECIMAL(10,2),

primary key (Vno) );

Page 10: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-10

create table Customer(AccNum INTEGER not null, Cname VARCHAR(20) not null, Prov VARCHAR(20), Cbal DECIMAL(6,2) not null, Climit DECIMAL(4,0) not null, primary key (AccNum) );

create table Transaction(Tno INTEGER not null, Vno INTEGER not null, AccNum INTEGER not null, Tdate DATE, Amount DECIMAL(6,2) not null, primary key (Tno), foreign key (Vno) references vendor(Vno), foreign key (AccNum) references Customer(AccNum) );

...continued

Page 11: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-11

Attribute domains in SQL

• INTEGER: integers representable with 32 bits

• SMALLINT: integers representable with 16 bits

• DECIMAL(m,n): fixed point numbers

• FLOAT: 32 bit floating point numbers

• CHAR(n): fixed length strings

• VARCHAR(n): variable length strings

• BIT(n): n bits

• BIT VARYING(n): variable number of bits

Page 12: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-12

• DATE (year, month, day)• TIME (hour, minute, second)• TIME(i) (hour, minute, second, second

fraction)• TIMESTAMP (date, time, second

fraction)• INTERVAL YEAR/MONTH (year month

interval)• INTERVAL DAY/TIME (day time interval)

• plus many, many product-specific (non-standard) extensions

...continued

Page 13: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-13

Modifying table definitions

• Table schemas can be changed after the table has been created:– adding columns

– removing columns

– removing constraints (e.g. p-key)

– some SQL implementations allow• renaming a column• modifying a column

• Example:ALTER TABLE Vendor ADD Street VARCHAR(15)

Page 14: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-14

Removing tables

• SQL operation is “drop”• Tables can be dropped at any time• Dropping a table deletes the schema

and the instance• All views, foreign-key definitions are

also removed• Example:

DROP TABLE Transaction

Page 15: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-15

SQL has a non-navigational DML:

E.g. “Find names and provinces of customers who owe more than $1000 to the company.”

select Cname, Prov from Customer where Cbal > 1000;

The SQL DML

Page 16: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-16

• basic querying:

select columns from R1,..., Rk

[ where filter ];

• result is a relation over columns (columns = “*” means all attributes in R1,..., Rk)

• R1,..., Rk: tables from which the data is

retrieved

• filter : conditions on tuples used to form the result; optional

...continued

Page 17: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-17

• conditions may include:– arithmetic operators +, -, *, /

– comparisons =, <>, <, <=, >, >=

– logical connectives and, or and not

...continued

E.g. “List the names of the customers who live in Ontario and whose balance is over 80% of their balance limit.”

select Cname from Customer where Prov = 'Ont' and Cbal > 0.8 * Climit;

Page 18: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-18

• basic insertion:

insert into Customervalues (104, ‘Anne', 'ON',0, 4000);

• deletion: delete Customer rows for customers named Smith:

delete from Customerwhere Cname = 'Smith';

• delete all transactions:

delete from Transaction;

...continued

Page 19: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-19

• modification, changing existing rows• set balance of account 102 to zero:

update Customer set Cbal = 0where AccNum = 102;

• add $100 to each customer’s monthly limit:

update Customer set Climit = Climit + 100;

...continued

Page 20: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-20

SQL external schema

• called views

• a view is a named query (result is usually computed when the view is used)

create view WatVendors as select VNo, VName, VBal from Vendor where City = 'Waterloo';

• views can be used in retrieval exactly like tables (but updates of views are restricted)

Page 21: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-21

Advantages of views

• logical data independence

• simplified perception of the database

• different views for different users

• restricting data access

Page 22: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-22

Basic relational concepts

Relating to descriptions of data:

• Attribute (column): a name denoting a property or characteristic

• Relation schema (table header): a finite set of attributes and a mapping of each attribute to a domain (defined below)

Page 23: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-23

Relating to data:

Domain: an “abstract data type” (i.e. a name, a set of values and a number of functions defined over the values)

• Null value: a special exception value (meaning “not known”, “not applicable”)

• Tuple: a set of attribute/value pairs, with each attribute occurring at most once

• Relation (table): a relation schema, and a finite set of tuples

• Relational database: a finite set of relation names and a mapping of each relation name to a relation

...continued

Page 24: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-24

Other:

• Intention of a relation: the associated relation schema

• Extension of a relation: the associated set of tuples

...continued

The relational model assumes no ordering of either rows or columns for any table.

Page 25: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-25

Basic rules

• Domain constraints: the value associated with each attribute in a tuple must occur in the set of values associated with the domain of the attribute; or the value is Null

• First normal form: domain values cannot be tuples or relations

• Completeness: each tuple in a relation has an attribute/value pair for precisely the set of attributes in the associated relation schema

• Closed world: the database “knows of” all tuples in all relations

• Unique rows: no two distinct tuples in any given relation consist of the same set of attribute/value pairs

Page 26: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-26

Keys

• Relation superkey: a subset of the associated relation schema for which no pair of distinct tuples in the relation will ever agree on the corresponding values.

• Relation candidate key: a minimal superkey

• Relation primary key: a distinguished candidate key of the relation

• Foreign key: primary key of one relation appearing as attributes of another relation

• Foreign keys enable capturing more complex entity structure

Page 27: CS 338The Relational Model2-1 The Relational Model Lecture Topics Overview of SQL Underlying relational model Relational database structure SQL DDL and.

CS 338 The Relational Model 2-27

Integrity of primary and foreign keys

• Entity integrity: No component of a primary key value may be the null value, nor may be updated.

• Referential integrity: A tuple with a non-null value for a foreign key that does not match the primary key value of a tuple in the referenced relation is not allowed.