page 1 topic 5 sql cps510 database systems abdolreza abhari school of computer science ryerson...

85
Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Upload: ira-clark

Post on 05-Jan-2016

217 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 1

Topic 5

SQL

CPS510Database SystemsAbdolreza Abhari

School of Computer ScienceRyerson University

Page 2: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 2

Topics in this Section

• SQL Introduction• Basic SQL data types• DDL statements

» Create, delete, update tables» Specifying constraints, keys, …

• Example schema definition• DML statements

» Querying the database» Modifying the database

– Insert, delete, and update

• Views

Page 3: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 3

Introduction

• Developed by IBM in 1970’s • Early prototype called System R• In 1986, ANSI (American National Standards Institute)

published first standard (SQL-86)• An extended standard SQL in 1989 (SQL-89)• ANSI/ISO version is SQL-92 Also known as SQL2• Current version is SQL:1999• All major database vendors support SQL• Note that:Both relvar and relation = table in SQLTuple = row in SQLAttribute = column in SQL

Page 4: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 4

Introduction

• SQL (Structures Query Language) Non-procedural language Aims to express most database operations

» Queries, updates, creating tables, etc.

Stand-alone SQL may not be able express everything you want» Embedded SQL gives more flexibility and expressive power

– Example: You can insert SQL statements to retrieve data from the database into a C or Java program

We will use DB2 SQL commands:» To use DB2 Command Line Processor: first activate db2

» with . db2init command then write SQL commands as:

– db2 “CREATE TABLE ……”

Page 5: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 5

Example

Page 6: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 6

Basic Data Types

• SQL supports several data types» Numeric

– Integer and floating-point numbers supported

» Character string

– Fixed- and variable-length supported

» Bit string

– Not supported in DB2

– We will not discuss bit strings

» Date

» Time

– Date and time formats in DB2 are depending on the country code of application. For example: YYYY-MM-DD for date and

HH.MM.SS for time

Page 7: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 7

Basic Data Types (cont’d)

• Some exact numeric data types present in SQL-92 and/or DB2 Integer values

» INTEGER

– SQL-92 leaves precision to implementation

– DB2 uses 4 bytes (-2,147,483,648 to 2,147,483,647)

» SMALLINT

– SQL-92 allows for smaller storage space Again precision is implementation-dependent

– DB2 uses 2 bytes for SMALLINT (-32,768 to 32,767)

Page 8: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 8

Basic Data Types (cont’d)

Fractional numbers» SQL-92 provides

– NUMERIC: accept default precision and scale

– NUMERIC(size): specify precision but with default scale

– NUMERIC(size, d): Specify both precision and scale

» DB2 also supports them.

» DECIMAL

– Same as NUMERIC in DB2– SQL-92 provides 40 digits for this and (size and d can be

specified)

Page 9: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 9

Basic Data Types (cont’d)

• Approximate numeric (floating point) data types SQL-92 supports three data types

» REAL

– Single-precision floating point with implementation-dependent precision

» DOUBLE PRECISION

– Implementation-dependent double-precision number

» FLOAT(p)

– Provides binary precision greater than or equal to p

DB2 provides all of them

Page 10: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 10

Basic Data Types (cont’d)

• Character strings Fixed-size string

» CHAR(size): size characters long

– Pads on the right with blanks for shorter strings

» Use CHAR for a single character (equivalent to CHAR(1))

Variable-length string» VARCHAR(size): No blank padding is done

– In DB2 can be from 1 to 32,672 bytes

» size is the integer value that shows the maximum length

Page 11: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 11

Basic Data Types (cont’d)

• Date representation SQL-92 format

» DATE– Year is exactly 4 digits: 0001 to 9999– Month is exactly 2 digits in the range: 01 to 12– Day is exactly 2 digits in the range: 01 to 31

Month value may restrict this range to 28, 29, or 30

• Time representation SQL-92 format

» TIME– Hour is exactly 2 digits: 00 to 23– Minutes is exactly 2 digits in the range: 00 to 59– Seconds is again 2 digits (but a fractional value is optional) in

the range: 00 to 61.999….

Page 12: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 12

Basic DDL Statements

• Three basic DDL statements CREATE TABLE

» To create a new table» Can be quite complex

