Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

86
Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Transcript of Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Page 1: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Chapter 2

The Relational Data Model & SQL

Copyright © 2004 Pearson Education, Inc.

Page 2: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Outline

Relational Model ConceptsRelational Model Constraints and Relational Database

SchemasUpdate Operations and Dealing with Constraint

ViolationsBasic SQL

Slide 2 -2

Page 3: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Relational Model ConceptsThe relational Model of Data is based on the

concept of a Relation.

A Relation is a mathematical concept based on the ideas of sets.

The strength of the relational approach to data management comes from the formal foundation provided by the theory of relations.

We review the essentials of the relational approach in this chapter.

Slide 2 -3

Page 4: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Relational Model Concepts

The model was first proposed by Dr. E.F. Codd of IBM in 1970 in the following paper:"A Relational Model for Large Shared Data Banks," Communications of the ACM, June 1970.

The above paper caused a major revolution in the field of Database management and earned Ted Codd the coveted ACM Turing Award.

Slide 2 -4

Page 5: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

INFORMAL DEFINITIONS

RELATION: A table of values– A relation may be thought of as a set of rows.– A relation may alternately be though of as a set of

columns.– Each row represents a fact that corresponds to a real-

world entity or relationship.– Each row has a value of an item or set of items that

uniquely identifies that row in the table.– Sometimes row-ids or sequential numbers are assigned

to identify the rows in the table.– Each column typically is called by its column name or

column header or attribute name.

Slide 2 -5

Page 6: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2 -6

Page 7: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

FORMAL DEFINITIONS

A Relation may be defined in multiple ways.The Schema of a Relation: R (A1, A2, .....An)

Relation schema R is defined over attributes A1, A2, .....An

A relation (or relation state) r = {t1, t2, t3, .., tm} (m tuples)

