Chapter4 Relational Model3
Chapter4 Relational Model3
Sixth Edition
Chapter 4
The Relational Model 3: Advanced
Topics
Objectives
Objectives (continued)
Make changes to the structure of a relational
database
Define and use the system catalog
Discuss stored procedures and triggers
Views
View: application programs or individual users
picture of the database
Less involved than full database
Simplification
Security
Views (continued)
Defining query: SELECT command that creates a
view
Indicates what to include in the view
Views (continued)
CREATE VIEW Housewares AS
SELECT PartNum, Description, OnHand, Price
FROM Part
WHERE Class='HW'
;
Views (continued)
Execution By DBMS
SELECT *
FROM Housewares
WHERE OnHand<25;
Views (continued)
To create a view in Access, create and save a
query
Changing field names in a view
SQL: include the new field names in the CREATE
VIEW command
Access: precede the name of the field with the
desired name, followed by a colon
Views (continued)
SQL command to create Housewares view:
CREATE VIEW Housewares (Pnum, Pdesc, OnHd,
Price) AS
SELECT PartNum, Description, OnHand, Price
FROM Part
WHERE Class='HW'
;
10
Views (continued)
11
Views (continued)
FIGURE 4-5: Access query design of the Housewares view with changed field names
12
Views (continued)
A view can join two or more tables
Advantages of views
Data independence
Each user has his or her own view
View should contain only fields required by the user
Greatly simplifies users perception of database
Security
13
Views (continued)
SQL command to create VSalesCust view from more than
one table:
CREATE VIEW VSalesCust (SNum, SLast,
SFirst, CNum, CName) AS
SELECT Rep.RepNum, LastName, FirstName,
CustomerNum, CustomerName
FROM Rep, Customer
WHERE Rep.RepNum=Customer.RepNum
;
Concepts of Database Management
14
Indexes
Conceptually similar to book index
Increase data retrieval efficiency
Record numbers automatically assigned and used
by DBMS
Index key: field or combination of fields on which
index is built
Advantages
Makes some data retrieval more efficient
15
Indexes (continued)
16
Indexes (continued)
FIGURE 4-11: Index for the Customer table on the CustomerNum field
17
Indexes (continued)
Disadvantages
Occupies space on disk
DBMS must update index whenever corresponding
data are updated
18
Indexes (continued)
SQL command to create an index:
CREATE INDEX ind_CustomerName
ON Customer (CustomerName)
;
Single-field index
Key is a single field
Also called a single-column index
Multiple-field index
More than one key field
Also called a multiple-column index
Concepts of Database Management
19
Indexes (continued)
20
10
Indexes (continued)
21
Security
Prevention of unauthorized access to database
Database administrator determines types of access
various users can have
SQL security mechanisms
GRANT: provides privileges to users
GRANT SELECT ON Customer TO Jones
;
22
11
Integrity Rules
Two integrity rules must be enforced by a relational
DBMS
Integrity rules defined by Dr. E. F. Codd
Entity integrity
Referential integrity
23
Entity Integrity
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 command
24
12
25
FIGURE 4-16: Specifying a primary key consisting of more than one field in
Access
26
13
Referential Integrity
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
Concepts of Database Management
27
Example:
FOREIGN KEY (RepNum) REFERENCES Rep
28
14
29
30
15
Legal-Values Integrity
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
SQL
CHECK clause enforces legal-values integrity
Example:
CHECK (CreditLimit IN (5000, 7500, 10000, 15000))
31
32
16
33
Structure Changes
Examples of changes to database structure
Adding and removing tables and fields
Changing characteristics of existing fields
Creating and dropping indexes
34
17
35
36
18
37
FIGURE 4-24: Dialog box that opens when a field in Access is deleted
38
19
39
40
20
System Catalog
System catalog (or catalog)
Contains information about tables in the database
Maintained automatically by DBMS
41
42
21
Stored Procedures
Client/server system
Database resides on a computer called the server
Users access database through clients
Client
Computer connected to a network
Has access through server to the database
43
44
22
45
46
23
Triggers
Action that occurs automatically in response to an
associated database operation such as an
INSERT, UPDATE, or DELETE command
Stored and compiled on the server
Need to temporarily change the delimiter
Access does not support triggers
47
48
24
49
50
25
Summary
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
Concepts of Database Management
51
Summary (continued)
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
Concepts of Database Management
52
26
Summary (continued)
In Access, delete a table by selecting the Delete
command on the tables 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
Concepts of Database Management
53
27