Transcript cos346day20

COS 346
Day 20
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-1
Agenda
• Questions?
• 2 Quizzes 2 go
– April 20
• DP Chap 9 & 11, SQL Chap 11
– May 4
• DP Chap 12 - 15
• Assignment 8 posted
– Due April 13
– 3 more assignments to Go
– Lowest assignment score dropped
• Capstone progress report Due
– Capstones projects and presentations are due May 12 at 10AM
• Today we will be discussing Managing Multi-user Databases
with SQL server 2000
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-2
David M. Kroenke’s
Database Processing:
Fundamentals, Design, and Implementation
Chapter Eleven:
Managing Databases with
SQL Server 2000
Part One
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-3
Introduction
• SQL Server 2000 can be installed on:
– Windows 2000 and Windows XP workstations
– Windows 2000 Server and Windows Server 2003
• There are two ways to create database, to insert data,
and to modify its structure:
– Use the GUI SQL Server Enterprise Manager
– Write SQL statements and submit them to SQL Server via the
SQL Query Analyzer utility
• Many SQL Server professionals choose to create
structures via SQL then modify them with the graphical
tools
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-4
View Ridge Gallery
• View Ridge Gallery is a small art gallery that has
been in business for 30 years
• It sells contemporary European and
North American fine art
• View Ridge has one owner,
three salespeople, and two workers
• View Ridge owns all of the art that it sells;
it holds no items on a consignment basis
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-5
Application Requirements
• View Ridge application requirements:
– Track customers and their artist interests
– Record gallery's purchases
– Record customers' art purchases
– List the artists and works that have appeared
in the gallery
– Report how fast an artist's works have sold
and at what margin
– Show current inventory in a Web page
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-6
View Ridge Gallery Database Design
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-7
The IDENTITY Keyword
• SQL Server supports the SQL-92 standard
• The IDENTITY keyword implements a new
constraint for surrogate keys:
– IDENTITY (m, n) creates a surrogate key with an
Identity Seed of m and an Identity Increment of n:
CREATE TABLE CUSTOMER(
CustomerID int
NOT
Name
char(25)NOT
CONSTRAINT CustomerPK
CONSTRAINT CustomerAK1
);
NULL IDENTITY (1000,1),
NULL,
RIMARY KEY (CustomerID),
UNIQUE (Name)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-8
SQL Server Enterprise Manager
Right-Click Databases, then use New Database… to create a new database
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-9
Creating a Database
Type in the new
database name, and
then click the OK
button.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-10
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-11
SQL Query Analyzer:
Starting SQL Query Analyzer from Enterprise Manager
Use the menu
command Tools
| SQL Query
Analyzer.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-12
SQL Query Analyzer:
Creating the TRANS table in SQL Query Analyzer
Use the Execute
Query button
after the SQL
command is
entered.
Enter the SQL
command in this
window.
Results appear
in this window.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-13
David M. Kroenke’s
Database Processing
Fundamentals, Design, and Implementation
(10th Edition)
End of Presentation:
Chapter Eleven Part One
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-14
David M. Kroenke’s
Database Processing:
Fundamentals, Design, and Implementation
Chapter Eleven:
Managing Databases with
SQL Server 2000
Part Two
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-15
SQL Server CREATE TABLE Statements
for the View Ridge Schema
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-16
SQL Server CREATE TABLE Statements
for the View Ridge Schema
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-17
SQL Server CREATE TABLE Statements
for the View Ridge Schema
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-18
SQL Server CREATE TABLE Statements
for the View Ridge Schema
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-19
Viewing Tables
in Enterprise Manager
Right-Click
the table
name, then
click Design
Table to view
table
columns and
properties.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-20
Viewing Table Columns and
Properties in Enterprise Manager
Right-click
the white
space and
then click
Properties to
see table
constraints.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-21
Viewing Table Constraints in
Enterprise Manager
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-22
Creating a Relationship Diagram in
Enterprise Manager
Right-click
Diagrams,
then click
New
Database
Diagram to
create a
relationships
diagram.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-23
Table Relationship Diagram
Right-click a
relationship
line, then
click
Properties to
see the
properties.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-24
WORK – TRANS Relationship Properties
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-25
Creating Views:
By SQL in SQL Query Analyzer
Do NOT put a
semi-colon (;)
at the end of
a CREATE
VIEW
statement in
SQL Query
Analyzer!
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-26
Creating Views:
By GUI in Enterprise Manager
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-27
David M. Kroenke’s
Database Processing
Fundamentals, Design, and Implementation
(10th Edition)
End of Presentation:
Chapter Eleven Part Two
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-28
David M. Kroenke’s
Database Processing:
Fundamentals, Design, and Implementation
Chapter Eleven:
Managing Databases with
SQL Server 2000
Part Three
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-29
Indexes
• Indexes are special data structures used to
improve database performance
• SQL Server automatically creates an index on all
primary and foreign keys
• Additional indexes may be assigned on other
columns that are:
– Frequently used in WHERE clauses
– Used for sorting data
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-30
Indexes
• SQL Server supports two kinds of indexes:
– Clustered index: the data are stored in the bottom
level of the index and in the same order as that index
– Nonclustered index: the bottom level of an index
contains pointers to the data
• Clustered indexes are faster than nonclustered
indexes for updating and retrieval
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-31
Creating an Index:
By GUI in Enterprise Manager
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-32
Application Logic
• SQL Server database application can be
processed using:
– Programming language, e.g., C#, C++, Visual Basic,
Java, to invoke SQL Server DBMS commands
– Stored procedures
– SQL Query Analyzer to invoke database commands
stored in .sql files
– Triggers
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-33
Stored Procedures
• A stored procedure is a TRANSACT/SQL (T/SQL)
complied program stored within the database:
– T/SQL surrounds basic SQL statements with programming
constructs such as parameters, variables, and logic structures
such as IF and WHILE.
• Stored procedures are programs that can:
–
–
–
–
Have parameters
Invoke other procedures and functions
Return values
Raise exceptions
• Creating stored procedures
– Write a stored procedure in a text file and process the
commands using the Query Analyzer, or
– Using Enterprise Manager
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-34
Parameters
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
Variables are declared
after the keyword AS
9-35
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-36
Triggers
• An SQL Server trigger is a T/SQL procedure that is invoked when a
specified database activity occurs
• Triggers can be used to:
–
–
–
–
Enforce business rules
Set complex default values
Update views
Implement referential integrity actions
• SQL Server only supports INSTEAD OF and AFTER triggers:
– A table may have one or more AFTER triggers
– AFTER triggers may not be assigned to views
– A view or table may have only one INSTEAD OF trigger for each
triggering action
• Triggers can roll back the transactions that caused them to be fired
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-37
This is an AFTER
trigger on
INSERT on the
table TRANS.
It is will set a
default value on
AskingPrice.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-38
Triggers:
Enforcing a Required Child Constraint
There is an M-M relationship between WORK and TRANSACTION:
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-39
Triggers:
Enforcing a Required Child Constraint
• The hard way
using two
triggers – this
one enforces
the required
child:
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-40
Triggers:
Enforcing a Required Child Constraint
• The hard way
using two
triggers – this
one deletes any
duplicate
transaction:
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-41
Triggers:
Enforcing a Required Child Constraint
A better way - Create the Work_Trans view:
CREATE VIEW
SELECT
FROM
ON
Work_Trans AS
Title, Description, Copy,
ArtistID, DateAcquired, AcquisitionPrice
WORK W JOIN TRANS T
W.WorkID = T.WorkID;
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-42
Triggers:
Enforcing a Required Child Constraint
• A better way
using one
trigger – this
one works
with the
Work_Trans
view:
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-43
Concurrency Control
• Three factors determine the concurrency control
behavior of SQL Server:
– Transaction isolation level
– Cursor concurrency setting
– Locking hints provided in the SELECT clause
• Locking behavior also changes, depending on whether
actions occur in the context of transactions or cursors
independently
– Therefore, SQL Server places locks on behalf of the developer
– Locks may be placed at many levels of granularity and may be
promoted or demoted as work progresses
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-44
SQL Server Concurrency Options
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-45
SQL Server 2000 Security
• SQL server provides two modes of
authentication:
– Windows only: the authentication is provided by the
windows operating system
– Mixed security: SQL Server will accept either the
windows-authenticated user name or it will perform its
own authentication
• Roles may be assigned to a SQL Server user
account:
– A role is a group of predefined authorities
– Public role has the authority only to connect to the
database
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-46
SQL Server Backup
• SQL Server supports several types of backup:
– A complete backup makes a copy of the entire
database
– A differential backup makes a copy of the database
changes since the last complete backup
– Differential backups are faster and can be taken more
frequently
– Complete backups are simpler to use for recovery
• The transaction log also needs to be periodically
backed up
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-47
Database Recovery
• Both data and log files are created by SQL
Server
• SQL Server provides a wizard for setting up
database maintenance plan, e.g., scheduling
database and log backups
• To recover a database with SQL Server:
– The database is restored from a prior database
backup
– Log after images are applied to the restored database
– At the end of the log, changes from any transaction
that failed to commit are then rolled back
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-48
SQL Server Recovery Models
• Three recovery models
– Simple recovery: neither logging is done nor log
records applied:
• To recover a database is to restore the database to the last
backup
• This method can be used for a database that is never
changed
– Full recovery: all database changes are logged
– Bulk-logged: all changes are logged except those
that cause large log entries
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-49
David M. Kroenke’s
Database Processing
Fundamentals, Design, and Implementation
(10th Edition)
End of Presentation:
Chapter Eleven Part Three
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition
© 2006 Pearson Prentice Hall
9-50