42
MIS 424 Guest Lecture

MIS 424 Guest Lecture

  • Upload
    kaili

  • View
    30

  • Download
    0

Embed Size (px)

DESCRIPTION

MIS 424 Guest Lecture. Welcome. MIS 424 Dr. Steve Ross April 30, 2007. Overview of SQL Server. - PowerPoint PPT Presentation

Citation preview

Page 1: MIS 424 Guest Lecture

MIS 424 Guest Lecture

Page 2: MIS 424 Guest Lecture

Welcome

Page 3: MIS 424 Guest Lecture

MIS 424Dr. Steve RossApril 30, 2007

Overview of SQL Server

Material for this lecture is drawn from SQL Server 2005 Database Essentials, SQL Queries for Mere Mortals, and the professor’s experience.

Star Wars sounds from http://www.galaxyfaraway.com/Multimedia/sounds.shtml

Page 4: MIS 424 Guest Lecture

Presentation Outline

Overview of Relational Database Management

Introduction to SQL Server Application Architecture

Page 5: MIS 424 Guest Lecture

Overview of Relational Database Management

How do databases “think”?

Page 6: MIS 424 Guest Lecture

What is a Relational Database Management System?

Software• Manages storage and retrieval of data

• Stored in two-dimensional tables Three major parts

• Data definition language (DDL)• Creation of the database structure

• Data manipulation language (DML)• CRUD

• Data control language (DCL)• Controls access to the data

Page 7: MIS 424 Guest Lecture

CRUD?

Create new records Read the data in existing recordsUpdate the data in existing recordsDelete records

A “complete” business application must accomplish the full range of CRUD on all fields of all tables.

Page 8: MIS 424 Guest Lecture

CRUD – SQL Commands

Create = INSERT Read = SELECT Update = UPDATE Delete = DELETE

Page 9: MIS 424 Guest Lecture

Sets

Tables are treated as sets• A command deals with the entire set or a

portion thereof determined by a filter

• No inherent order unless indexed

• Record-by-record processing requires special methods

The result of a SELECT command is another set

Page 10: MIS 424 Guest Lecture

Relating Data – Joins

Field(s) in one table compared to field(s) in another• Inner join … records with matching values

• Left/Right outer join … all records of one paired with records of the other with matching values

• Full outer join … all records of both

• Joins are normally based on equal values, but may be based on any relation between the values of the fields (e.g., <, >, between …)

Page 11: MIS 424 Guest Lecture

Sample QuerySELECT S.TERM,S.SUBJECT,S.COURSE_NUMBER,FIRST_NAME + ' ' + LAST_NAME AS S_NAMEFROM dbo.S_SCHEDULE S

INNER JOIN dbo.S_HIST_DETAIL HD ON S.TERM=HD.TERM AND S.CRN=HD.CRNINNER JOIN dbo.G_PERSON P ON HD.PIDM=P.PIDMINNER JOIN dbo.S_REG_DETAIL RD ON RD.PIDM=P.PIDM

WHERE S.PRIMARY_INSTRUCTOR_PIDM=5120 AND RD.TERM='200720' AND RD.CRN='21070'

UNION

SELECT S.TERM,S.SUBJECT,S.COURSE_NUMBER,FIRST_NAME + ' ' + LAST_NAME AS S_NAMEFROM dbo.S_SCHEDULE S

INNER JOIN dbo.S_REG_DETAIL RD0 ON S.TERM=RD0.TERM AND S.CRN=RD0.CRNINNER JOIN dbo.G_PERSON P ON RD0.PIDM=P.PIDMINNER JOIN dbo.S_REG_DETAIL RD ON RD.PIDM=P.PIDM

WHERE S.PRIMARY_INSTRUCTOR_PIDM=5120 AND RD.TERM='200720' AND RD.CRN='21070'

ORDER BY S.SUBJECT,S.COURSE_NUMBER,S.TERM

Page 12: MIS 424 Guest Lecture

ResultTERM SUBJECT COURSE_NUMBER S_NAME------ ------- ------------- ---------------------200540 MIS 320 Hyun Park200640 MIS 421 Hyun Park200710 MIS 421 Alexander Nichols200710 MIS 421 Daniel Stead200720 MIS 421 Adam Opitz200720 MIS 431 Alexander Nichols

(6 row(s) affected)

Page 13: MIS 424 Guest Lecture

Introduction to SQL Server

Is it any different from Access?

Page 14: MIS 424 Guest Lecture

