5. RDBMS Project This project provided a list of columns and business rules that are to be used as a guide for development of the proposed PiggyBank database. The goal is a database in the third normal form but the developer is allowed to denormalize for a valid business reason. The column list that is provided has redundancies and new columns may be necessary. The developer is to make the best choices for primary and foreign key constraints. The expected deliverable is an ER diagram with crows-foot notation.
7. Bank Project We are given a modified version of the ER diagram we developed earlier and tasked with developing stored procedures for the following: Create new customer and update customer Create new account (savings or checking), update account status and add user Change account status, close account, reactivate account Deposit, Withdrawal and Transfer Overdraft setup (general and specific accounts) ATM procedures Show balance for last five transactions Show transaction history for the last 30 days Additionally create a trigger to prevent drop and alteration of tables plus a trigger to prevent deletes and updates to the transaction table (and any others deemed necessary)
9. Bank Project – Try/Catch Block BEGIN TRY -validate parameters - -state codes are used to send outcome to the caller BEGIN TRANSACTION -set Nocount on to prevent extra results from interfering with select statements BEGIN -body of procedure END COMMIT TRANSACTION END TRY BEGIN CATCH . IF @@TRANCOUNT>0 ROLLBACK TRANSACTION --Declare local variables to hold the error passed back to the caller DECLARE @ERRORMESSAGE NVARCHAR(4000); DECLARE @ERRORSEVERITY INT; DECLARE @ERRORSTATE INT; -- Set the variables to the error values SELECT @ERRORMESSAGE = ERROR_MESSAGE(), @ERRORSEVERITY = ERROR_SEVERITY(), @ERRORSTATE = ERROR_STATE(); --Raise the error to the caller with the error variables RAISERROR (@ERRORMESSAGE, @ ERRORSEVERITY,@ERRORSTATE) END CATCH
10. Bank Project – New Account Procedure BEGIN TRANSACTION -- INSERT INTO ACCOUNT TABLE B EGIN BEGIN INSERT account (AccountTypeID, AccountStatusID,CurrentBalance, O verDraftAccountID, GeneralOverDraft) VALUES (@AccountTypeID, 1, @InitialAmount, @OverDraftAccountID, @GeneralOverDraft) END --Declare local variable to hold the new AccountID -to be used to add new records to the Transactions and CustomerAccount tables declare @newAccountNo int --Set the value to the new AccountID set @newAccountNo=scope_identity() BEGIN -- INSERT INTO TRANSACTIONS TABLE INSERT TRANSACTIONS (AccountID, TransactionTypeID, CustomerID, TransactionDate, TransactionAmount, NewBalance) Values (@NewAccountNo, 1, @CustomerID, getdate(), @InitialAmount, @InitialAmount) END BEGIN --INSERT INTO CUSTOMERACCOUNT TABLE INSERT CustomerAccount (AccountID, CustomerID) VALUES (@newAccountNo, @CustomerID) END END COMMIT TRANSACTION
11. Bank Project – Transfer Funds Procedure BEGIN TRANSACTION SET NOCOUNT ON; declare @BeginOutBalance money set @BeginOutBalance=( SELECT CurrentBalance FROM Account WHERE Accountid=@FromAccountID) BEGIN INSERT Transactions (AccountID, TransactionTypeId, CustomerID, TransactionDate, TransactionAmount, NewBalance) VALUES (@FromAccountId, 4, @CustomerID, Getdate(), @TransferAmount, @BeginOutBalance-@TransferAmount) END DECLARE @NewOutbalance INT SET @NewOutBalance=(@BeginOutBalance-@TransferAmount) BEGIN Update Account SET CurrentBalance=@NewOutBalance WHERE AccountID=@FromAccountID END DECLARE @BeginInBalance money SET @BeginInBalance=( SELECT CurrentBalance FROM Account WHERE Accountid=@ToAccountID) BEGIN INSERT Transactions (AccountID, TransactionTypeId, CustomerID, TransactionDate, TransactionAmount, NewBalance) VALUES (@ToAccountId, 5, @CustomerID, Getdate(), @TransferAmount, @BeginInBalance+@TransferAmount) END DECLARE @NewInBalance INT SET @NewInBalance=(@BeginInBalance+@TransferAmount) BEGIN Update Account SET CurrentBalance=@NewInBalance WHERE AccountID=@ToAccountID END COMMIT TRANSACTION
12. Bank Project – DDL Trigger CREATE TRIGGER tgr_ddl_safe ON DATABASE FOR ALTER_VIEW, DROP_VIEW, ALTER_TABLE, DROP_TABLE AS BEGIN SET NOCOUNT ON --Throw an error RAISERROR('You may not drop or alter database objects.',11,1) IF @@TRANCOUNT>0 BEGIN ROLLBACK TRANSACTION END END
13. Bank Project – DML Trigger CREATE TRIGGER [dbo].[delTransactions] on [dbo].[Transactions] INSTEAD OF DELETE, UPDATE AS BEGIN SET NOCOUNT ON; DECLARE @DeleteCount int; SELECT @DeleteCount=Count(*) from Deleted; if @DeleteCount>0 BEGIN RAISERROR (N'Transactions cannot be deleted or updated.', 10,1); IF @@TRANCOUNT >0 BEGIN ROLLBACK TRANSACTION; END END; END;
14. Library Project A database has been created to support the principal functions of a lending library’s day-to-day operations: adding new members (adult and juvenile), checking books out and in, adding new books, renewing memberships, changing a juvenile record to an adult record once the juvenile reaches 18 and updating adult record information. Books are identified by International Standard Book Number (ISBN). Books with the same title will have different ISBN based upon language and cover type. The library also holds multiple copies of each ISBN. Loan history is considered critical information and must be kept indefinitely. Library memberships are good for one year from issue date. Adults are issued memberships and juveniles are associated with an adult sponsor until they reach 18. Books are checked out for 14 days and members may have only 4 books out at any one time. The deliverable is to create stored procedures to support system functionality and any necessary views and a variety of reports that can be viewed online.
16. Library Project – Add New Member Proc BEGIN TRANSACTION BEGIN INSERT MEMBER (LastName, FirstName, MiddleInitial, Photograph) Values (@LastName, @FirstName, @MiddleInitial, @Photograph) END if @@error<> 0 Begin print'Member name information failed' ROLLBACK TRANSACTION Return -8 End Set @newidentity=scope_identity() --STATE HAS DEFAULT CONSTRAINT If @state is null BEGIN INSERT ADULT (Member_no, Street, City, Zip, Phone_no, Expr_date) VALUES (@newidentity, @Street, @City, @Zip, @Phone_no, getdate()+365) END ELSE BEGIN INSERT ADULT (Member_no, Street, City, State, Zip, Phone_no, Expr_date) VALUES (@newidentity, @Street, @City, @State, @Zip, @Phone_no,getdate()+ 365) END if @@error<>0 BEGIN PRINT 'Address insert failed' ROLLBACK TRANSACTION RETURN -9 END Set @member_no=@NEWIDENTITY -- To populate the output parameter COMMIT TRANSACTION RETURN 0 -- SUCCESS
17. Library Project – Check in Item Proc BEGIN TRANSACTION BEGIN UPDATE copy SET on_loan='N'where isbn=@isbn and copy_no=@copy_no END BEGIN INSERT LOANHIST SELECT @isbn,@copy_no,out_date,title_no,member_no, due_date,getdate(),NULL, NULL,NULL,NULL from loan where isbn=@isbn and copy_no=@copy_no END BEGIN delete loan where @isbn=isbn and @copy_no=copy_no END COMMIT TRANSACTION
18. Library Project – Kids Club Report Beginning Reader June 28 Barr, Angela S Beginning Reader June 28 Barr, Angela K Junior Reader June 28 Anderson, Karl B Junior Reader June 28 Anderson, Jose H Reading Ace August 17 Anderson, Jose A Junior Reader June 28 Anderson, Frank Y Notes Birthday Member Name 7 years old 8 years old 9 years old Legend: The Kids Club
19. Library Project – Kids Club Procedure SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROC [dbo].[uspKidsClub] AS SELECT (Lastname+', '+firstname+' '+middleinitial) AS membername, cast(datediff(day,(birth_date+.5),Getdate())/(365.25)AS int) AS age, datename(month,Birth_date) AS birthMonth, day(Birth_date) AS DayOfBirth, juvenile.member_no FROM juvenile inner Join member ON member.member_no=juvenile.member_no WHERE datediff(day,(birth_date+.5),Getdate())/(365.25) between 7 and 10 ORDER BY memberName GO
20. Library Project – Most Active Report 4 2009 January 66 Anderson, Linda B 4 2009 January 16 Anderson, Michael A Active Reader 6 2009 January 860 Barr, Angela G Active Reader 9 2009 January 2029 Chen, Amy A Active Reader 9 2009 January 516 Anderson, Shelly R Avid Reader 10 2009 January 2609 Chen, Gary T Avid Reader 11 2009 January 5547 Harui, Gary E Avid Reader 12 2009 January 4955 Graff, Mary Anne H Avid Reader 12 2009 January 302 Anderson, Michael K Reader Status No of checkouts Year Month Member Number Member Name For Member Activity January 2009 Most Active Members Report The Library Project
21. Library Project – Most Active Procedure CREATE proc [dbo].[uspCheckoutHist] (@Year varchar(4), @Month varchar(15)) AS SELECT (member.lastname+', '+ member.firstname+' '+member.middleInitial) as Fullname, count(out_date)as No_of_checkouts, dateNAME(mm,out_date) as Month, datepart(month,out_date) as mo_int, datepart(year,out_date) as [Year], loanhist.member_no FROM loanhist inner join member ON member.member_no=loanhist.member_no WHERE datepart(year,out_date)=@Year and dateNAME(mm,out_date)=@Month GROUP BY l oanhist.member_no,datename(mm,out_date),datepart(year,out_date), datepart( month,out_date), (member.lastname+', '+ member.firstname+' '+member.middleInitial) ORDER BY count(out_date) DESC,member_no,year desc, mo_int asc
22. Online Movie Project The online movie rental company, BlockFlix, has stores nationwide and is creating a centralized database for tracking inventory, sales, customers and memberships. The stores rent VHS movies, DVD’s and video games as well as selling new and used movies, movie memorabilia, games, game consoles, snack foods and drinks. A new website will be created that uses the database to rent movies online. Database access must be through stored procedures. All inventory is received at one location and distributed to stores. Stores have ancillary kiosk locations in supermarkets and other retail locations that allow member or non-member rentals at $ 1.00 per day per movie. Online and store customers purchase a yearly membership at one of six levels with right to a certain number of movies out at any one time based upon their membership level. Additionally each member will have a queue of movies they would like to be mailed to them as they return movies as specified by their membership level. Management is expecting a high-availability database solution and mandate that all transactions are to be stored indefinitely within the database.