2011-ch07-SQL-1

77
Modern Database Management Modern Database Management 10 10 th th Edition Edition Jeffrey A. Hoffer, Mary B. Prescott, Jeffrey A. Hoffer, Mary B. Prescott, Heikki Topi Heikki Topi 1

Transcript of 2011-ch07-SQL-1

Page 1: 2011-ch07-SQL-1

Modern Database ManagementModern Database Management

1010thth Edition EditionJeffrey A. Hoffer, Mary B. Prescott, Jeffrey A. Hoffer, Mary B. Prescott,

Heikki Topi Heikki Topi

1

Page 2: 2011-ch07-SQL-1

Definition of terms Interpret history and role of SQL Define a database using SQL data definition

language Write single table queries using SQL Establish referential integrity using SQL Discuss SQL:1999 and SQL:200n standards

2

Page 3: 2011-ch07-SQL-1

Normalization theory is based on the observation that ◦ relations with certain properties are more

effective in inserting, updating and deleting data than other sets of relations containing the same data

Normalization is a multi-step process beginning with an “unnormalized” relation

3

Page 4: 2011-ch07-SQL-1

4

Boyce-Codd andHigher

Functional dependency of non key attributes on the primary key - Atomic values only

Full Functional dependency of nonkey attributes on the primary key

No transitive dependency between nonkey attributes

All determinants are candidate keys - Single multivalued dependency

Page 5: 2011-ch07-SQL-1

Relational Algebra is a collection of operators that take relations as their operands and return a relation as their results

First defined by Codd◦ Include 8 operators

4 derived from traditional set operators 4 new relational operations

5

From: C.J. Date, Database Systems 8th ed.

Page 6: 2011-ch07-SQL-1

Restrict Project Product Union Intersect Difference Join Divide

6

Page 7: 2011-ch07-SQL-1

Extracts specified tuples (rows) from a specified relation (table) ◦ Restrict is AKA “Select”

7

Page 8: 2011-ch07-SQL-1

Extracts specified attributes(columns) from a specified relation.

8

Page 9: 2011-ch07-SQL-1

Builds a relation from two specified relations consisting of all possible concatenated pairs of tuples, one from each of the two relations. (AKA Cartesian Product)

9

abc

xy

xyxyxy

aabbcc

Product

Page 10: 2011-ch07-SQL-1

Builds a relation consisting of all tuples appearing in either or both of two specified relations.

10

Page 11: 2011-ch07-SQL-1

Builds a relation consisting of all tuples appearing in both of two specified relations

11

Page 12: 2011-ch07-SQL-1

Builds a relation consisting of all tuples appearing in first relation but not the second.

12

Page 13: 2011-ch07-SQL-1

Builds a relation from two specified relations consisting of all possible concatenated pairs, one from each of the two relations, such that in each pair the two tuples satisfy some condition. (E.g., equal values in a given col.)

13

A1 B1A2 B1A3 B2

B1 C1B2 C2B3 C3

A1 B1 C1A2 B1 C1A3 B2 C2

(Naturalor Inner) Join

Page 14: 2011-ch07-SQL-1

Outer Joins are similar to PRODUCT -- but will leave NULLs for any row in the first table with no corresponding rows in the second.

14

A1 B1A2 B1A3 B2A4 B7

B1 C1B2 C2B3 C3

A1 B1 C1A2 B1 C1A3 B2 C2A4 * *

Outer Join

Page 15: 2011-ch07-SQL-1

Takes two relations, one binary and one unary, and builds a relation consisting of all values of one attribute of the binary relation that match (in the other attribute) all values in the unary relation.

15

a

xy

xyzxy

aaabc

Divide

Page 16: 2011-ch07-SQL-1

Structured Query Language

The standard for relational database management systems (RDBMS)

RDBMS: A database management system that manages data as a collection of tables in which all relationships are represented by common values in related tables

16

Page 17: 2011-ch07-SQL-1

1970–E. Codd develops relational database concept