– Takes various types of constraints into consideration ALTER TABLE

» To update/modify an existing table– Adding/deleting a column– Updating an existing column (e.g. changing its data type)

DROP TABLE» To delete a table» Much simpler than the other two statements

Page 13: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 13

Creating Tables

• Tables can be created using CREATE TABLE statement example

CREATE TABLE professor(ProfName VARCHAR(25),ProfOffice VARCHAR(10),ProfPhone VARCHAR(12)

) Case does not matter

» Enclosing table and column names in double quotes makes the names case-sensitive

Disastrous for users and developers

Page 14: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 14

Creating Tables (cont’d)

• CREATE TABLE statement allows specification of a variety of constraints on a table

» NULL and default values» Candidate keys» Primary keys» Foreign keys» Check conditions

– e.g. simple range check (0 mark 100) The more constraints you specify

» the more work for the DMBS to maintain the data– takes more time to update the table

» less work for applications to maintain the data

Page 15: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 15

Creating Tables (cont’d)

• NULL values NULL values are used to represent information that is

out of bounds NULL values alleviate the need to use blanks, 0, -1 to

indicate– not available– not applicable– unknown

By default, NULL values are allowed– Specify NOT NULL if null values are not allowed for a

particular column/attribute

– NOT NULL is typically used with key attributes

Page 16: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 16

Creating Tables (cont’d)

We can modify our previous example asCREATE TABLE professor(

ProfName VARCHAR(25) NOT NULL,

ProfOffice VARCHAR(10),

ProfPhone VARCHAR(12)

)

ProfName is the key to the relation» We do not allow entry of tuples with a NULL value for this

field– Semantically, it means we should know the professor’s

name before we enter his/her other details

We allow NULL values for the other two columns

Page 17: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 17

Creating Tables (cont’d)

• DEFAULT values For attributes, we can also specify a default value

– Used when no value is given when a tuple is inserted into the table

We can modify our previous example as

CREATE TABLE professor(ProfName VARCHAR(25) NOT NULL,ProfOffice VARCHAR(10) DEFAULT ’4500HP’,ProfPhone VARCHAR(12) DEFAULT ’520-4333’

)

ProfOffice and ProfPhone will have the specified default values

Page 18: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 18

Creating Tables (cont’d)

• Candidate keys Can be specified using UNIQUE clause Example:

CREATE TABLE professor(

ProfName VARCHAR(25) UNIQUE,

ProfOffice VARCHAR(10) DEFAULT ’4500HP’,

ProfPhone VARCHAR(12) DEFAULT ’520-4333’

) ProfName is a candidate key

» Since NOT NULL is not specified, one NULL tuple is allowed Not recommended

» Should include NOT NULL (recommended practice)» DB2 requires NOT NULL

Page 19: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 19

Creating Tables (cont’d)

• Rewriting the previous example:

CREATE TABLE professor(ProfName VARCHAR(25) NOT NULL UNIQUE,ProfOffice VARCHAR(10) DEFAULT ’4500HP’,ProfPhone VARCHAR(12) DEFAULT ’520-4333’

)

In SQL2, we can write » NOT NULL UNIQUE or » UNIQUE NOT NULL

SQL-89 allowed only » NOT NULL UNIQUE

Page 20: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 20

Creating Tables (cont’d)

• We can write the previous statement as:

CREATE TABLE professor (

ProfName VARCHAR(25) NOT NULL,

ProfOffice VARCHAR(10) DEFAULT ’4500HP’,

ProfPhone VARCHAR(12) DEFAULT ’520-4333’,

UNIQUE (ProfName)

)

This form uses UNIQUE as a table constraint instead of specifying it as a column constraint

» Useful to specify candidate keys with multiple columns

Specification of candidate keys is useful to enforce uniqueness of the attribute values

Page 21: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 21

Creating Tables (cont’d)

• Primary key One of the candidate keys

» Attach special significance/characteristics

» Only one primary key per table

» No NULL values are allowed in primary key column(s)

– No need for NOT NULL in SQL

– DB2 requires NOT NULL for primary key

Specification is similar to candidate key specification» Use PRIMARY KEY instead of UNIQUE

» Column and table constraints forms can be used

Page 22: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 22

Creating Tables (cont’d)

