-
Notifications
You must be signed in to change notification settings - Fork 0
/
EconomicManagementDB.sql
96 lines (82 loc) · 2.56 KB
/
EconomicManagementDB.sql
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
USE master
GO
DROP DATABASE IF EXISTS [EconomicManagementDB]
GO
CREATE DATABASE [EconomicManagementDB]
GO
USE [EconomicManagementDB]
GO
-- StandarEmail será el email en mayusculas
CREATE TABLE [Users](
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Email] [nvarchar](256) NOT NULL,
[StandarEmail] [nvarchar](256) NOT NULL,
[Password] [nvarchar](max) NOT NULL,
)
GO
-- Estas operaciones pueden ser: ingresos o gastos
CREATE TABLE [OperationTypes](
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Description] [nvarchar](50) NOT NULL,
)
GO
-- ejemplo: bancarias, prestamos, efectivo, credito, etc.
CREATE TABLE [AccountTypes](
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[UserId] [int] NOT NULL,
[OrderAccount] [int] NOT NULL,
CONSTRAINT [FK_AccountTypesUsers] FOREIGN KEY (UserId) REFERENCES Users(Id)
)
GO
CREATE TABLE [Accounts](
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[AccountTypeId] [int] NOT NULL,
[Balance] [decimal](18, 2) NOT NULL,
[Description] [nvarchar](1000) NULL,
CONSTRAINT [FK_AccountType] FOREIGN KEY (AccountTypeId) REFERENCES AccountTypes(Id)
)
GO
-- Cada usuario puede tener sus propias categorias para clasificar sus transacciones.
CREATE TABLE Categories(
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[OperationTypeId] [int] NOT NULL,
[UserId] [int] NOT NULL,
CONSTRAINT [FK_CategoriesOperations] FOREIGN KEY (OperationTypeId) REFERENCES OperationTypes(Id),
)
GO
CREATE TABLE [Transactions](
[Id] [int] IDENTITY(1,1) PRIMARY KEY NOT NULL,
[UserId] [int] NOT NULL,
[TransactionDate] [datetime] NOT NULL,
[Total] [decimal](18, 2) NOT NULL,
[OperationTypeId] [int] NOT NULL,
[Description] [nvarchar](1000) NULL,
[AccountId] [int] NOT NULL,
[CategoryId] [int] NOT NULL,
CONSTRAINT [FK_TransactionsUsers] FOREIGN KEY (UserId) REFERENCES Users(Id),
CONSTRAINT [FK_TransactiosOperationType] FOREIGN KEY (OperationTypeId) REFERENCES OperationTypes(Id),
CONSTRAINT [FK_TransactionsAccount] FOREIGN KEY (AccountId) REFERENCES Accounts(Id) ON DELETE CASCADE,
CONSTRAINT [FK_TransactionsCategories] FOREIGN KEY (CategoryId) REFERENCES Categories(Id)
)
GO
CREATE PROCEDURE SP_AccountType_Insert
@Name nvarchar(50),
@UserId int
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Order int;
SELECT @Order = COALESCE(MAX(OrderAccount), 0)+1
FROM AccountTypes
WHERE UserId = @UserId
INSERT INTO AccountTypes(Name, UserId, OrderAccount)
VALUES (@Name, @UserId, @Order);
SELECT SCOPE_IDENTITY();
END
GO
INSERT INTO OperationTypes VALUES
('Ingreso'),
('Gasto')