SQL Server

Microsoft Product• Originally developed by Sybase and

purchased by Microsoft

“Recent” version: SQL Server 2000 “Current” version: SQL Server 2005

• A.k.a. Yukon

• Integrated with Whidbey – next version of Visual Studio

Page 15: MIS 424 Guest Lecture

What about Microsoft Access?

Both are DBMS Both allow a lot of records SQL-Server

• More power in DDL, DML, DCL

• Handles more data and more concurrent users Access

• Less expensive

• More readily available

• Includes user interface tools (forms and reports)

Page 16: MIS 424 Guest Lecture

Hierarchy of Objects

The Server: RELIANT.CBE.WWU.EDU• The Instance: MIS424S05 or (port #) 2767

• The Database: KKPMusicStoreDatabase Objects

• Specific Object:tblCustomers

Page 17: MIS 424 Guest Lecture

Security

On the server/instance• Logins

• Server Roles

In the database• Users

• Database Roles

Permissions• Action

• Object

Page 18: MIS 424 Guest Lecture

The DBA’s Three Favorite SQL Server Tools

Management Studio Object Explorer

Page 19: MIS 424 Guest Lecture

The DBA’s Three Favorite SQL Server Tools Management Studio Object Explorer

• Graphical interface that allows • Creation of database objects (DDL)

• Tables• Views• Stored procedures• User-defined functions

• Specification of rights (DCL)• Logins • Server roles• Database roles

• Allows direct editing of database data • Usually a bad idea!

Page 20: MIS 424 Guest Lecture

The DBA’s Three Favorite SQL Server Tools

Page 21: MIS 424 Guest Lecture

The DBA’s Three Favorite SQL Server Tools

Page 22: MIS 424 Guest Lecture

The DBA’s Three Favorite SQL Server Tools

Management Studio Query• Object browser

• Command line interface• Create and test statements

• Execute statements – DDL, DML, DCL

• Execution plan

Page 23: MIS 424 Guest Lecture

The DBA’s Three Favorite SQL Server Tools

Page 24: MIS 424 Guest Lecture

The DBA’s Three Favorite SQL Server Tools

Books Online• Help facility

• Help that’s actually helpful!

Page 25: MIS 424 Guest Lecture

Application Architecture

How do we get the information to and from the

user?

Page 26: MIS 424 Guest Lecture

N-Tier Architecture*

* Graphic from P.J. Pratt and J.J. Adamski, Concepts of Database Management, 4th Ed.

Page 27: MIS 424 Guest Lecture

Primary Concern of the DBA:Maintaining Data Integrity

Protect existence• Data are available when needed

Maintain quality• Data are accurate, complete, and current

Ensure confidentiality• Data are accessed only by those authorized

to do so

Page 28: MIS 424 Guest Lecture

How much access does the database administrator grant the programming staff?

DBA ProgrammerTables

Views

Procedures

Page 29: MIS 424 Guest Lecture

Maintaining Integrity in Tables

Field data type, size Check constraints Entity integrity (primary key) Referential integrity (foreign key) Triggers

• Procedures that execute on insert, update, or delete actions on the table

Page 30: MIS 424 Guest Lecture

Using Views to Maintain Integrity

(In Access, views are called queries) Specify only certain fields

• Output fields can be renamed to “hide” internal structure

Filter the records• Conditions can be based on characteristics of

the user, e.g., his/her ID, department, status

Page 31: MIS 424 Guest Lecture

Stored Procedures

Similar to sub-routines in other languages Provide all aspects of CRUD, most DDL, DCL

• Very good way to return result sets Transact-SQL (T-SQL) programming language Procedural structure

• Sequence

• Iteration

• Condition

Page 32: MIS 424 Guest Lecture

Stored Procedures cont’d

Input and output parameters provide a consistent interface between programmer and database administrator• Facilitates data structure changes

Allow a great deal of internal error-checking and validation

Page 33: MIS 424 Guest Lecture

Stored Procedure Example ICREATE PROCEDURE dbo.uspInsertNewPOS ( @WWUID nchar(10), @CurrID int, @StartQtr int, @Quarter int, @CourseAbbr

nchar (4), @CourseNumb nchar(4), @OtherCrs nvarchar(20), @ElectSet smallint, @ElectCrs smallint, @CourseCategory nchar(2) )

ASSET NOCOUNT ON

INSERT INTO tblStuPOS (WWUID,CurrID,StartQtr,ElectSet,ElectCrs,CourseAbbr,CourseNumb,Quarter,OtherCrs,CourseCategory) VALUES ( @WWUID, @CurrID, @StartQtr, @ElectSet, @ElectCrs, @CourseAbbr, @CourseNumb, @Quarter, @OtherCrs, @CourseCategory )

SET NOCOUNT OFFGO

This procedure accepts input data

via parameters and inserts it into

a record in a table.

Page 34: MIS 424 Guest Lecture

Stored Procedure Example IICREATE PROCEDURE dbo.uspFoundationCourses ( @CurrID int, @WWUID nchar(10) ) ASSET NOCOUNT ON

SELECT * INTO #tblSelectedStuPOS FROM tblStuPOS WHERE WWUID=@WWUID

SELECT C.CourseAbbr, C.CourseNumb, C.CurrID, C.DeptAbbr, C.DegreeName, C.Concentration,C.Requirement, isnull(P.Quarter,999999) as Quarter

FROM dbo.vueCurrCourses C LEFT OUTER JOIN #tblSelectedStuPOS P ON C.CourseAbbr=P.CourseAbbr AND C.CourseNumb = P.CourseNumb

WHERE (C.Requirement='FD') and C.CurrID=@CurrID ORDER BY C.CourseAbbr, C.CourseNumb

SET NOCOUNT OFFGO This procedure creates a temporary table

containing an individual’s data and outputs

a list of “foundation” courses the individual

must take for a specific major (CurrID).

Page 35: MIS 424 Guest Lecture

Stored Procedure Example IIICREATE PROCEDURE dbo.uspDeleteOldPOS

( @WWUID nchar(10))

ASSET NOCOUNT ON

DELETE FROM tblStuPOS WHERE WWUID=@WWUID

SET NOCOUNT OFFGO

This procedure accepts a WWU ID

number via a parameter and deletes

corresponding records in the table.

Page 36: MIS 424 Guest Lecture

User-Defined Functions

Similar to functions in other languages• Parameters for input

• Single result returned

T-SQL programming language Procedural structure

• Sequence

• Iteration

• Condition

Page 37: MIS 424 Guest Lecture

User-Defined Function Example ICREATE FUNCTION dbo.fnPassedCourse

(@dgrade float,@grade nvarchar(3))

RETURNS bit AS BEGIN

DECLARE @output bitIF @dgrade > 1.5 or upper(@grade) = 'S'

SET @output = 1ELSE

SET @output = 0RETURN @output

END This function returns a 1 if the person

received a grade of S, C– or better, a 0

otherwise. The actual grade is passed

to the function, but concealed in output.

Page 38: MIS 424 Guest Lecture

User-Defined Function Example IICREATE FUNCTION dbo.fnLatestDepartment

( @Person2Edit CHAR(9) )

RETURNS nvarchar(4) AS BEGIN

DECLARE @output nvarchar (4)SELECT @output = DeptAbbr

FROM dbo.tblAppointment WHERE PersonID=@Person2Edit AND TermCode = (SELECT MAX(TermCode) FROM dbo.tblAppointment WHERE PersonID = @Person2Edit)

RETURN @outputEND

Given a professor’s WWU ID, this

function returns the abbreviation of

the department associated with his

or her most recent appointment.

Page 39: MIS 424 Guest Lecture

Triggers

Attached to a table “Fires” on insert, update, or delete Able to access

• Old (deleted or updated) values

• New (inserted or updated) values Trigger can reference and change other

tables

Page 40: MIS 424 Guest Lecture

Trigger Examples CREATE TRIGGER ut_CreateSale ON [dbo].[tblSaleItem]

FOR INSERT, UPDATE AS UPDATE vueProductInventory

SET QuantityOnHand = QuantityOnHand - (SELECT QuantitySold FROM INSERTED) WHERE ProductCode = (SELECT ProductCode FROM INSERTED)

CREATE TRIGGER ut_DeleteSale ON [dbo].[tblSaleItem] FOR UPDATE, DELETE AS

UPDATE vueProductInventory SET QuantityOnHand = QuantityOnHand + (SELECT QuantitySold FROM DELETED)WHERE ProductCode = (SELECT ProductCode FROM DELETED)

These triggers change a quantity-on-

hand field in another table whenever a

sale is inserted, updated, or deleted.

Page 41: MIS 424 Guest Lecture

What are your questions?

Page 42: MIS 424 Guest Lecture

Thank You for your attention and interest