1974-1979–System R with Sequel (later SQL) created at IBM Research Lab

1979–Oracle markets first relational DB with SQL

1986–ANSI SQL standard released 1989, 1992, 1999, 2003–Major ANSI

standard updates Current–SQL is supported by most

major database vendors

17

Page 18: 2011-ch07-SQL-1

Structured Query Language Used for both Database Definition,

Modification and Querying Basic language is standardized across

relational DBMS’s. Each system may have proprietary extensions to standard.

Relational Calculus combines Restrict, Project and Join operations in a single command. SELECT.

18

Page 19: 2011-ch07-SQL-1

Specify syntax/semantics for data definition and manipulation

Define data structures Enable portability Specify minimal (level 1) and complete

(level 2) standards Allow for later growth/enhancement to

standard

19

Page 20: 2011-ch07-SQL-1

Reduced training costs Productivity Application portability Application longevity Reduced dependence on a single vendor Cross-system communication

20

Page 21: 2011-ch07-SQL-1

21

SQLData Types

Ref TypesPredefinedTypes

ArraysROWData Struct

User-DefinedTypes

Numeric String DateTime Interval Boolean

Date

Time

Timestamp

Bit Character Blob

Fixed

Varying

CLOB

Fixed

Varying

ApproximateExact

NEWIN SQL99

Page 22: 2011-ch07-SQL-1

Catalog ◦ A set of schemas that constitute the description of a database

Schema◦ The structure that contains descriptions of objects created by a

user (base tables, views, constraints)

Data Definition Language (DDL)◦ Commands that define a database, including creating, altering, and

dropping tables and establishing constraints

Data Manipulation Language (DML)◦ Commands that maintain and query a database

Data Control Language (DCL)◦ Commands that control a database, including administering

privileges and committing data

22

Page 23: 2011-ch07-SQL-1

23

Figure 7-1A simplified schematic of a typical SQL environment, as described by the SQL: 200n standard

Page 24: 2011-ch07-SQL-1

24

Page 25: 2011-ch07-SQL-1

25

Figure 6-4 10th edition or Figure 7-4 9th editionDDL, DML, DCL, and the database development process

Page 26: 2011-ch07-SQL-1

Data Definition Language (DDL) Major CREATE statements:

◦ CREATE SCHEMA–defines a portion of the database owned by a particular user

◦ CREATE TABLE–defines a table and its columns

◦ CREATE VIEW–defines a logical table from one or more tables or tables or views

26

Page 27: 2011-ch07-SQL-1

CHARACTER SET(other than English), COLLATION(order that a character set will

assume), TRANSLATION(rules that maps characters from a

source character to destination), ASSERTION (check constraints), DOMAIN (set of valid values)

27

Page 28: 2011-ch07-SQL-1

28

Figure 7-5 General syntax for CREATE TABLE

Steps in table creation:

1. Identify data types for attributes

2. Identify columns that can and cannot be null

3. Identify columns that must be unique (candidate keys)

4. Identify primary key–foreign key mates

5. Determine default values

6. Identify constraints on columns (domain specifications)

7. Create the table and associated indexes

Page 29: 2011-ch07-SQL-1

29

Page 30: 2011-ch07-SQL-1

30

Figure 7-6 SQL database definition commands for Pine Valley Furniture

Overall table definitions

p,s precision and scaleLength and decimal

Page 31: 2011-ch07-SQL-1

31

Defining attributes and their data types

Page 32: 2011-ch07-SQL-1

32

Non-nullable specification

Identifying primary key

Primary keys can never have NULL values

Page 33: 2011-ch07-SQL-1

33

Non-nullable specifications

Primary key

Some primary keys are composite– composed of multiple attributes

Page 34: 2011-ch07-SQL-1

34

Default value

Domain constraint

Controlling the values in attributes

Page 35: 2011-ch07-SQL-1

35

Primary key of parent table

Identifying foreign keys and establishing relationships

Foreign key of dependent table

Page 36: 2011-ch07-SQL-1

