Presentation is loading. Please wait.

Presentation is loading. Please wait.

IST359 M005 Yang Wang 342 Hinds SQL Programming And The External Model.

Similar presentations


Presentation on theme: "IST359 M005 Yang Wang 342 Hinds SQL Programming And The External Model."— Presentation transcript:

1 IST359 M005 Yang Wang ywang@syr.edu 342 Hinds http://blackboard.syr.edu SQL Programming And The External Model

2 Acknowledgements and caveat These slides draw liberally, with permission, from the following sources: IST359 materials by Prof. Michael Fudge Jr. Slides from Murach’s SQL Server 2008 book Caveat (beware): At best, PowerPoint slides are only a pale limitation of the entirety of a class meeting. In IST359 in particular, the lectures will cover topics beyond what appears in these slides. Don’t rely on them as a substitute for attending class.

3 Learning Objectives Understand the rationale and purpose of SQL programming Understand and explain the external data model Learn how to create views, stored procedures, and functions Learn how to perform program flow control in T-SQL

4 Where are we?

5 Internal –vs- External Data Models INTERNAL Optimal design for storing data Complex & atomic in nature Implemented with Tables, keys, constraints EXTERNAL Mimics user functionality Abstracted & simplified in nature Implemented with views, stored procedures, functions

6 The External Model == Abstractions Views – abstractions for the SELECT statement. Simplifies complex queries with joins, etc. Stored Procedures – abstractions for DML. Simplifies a complex series of INSERTS, UPDATES or DELETES. User-defined Functions – abstractions for custom program logic where values need to be returned.

7 The External Model

8 Create and set a variable DECLARE @today as DATETIME SET @today = getdate()

9 Create a procedure CREATE PROCEDURE dbo.p_add_zipcode ( @zip as char(5), @city as varchar(50), @state as char(2) ) AS BEGIN INSERT INTO zipcodetable ( ZIPCD, CITY, ST) VALUES ( @zip, @city, @state); RETURN @@ROWCOUNT; END

10 Call a procedure EXECUTE p_add_zipcode ‘13039’, ‘Cicero’, ‘NY’ EXECUTE @success = p_add_zipcode ‘90210’, ‘Bev Hills’, ‘CA’ IF @success=1 PRINT ‘Zip code Added!’

11 Create a function CREATE FUNCTION dbo.lookup_zip( @city as varchar(50), @state as char(2) ) RETURNS char(5) AS BEGIN RETURN (SELECT ZIPCD FROM zipcodetable WHERE CITY=@city AND ST=@state) END

12 Call a function SELECT lookup_zip(‘Clay’,’NY’) as ‘zip code’

13 FUDGE FICTITIOUS CREDIT UNION Let’s explore the external data model and T-SQL programming by example. QUICK DEMO

14 Basic Data Model (Internal Model)

15 FFCU – Simplified User Stories 1.When a person signs up, then they become a new customer. All new customers have an opening balance in their savings account. 2.A customer can deposit funds into their account. 3.A customer can withdrawal funds from their account 4.A customer can have view balances their 2 types of accounts, Checking and Savings 5.A customer can transfer funds between their accounts. (checking to savings and vice-versa)

16 External Model p_signup – sign a user up for an account p_deposit – deposit funds into user’s account p_withdrawl – take out funds from a user’s account p_transfer – transfer funds from one user account to another v_myaccounts – show balances for the user’s account

17 p_signup INSERT INTO Customers... INSERT INTO Accounts… – (Savings with initial starting amount) INSERT INTO Accounts… – (Checking)

18 p_transfer UPDATE Accounts… – (Remove amount from account) UPDATE Accounts… – (Add amount to account)

19 v_myaccounts Show detailed account information for Customer, Account(s), and Type(s) of Accounts. Create a view ffcu_vMyAccounts Read the view select * from ffcu_vMyAccounts


Download ppt "IST359 M005 Yang Wang 342 Hinds SQL Programming And The External Model."

Similar presentations


Ads by Google