Exp: CUSTOMER (Cust-id, Cust-name, Address, Phone#)

Slide 2 -7

Page 8: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

FORMAL DEFINITIONS

A tuple t is an ordered set of values

t = <v1, v2, …, vn> (n values)Each value is derived from an appropriate domain.

vi is a element of dom(Ai) or Null value

Exp: <632895, "John Smith", "101 Main St. Atlanta, GA 30332", "(404) 894-2000"> is a tuple belonging to the CUSTOMER relation.

A relation may be regarded as a set of tuples (rows).Columns in a table are also called attributes of the relation.

Slide 2 -8

Page 9: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

FORMAL DEFINITIONS

A domain has a logical definition: e.g.,“USA_phone_numbers” are the set of 10 digit phone numbers valid in the U.S.

A domain may have a data-type or a format defined for it. The USA_phone_numbers may have a format: (ddd)-ddd-dddd where each d is a decimal digit. E.g., Dates have various formats such as monthname, date, year or yyyy-mm-dd, or dd mm,yyyy etc.

An attribute designates the role played by the domain. E.g., the domain Date may be used to define attributes “Invoice-date” and “Payment-date”.

Slide 2 -9

Page 10: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

FORMAL DEFINITIONS

The relation is formed over the cartesian product of the sets; each set has values from a domain; that domain is used in a specific role which is conveyed by the attribute name.

For example, attribute Cust-name is defined over the domain of strings of 25 characters. The role these strings play in the CUSTOMER relation is that of the name of customers.

Formally,

Given R(A1, A2, .........., An) r(R) dom (A1) X dom (A2) X ....X dom(An)

R: schema of the relation r of R: a specific state or population of R.R is also called the intension of a relation r is also called the extension of a relation

Slide 2 -10

Page 11: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

FORMAL DEFINITIONS

Let S1 = {0,1}Let S2 = {a,b,c}

Let R S1 X S2

Then for example: r(R) = {<0,a> , <0,b> , <1,c> }

is one possible “state” or “population” or “extension” r of the relation R, defined over domains S1 and S2. It has three tuples.

Slide 2 -11

Page 12: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

DEFINITION SUMMARY

Informal Terms Formal Terms

Table Relation

Column Attribute/Domain

Row Tuple

Values in a column Domain

Table Definition Schema of a Relation

Populated Table Extension

Chapter 2 12

Page 13: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Example

Slide 2-13

Page 14: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

CHARACTERISTICS OF RELATIONS

Ordering of tuples in a relation r(R): The tuples are not considered to be ordered, even though they appear to be in the tabular form.

 Ordering of attributes in a relation schema R (and of values within each tuple): We will consider the attributes in R(A1, A2, ..., An) and the values in t=<v1, v2, ..., vn> to be ordered .

 Values in a tuple: All values are considered atomic (indivisible). A special null value is used to represent values that are unknown or inapplicable to certain tuple.

Slide 2 -14

Page 15: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

CHARACTERISTICS OF RELATIONS

Notation:

- We refer to component values of a tuple t by t[Ai] = vi (the value of attribute Ai for tuple t).

Similarly, t[Au, Av, ..., Aw] refers to the subtuple of t containing the values of attributes Au, Av, ..., Aw, respectively.

Slide 2 -15

Page 16: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

CHARACTERISTICS OF RELATIONS

Slide 2-16

t2[Name] = “Barbara Benson”t2[Name, SSN] = “Barbara Benson”, 533-69-1238

Page 17: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Relational Integrity Constraints(Schema –based contraints)

Constraints are conditions that must hold on all valid relation instances. There are three main types of constraints:

1. Domain contraints

2. Key constraints

3. Entity integrity constraints

4. Referential integrity constraints

Slide 2 -17

Page 18: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Key Constraints

Superkey of R: A set of attributes SK of R such that no two tuples in any valid relation instance r(R) will have the same value for SK. That is, for any distinct tuples t1 and t2 in r(R), t1[SK] t2[SK].

Key of R: A "minimal" superkey; that is, a superkey K such that removal of any attribute from K results in a set of attributes that is not a superkey.

Example: The CAR relation schema:

CAR(State, Reg#, SerialNo, Make, Model, Year)has two keys Key1 = {State, Reg#}, Key2 = {SerialNo},

which are also superkeys. {SerialNo, Make} is a superkey but not a key.

If a relation has several candidate keys, one is chosen arbitrarily to be the primary key. The primary key attributes are underlined.

Slide 2 -18

Page 19: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Key Constraints

Slide 2 -19

Page 20: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Entity Integrity ContraintsRelational Database Schema: A set S of relation schemas

that belong to the same database. S is the name of the database.

S = {R1, R2, ..., Rn}Entity Integrity: The primary key attributes PK of each

relation schema R in S cannot have null values in any tuple of r(R). This is because primary key values are used to identify the individual tuples.

t[PK] null for any tuple t in r(R)  Note: Other attributes of R may be similarly constrained

to disallow null values, even though they are not members of the primary key.

Slide 2 -20

Page 21: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Referential Integrity ConstraintsA constraint involving two relations (the previous constraints

involve a single relation).Used to specify a relationship among tuples in two relations:

the referencing relation and the referenced relation.Tuples in the referencing relation R1 have attributes FK

(called foreign key attributes) that reference the primary key attributes PK of the referenced relation R2. A tuple t1 in R1 is said to reference a tuple t2 in R2 if t1[FK] = t2[PK].

Referential integrity constraints arise from the relationships among the entities

A referential integrity constraint can be displayed in a relational database schema as a directed arc from R1.FK to R2.

Slide 2 -21

Page 22: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2 -22

Page 23: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Referential Integrity Constraint

Statement of the constraintThe value in the foreign key column (or

columns) FK of the the referencing relation R1 can be either: (1) a value of an existing primary key value of the corresponding primary key PK in the referenced relation R2,, or..

(2) a null.

In case (2), the FK in R1 should not be a part of its own primary key.

Slide 2 -23

Page 24: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Other Types of ConstraintsSemantic Integrity Constraints:- based on application semantics and cannot

be expressed by the data model- E.g., “the max. no. of hours per employee

for all projects he or she works on is 56 hrs per week”

- SQL-99 allows triggers and ASSERTIONS to allow for some of these

Another type: Transition Contraints

Slide 2 -24

Page 25: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2 -25

Page 26: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Case study

Company Database

Slide 2 -26

Page 27: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Requirements of the Company (oversimplified for illustrative purposes)– The company is organized into DEPARTMENTs.

Each department has a name, number and an employee who manages the department. We keep track of the start date of the department manager.

– Each department controls a number of PROJECTs. Each project has a name, number and is located at a single location.

Example COMPANY Database

Slide 2 -27

Page 28: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

–We store each EMPLOYEE’s social security number, address, salary, sex, and birthdate. Each employee works for one department but may work on several projects. We keep track of the number of hours per week that an employee currently works on each project. We also keep track of the direct supervisor of each employee.

–Each employee may have a number of DEPENDENTs. For each dependent, we keep track of their name, sex, birthdate, and relationship to employee.

Example COMPANY Database

Slide 2 -28

Page 29: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2-29

Page 30: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2-30

Page 31: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

5.7

Slide 2-31

Page 32: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Update Operations on Relations INSERT a tuple.DELETE a tuple.MODIFY a tuple.

  Integrity constraints should not be violated by the update

operations.Several update operations may have to be grouped

together.Updates may propagate to cause other updates

automatically. This may be necessary to maintain integrity constraints.

Slide 2 -32

Page 33: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2-33

Page 34: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Examples:

Slide 2 -34

Page 35: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Update Operations on RelationsIn case of integrity violation, several actions can

be taken:– Cancel the operation that causes the violation (REJECT

or RESTRICT option)

– Perform the operation but inform the user of the violation

– Trigger additional updates so the violation is corrected (CASCADE option, SET NULL option)

– Execute a user-specified error-correction routine

Slide 2 -35

Page 36: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

In-Class Exercise

(Taken from Exercise 5.15)

Consider the following relations for a database that keeps track of student enrollment in courses and the books adopted for each course:

STUDENT(SSN, Name, Major, Bdate)

COURSE(Course#, Cname, Dept)

ENROLL(SSN, Course#, Quarter, Grade)

BOOK_ADOPTION(Course#, Quarter, Book_ISBN)

TEXT(Book_ISBN, Book_Title, Publisher, Author)

Draw a relational schema diagram specifying the foreign keys for this schema.

Slide 2-36

Page 37: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2-37

Page 38: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2-38

Discuss all integrity contraints violated by each following operation

Page 39: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Basic SQLSQL Data Definition & Data TypesSpecifying Constraints in SQLBasic Retrieval Queries in SQL INSERT, DELETE, UPDATE

Slide 2-39

Page 40: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SQL developments: an overview

In 1986, ANSI and ISO published an initial standard for SQL: SQL-86 or SQL1

In 1992, first major revision to ISO standard occurred, referred to as SQL2 or SQL-92

In 1999, SQL-99 (SQL3) was released with support for object-oriented data management

In late 2003, SQL-2003 was releasedNow: SQL-2006 was published

Chapter 2-40Slide 2 -40

Page 41: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SQL

DDL: Create, Alter, DropDML: Select, Insert, Update, DeleteDCL: Commit, Rollback, Grant, Revoke

Chapter 2-41Slide 2 -41

Page 42: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

CREATE SCHEMA

Started with SQL 92A SQL Schema: is to group together tables

and other constructs that belong to the same database application

CREATE SCHEMA SchemaName AUTHORIZATION AuthorizationIdentifier

Slide 2 -42

Page 43: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

CREATE TABLE

Specifies a new base relation by giving it a name, and specifying each of its attributes and their data types (INTEGER, FLOAT, DECIMAL(i,j), CHAR(n), VARCHAR(n))

A constraint NOT NULL may be specified on an attribute

CREATE TABLE DEPARTMENT( DNAME VARCHAR(10) NOT NULL,

DNUMBER INTEGER NOT NULL,MGRSSN CHAR(9),MGRSTARTDATE CHAR(9) );

Slide 2 -43

Page 44: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

CREATE TABLE

CREATE TABLE Company.TableName …

orCREATE TABLE TableName …

Slide 2 -44

Page 45: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

CREATE TABLE

CREATE TABLE TableName ({colName dataType [NOT NULL] [UNIQUE][DEFAULT defaultOption][CHECK searchCondition] [,...]}[PRIMARY KEY (listOfColumns),]{[UNIQUE (listOfColumns)] [,…]}{[FOREIGN KEY (listOfFKColumns) REFERENCES ParentTableName [(listOfCKColumns)] [ON UPDATE referentialAction] [ON DELETE referentialAction ]] [,…]}{[CHECK (searchCondition)] [,…] })

Slide 2 -45

Page 46: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Data Types Numeric: INT or INTEGER, FLOAT or REAL, DOUBLE

PRECISION, … Character string: fixed length CHAR(n), varying length

VARCHAR(n) Bit string: BIT(n), e.g. B’1001’ Boolean: true, false or NULL DATE: Made up of year-month-day in the format yyyy-mm-dd TIME: Made up of hour:minute:second in the format hh:mm:ss TIME(i): Made up of hour:minute:second plus i additional

digits specifying fractions of a second format is hh:mm:ss:ii...i TIMESTAMP: Has both DATE and TIME components

Slide 2 -46

Page 47: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Data TypesA domain can be declared and used with the

attribute specification

CREATE DOMAIN DomainName AS DataType [CHECK conditions];

Example:

Slide 2 -47

Page 48: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Specifying Constraints in SQL

Specifying Attribute Constraints and Attribute Defaults

Default values– DEFAULT <value> can be specified for an attribute– If no default clause is specified, the default value is NULL for

attributes that do not have the NOT NULL constraintCHECK clause: restrict attribute or domain values

DNUMBER INT NOT NULL CHECK (DNUMBER>0 AND DNUMBER<21);

– CREATE DOMAIN can also be used in conjunction with the CHECK clause:

CREATE DOMAIN D_NUM AS INTEGER CHECK (D_NUM>0 AND D_NUM<21);

Slide 2 -48

Page 49: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Specifying Constraints in SQL

Specifying Key ConstraintsKey attributes can be specified via the PRIMARY KEY

and UNIQUE phrases

CREATE TABLE DEPT( DNAME VARCHAR(10) NOT NULL,

DNUMBER INTEGER NOT NULL,MGRSSN CHAR(9),MGRSTARTDATE CHAR(9),PRIMARY KEY (DNUMBER),UNIQUE (DNAME),FOREIGN KEY (MGRSSN) REFERENCES EMP );

Or Dnumber INTEGER PRIMARY KEY;

Slide 2 -49

Page 50: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

REFERENTIAL INTEGRITY OPTIONS

Specifying Referential Integrity Constraints: FOREIGN KEY clause. Can specify RESTRICT, CASCADE, SET NULL or SET DEFAULT on referential integrity constraints

CREATE TABLE DEPT ( DNAME VARCHAR(10) NOT NULL,DNUMBER INTEGER NOT NULL,MGRSSN CHAR(9),MGRSTARTDATE CHAR(9),PRIMARY KEY (DNUMBER),UNIQUE (DNAME),FOREIGN KEY (MGRSSN) REFERENCES EMPON DELETE SET DEFAULT ON UPDATE CASCADE );

Slide 2 -50

Page 51: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Specifying Constraints in SQL

Giving names to constraints

Slide 2 -51

Page 52: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Specifying Constraints in SQL

Specifying Constraints on Tuples (tuple-based) using CHECK: at the end of CREATE TABLE

Example:

Slide 2 -52

Page 53: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

5.7

Slide 2-53

Page 54: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

CREATE TABLE

CREATE TABLE TableName ({colName dataType [NOT NULL] [UNIQUE][DEFAULT defaultOption][CHECK searchCondition] [,...]}[PRIMARY KEY (listOfColumns),]{[UNIQUE (listOfColumns)] [,…]}{[FOREIGN KEY (listOfFKColumns) REFERENCES ParentTableName [(listOfCKColumns)] [ON UPDATE referentialAction] [ON DELETE referentialAction ]] [,…]}{[CHECK (searchCondition)] [,…] })

Slide 2 -54

Page 55: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2 -55

Page 56: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Basic Retrieval Queries in SQL

SELECT statementSQL relation (table) is a multi-set (sometimes

called a bag) of tuples; it is not a set of tuplesSQL relations can be constrained to be sets by

specifying PRIMARY KEY or UNIQUE attributes, or by using the DISTINCT option in a query

Slide 2 -56

Page 57: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Basic Retrieval Queries in SQL (cont.)

Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE block

SELECT <attribute list>FROM <table list>WHERE <condition>

– <attribute list> is a list of attribute names whose values are to be retrieved by the query

– <table list> is a list of the relation names required to process the query

– <condition> is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query

Slide 2 -57

Page 58: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

5.7

Slide 2-58

Page 59: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2-59

Page 60: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Simple SQL QueriesAll subsequent examples use the COMPANY databaseExample of a simple query on one relationQuery 0: Retrieve the birthdate and address of the employee

whose name is 'John B. Smith'.

Q0: SELECT BDATE, ADDRESSFROM EMPLOYEEWHERE FNAME='John' AND MINIT='B’

AND LNAME='Smith’

– The SELECT-clause specifies the projection attributes and the WHERE-clause specifies the selection condition

– The result of the query may contain duplicate tuples

Slide 2 -60

Page 61: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Simple SQL Queries (cont.)

Query 1: Retrieve the name and address of all employees who work for the 'Research' department.

Q1: SELECT FNAME, LNAME, ADDRESSFROM EMPLOYEE, DEPARTMENTWHERE DNAME='Research' AND

DNUMBER=DNO

– (DNAME='Research') is a selection condition– (DNUMBER=DNO) is a join condition

Slide 2 -61

Page 62: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Simple SQL Queries (cont.)Query 2: For every project located in 'Stafford', list the project

number, the controlling department number, and the department manager's last name, address, and birthdate.

Q2: SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS

FROM PROJECT, DEPARTMENT, EMPLOYEEWHERE DNUM=DNUMBER AND MGRSSN=SSN

AND PLOCATION='Stafford'

– In Q2, there are two join conditions– The join condition DNUM=DNUMBER relates a project to

its controlling department– The join condition MGRSSN=SSN relates the controlling

department to the employee who manages that departmentSlide 2 -62

Page 63: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Aliases, * and DISTINCT, Empty WHERE-clause

In SQL, we can use the same name for two (or more) attributes as long as the attributes are in different relationsA query that refers to two or more attributes with the same name must qualify the attribute name with the relation name by prefixing the relation name to the attribute name

Example:

EMPLOYEE.LNAME, DEPARTMENT.DNAME

Slide 2 -63

Page 64: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

ALIASES Some queries need to refer to the same relation twice In this case, aliases are given to the relation name Query 8: For each employee, retrieve the employee's name, and the name

of his or her immediate supervisor.

Q8: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME

FROM EMPLOYEE E SWHERE E.SUPERSSN=S.SSN

– In Q8, the alternate relation names E and S are called aliases or tuple variables for the EMPLOYEE relation

– We can think of E and S as two different copies of EMPLOYEE; E represents employees in role of supervisees and S represents employees in role of supervisors

Slide 2 -64

Page 65: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

ALIASES (cont.)Aliasing can also be used in any SQL query for convenience

Can also use the AS keyword to specify aliases

Q8: SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAME

FROM EMPLOYEE AS E, EMPLOYEE AS SWHERE E.SUPERSSN=S.SSN

Slide 2 -65

Page 66: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

UNSPECIFIED WHERE-clause

A missing WHERE-clause indicates no condition; hence, all tuples of the relations in the FROM-clause are selected

This is equivalent to the condition WHERE TRUEQuery 9: Retrieve the SSN values for all employees.

Q9: SELECT SSNFROM EMPLOYEE

If more than one relation is specified in the FROM-clause and there is no join condition, then the CARTESIAN PRODUCT of tuples is selected

Slide 2 -66

Page 67: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

UNSPECIFIED WHERE-clause (cont.)

Example:

Q10: SELECT SSN, DNAMEFROM EMPLOYEE, DEPARTMENT

– It is extremely important not to overlook specifying any selection and join conditions in the WHERE-clause; otherwise, incorrect and very large relations may result

Slide 2 -67

Page 68: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

USE OF *To retrieve all the attribute values of the selected tuples, a * is

used, which stands for all the attributesExamples:

Q1C: SELECT *FROM EMPLOYEEWHERE DNO=5

Q1D: SELECT *FROM EMPLOYEE, DEPARTMENTWHERE DNAME='Research' AND

DNO=DNUMBER

Slide 2 -68

Page 69: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

USE OF DISTINCTSQL does not treat a relation as a set; duplicate tuples can

appearTo eliminate duplicate tuples in a query result, the keyword

DISTINCT is usedFor example, the result of Q11 may have duplicate SALARY

values whereas Q11A does not have any duplicate valuesQ11: SELECT SALARY

FROM EMPLOYEE Q11A: SELECT DISTINCT SALARY

FROM EMPLOYEE

Slide 2 -69

Page 70: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SUBSTRING COMPARISON

The LIKE comparison operator is used to compare partial strings

'%' (or '*' in some implementations) replaces an arbitrary number of characters

'_' replaces a single arbitrary character

Slide 2 -70

Page 71: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SUBSTRING COMPARISON (cont.)

Query 25: Retrieve all employees whose address is in Houston, Texas. Here, the value of the ADDRESS attribute must contain the substring 'Houston,TX'.

Q25: SELECT FNAME, LNAMEFROM EMPLOYEEWHERE ADDRESS LIKE

'%Houston,TX%’

Slide 2 -71

Page 72: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

SUBSTRING COMPARISON (cont.)

Query 26: Retrieve all employees who were born during the 1950s. Here, '5' must be the 8th character of the string (according to our format for date), so the BDATE value is '_______5_', with each underscore as a place holder for a single arbitrary character.

Q26: SELECT FNAME, LNAMEFROM EMPLOYEEWHERE BDATE LIKE

'_______5_’

The LIKE operator allows us to get around the fact that each value is considered atomic and indivisible; hence, in SQL, character string attribute values are not atomic

Slide 2 -72

Page 73: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

ARITHMETIC OPERATIONS

The standard arithmetic operators '+', '-'. '*', and '/‘ can be applied to numeric values in an SQL query result

Query 27: Show the effect of giving all employees who work on the 'ProductX' project a 10% raise.

Q27: SELECT FNAME, LNAME, 1.1*SALARYFROM EMPLOYEE, WORKS_ON,

PROJECTWHERE SSN=ESSN AND PNO=PNUMBER AND

PNAME='ProductX’

Slide 2 -73

Page 74: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Specifying Updates in SQL

There are three SQL commands to modify the database; INSERT, DELETE, and UPDATE

Slide 2 -74

Page 75: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

INSERT

To add one or more tuples to a relationAttribute values should be listed in the

same order as the attributes were specified in the CREATE TABLE command

Slide 2 -75

Page 76: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

INSERT (cont.) Example:

U1: INSERT INTO EMPLOYEEVALUES ('Richard','K','Marini', '653298653', '30-DEC-

52','98 Oak Forest,Katy,TX', 'M', 37000,'987654321', 4 )

An alternate form of INSERT specifies explicitly the attribute names that correspond to the values in the new tuple

Attributes with NULL values can be left out Example: Insert a tuple for a new EMPLOYEE for whom we only

know the FNAME, LNAME, and SSN attributes.

U1A: INSERT INTO EMPLOYEE (FNAME, LNAME, SSN) VALUES ('Richard', 'Marini', '653298653')

Slide 2 -76

Page 77: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

INSERT (cont.)

Important Note: Only the constraints specified in the DDL commands are automatically enforced by the DBMS when updates are applied to the database

Another variation of INSERT allows insertion of multiple tuples resulting from a query into a relation

Slide 2 -77

Page 78: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

INSERT (cont.) Example: Suppose we want to create a temporary table that has the name, number of

employees, and total salaries for each department. A table DEPTS_INFO is created by U3A, and is loaded with the summary information retrieved from the database by the query in U3B.

U3A: CREATE TABLE DEPTS_INFO(DEPT_NAME VARCHAR(10),NO_OF_EMPS INTEGER,TOTAL_SAL INTEGER);

U3B: INSERT INTODEPTS_INFO (DEPT_NAME, NO_OF_EMPS, TOTAL_SAL)

SELECT DNAME, COUNT (*), SUM (SALARY) FROM DEPARTMENT, EMPLOYEEWHERE DNUMBER=DNOGROUP BY DNAME ;

Slide 2 -78

Page 79: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

INSERT (cont.)

Note: The DEPTS_INFO table may not be up-to-date if we change the tuples in either the DEPARTMENT or the EMPLOYEE relations after issuing U3B. We have to create a view (see later) to keep such a table up to date.

Slide 2 -79

Page 80: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

DELETE

Removes tuples from a relation Includes a WHERE-clause to select the tuples to be deletedTuples are deleted from only one table at a time (unless

CASCADE is specified on a referential integrity constraint)

A missing WHERE-clause specifies that all tuples in the relation are to be deleted; the table then becomes an empty table

The number of tuples deleted depends on the number of tuples in the relation that satisfy the WHERE-clause

Referential integrity should be enforced

Slide 2 -80

Page 81: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

DELETE (cont.)Examples:

U4A: DELETE FROM EMPLOYEEWHERE LNAME='Brown’

U4B: DELETE FROM EMPLOYEEWHERE SSN='123456789’

U4C: DELETE FROM EMPLOYEEWHERE DNO IN (SELECT DNUMBER

FROM DEPARTMENTWHERE DNAME='Research')

U4D: DELETE FROM EMPLOYEE

Slide 2 -81

Page 82: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

UPDATEUsed to modify attribute values of one or more

selected tuplesA WHERE-clause selects the tuples to be modifiedAn additional SET-clause specifies the attributes to

be modified and their new valuesEach command modifies tuples in the same relationReferential integrity should be enforced

Slide 2 -82

Page 83: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

UPDATE (cont.)Example: Change the location and controlling department

number of project number 10 to 'Bellaire' and 5, respectively.

U5: UPDATE PROJECTSET PLOCATION = 'Bellaire', DNUM

= 5WHERE PNUMBER=10

Slide 2 -83

Page 84: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

UPDATE (cont.) Example: Give all employees in the 'Research' department a 10% raise

in salary.

U6: UPDATE EMPLOYEESET SALARY = SALARY *1.1WHERE DNO IN (SELECT DNUMBER

FROM DEPARTMENT WHERE DNAME='Research')

In this request, the modified SALARY value depends on the original SALARY value in each tuple

The reference to the SALARY attribute on the right of = refers to the old SALARY value before modification

The reference to the SALARY attribute on the left of = refers to the new SALARY value after modification

Slide 2 -84

Page 85: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition Slide 2-85

Page 86: Chapter 2 The Relational Data Model & SQL Copyright © 2004 Pearson Education, Inc.

Copyright © 2004 Ramez Elmasri and Shamkant NavatheElmasri/Navathe, Fundamentals of Database Systems, Fourth Edition

Summary of SQL Queries

A query in SQL can consist of up to six clauses, but only the first two, SELECT and FROM, are mandatory. The clauses are specified in the following order:

SELECT <attribute list>FROM <table list>[WHERE <condition>][GROUP BY <grouping attribute(s)>][HAVING <group condition>][ORDER BY <attribute list>]

Slide 2 -86