-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbanking_system_DDL.sql
More file actions
70 lines (62 loc) · 2.45 KB
/
banking_system_DDL.sql
File metadata and controls
70 lines (62 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* Date: 2-Feb-2025
* Author: Mark Holahan
* Purpose: python-oop-mini-project DDL
* RDBMS: Stood up a SQL Server instance on Azure
*/
USE [FauxBankingSystemDB]
GO
-- Drop Foreign Key from Account Table if it exists
IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_Account_Customer' AND parent_object_id = OBJECT_ID('dbo.Account'))
ALTER TABLE dbo.Account DROP CONSTRAINT FK_Account_Customer;
GO
-- Drop Foreign Key from TransactionLog Table if it exists
IF EXISTS (SELECT 1 FROM sys.foreign_keys WHERE name = 'FK_TransactionLog_Account' AND parent_object_id = OBJECT_ID('dbo.TransactionLog'))
ALTER TABLE dbo.TransactionLog DROP CONSTRAINT FK_TransactionLog_Account;
GO
-- Drop Tables if they exist
IF OBJECT_ID('dbo.TransactionLog', 'U') IS NOT NULL DROP TABLE dbo.TransactionLog;
IF OBJECT_ID('dbo.Account', 'U') IS NOT NULL DROP TABLE dbo.Account;
IF OBJECT_ID('dbo.Customer', 'U') IS NOT NULL DROP TABLE dbo.Customer;
IF OBJECT_ID('dbo.[Log]', 'U') IS NOT NULL DROP TABLE dbo.[Log];
GO
-- Customer Table
CREATE TABLE dbo.Customer (
CustomerID INT IDENTITY(1,1) PRIMARY KEY,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
Email NVARCHAR(100) UNIQUE NOT NULL,
PhoneNumber NVARCHAR(20) NULL,
CreatedAt DATETIME DEFAULT GETDATE(),
UpdatedAt DATETIME DEFAULT GETDATE()
);
GO
-- Account Table
CREATE TABLE dbo.Account (
AccountID INT IDENTITY(1,1) PRIMARY KEY,
CustomerID INT NOT NULL,
AccountType NVARCHAR(20) CHECK (AccountType IN ('Checking', 'Savings')),
Balance DECIMAL(18,2) CHECK (Balance >= 0) DEFAULT 0,
CreatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (CustomerID) REFERENCES dbo.Customer(CustomerID) ON DELETE CASCADE
);
GO
CREATE TABLE dbo.TransactionLog (
TransactionID INT IDENTITY(1,1) PRIMARY KEY,
AccountID INT NOT NULL,
RelatedAccountID INT NULL, -- For transfers
TransactionType NVARCHAR(20) CHECK (TransactionType IN ('Deposit', 'Withdrawal', 'Transfer')),
Amount DECIMAL(18,2) CHECK (Amount > 0) NOT NULL,
TransactionTimestamp DATETIME DEFAULT GETDATE(),
FOREIGN KEY (AccountID) REFERENCES dbo.Account(AccountID) ON DELETE CASCADE,
FOREIGN KEY (RelatedAccountID) REFERENCES dbo.Account(AccountID) ON DELETE NO ACTION
);
GO
-- Log Table
CREATE TABLE dbo.Log (
LogID INT IDENTITY(1,1) PRIMARY KEY,
LogLevel NVARCHAR(20) CHECK (LogLevel IN ('INFO', 'WARNING', 'ERROR')),
Message NVARCHAR(MAX) NOT NULL,
LogTimestamp DATETIME DEFAULT GETDATE()
);
GO