Example 1: Uses column constraint form

CREATE TABLE professor (ProfName VARCHAR(25) NOT NULL PRIMARY KEY,

ProfOffice VARCHAR(10) DEFAULT ’4500HP’,ProfPhone VARCHAR(12) DEFAULT ’520-

4333’)

Example 2: Uses table constraint form

CREATE TABLE teaches (CourseNo INTEGER NOT NULL,ProfName VARCHAR(25) NOT NULL,Term CHAR NOT NULL,PRIMARY KEY (CourseNo, ProfName))

Page 23: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 23

Creating Tables (cont’d)

• Foreign key A combination of columns of one relation that

references primary key attributes of a second relation» A tuple in the first table can exist only if there is a tuple in the

second table with the corresponding primary key (same value)

Also known as referential integrity constraint

CREATE TABLE teaches (

CourseNo INTEGER REFERENCES course(CourseNo) NOT NULL,ProfName VARCHAR(25) REFERENCES professor(ProfName) NOT NULL,

Term CHAR NOT NULL,

PRIMARY KEY (CourseNo, ProfName))

Page 24: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 24

Creating Tables (cont’d)

Another example

CREATE TABLE enrolled (StudentNo INTEGER REFERENCES student(StudentNo) NOT NULL,CourseNo INTEGER NOT NULL,ProfName VARCHAR(25) NOT NULL,Status CHAR NOT NULL,PRIMARY KEY (StudentNo, CourseNo, ProfName),FOREIGN KEY (CourseNo, ProfName)

REFERENCES teaches(CourseNo, ProfName))

No need to establish CourseNo and ProfName as foreign keys

– Taken care of by teaches table (see next slides)

Page 25: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 25

Creating Tables (cont’d)

• Referential integrity actions in SQL2 On delete or update

» SET DEFAULT

– The attribute value is set to its default valueTypically used with delete

» SET NULL

– The attribute value is set to NULL valueTypically used with delete

» CASCADE

– Updates are propagated (attribute value is updated)

– Tuple is deleted (when the other tuple is deleted)

» NO ACTION

Page 26: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 26

Creating Tables (cont’d)

• DB2 Supports CASCADE option on deleteExample

CREATE TABLE can_teach ( CourseNo INTEGER REFERENCES

course(CourseNo) ON DELETE CASCADE, ProfName VARCHAR(25) REFERENCES

professor(ProfName) ON DELETE CASCADE, Preference NUMERIC DEFAULT 0, Evaluation NUMERIC DEFAULT NULL, PRIMARY KEY (CourseNo, ProfName))

Page 27: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 27

Creating Tables (cont’d)

• Constraint names We can assign names to constraints Example

PRIMARY KEY (CourseNo, ProfName)

can be written asCONSTRAINT teaches_pk PRIMARY KEY

(CourseNo, ProfName)

We can refer to this constraint by its name teaches_pk

Page 28: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 28

Creating Tables (cont’d)

• Check constraint Can be used to ensure that every row in the table

satisfies the condition Format

CHECK condition» Can use only values in a single row of the table

– Cannot refer to values in other rows» condition can be any valid expression that evaluates to

TRUE or FALSE

– Can contain functions, any columns from this table, and literals

» Use column constraint form for single column constraints

Page 29: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 29

Creating Tables (cont’d)

Example» Suppose we know that course number ranges from 900 to 957

» We can create a CHECK constraint to ensure this

CREATE TABLE course (

CourseNo NUMERIC CHECK (CourseNo BETWEEN

900 AND 957),

CourseName VARCHAR(25) NOT NULL,

Credits NUMERIC NOT NULL CHECK (Credits IN (3,6)),

PRIMARY KEY (CourseNo)

)

Page 30: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 30

Dropping Tables

•To delete tables, use DROP TABLE as follows:DROP TABLE professor;

•If we want to delete the tables that are in a dependency tree it is better to delete them in the following order

DROP TABLE enrolled;DROP TABLE teaches;DROP TABLE can_teach;DROP TABLE student;DROP TABLE course;DROP TABLE professor

Dependency tree

Page 31: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 31

Example

PROFESSOR table

