400 likes | 758 Views
Chapter 4. The Relational Model 3: Advanced Topics. Objectives. Define, describe, and use views Use indexes to improve database performance Discuss entity, referential, and legal-values integrity Make changes to the structure of a relational database Define and use the system catalog.
E N D
Chapter 4 The Relational Model 3: Advanced Topics Concepts of Database Management, 4th Edition, Pratt & Adamski
Objectives • Define, describe, and use views • Use indexes to improve database performance • Discuss entity, referential, and legal-values integrity • Make changes to the structure of a relational database • Define and use the system catalog Concepts of Database Management, 4th Edition, Pratt & Adamski
Views • Application program’s or individual user’s picture of the database • Less involved than full database • Offers simplification • Provides measure of security • Sensitive tables or columns omitted where not appropriate Concepts of Database Management, 4th Edition, Pratt & Adamski
SQL to Create ViewFigure 4.1 CREATE VIEW Housewares AS SELECT PartNum, Description, OnHand, Price FROM Part WHERE Class=‘HW’ ; Concepts of Database Management, 4th Edition, Pratt & Adamski
Housewares View of Database Figure 4.2 Concepts of Database Management, 4th Edition, Pratt & Adamski
Query on a View • Selects data only from Tables created in the view • Query is merged with query used to create view SELECT * FROM Housewares WHERE OnHand< 25 ; Actually executes as SELECT PartNum, Description, OnHand, Price FROM Part WHERE Class=‘HW’ AND OnHand< 25 ; Concepts of Database Management, 4th Edition, Pratt & Adamski
Access Query Design of View Figures 4.3 - 4.4 Concepts of Database Management, 4th Edition, Pratt & Adamski
Access Query Design of View with Changed Field Names Figures 4.5 - 4.6 Concepts of Database Management, 4th Edition, Pratt & Adamski
SalesCust View SQL Statement CREATE VIEW SalesCust (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, 4th Edition, Pratt & Adamski
SalesCust View Figure 4.7 Concepts of Database Management, 4th Edition, Pratt & Adamski
Access Query Design of SalesCust View Figure 4.8 Concepts of Database Management, 4th Edition, Pratt & Adamski
Access Query Design of SalesCust View (con’t.) Figure 4.9 Concepts of Database Management, 4th Edition, Pratt & Adamski
Advantages of Views • Provides data independence • Same data viewed by different users in different ways • Contains only information required by a given user Concepts of Database Management, 4th Edition, Pratt & Adamski
Indexes • Conceptually similar to book index • Increases data retrieval efficiency • Automatically assigns record numbers • Used by DBMS, not by users • Fields on which index built called Index Key Concepts of Database Management, 4th Edition, Pratt & Adamski
Customer Table with Record Numbers Figure 4.10 Concepts of Database Management, 4th Edition, Pratt & Adamski
Customer Table Index on CustomerNum Figure 4.11 Concepts of Database Management, 4th Edition, Pratt & Adamski
Table Indexes on CreditLimit, RepNum Figure 4.12 Concepts of Database Management, 4th Edition, Pratt & Adamski
Pros/Cons of Indexes • Can be added or dropped without loss of function • Can make retrieval more efficient • Occupies space that might be required for other functions • DBMS must update index whenever corresponding data are updated Concepts of Database Management, 4th Edition, Pratt & Adamski
SQL to Create Index CREATE INDEX CustomerName ON Customer (CustomerName) ; Concepts of Database Management, 4th Edition, Pratt & Adamski
SQL to Delete Index DROP INDEX RepBal ; Concepts of Database Management, 4th Edition, Pratt & Adamski
Index on Single Field in Access Figure 4.13 Concepts of Database Management, 4th Edition, Pratt & Adamski
Index on Multiple Fields in Access Figure 4.14 Concepts of Database Management, 4th Edition, Pratt & Adamski
Security • Prevention of unauthorized access to database • Two SQL security mechanisms • GRANT provides privileges to users • REVOKE removes privileges from users GRANT SELECT ON Customer TO JONES ; REVOKE SELECT ON Customer FROM JONES ; Concepts of Database Management, 4th Edition, Pratt & Adamski
Integrity Rules • Related to foreign keys and primary keys • Defined by Dr. E.F. Codd • Entity integrity • No field that is part of the primary key may accept null values • Referential integrity • 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 Concepts of Database Management, 4th Edition, Pratt & Adamski
Primary Key in Access Figure 4.15 PRIMARY KEY (CustomerNum) Concepts of Database Management, 4th Edition, Pratt & Adamski
Multi-Field Primary Key in Access Figure 4.16 PRIMARY KEY (OrderNum, PartNum) Concepts of Database Management, 4th Edition, Pratt & Adamski
Relationships Window to Relate Tables in Access Figure 4.17 FOREIGN KEY (RepNum) REFERENCES Rep Concepts of Database Management, 4th Edition, Pratt & Adamski
Specifying Referential Integrity Figure 4.18 Concepts of Database Management, 4th Edition, Pratt & Adamski
Violating Referential Integrity on Adding Figure 4.19 Concepts of Database Management, 4th Edition, Pratt & Adamski
Violating Referential Integrity on Deleting Figure 4.20 Concepts of Database Management, 4th Edition, Pratt & Adamski
Legal-Values Integrity • States no record can exist with field values other than legal ones • Use SQL CHECK clause CHECK (CreditLimit IN (5000, 7500, 10000, 15000)) ; Concepts of Database Management, 4th Edition, Pratt & Adamski
Other SQL Adding new field ALTER TABLE Customer ADD CustType CHAR(1) ; Changing field properties ALTER TABLE Customer CHANGE COLUMN CustomerName TO CHAR(50) ; Concepts of Database Management, 4th Edition, Pratt & Adamski
Add Field in Access Figure 4.22 Concepts of Database Management, 4th Edition, Pratt & Adamski
Change Field Characteristic in Access Figure 4.23 Concepts of Database Management, 4th Edition, Pratt & Adamski
Other SQL Deleting field ALTER TABLE Part DELETE Warehouse ; DeleteSQL Table DROP TABLE SmallCust ; Concepts of Database Management, 4th Edition, Pratt & Adamski
Delete Field in Access Figure 4.24 Concepts of Database Management, 4th Edition, Pratt & Adamski
Delete Table in Access Figure 4.25 Concepts of Database Management, 4th Edition, Pratt & Adamski
System Catalog • Information about database kept in system catalog • Maintained by DBMS • Example catalog has two tables • Systables • Syscolumns Concepts of Database Management, 4th Edition, Pratt & Adamski
Systables Table Figure 4.26 Concepts of Database Management, 4th Edition, Pratt & Adamski
Partial Syscolumns Table Figure 4.27 Concepts of Database Management, 4th Edition, Pratt & Adamski