0% found this document useful (0 votes)
854 views

SQLTRANSACTIONS

The document contains SQL statements that create tables to store student and payment data, insert initial records, and define four transactions. The transactions update student balances and make payments, with one transaction rolling back due to an incorrect student ID. Another transaction checks if an overpayment was made before completing the transaction.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
854 views

SQLTRANSACTIONS

The document contains SQL statements that create tables to store student and payment data, insert initial records, and define four transactions. The transactions update student balances and make payments, with one transaction rolling back due to an incorrect student ID. Another transaction checks if an overpayment was made before completing the transaction.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

CREATE TABLE [dbo].

[Payment](
[P_ID] [int] NULL,
[Date] [date] NULL,
[Amount] [decimal](18, 2) NULL,
[S_ID] [int] NULL
);
CREATE TABLE [dbo].[Students](
[S_ID] [int] NULL,
[Name] [varchar](50) NULL,
[Tuition_Fee] [decimal](18, 2) NULL,
[Balance] [decimal](18, 2) NULL
);
INSERT [dbo].[Payment] ([P_ID], [Date], [Amount], [S_ID]) VALUES (1, CAST(N'2020-
07-20' AS Date), CAST(23000.00 AS Decimal(18, 2)), 10);
INSERT [dbo].[Students] ([S_ID], [Name], [Tuition_Fee], [Balance]) VALUES (10,
N'Abaddon', CAST(23000.00 AS Decimal(18, 2)), CAST(0.00 AS Decimal(18, 2)));
INSERT [dbo].[Students] ([S_ID], [Name], [Tuition_Fee], [Balance]) VALUES (20,
N'Medusa', CAST(25000.00 AS Decimal(18, 2)), CAST(13000.00 AS Decimal(18, 2)));
INSERT [dbo].[Students] ([S_ID], [Name], [Tuition_Fee], [Balance]) VALUES (50,
N'Slark', CAST(18000.00 AS Decimal(18, 2)), CAST(18000.00 AS Decimal(18, 2)));
INSERT [dbo].[Students] ([S_ID], [Name], [Tuition_Fee], [Balance]) VALUES (70,
N'Leshrac', CAST(29000.00 AS Decimal(18, 2)), CAST(26000.00 AS Decimal(18, 2)));
INSERT [dbo].[Students] ([S_ID], [Name], [Tuition_Fee], [Balance]) VALUES (90,
N'Lina', CAST(15000.00 AS Decimal(18, 2)), CAST(15000.00 AS Decimal(18, 2)));

======command 1======
DECLARE @Pay AS Decimal(18, 2)
BEGIN
BEGIN TRANSACTION
--medusa pays 1000 on july 21 2020
SET @Pay = 1000
INSERT into Payment VALUES (2,CAST(N'2020-07-21' AS Date),@Pay,20)
UPDATE Students SET balance = balance - @Pay WHERE s_id = 20
COMMIT
BEGIN TRANSACTION
--leshrac pays 1000 on july 21 2020
SET @Pay = 1000
INSERT into Payment VALUES (3,CAST(N'2020-07-21' AS Date),@Pay,70)
UPDATE Students SET balance = balance - @Pay WHERE s_id = 70
COMMIT
END

======command 2======
DECLARE @Pay AS Decimal(18, 2)
BEGIN
BEGIN TRANSACTION
--lina pays full on july 28 2020 but cashier mistakes Student ID for slark's
Set @Pay = (SELECT tuition_fee FROM Students where s_id = 90)
INSERT into Payment VALUES (4,CAST(N'2020-07-28' AS Date),@Pay,50)
UPDATE Students SET balance = balance - @Pay WHERE s_id = 50
ROLLBACK
BEGIN TRANSACTION
--lina pays full on july 28 2020 this time correct
Set @Pay = (SELECT tuition_fee FROM Students where s_id = 90)
INSERT into Payment VALUES (4,CAST(N'2020-07-28' AS Date),@Pay,90)
UPDATE Students SET balance = balance - @Pay WHERE s_id = 90
COMMIT
END

======command 3======
DECLARE @Pay AS Decimal(18, 2)
DECLARE @id AS INT
BEGIN
BEGIN TRANSACTION
--medusa paid more than her balance on august 1,2020
Set @Pay = 14000
SET @id = 20
INSERT into Payment VALUES (5,CAST(N'2020-08-01' AS Date),@Pay,@id)
UPDATE Students SET balance = balance - @Pay WHERE s_id = @id
DECLARE @bal AS Decimal(18, 2)
SET @bal= (SELECT balance from Students where s_id = @id)
if @bal < @Pay
BEGIN
ROLLBACK
PRINT N'The Transaction could not proceed, the payment have been was
overpaid. Please try again.'
PRINT N'Your tuition balance: ' + CAST(@bal AS NVARCHAR(30)) + N'|Your
payment: ' + CAST(@Pay AS NVARCHAR(30))
END
END
=======command 4======
DECLARE @Pay AS Decimal(18, 2)
DECLARE @id AS INT --student id
DECLARE @tid AS INT --transact id
BEGIN
BEGIN TRANSACTION
--slark paid more than her balance on august 5,2020
Set @Pay = 15300
SET @id = 50
SET @tid = 5
INSERT into Payment VALUES (@tid,CAST(N'2020-08-05' AS Date),@Pay,@id)
DECLARE @bal AS Decimal(18, 2)
DEClARE @date AS NVARCHAR(50)
SET @date = CAST((SELECT date from Payment where p_id = @tid)AS NVARCHAR(50))
IF '2020-08-01' = @date
BEGIN
SET @bal = (SELECT balance from Students where s_id = @id)
SET @bal = @bal - (@bal * 0.15)
END
UPDATE Students SET balance = @bal - @Pay WHERE s_id = @id
COMMIT
END

You might also like