-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate-DDBB-Queries.sql
More file actions
160 lines (119 loc) · 5.19 KB
/
Copy pathCreate-DDBB-Queries.sql
File metadata and controls
160 lines (119 loc) · 5.19 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
-- Create a Database
-- This creates a new database named FranciscoBretones.
-- The database will be used to store all the subsequent tables and data.
CREATE DATABASE FranciscoBretones;
USE FranciscoBretones; -- Sets the context to the newly created database.
-- Create the Cliente Table
-- This table stores customer information such as name, address, phone, email, and registration date.
CREATE TABLE Cliente (
IdCliente INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Nombre VARCHAR(50) NOT NULL,
Apellido VARCHAR(50) NOT NULL,
Fnacimiento DATE NOT NULL,
Domicilio VARCHAR(50) NOT NULL,
idPais CHAR(3) NOT NULL,
Telefono VARCHAR(12) NULL,
Email VARCHAR(30) NOT NULL,
Observaciones VARCHAR(1000) NULL,
FechaAlta DATETIME NOT NULL,
FOREIGN KEY (idPais) REFERENCES Pais(IdPais) -- Relationship with Pais
);
-- Create the Record Table
-- This table stores records with an auto-incremented ID, a date, and optional observations.
CREATE TABLE Record (
IdRecord INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
FechaRecord DATE NOT NULL,
Observaciones VARCHAR(1000) NULL
);
-- Create the RecordCliente Table
-- This table links records to customers and campaigns using a composite primary key.
CREATE TABLE RecordCliente (
IdRecord INT NOT NULL,
IdCliente INT NOT NULL,
IdCampania INT NOT NULL,
PRIMARY KEY (IdRecord, IdCliente, IdCampania),
FOREIGN KEY (IdRecord) REFERENCES Record(IdRecord), -- Relationship with Record
FOREIGN KEY (IdCliente) REFERENCES Cliente(IdCliente), -- Relationship with Cliente
FOREIGN KEY (IdCampania) REFERENCES Campania(IdCampania) -- Relationship with Campania
);
-- Create the Pais Table
-- This table lists countries with a 3-character code and name.
CREATE TABLE Pais (
IdPais CHAR(3) NOT NULL PRIMARY KEY,
NombrePais VARCHAR(100) NOT NULL
);
-- Create the HoraCaptacion Table
-- This table stores data about lead capture times including dates, status, and observations.
CREATE TABLE HoraCaptacion(
idHCaptacion INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
fechaCaptacion DATE NOT NULL,
estadoCaptacion SMALLINT NOT NULL,
Observaciones VARCHAR(100) NOT NULL,
FOREIGN KEY (estadoCaptacion) REFERENCES HorarioEstado(idEstado) -- Relationship with HorarioEstado
);
-- Create the HoraCapClienteCampania Table
-- This table links lead capture times to clients and campaigns using a composite primary key.
CREATE TABLE HoraCapClienteCampania (
idHCaptacion INT NOT NULL,
idCliente INT NOT NULL,
idCampania INT NOT NULL,
PRIMARY KEY (idHCaptacion, idCliente, idCampania),
FOREIGN KEY (idHCaptacion) REFERENCES HoraCaptacion(idHCaptacion), -- Relationship with HoraCaptacion
FOREIGN KEY (idCliente) REFERENCES Cliente(IdCliente), -- Relationship with Cliente
FOREIGN KEY (idCampania) REFERENCES Campania(IdCampania) -- Relationship with Campania
);
-- Create the HorarioEstado Table
-- This table stores different statuses with a description.
CREATE TABLE HorarioEstado (
idEstado SMALLINT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Descripcion VARCHAR(50) NOT NULL
);
-- Create the Producto Table
-- This table stores product information including a unique ID and product name.
CREATE TABLE Producto (
IdProducto INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Producto VARCHAR(100) NOT NULL
);
-- Create the Compra Table
-- This table stores purchase information including concepts, dates, amounts, and observations.
CREATE TABLE Compra (
IdCompras INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Concepto INT NOT NULL,
Fecha DATETIME NOT NULL,
Monto MONEY NOT NULL,
Observaciones VARCHAR(1000) NULL,
FOREIGN KEY (Concepto) REFERENCES ConceptoCompra(IdConcepto) -- Relationship with ConceptoCompra
);
-- Create the CompraCliente Table
-- This table links purchases to customers and lead capture times using a composite primary key.
CREATE TABLE CompraCliente (
IdCompras INT NOT NULL,
IdCliente INT NOT NULL,
idHCaptacion INT NOT NULL,
PRIMARY KEY (IdCompras, IdCliente, idHCaptacion),
FOREIGN KEY (IdCompras) REFERENCES Compra(IdCompras), -- Relationship with Compra
FOREIGN KEY (IdCliente) REFERENCES Cliente(IdCliente), -- Relationship with Cliente
FOREIGN KEY (idHCaptacion) REFERENCES HoraCaptacion(idHCaptacion) -- Relationship with HoraCaptacion
);
-- Create the Campania Table
-- This table stores campaign information with a unique ID and campaign name.
CREATE TABLE Campania (
IdCampania INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
NombreCampaña VARCHAR(50) NOT NULL
);
-- Create the CampaniaProducto Table
-- This table links campaigns to products using a composite primary key.
CREATE TABLE CampaniaProducto (
IdCampania INT NOT NULL,
IdProducto INT NOT NULL,
Descripcion VARCHAR(100) NOT NULL,
PRIMARY KEY (IdCampania, IdProducto),
FOREIGN KEY (IdProducto) REFERENCES Producto(IdProducto), -- Relationship with Producto
FOREIGN KEY (IdCampania) REFERENCES Campania(IdCampania) -- Relationship with Campania
);
-- Create the ConceptoCompra Table
-- This table stores purchase concepts with a unique ID and description.
CREATE TABLE ConceptoCompra (
IdConcepto INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
Concepto VARCHAR(100) NOT NULL
);