Download presentation
Presentation is loading. Please wait.
Published byIrma Berry Modified over 9 years ago
1
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics
2
2 Concepts of Database Management, 5t h Edition Objectives u Define, describe, and use views u Use indexes to improve database performance u Examine the security features of a database management system (DBMS) u Discuss entity, referential, and legal-values integrity u Make changes to the structure of a relational database u Define and use the system catalog
3
3 Concepts of Database Management, 5t h Edition Views u Application program’s or individual user’s picture of the database u Less involved than full database u Offers simplification u Provides measure of security l Sensitive tables or columns omitted where not appropriate
4
4 Concepts of Database Management, 5t h Edition SELECT Command u Called the defining query u Indicates precisely what to include in the view u Query acts as a sort of window into the database u Does not produce a new table, only the view of the table
5
5 Concepts of Database Management, 5t h Edition Figure 4.1: SQL to Create View CREATE VIEW Housewares AS SELECT PartNum, Description, OnHand, Price FROM Part WHERE Class=‘HW’ ;
6
6 Concepts of Database Management, 5t h Edition Query on a View u With a query that involves a view, the DBMS changes the query to one that selects data from table(s) in the database that created the view u The DBMS merges the query with the query that defines the view to form the query that is actually executed u One advantage of this approach is that the view never exists in its own right so any update to the table is immediately available in the view u If the view were a table, this would not be the case
7
7 Concepts of Database Management, 5t h Edition Query on a View u Selects data only from Tables created in the view u Query is merged with query used to create view SELECT * FROM Housewares WHERE OnHand< 25 ; SELECT PartNum, Description, OnHand, Price FROM Part WHERE Class=‘HW’ AND OnHand< 25 ; Actually executes as
8
8 Concepts of Database Management, 5t h Edition Figures 4.3 - 4.4: Access Query Design of View
9
9 Concepts of Database Management, 5t h Edition Access Query Design View with Changed Field Names u SQL can be used to change the field names in a view by including the new field names in the CREATE VIEW statement u The CREATE VIEW statement would be: CREATE VIEW SalesCust (Snum, SLast, SFirst, Cnum, CName) AS SELECT Rep.RepNum, LastName, FirstName, CustomerNum, CustomerName FROM Rep, Customer WHERE Rep.RepNum=Customer.RepNum ;
10
10 Concepts of Database Management, 5t h Edition Figures 4.5-4.6: Access Query Design of View with Changed Field Names
11
11 Concepts of Database Management, 5t h Edition Row and Column Subset View u Consists of a subset of the rows and columns in some individual table u Because the query can be any SQL query, a view could also join two or more tables
12
12 Concepts of Database Management, 5t h Edition Advantages of Views u Provides data independence u Same data viewed by different users in different ways u Contains only information required by a given user
13
13 Concepts of Database Management, 5t h Edition Indexes u Conceptually similar to book index u Increases data retrieval efficiency u Automatically assigns record numbers u Used by DBMS, not by users u Fields on which index built called Index Key
14
14 Concepts of Database Management, 5t h Edition Figure 4.10: Customer Table with Record Numbers
15
15 Concepts of Database Management, 5t h Edition Figure 4.11: Customer Table Index on CustomerNum
16
16 Concepts of Database Management, 5t h Edition Figure 4.12: Table Indexes on CreditLimit, RepNum
17
17 Concepts of Database Management, 5t h Edition Pros/Cons of Indexes u Can be added or dropped without loss of function u Can make retrieval more efficient u Occupies space that might be required for other functions u DBMS must update index whenever corresponding data are updated
18
18 Concepts of Database Management, 5t h Edition SQL to Create Index CREATE INDEX CustomerName ON Customer (CustomerName) ;
19
19 Concepts of Database Management, 5t h Edition Creating Indexes u Single-field index – an index whose key is a single field u Multiple-field index l An index with more than one key field l List the most important key first l If data for either key appears in descending order, follow the field name with the letters DESC
20
20 Concepts of Database Management, 5t h Edition SQL to Delete Index DROP INDEX RepBal ;
21
21 Concepts of Database Management, 5t h Edition Figure 4.13: Index on Single Field in Access
22
22 Concepts of Database Management, 5t h Edition Figure 4.14: Index on Multiple Fields in Access
23
23 Concepts of Database Management, 5t h Edition Security u Prevention of unauthorized access to database u Two SQL security mechanisms l GRANT provides privileges to users l REVOKE removes privileges from users GRANT SELECT ON Customer TO JONES ; REVOKE SELECT ON Customer FROM JONES ;
24
24 Concepts of Database Management, 5t h Edition Integrity Rules u Related to foreign keys and primary keys u Defined by Dr. E.F. Codd u Entity integrity l No field that is part of the primary key may accept null values
25
25 Concepts of Database Management, 5t h Edition Integrity Rules (con’t) u To specify primary key, enter a PRIMARY KEY clause in either an ALTER TABLE or a CREATE TABLE command u Foreign key – a field (or collection of fields) in a table whose value is required to match the value of the primary key for a second table
26
26 Concepts of Database Management, 5t h Edition Figure 4.15: Primary Key in Access PRIMARY KEY (CustomerNum)
27
27 Concepts of Database Management, 5t h Edition Figure 4.16: Multi-Field Primary Key in Access PRIMARY KEY (OrderNum, PartNum)
28
28 Concepts of Database Management, 5t h Edition Referential integrity u If Table A contains a foreign key matching the primary key of Table B, then values must match for some row in Table B or be null u Usually a foreign key is in a different table from the primary key it is required to match u The only restriction is that the foreign key must have a name that is different from the primary key because the fields are in the same table
29
29 Concepts of Database Management, 5t h Edition Figure 4.17: Relationships Window to Relate Tables in Access FOREIGN KEY (RepNum) REFERENCES Rep
30
30 Concepts of Database Management, 5t h Edition Cascade Delete and Update u Cascade delete - ensures that the deletion of a master record deletes all records in sub tables related to it u Cascade update – ensures that changes made to the primary key of the master table are also made in the related records
31
31 Concepts of Database Management, 5t h Edition Figure 4.18: Specifying Referential Integrity
32
32 Concepts of Database Management, 5t h Edition Enforcing Referential Integrity u With referential integrity enforced, users are not allowed to enter a record that does not match any sales rep currently in the Rep table u An error message, such as the one shown in Figure 4.19, appears when an attempt is made to enter an invalid record
33
33 Concepts of Database Management, 5t h Edition Figure 4.19: Violating Referential Integrity on Adding
34
34 Concepts of Database Management, 5t h Edition Legal-Values Integrity u States no record can exist with field values other than legal ones u Use SQL CHECK clause u Validation rule – in Access, a rule that data entered into a field must follow u Validation – in Access, text to inform the user of the reason for the rejection when the user attempts to enter data that violates the rule CHECK (CreditLimit IN (5000, 7500, 10000, 15000)) ;
35
35 Concepts of Database Management, 5t h Edition Validation Rule in Access
36
36 Concepts of Database Management, 5t h Edition Structure Changes u Can change the database structure l By adding and removing tables and fields l By changing the characteristics of existing fields l By creating and dropping indexes u The exact manner in which these changes are accomplished varies from one system to another u Most systems allow all of these changes to be made quickly and easily u Made using the SQL ALTER TABLE command
37
37 Concepts of Database Management, 5t h Edition Structure Changes – Add and Change ALTER TABLE Customer ADD CustType CHAR(1) ; Adding new field Changing field properties ALTER TABLE Customer CHANGE COLUMN CustomerName TO CHAR(50) ;
38
38 Concepts of Database Management, 5t h Edition Figure 4.22: Add Field in Access
39
39 Concepts of Database Management, 5t h Edition Figure 4.23: Change Field Characteristic in Access
40
40 Concepts of Database Management, 5t h Edition Structure Changes - Delete ALTER TABLE Part DELETE Warehouse ; Deleting field Delete SQL Table DROP TABLE SmallCust ;
41
41 Concepts of Database Management, 5t h Edition Figure 4.24: Delete Field in Access
42
42 Concepts of Database Management, 5t h Edition Figure 4.25: Delete Table in Access
43
43 Concepts of Database Management, 5t h Edition System Catalog u Information about database kept in system catalog u Maintained by DBMS u Example catalog has two tables l Systables – information about the tables known to SQL l Syscolumns – information about the columns or fields within these tables
44
44 Concepts of Database Management, 5t h Edition System Catalog (con’t.) u Other possible tables l Sysindexes – information about the indexes that are defined on these tables l Sysviews – information about the views that have been created
45
45 Concepts of Database Management, 5t h Edition Figure 4.26: Systables Table
46
46 Concepts of Database Management, 5t h Edition Summary u Views - used to give each user his or her own view of the data in a database u View is defined in structured query language (SQL) by using a defining query u Indexes are often used to facilitate data retrieval from the database u Security is provided in SQL systems using the GRANT and REVOKE commands u Entity integrity is the property that states that no field that is part of the primary key can accept null values
47
47 Concepts of Database Management, 5t h Edition Summary u Referential integrity - property stating that the value in any foreign key field must either be null or match an actual value in the primary key field of another table u Legal-values integrity is the property that states that the value entered in a field must be one of the legal values u The ALTER TABLE command allows you to add fields to a table, delete fields, or change the characteristics of fields
48
48 Concepts of Database Management, 5t h Edition Summary u The DROP TABLE command lets you delete a table from a database u The system catalog is a feature of many relational DBMSs that stores information about the structure of a database
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.