Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.

47
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition

Transcript of Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management Seventh Edition.

Chapter 4The Relational Model 3: Advanced

Topics

Concepts of Database Management

Seventh Edition

Objectives

2

Define, describe, and use viewsUse indexes to improve database

performanceExamine the security features of a DBMSDiscuss entity, referential, and legal-values

integrity

Objectives (continued)

3

Make changes to the structure of a relational database

Define and use the system catalogDiscuss stored procedures, triggers, and

data macros

Views

4

View: application program’s or individual user’s picture of the database

Less involved than full databaseSimplificationSecurity

Views (continued)

5

Defining query: SELECT command that creates a viewIndicates what to include in the view

Query acts as a window into the databaseDoes not produce a new tableQuery that involves a view

DBMS does not execute the query in this form

Query actually executed is created by merging this query with the query that defines the view

Views (continued)CREATE VIEW Housewares ASSELECT PartNum, Description, OnHand, PriceFROM PartWHERE Class='HW';

6

FIGURE 4-1: Housewares view

Views (continued)

7

To create a view in Access, create and save a query

Changing field names in a viewSQL: include the new field names in the

CREATE VIEW commandAccess: precede the name of the field with

the desired name, followed by a colonRow-and-column subset view

Subset of rows and columns in an individual table

Views (continued)

8

FIGURE 4-3: Access query design of the Housewares view

Views (continued)

9

FIGURE 4-5: Access query design of the Housewares view with changed field names

Views (continued)

10

A view can join two or more tablesAdvantages of views

Data independenceEach user has his or her own viewView should contain only fields required by

the userGreatly simplifies user’s perception of databaseSecurity

Indexes

11

Conceptually similar to book indexIncrease data retrieval efficiencyRecord numbers automatically assigned

and used by DBMSIndex key: field or combination of fields on

which index is builtAdvantages

Makes some data retrieval more efficient

Indexes (continued)

12

FIGURE 4-10: Customer table with record numbers

Indexes (continued)

13

FIGURE 4-11: Index for the Customer table on the CustomerNum field

Indexes (continued)

14

DisadvantagesOccupies space on diskDBMS must update index whenever

corresponding data are updatedCreate an index on a field (or fields) when:

Field is the primary key of the tableField is the foreign key in a relationshipField will be frequently used as a sort fieldNeed to frequently locate a record based on

a value in this field

Indexes (continued)

15

SQL command to create an index:CREATE INDEX CustomerNameON Customer (CustomerName);

Single-field indexKey is a single fieldAlso called a single-column index

Multiple-field indexMore than one key fieldAlso called a multiple-column index

Indexes (continued)

16

FIGURE 4-13: Creating an index on a single field in Access

Indexes (continued)

17

FIGURE 4-14: Creating a multiple-field index in Access

Security

18

Prevention of unauthorized access to database

Database administrator determines types of access various users can have

SQL security mechanismsGRANT: provides privileges to usersGRANT SELECT ON Customer TO Jones;

REVOKE: removes privileges from usersREVOKE SELECT ON Customer FROM Jones;

Integrity Rules

19

Two integrity rules must be enforced by a relational DBMSIntegrity rules defined by Dr. E.F. Codd Entity integrityReferential integrity

Entity Integrity

20

No field that is part of primary key may accept null values

To specify primary key in SQL:Enter a PRIMARY KEY clause in either an

ALTER TABLE or a CREATE TABLE commandTo designate primary key in Access:

Select primary key field in Table Design viewClick the Primary Key button in the Tools

group on the Table Tools Design tab

Entity Integrity (continued)SQL command to specify a primary key:PRIMARY KEY (CustomerNum)

21

FIGURE 4-15: Specifying a primary key in Access

Entity Integrity (continued)SQL command when more than one field included:PRIMARY KEY (OrderNum, PartNum)

22

FIGURE 4-16: Specifying a primary key consisting of more than one field in Access

Referential Integrity

23

Foreign key: field(s) whose value is required to match the value of the primary key for a second table

Referential integrity: if table A contains a foreign key that matches the primary key of table B, the values of this foreign key must match the value of the primary key for some row in table B or be null

To specify referential integrity in SQL:FOREIGN KEY clause in either the CREATE

TABLE or ALTER TABLE commands

Referential Integrity (continued)

24

To specify a foreign key, must specify both:Field that is a foreign keyTable whose primary key the field is to match

Example:FOREIGN KEY (RepNum) REFERENCES Rep

In Access, specify referential integrity while defining relationships

Referential Integrity (continued)

