A Guide To SQL, Ninth Edition: Chapter Seven Database Administration
A Guide To SQL, Ninth Edition: Chapter Seven Database Administration
Chapter Seven
Database Administration
Objectives
• Understand, create, and drop views
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 3
Introduction
• Database administration
– Process of managing a database
• Database administrator
– Person or entire group in a business
organization charged with managing the
database
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 4
Creating and Using Views
• View
– A program’s or individual user’s picture of the
database
• Base tables
– Existing, permanent tables in a relational database
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 5
Creating and Using Views
(continued)
• Usually includes less information than full
database
– Simplifies data processing for the user
– Provides a measure of security by omitting sensitive
information
• Unavailable to user
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 6
Creating and Using Views
(continued)
• Created by a defining query
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 7
Creating and Using Views
(continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 9
Creating and Using Views
(continued)
• Data shown in Figure 7-2 does not exist in this
form
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 10
Creating and Using Views
(continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 11
Creating and Using Views
(continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 12
Creating and Using Views
(continued)
• Actual query executed by SQL
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 13
Creating and Using Views
(continued)
• Can assign column names in view that are
different than base table
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 14
Creating and Using Views
(continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 18
Using a View to Update Data
• Benefits of views are for retrieval purposes only
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 19
Updating Row-and-Column Subset
Views
• Can update (usually) if view contains primary key
• Cannot update when primary key is not included
No primary key
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 20
Updating Views Involving Joins
• Can update when a view is derived by joining
two tables on primary key of each table
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 21
Updating Views Involving Statistics
• Most difficult to update
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 22
Dropping a View
• Remove a view that is no longer needed with
DROP VIEW command
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 23
Security
• Prevention of unauthorized access to a
database
– Some users may be able to retrieve and update
anything in database
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 24
Security (continued)
• GRANT command
– Main mechanism for providing access to
database
• Database administrator can grant different
types of privileges to users and revoke
them later
• Privileges include rights to select, insert,
update, index, and delete table data
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 25
Security (continued)
• Database administrator uses REVOKE
command to remove privileges from users
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 26
Indexes
• Speeds up the searching of tables
• Similar to an index in a book
Figure 7-19: Index for the CUSTOMER table on the CUSTOMER_NUM column
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 27
Indexes (continued)
Figure 7-20: Indexes for the CUSTOMER table on the CREDIT_LIMIT and REP_NUM columns
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 28
Indexes (continued)
• SQL manages indexes
• User determines columns on which to
build indexes
• Disadvantages
– Index occupies disk space
– DBMS must update index as data is entered
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 29
Creating an Index
• Use CREATE INDEX command
– Name the index
– Identify the table
– Identify the column or columns
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 30
Creating an Index (continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 31
Creating an Index (continued)
Figure 7-22: Creating the REP_NAME index on the LAST_NAME and FIRST_NAME columns
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 32
Dropping an Index
• Use DROP INDEX to delete an index
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 33
Creating Unique Indexes
• To ensure uniqueness of non-primary key data,
you can create a unique index
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 34
System Catalog
• Contains information about tables in database;
also called data dictionary
• Use SYSTABLES to list all tables in database
• Use SYSCOLUMNS to list all columns in a table
• Use SYSVIEWS to list information about views
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 35
System Catalog (continued)
• In Oracle, use:
– DBA_TABLES to list information about tables
– DBA_TAB_COLUMNS to list information
about columns
– DBA_VIEWS to list information about views
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 36
Update of the System Catalog
• DBMS updates automatically when
– Create, alter, drop tables
– Create or drop indexes
• Users should not update catalog directly
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 37
Integrity Constraints in SQL
• Rule for the data in the database
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 38
Integrity Constraints in SQL
(continued)
• Integrity support is process of specifying integrity
constraints for the database
• Clauses to support integrity constraints can be
specified within a CREATE TABLE or
ALTER TABLE command
– ADD PRIMARY KEY
– ADD FOREIGN KEY
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 39
Integrity Constraints in SQL
(continued)
• Primary keys
– Use ADD PRIMARY KEY clause on ALTER TABLE
command to add after creating a table
• Foreign keys
– A column in one table whose value matches the
primary key in another
• Legal values
– The CHECK clause ensures only legal values are
allowed in a given column
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 40
Integrity Constraints in SQL
(continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 41
Integrity Constraints in SQL
(continued)
• After creating a foreign key, DBMS rejects any
update that violates the foreign key constraint
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 42
Integrity Constraints in SQL
(continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 43
Integrity Constraints in SQL
(continued)
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 44
Summary
• Views
– CREATE VIEW command
– Benefits
– Update issues
– DROP VIEW command
• Security features
– GRANT
– REVOKE
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 45
Summary (continued)
• Indexes
– Make data retrieval more efficient
– CREATE INDEX
– DROP INDEX
• System catalog information
– SYSTABLES, SYSCOLUMNS, SYSVIEWS
– DBA_TABLES, DBA_TAB_COLUMNS,
DBA_VIEWS
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 46
Summary (continued)
• Integrity constraints
– ADD PRIMARY KEY
– ADD FOREIGN KEY
– CHECK
©2016 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as
permitted in a license distributed with a certain product or service or otherwise on a password-protected website for classroom use. 47