Referential integrity–constraint that ensures that foreign key values of a table must match primary key values of a related table in 1:M relationships

Restricting:◦ Deletes of primary records◦ Updates of primary records◦ Inserts of dependent records

36

Page 37: 2011-ch07-SQL-1

37

Relational integrity is enforced via the primary-key to foreign-key match

Figure 7-7 Ensuring data integrity through updates

Page 38: 2011-ch07-SQL-1

ALTER TABLE statement allows you to change column specifications:◦ ALTER TABLE CUSTOMER_T ADD TYPE

VARCHAR(2); DROP TABLE statement allows you to

remove tables from your schema:◦ DROP TABLE CUSTOMER_T

38

Page 39: 2011-ch07-SQL-1

Adds data to a table Inserting into a table

◦ INSERT INTO CUSTOMER_T INSERT INTO CUSTOMER_T ◦ VALUES (VALUES (001, ‘Contemporary Casuals’, ‘1355 S. 001, ‘Contemporary Casuals’, ‘1355 S.

Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601););◦ VALUE – “s” Sequence!!! VALUE – “s” Sequence!!!

39

Page 40: 2011-ch07-SQL-1

Inserting a record that has some null attributes requires identifying identifying the fields that actually get datathe fields that actually get data

◦ INSERT INTO PRODUCT_T INSERT INTO PRODUCT_T (PRODUCT_ID, (PRODUCT_ID, PRODUCT_DESCRIPTION,PRODUCT_FINISH, PRODUCT_DESCRIPTION,PRODUCT_FINISH, STANDARD_PRICE, PRODUCT_ON_HAND) STANDARD_PRICE, PRODUCT_ON_HAND) VALUES VALUES (1, (1, ‘End Table’, ‘Cherry’, 175, 8);‘End Table’, ‘Cherry’, 175, 8);

40

Page 41: 2011-ch07-SQL-1

Inserting from another table◦INSERT INTO CA_CUSTOMER_T INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T SELECT * FROM CUSTOMER_T WHERE STATE = ‘CA’;WHERE STATE = ‘CA’; A SUBSET from another tableA SUBSET from another table

41

Page 42: 2011-ch07-SQL-1

42

Inserting into a table does not require explicit customer ID Inserting into a table does not require explicit customer ID entry or field list entry or field list