25

FIGURE 4-18: Specifying referential integrity in Access

Referential Integrity (continued)

26

FIGURE 4-19: Referential integrity violation when attempting to add a record

Legal-Values Integrity

27

Legal values: set of values allowable in a field

Legal-values integrity: no record can exist with a value in the field other than one of the legal values

SQLCHECK clause enforces legal-values integrityExample:CHECK (CreditLimit IN (5000, 7500, 10000, 15000))

Legal-Values Integrity (continued)

28

AccessValidation rule: must be followed by data

enteredValidation text: informs user of the reason

for rejection of data that violates the rule

Legal-Values Integrity (continued)

29

FIGURE 4-21: Specifying a validation rule in Access

Structure Changes

30

Examples of changes to database structure Adding and removing tables and fieldsChanging characteristics of existing fieldsCreating and dropping indexes

SQL ALTER TABLE command changes table’s structure

To add a new field to the Customer table:ALTER TABLE CustomerADD CustType CHAR(1);

Structure Changes (continued)

31

FIGURE 4-22: Adding a field in Access

Structure Changes (continued)

32

Changing properties of existing fieldsALTER TABLE CustomerCHANGE COLUMN CustomerName TO CHAR(40);

Deleting a field from a tableALTER TABLE PartDELETE Warehouse;

DROP TABLE command deletes a tableDROP TABLE SmallCust;

Structure Changes (continued)

33

FIGURE 4-23: Changing a field property in Access

Structure Changes (continued)

34

FIGURE 4-24: Dialog box that opens when a field in Access is deleted

Structure Changes (continued)

35

FIGURE 4-25: Deleting a table in Access

Making Complex Changes

36

Some changes might not be allowed by your DBMS

In these situations, you can:Use CREATE TABLE command to describe the

new tableInsert values into it using INSERT command

combined with a SELECT clauseSELECT INTO command can create the new

table in a single operation

System Catalog

37

System catalog (or catalog)Contains information about tables in the

databaseMaintained automatically by DBMS

Example catalog has two tablesSystables: information about the tables

known to SQLSyscolumns: information about the columns

or fields within these tables

System Catalog (continued)

38

Other possible tablesSysindexes: information about indexesSysviews: information about views

Catalog can be used to determine information about the structure of the database

Documenter: allows user to print detailed documentation about any table, query, report, form, or other object in the database

MySQL uses SHOW TABLES, SHOW INDEXES, and SHOW COLUMNS commands

Stored Procedures

39

Client/server system Database resides on a computer called the

server Users access database through clients

ClientComputer connected to a networkHas access through server to the database

Stored Procedures (continued)

40

Stored procedureSpecial file used to store a query that is run

oftenPlaced on the serverImproves overall performanceConvenience

Stored Procedures (continued)

41

MySQLDelimiter: semicolon at the end of a MySQL

commandNeed to temporarily change the delimiter for

a stored procedureTo use a stored procedure: CALL followed by

the procedure nameAccess does not support stored procedures

Use a parameter query instead

Triggers

42

Action that occurs automatically in response to an associated database operation such as an INSERT, UPDATE, or DELETE command

Stored and compiled on the serverNeed to temporarily change the delimiterAccess does not support triggers

Access 2010 has data macros that have similar functionality

Data Macros in Access 2010

43

Figure 4-29: Macro Designer window for the After Insert event associated with the OrderLine table

Summary

44

Views give each user his or her own view of the data in a database

Indexes facilitate data retrieval from the database

Security is provided in SQL systems using the GRANT and REVOKE commands

Entity integrity: no field that is part of the primary key can accept null values

Referential integrity: value in any foreign key field must be null or must match an actual value in the primary key field of another table

Summary (continued)

45

Legal-values integrity: value entered in a field must be one of the legal values that satisfies some particular condition

ALTER TABLE command allows you to add fields to a table, delete fields, or change the characteristics of fields

In Access, change the structure of a table by making the changes in the table design

DROP TABLE command lets you delete a table from a database

Summary (continued)

46

In Access, delete a table by selecting the Delete command on the table’s shortcut menu in the Navigation Pane

System catalog stores information about the structure of a database

Stored procedure: query saved in a file that users can execute later

Trigger: action that occurs automatically in response to an associated database operation such as an INSERT, UPDATE, or DELETE

Data macros: Access 2010 equivalent of triggers

Chapter 4 HomeworkDue: 2/28/2013Pages: 150 (7e) or 145 (6e)Do all ODD questions in the Premiere

Products Exercises.

47