CREATE TABLE professor ( ProfName VARCHAR(25) PRIMARY KEY NOT NULL, ProfOffice VARCHAR(10) DEFAULT

'4500HP‘ ,

ProfPhone VARCHAR(12) DEFAULT '520-4333'

)

Page 32: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 32

Example

COURSE table

CREATE TABLE course ( CourseNo INTEGER PRIMARY KEY NOT NULL CHECK (CourseNo BETWEEN

100 AND 700), CourseName VARCHAR(45) NOT NULL, Credits INTEGER NOT NULL CHECK

(Credits IN (3,6))

)

Page 33: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 33

Example

STUDENT table

CREATE TABLE student ( StudentNo INTEGER NOT NULL PRIMARY KEY, StudentName VARCHAR(30) NOT NULL, Degree VARCHAR(10), GPA NUMERIC CHECK

(GPA BETWEEN 0 and 12)

)

Page 34: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 34

Example

CAN_TEACH table CREATE TABLE can_teach ( CourseNo INTEGER REFERENCES

course(CourseNo) ON DELETE CASCADE, ProfName VARCHAR(25) REFERENCES

professor(ProfName) ON DELETE CASCADE, Preference NUMERIC DEFAULT 0, Evaluation NUMERIC(2,1) DEFAULT NULL CHECK (Evaluation BETWEEN 0 AND 5), CONSTRAINT can_teach_pk PRIMARY KEY (CourseNo, ProfName))

Page 35: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 35

Example

TEACHES table CREATE TABLE teaches ( CourseNo INTEGER, ProfName VARCHAR(25), Term CHAR CHECK

(term IN ('F','W','S')),

CONSTRAINT teaches_pk PRIMARY KEY (CourseNo, ProfName), FOREIGN KEY (CourseNo, ProfName)

REFERENCES can_teach

ON DELETE CASCADE)

Page 36: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 36

Example

ENROLLED table CREATE TABLE enrolled ( CourseNo INTEGER, ProfName VARCHAR(25), StudentNo INTEGER REFERENCES

student(StudentNo) ON DELETE CASCADE, Status CHAR CHECK

(Status IN ('C','A')), PRIMARY KEY (CourseNo,ProfName,StudentNo), FOREIGN KEY (CourseNo,ProfName)REFERENCES

teaches (CourseNo,ProfName) ON DELETE CASCADE)

Page 37: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 37

Altering Tables

• One of the ways of altering a table (supported by DB2) is Add a column to the table

• Use ALTER TABLE statement

• To add a column to represent the rank of a professor, we can use

ALTER TABLE professor ADD

Rank CHAR

• We can add a column at any time if NOT NULL is not specified We cannot use NOT NULL as in

ALTER TABLE professor ADD Rank CHAR NOT NULL

Page 38: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 38

SQL Queries

Page 39: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 39

SQL Queries (cont’d)

• Uses SELECT statement to query the database

• A simple form of SELECT statement isSELECT A1, A2, ..., An

FROM r1, r2, ..., rm

WHERE cond

• The name of columns can not be duplicated

Page 40: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 40

SQL Queries (cont’d)

Q1: List all attributes of all studentsSELECT *

FROM student» We can use * to list all attributes

» WHERE clause is optionalWhen not specified, all tuples are selected

Q2: List student names and their GPAs of all studentsSELECT StudentName, GPA

FROM student» When an attribute list is given, only the listed attributes are

displayed

Page 41: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 41

SQL Queries (cont’d)

Q3: List all attributes of students with a GPA 10SELECT *

FROM student

WHERE GPA >= 10

• You can use> greater than

>= greater than or equal to

< less than

<= less than or equal to

<> not equal to

Page 42: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 42

SQL Queries (cont’d)

Q4: List all attributes of students with a GPA 10 (sort the output in descending order by GPA)SELECT *

FROM student

WHERE GPA >= 10

ORDER BY GPA DESC

You can replace DESC by ASC to sort in ascending order

Ascending order is the default » If you do not want to depend on this default

specify ASC explicitly

Page 43: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 43

SQL Queries (cont’d)

Q5: List the professors teaching a course in the winter term (sort the output in ascending order by name)SELECT ProfName AS Winter_ProfessorsFROM teachesWHERE Term = 'W'ORDER BY ProfName The output uses Winter_professors heading instead of ProfName Output contains duplicates if a professor is teaching more than one