INSERT INTO CUSTOMER_T VALUES ( ‘Contemporary INSERT INTO CUSTOMER_T VALUES ( ‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);

New with SQL:2003/SQL:200n

INSERT INTO CUSTOMER_T INSERT INTO CUSTOMER_T VALUES (VALUES (001, ‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, 001, ‘Contemporary Casuals’, ‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601‘Gainesville’, ‘FL’, 32601););

Page 43: 2011-ch07-SQL-1

Removes rows from a table Delete certain rows

◦ DELETE FROM CUSTOMER_T WHERE STATE = ‘HI’;DELETE FROM CUSTOMER_T WHERE STATE = ‘HI’; Delete all rows

◦ DELETE FROM CUSTOMER_T;DELETE FROM CUSTOMER_T;◦ Careful!!!Careful!!!

43

Page 44: 2011-ch07-SQL-1

Modifies data in existing rows

UPDATE PRODUCT_T UPDATE PRODUCT_T SET UNIT_PRICE = 775 SET UNIT_PRICE = 775 WHERE WHERE PRODUCT_ID = 7;PRODUCT_ID = 7;

Note: the WHERE clause may be a Note: the WHERE clause may be a subquerysubquery

44

Page 45: 2011-ch07-SQL-1

45

Makes it easier to update a table…allows combination of Insert and Update in one statement

Useful for updating master tables with new data

Page 46: 2011-ch07-SQL-1

Control processing/storage efficiency:◦ Choice of indexes◦ File organizations for base tables◦ File organizations for indexes◦ Data clustering◦ Statistics maintenance

Creating indexes◦ Speed up random/sequential access to base

table data◦ Example

CREATE INDEX NAME_IDX ON CUSTOMER_T(CUSTOMER_NAME)

This makes an index for the CUSTOMER_NAME field of the CUSTOMER_T table

46

Page 47: 2011-ch07-SQL-1

Creating indexes◦Example CREATE INDEX NAME_IDX ON

CUSTOMER_T(CUSTOMER_NAME); This makes an index for the

CUSTOMER_NAME field of the CUSTOMER_T table

◦Remove index: DROP INDEX NAME_IDX;

47

Page 48: 2011-ch07-SQL-1

Clauses of the SELECT statement: (Sequence!!)◦ SELECTSELECT

List the columns (and expressions) that should be returned from the query

◦ FROMFROM Indicate the table(s) or view(s) from which data will be obtained

◦ WHEREWHERE Indicate the conditions under which a row will be included in

the result◦ GROUP BYGROUP BY

Indicate categorization of results ◦ HAVINGHAVING

Indicate the conditions under which a category (group) will be included

◦ ORDER BYORDER BY Sorts the result according to specified criteria

48

Page 49: 2011-ch07-SQL-1

49

Figure 7-10 SQL statement processing order (adapted from van der Lans, p.100)

Page 50: 2011-ch07-SQL-1

50

SELECT Conditions

1. = equal to a particular value2. >= greater than or equal to a particular

value3. > greater than a particular value4. <= less than or equal to a particular

value5. <> not equal to a particular value6. LIKE “*term*” (may be other wild cards

in other systems)7. IN (“opt1”, “opt2”,…,”optn”)8. BETWEEN val1 AND val29. IS NULL

Page 51: 2011-ch07-SQL-1

Find products with standard price less than $275

SELECTSELECT PRODUCT_NAME, STANDARD_PRICE

FROMFROM PRODUCT_V WHEREWHERE STANDARD_PRICE < 275;

51

Table 7-3: Comparison Operators in SQL

Page 52: 2011-ch07-SQL-1

Alias is an alternative column or table name

SELECT CUSTCUST.CUSTOMER_NAME AS NAMENAME, CUST.CUSTOMER_ADDRESS

FROM CUSTOMER_V AS CUSTCUSTWHERE NAMENAME = ‘Home

Furnishings’;

52

Page 53: 2011-ch07-SQL-1

What is average standard price for each product in inventory?

SELECT AVG (STANDARD_PRICE)AVG (STANDARD_PRICE) AS AVERAGE FROM PROCUCT_V;

AVG, COUNT, MAX, MIN, SUM; LN, EXP, POWER, SQRT

53

Page 54: 2011-ch07-SQL-1

Using the COUNT aggregate function to find totalsSELECT COUNT (*)COUNT (*) FROM ORDER_LINE_V

WHERE ORDER_ID = 1004;

Note 1: with aggregate functions you can’t have single-valued columns included in the SELECT clause --

SELECT PRODUCT_IDPRODUCT_ID, COUNT(*)COUNT(*) FROM ORDER_LINE_V

WHERE ORDER_ID = 1004;Note 1: CPOOUNT (*) and COUNT

54

Page 55: 2011-ch07-SQL-1

ANDAND, OROR, and NOTNOT Operators for customizing conditions in WHERE clause

SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE

FROM PRODUCT_VWHERE (PRODUCT_DESCRIPTION LIKELIKE ‘%%Desk’OROR PRODUCT_DESCRIPTION LIKELIKE ‘%%Table’) ANDAND STANDARD_PRICE > 300;

55

Note: the LIKE operator allows you to compare strings using wildcards. For example, the % wildcard in ‘%Desk’ indicates that all strings that have any number of characters preceding the word “Desk” will be allowed

Page 56: 2011-ch07-SQL-1

56

Page 57: 2011-ch07-SQL-1

Compare:

SELECT ORDER_IDFROM ORDER_LINE_V;

and –

SELECT DISTINCT ORDER_IDFROM ORDER_LINE_V;

But:But:SELECT DISTINCT ORDER_ID, ORDER_QUANTITYSELECT DISTINCT ORDER_ID, ORDER_QUANTITY

FROM ORDER_LINE_V;FROM ORDER_LINE_V;

57

Page 58: 2011-ch07-SQL-1

Sort the results first by STATE, and within a state by CUSTOMER_NAME

SELECT CUSTOMER_NAME, CITY, STATEFROM CUSTOMER_VWHERE STATE ININ (‘FL’, ‘TX’, ‘CA’, ‘HI’)ORDER BYORDER BY STATE, CUSTOMER_NAME;

58

Note: the IN operator in this example allows you to include rows whose

STATE value is either FL, TX, CA, or HI. It is more efficient than separate OR conditions

Page 59: 2011-ch07-SQL-1

For use with aggregate functions◦ Scalar aggregate: single value returned from SQL query

with aggregate function◦ Vector aggregate: multiple values returned from SQL

query with aggregate function (via GROUP BY)

SELECT CUSTOMER_STATE, COUNT(CUSTOMER_STATE)

FROM CUSTOMER_VGROUP BYGROUP BY CUSTOMER_STATE;

Note: you can use single-value fields with aggregate functions *ifif* they are included in the they are included in the GROUP BY clause; GROUP BY clause; OR: … (in an aggregate OR: … (in an aggregate function)function)

59

Page 60: 2011-ch07-SQL-1

For use with GROUP BY

SELECT CUSTOMER_STATE, COUNT(CUSTOMER_STATE)

FROM CUSTOMER_V GROUP BY CUSTOMER_STATE HAVINGHAVING COUNT(CUSTOMER_STATE)

> 1;

Like a WHERE clause, but it operates on groups it operates on groups (categories), not on individual rows. Here, only those groups with total numbers greater than 1 will be included in final result

60

Page 61: 2011-ch07-SQL-1

Views provide users controlled access to tables Base Table–table containing the raw data Dynamic View

◦ A “virtual table” created dynamically upon request by a user

◦ No data actually stored; instead data from base table made available to user

◦ Based on SQL SELECT statement on base tables or other views

Materialized View◦ Copy or replication of data◦ Data actually stored◦ Must be refreshed periodically to match the corresponding

base tables

61

Page 62: 2011-ch07-SQL-1

CREATE VIEW EXPENSIVE_STUFF_V ASSELECT PRODUCT_ID, PRODUCT_NAME, UNIT_PRICEFROM PRODUCT_TWHERE UNIT_PRICE >300WITH CHECK_OPTION;

62

View has a nameView is based on a SELECT statementCHECK_OPTION works only for updateable views and prevents updates that would create rows not included in the view

Page 63: 2011-ch07-SQL-1

Simplify query commands Assist with data security (but don't rely on

views for security, there are more important security measures)

Enhance programming productivity Contain most current base table data Use little storage space Provide customized view for user Establish physical data independence

63

Page 64: 2011-ch07-SQL-1

Use processing time each time view is referenced

May or may not be directly updateable

64

Page 65: 2011-ch07-SQL-1

SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH, STANDARD_PRICE

FROM PRODUCT_VWHERE (PRODUCT_DESCRIPTION LIKELIKE ‘%%Desk’OROR PRODUCT_DESCRIPTION LIKELIKE ‘%%Table’) ANDAND STANDARD_PRICE > 300;

Compare – no parentheses:Compare – no parentheses:SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH,

STANDARD_PRICEFROM PRODUCT_V

WHERE PRODUCT_DESCRIPTION LIKELIKE ‘%%Desk’OROR PRODUCT_DESCRIPTION LIKELIKE ‘%%Table’ANDAND STANDARD_PRICE > 300;

65

Relating to Slide 38/39Relating to Slide 38/39

Page 66: 2011-ch07-SQL-1

66

Page 67: 2011-ch07-SQL-1

67

Page 68: 2011-ch07-SQL-1

◦P. 341: The HAVING clause acts like a WHERE clause, but it identifies groups that meet a criterion, rather than rows. Therefore, you will usually see a HAVING clause …

◦ WHERE qualifies a set of rows, while HAVING…

68

Addendum for 3/28 class(1) –Addendum for 3/28 class(1) –HAVING clauseHAVING clause

Page 69: 2011-ch07-SQL-1

◦P. 343: Syntax of CREATE VIEW:◦CREATE VIEW view-name ASSELECT (that provides the rows and columns of the view)

◦ Example P. 344: CREATE VIEW ORDER_TOTALS_V AS

SELECT PRODUCT_ID PRODUCT,SUM(STANDARD_PRICE*QUANTITY) TOTALFROM INVOICE_V

GROUP BY PRODUCT_ID;

69

Addendum for 3/28 class(2) – Addendum for 3/28 class(2) – ViewsViews

Page 70: 2011-ch07-SQL-1

Every loan has at least one borrower who maintains an account with a minimum balance or $1000.00

create assertion balance_constraint check (not exists ( select *

from loan where not exists (

select * from borrower, depositor, account where loan.loan_number = borrower.loan_number

and borrower.customer_name = depositor.customer_name

and depositor.account_number = account.account_number

and account.balance >= 1000)))

70Reference: Korth

Page 71: 2011-ch07-SQL-1

The sum of all loan amounts for each branch must be less than the sum of all account balances at the branch.

create assertion sum_constraint check (not exists (select * from branch

where (select sum(amount ) from loan

where loan.branch_name = branch.branch_name )

>= (select sum (amount ) from account

where loan.branch_name = branch.branch_name )))

71

Page 72: 2011-ch07-SQL-1

Forms of authorization on parts of the database:

Read - allows reading, but not modification of data. Insert - allows insertion of new data, but not modification

of existing data. Update - allows modification, but not deletion of data. Delete - allows deletion of data.

Forms of authorization to modify the database schema (covered in Chapter 8):

Index - allows creation and deletion of indices. Resources - allows creation of new relations. Alteration - allows addition or deletion of attributes in a

relation. Drop - allows deletion of relations.

72

Page 73: 2011-ch07-SQL-1

The grant statement is used to confer authorizationgrant <privilege list>on <relation name or view name> to <user

list> <user list> is:

◦ a user-id◦ public, which allows all valid users the privilege granted◦ A role (more on this in Chapter 8)

Granting a privilege on a view does not imply granting any privileges on the underlying relations.

The grantor of the privilege must already hold the privilege on the specified item (or be the database administrator).

73

Page 74: 2011-ch07-SQL-1

select: allows read access to relation,or the ability to query using the view◦ Example: grant users U1, U2, and U3 select

authorization on the branch relation:

grant select on branch to U1, U2, U3

insert: the ability to insert tuples update: the ability to update using the SQL

update statement delete: the ability to delete tuples. all privileges: used as a short form for all the

allowable privileges more in Chapter 8

74

Page 75: 2011-ch07-SQL-1

The revoke statement is used to revoke authorization.revoke <privilege list>on <relation name or view name> from <user list>

Example:revoke select on branch from U1, U2, U3

All privileges that depend on the privilege being revoked are also revoked.

<privilege-list> may be all to revoke all privileges the revokee may hold.

If the same privilege was granted twice to the same user by different grantees, the user may retain the privilege after the revocation.

75

Page 76: 2011-ch07-SQL-1

EXEC SQL statement is used to identify embedded SQL request to the preprocessor

EXEC SQL <embedded SQL statement > END_EXEC

Note: this varies by language (for example, the Java embedding uses # SQL { …. }; )

From within a host language, find the names and cities of customers with more than the variable amount dollars in some account.

76

Page 77: 2011-ch07-SQL-1

Specify the query in SQL and declare a cursor for it EXEC SQL

declare c cursor for select depositor.customer_name, customer_city from depositor, customer, account where depositor.customer_name = customer.customer_name and depositor account_number = account.account_number

and account.balance > :amount END_EXEC

77