winter course Use DINSTINCT to eliminate duplicatesSELECT DISTINCT ProfName AS Winter_ProfessorsFROM teachesWHERE Term = 'W'ORDER BY ProfName

Page 44: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 44

SQL Queries (cont’d)

Q6: List all students (student name and GPA only) of B.C.S. students with a GPA 10SELECT StudentName, GPAFROM studentWHERE GPA >= 10

AND Degree = 'B.C.S' Logical operators AND, OR, and NOT can be used to

combine several simple conditions Precedence:

NOT highestAND middleOR lowest

» Parentheses can be used to override the default precedence

Page 45: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 45

SQL Queries (cont’d)

Q7: List all students (student name and GPA only) in the B.C.S. program with a GPA 10 or those in the B.A. program with a GPA 10.5

SELECT StudentName, GPAFROM studentWHERE (GPA >= 10 AND Degree = 'B.C.S') OR (GPA >= 10.5 AND Degree = 'B.A') Works without the parentheses ( ) because AND has a higher

precedence than OR

Page 46: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 46

SQL Queries (cont’d)

Q8: List all students (student name and degree only) who are not in the B.C.S. programSELECT StudentName, Degree

FROM student

WHERE Degree <> 'B.C.S'

• We can also use logical NOT operatorSELECT StudentName, Degree

FROM student

WHERE NOT(Degree = 'B.C.S')

Page 47: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 47

SQL Queries (cont’d)

Q9: List all students (student number and name only) who are enrolled in Prof. Smith’s 100 classSELECT student.StudentNo, StudentNameFROM enrolled, studentWHERE ProfName = 'Smith' AND CourseNo = 100 AND enrolled.StudentNo =

student.StudentNo

ORDER BY StudentNo ASC;» We need to join two tables

Last condition specifies the join condition» We can use the same attribute name in different tables» Use the table name to identify the attribute as in

student.StudentNoUnique attributes do not need the table prefix

Page 48: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 48

SQL Queries (cont’d)

student# student_name

12345 John12346 Margaret

student# course#

12345 9510012345 9530512346 95305

student# student_name course#

12345 John 9510012345 John 9530512346 Margaret 95305

Student

enrolled

The join of two tables on stu#

•Join Example

Page 49: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 49

SQL Queries (cont’d)

Q9b: List all students (student number and name only) who are enrolled in Prof. Smith’s 100 classSELECT s.StudentNo, StudentNameFROM enrolled e, student sWHERE ProfName = 'Smith' AND CourseNo = 100 AND e.StudentNo = s.StudentNoORDER BY StudentNo ASC;

We can use table alias (e for enrolled and s for student) even from the beginning

SQL-92 syntax uses FROM enrolled AS e, student AS s

After aliasing, you have to use alias names (not the original names). This is also true for DB2

Page 50: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 50

SQL Queries (cont’d)

Q10: List all students (student names only) who are enrolled in Prof. Post’s “Introduction to Database Systems” course

SELECT StudentName

FROM course c, enrolled e, student s

WHERE c.CourseName =

'Introduction to Database Systems'

AND ProfName = 'Post'

AND c.CourseNo = e.CourseNo

AND e.StudentNo = s.StudentNo;

» We have to join three tables

» Last two conditions give the join conditions course and enrolled on CourseNo enrolled and student on StudentNo

Page 51: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 51

SQL Queries (cont’d)

• SQL supports three set operations» Union :DB2 uses UNION» Intersection: DB2 uses INTERSECT» Difference: DB2 uses MINUS

The participating tables must be union compatible» Two tables are union compatible if

– Both have same number of columns– Each column in the first table has the same data type as

the corresponding column in the second table» The result table would have the same number of columns and

data types as the source tables All three operation require that the two source files be

union compatible

Page 52: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 52

SQL Queries (cont’d)

student# student_name

12345 John12346 Margaret

student# student_name

12345 John32456 Janet23456 Jim

student# student_name

12345 John

student

TA

Intersection of TA and student

•Intersection Example

Page 53: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 53

SQL Queries (cont’d)

student# student_name

12345 John12346 Margaret

student# student_name

12345 John 32456 Janet 23456 Jim

student# student_name

12345 John12346 Margaret32456 Janet23456 Jim

student# student_name

32456 Janet23456 Jim

student

TA

Student union TA

TA minus student

•Union and Minus Example

Page 54: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 54

SQL Queries (cont’d)

Q11: List of students concurrently taking 305 & 403 in Prof. Peters' 403 class

(SELECT s.StudentNo, s.StudentName FROM student s, enrolled e WHERE ProfName = 'Peters' AND CourseNo = 403 AND s.StudentNo = e.StudentNo)INTERSECT(SELECT s.StudentNo, s.StudentName FROM teaches t1,teaches t2,enrolled e,student s WHERE t1.CourseNo = 403 AND t1.ProfName = 'Peters' AND t1.Term = t2.Term AND t2.CourseNo = 305 AND t2.CourseNo = e.CourseNo AND t2.ProfName = e.ProfName AND s.StudentNo = e.StudentNo)

Page 55: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 55

SQL Queries (cont’d)

Q12: Give a list of professors who can teach 102 but are not assigned to teach this course(SELECT ProfName

FROM can_teach

WHERE CourseNo = 102)

MINUS

(SELECT ProfName

FROM teaches

WHERE CourseNo = 102)

Page 56: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 56

SQL Queries (cont’d)

Q13: List of professors who are not teaching any course or teaching only summer courses

((SELECT ProfName FROM professor) MINUS (SELECT ProfName FROM teaches))UNION ((SELECT ProfName FROM teaches WHERE Term = 'S') MINUS (SELECT ProfName FROM teaches WHERE Term = 'F’ /* A better way is OR Term = 'W')) WHERE Term <> ’S’*/

Page 57: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 57

SQL Queries (cont’d)

Q14: List of courses not offered in the summer termSELECT *FROM courseWHERE CourseNo NOT IN (SELECT CourseNo FROM teaches WHERE Term = 'S')

• Can also be written using MINUS operator as(SELECT * FROM course)MINUS(SELECT c.* FROM course c, teaches t WHERE c.CourseNo = t.CourseNo AND Term = 'S')

Page 58: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 58

SQL Queries (cont’d)

Q15: List all courses that are first courses (course title starts with Introduction or principles)

SELECT *

FROM course

WHERE CourseName LIKE 'Introduction%'

OR

CourseName LIKE 'Principles%'

Case sensitive matching % wildcard

» Matches 0 or more characters

Underscore ( _ ) » Matches a single character

Page 59: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 59

SQL Queries (cont’d)

Q16: List of students whose GPA is between 10 and 12SELECT *FROM studentWHERE GPA BETWEEN 10 AND 12

• BETWEEN adds no additional expressive power to SQL• It is simply a shorthand for range restriction

Can be done with relational operators ( , ) and the logical AND operator

• We can rewrite the above query without using BETWEEN asSELECT *FROM studentWHERE GPA >= 10 AND GPA <= 12

Page 60: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 60

SQL Queries (cont’d)

Q17: List all professors who teaches a student who is also taking Prof. Peters' 100 courseSELECT DISTINCT ProfNameFROM enrolled e1WHERE EXISTS (SELECT * FROM enrolled e2 WHERE e2.ProfName = 'Peters' AND e2.CourseNo = 100 AND e2.StudentNo = e1.StudentNo AND e1.ProfName <> 'Peters')

• Format is EXISTS (Subquery)

Page 61: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 61

Aggregate Functions

• Aggregate functions take a set/multiset of values and return a single value

• SQL provides five aggregate functions» Average: AVG

» Minimum: MIN

» Maximum: MAX

» Total: SUM

» Count: COUNT

• DB2 provides all of them plus Variance: VARIANCE

Page 62: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 62

Aggregate Functions (cont’d)

Q18: Find the average GPA of all studentsSELECT 'Average GPA is ', AVG(GPA)FROM student

» NULL tuples are excluded from the average computation (as if they didn't exist)

Q19: Find the minimum, maximum, average, and variance of the GPA of all studentsSELECT MIN(GPA), MAX(GPA), AVG(GPA),

VARIANCE(GPA)FROM student VARIANCE is not part of SQL-92

» Available in DB2

Page 63: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 63

Grouping in SELECT Statement

• SELECT statement may contain up to six clausesSELECT A1, A2, ..., An

FROM r1, r2, ..., rm

[WHERE cond]

[GROUP BY <group attributes>]

[HAVING <group conditions>]

[ORDER BY <order attributes>]

The clauses are specified in the given order Clauses in [ ] are optional

Page 64: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 64

Grouping in SELECT Statement

Q20: List for each course, the number of students registered for the courseSELECT CourseNo, COUNT(StudentNo) AS

Number_Enrolled

FROM enrolled

GROUP BY CourseNo

• To eliminate duplicates in the count, use DISTINCT

SELECT CourseNo, COUNT(DISTINCT StudentNo) AS

Number_EnrolledFROM enrolled

GROUP BY CourseNo

Page 65: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 65

Grouping in SELECT Statement

Q21: List the number of non-first term students in each degree program

» First term students have NULL as their GPA

SELECT Degree, COUNT(*) AS registered

FROM student

WHERE GPA IS NOT NULL /* cannot write GPA<> NULL */

GROUP BY Degree

ORDER BY registered

We can use IS NULL and IS NOT NULL to test NULL conditions of a row

Page 66: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 66

Modifying the Database

• SQL provides three basic ways to change the database INSERT:

» Adds a new row to the selected table

– Direct entry of a record

– Result of a query

DELETE: » Removes a row or a set of rows from the selected table

UPDATE: » Changes the values of an existing row in the selected table

Page 67: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 67

Modifying the Database (cont’d)

• Insertion of records into tables can done in two basic ways:

» Explicitly specify the record to be inserted» Implicitly specify the record set by specifying a query

– Result record set of the query is inserted into the table We can use two formats for the first type of insertion

INSERT INTO tableVALUES (value1, value2, . . ., valuen)

Example

INSERT INTO professor VALUES

('Post','4528HP','520-4352')» Useful to insert values for all attributes of the table

Page 68: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 68

Modifying the Database (cont’d)

If we want to insert values for only a subset of attributes, we have to specify the attribute namesINSERT INTO table(attribute1,…, attributen)VALUES (value1, value2, . . ., valuen)» Attributes can be listed in any order

No need to correspond to the order of the attributes in the table

One-to-one correspondence should be there between the attributes and the values specified

Example

INSERT INTO professor (ProfName) VALUES

(‘Peters’)

Page 69: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 69

Modifying the Database (cont’d)

• We can insert the results of a query into a tableCREATE TABLE honour_student (

StudentNo INTEGER PRIMARY KEY,

StudentName VARCHAR(30) NOT NULL,

Degree VARCHAR(10),

GPA NUMERIC CHECK

(GPA BETWEEN 10 and 12))

INSERT INTO honour_student

SELECT *

FROM student

WHERE GPA >= 10

Page 70: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 70

Modifying the Database (cont’d)

• DELETION The basic format is

DELETE FROM tableWHERE <cond>

Example 1: Delete 305 course from course tableDELETE FROM courseWHERE CourseNo = 305

» Due to the referential integrity constraints specified for the

database, all tuples that refer to 305 in professor,

can_teach, teaches and enrolled tables are also

deleted

Page 71: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 71

Modifying the Database (cont’d)

• We can also use a query for deletion Example 2: Delete all students who are enrolled in 102

DELETE FROM student

WHERE StudentNo IN

(SELECT StudentNo

FROM enrolled

WHERE CourseNo = 102)

» Again leads to cascading deletes due to the referential integrity constraints specified for the database

Page 72: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 72

Modifying the Database (cont’d)

• To modify the database records, use UPDATE• The format is

UPDATE table

SET attribute1= expression1,...,

attributen= expressionn

WHERE <cond>

Example 1: Convert GPA from 12-point to 4-point system

UPDATE student

SET GPA = GPA * 4/12

Page 73: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 73

Modifying the Database (cont’d)

Example 2: Add the department code 94 to the first year courses (starting with 100)

UPDATE courseSET CourseNo = CourseNo + 94000WHERE CourseNo BETWEEN 100 AND 199 Example 3: Promote to B.C.S all B.Sc students with a GPA at least

equal to the average B.C.S student GPA

UPDATE studentSET degree = 'B.C.S'WHERE Degree = 'B.Sc' AND GPA >= (SELECT AVG(GPA) FROM Student WHERE Degree ='B.C.S')

Page 74: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 74

Views

• View defines a virtual table (as opposed to base tables)

» For most part, these virtual tables can be used just like the base tables

• Suppose that only a B.C.S student with a GPA at least 10 is qualified to apply for TA

• We can create a potential_TA view asCREATE VIEW potential_TA AS (SELECT * FROM student WHERE Degree = 'B.C.S' AND GPA >= 10)

Page 75: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 75

Views (cont’d)

• You can specify attributes that the created view would have

• Since we know all students in potential_TA view are B.C.S student, we may drop the Degree attribute and give appropriate attribute names

CREATE VIEW potential_TA1(TA_StudentNo, TA_name, GPA) AS

(SELECT StudentNo, StudentName, GPA FROM student WHERE Degree = 'B.C.S' AND GPA >= 10)

Page 76: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 76

Views (cont’d)

• Views can be deleted by using DROP VIEWDROP VIEW potential_TA

• Views can be used just like base tables in queries• Examples

SELECT *

FROM potential_TA

SELECT *

FROM potential_TA1

WHERE GPA >= 11

Page 77: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 77

Views (cont’d)

• Insertions/updates: For most part, views can be treated like base tables Examples: Successful inserts (all update the student

table by inserting records)INSERT INTO potential_TA VALUES

(13334,'John Majors','B.C.S', 11.8)

» Insertions into potential_TA1 viewINSERT INTO potential_TA1 VALUES

(13243,'Connie Smith',11.3)INSERT INTO potential_TA1(TA_StudentNo,

TA_name) VALUES (43243,'Major Smith')

Page 78: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 78

Views (cont’d)

• The following insertion into the view is unsuccessful

INSERT INTO potential_TA(TA_StudentNo) VALUES (41243)

ERROR: SQL0407N Assignment of a NULL value to a NOT NULL column

The reason: The base table student has NOT NULL for StudentName column

Page 79: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 79

Views (cont’d)

• Views versus creating tables View is not created at definition

» It materializes up on first use View need not create a physical table

» Instead, DBMS can derive the table through some means– Implementation-dependent

View is updated automatically if the base table data is updated

» Creating a table and inserting records is not dynamic

• Create table should be used for the base tables• Views can used to provide a specific view of the

database to a group of users

Page 80: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 80

Views (cont’d)

• Views serve two important purposes Performance optimization

» Create a view if certain sub-queries or expressions are repeated in existing queries

– These sub-queries/expressions can be computed efficiently

Security» Users can be provided only with the data that is needed for

their applications

– This data can be derived from various tables

– Certain data can be hidden by providing only statistical values (as opposed to individual values)

Page 81: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 81

More SQL Commands (Provided by DB2)

• Join (cross product): Returns all combinations of the rows from two tables: (see next slide)

num1 num2

1 2

10 20

num3 num4

3 4

30 40

A B

Page 82: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 82

More SQL Commands (Cont’d)

SELECT num1,num2,num3,num4

FROM A,B» Result contains 4 records

num1 num2 num3 num4

1 2 3 4

10 20 3 4

1 2 30 40

10 20 30 40

Page 83: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 83

More SQL Commands (Cont’d)

SELECT num1,num2,num3,num4

FROM A left outer join B on num1=num3

» Result contains 2 records includes the rows from the left table that are not return by inner join

num1 num2 num3 num4

1 2 - -

10 20 - -

Page 84: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 84

More SQL Commands (Cont’d)

SELECT num1,num2,num3,num4

FROM A right outer join B on num1=num3

» Result contains 2 records includes the rows from the left table that are not return by inner join

num1 num2 num3 num4

- - 3 4

- - 30 40

Page 85: Page 1 Topic 5 SQL CPS510 Database Systems Abdolreza Abhari School of Computer Science Ryerson University

Page 85

More SQL Commands (Cont’d)

• Indexes are used to improve the efficiency• User-defined indexes can be created on the fields

that are involved in lots of queries.

CREATE INDEX stlname on student (lname)• DB2 creates indexes for primary key and unique

fields• Removing the indexes

DROP INDEX <indexname>