· 5 years ago · Nov 14, 2020, 06:32 PM
1Use master
2
3go
4
5If not exists (Select * from sys.databases where name = 'GestionCalzados')
6Begin
7
8Create database GestionCalzados
9
10End
11
12go
13
14Use GestionCalzados
15
16go
17
18Set Dateformat 'DMY'
19
20go
21
22Create Table Marcas(
23 ID smallint identity(1, 1),
24 Descripcion varchar(100) not null
25)
26
27go
28
29Create Table TiposCalzado(
30 ID smallint identity(1, 1),
31 Nombre varchar(50) not null
32)
33
34go
35
36Create Table Colores(
37 ID smallint identity(1, 1),
38 Color varchar(100) not null
39)
40
41go
42
43Create Table Talles(
44 ID smallint identity(1, 1),
45 Numero tinyint not null
46)
47
48go
49
50Create Table Paises(
51 ID smallint identity(1, 1),
52 Nombre varchar(50) not null
53)
54
55go
56
57Create Table Usuarios(
58 ID bigint identity(1, 1),
59 NombreUsuario varchar(50) not null,
60 Contraseña varchar(100) not null,
61)
62
63go
64
65Create Table DatosUsuarios(
66 IDUsuario bigint not null,
67 Nombre varchar(50) not null,
68 Apellido varchar(50) not null,
69 Nacimiento date not null,
70 Dni varchar(50) not null,
71 Genero char null,
72 Telefono varchar(20) not null,
73 Email varchar(50) not null,
74 Domicilio varchar(200) not null,
75 CodPostal smallint not null,
76 IDPais smallint not null
77)
78
79go
80
81Create Table Calzados(
82 ID bigint identity(1, 1),
83 Modelo varchar(150) not null,
84 Precio money not null,
85 ImagenUrl varchar(200) null,
86 IDMarca smallint not null,
87 IDTipoCalzado smallint not null
88)
89
90go
91
92Create Table Stock(
93 IDCalzado bigint not null,
94 IDTalle smallint not null,
95 IDColorBase smallint not null,
96 IDColor smallint not null,
97 StockDisponible bigint not null
98
99)
100
101go
102
103Create Table DatosClientes(
104 ID bigint identity(1, 1),
105 Nombre varchar(50) not null,
106 Apellido varchar(50) not null,
107 Nacimiento date not null,
108 Dni varchar (50) not null,
109 Genero char null,
110 Telefono varchar(20) not null,
111 Email varchar(50) not null,
112 Domicilio varchar(200) not null,
113 CodPostal smallint not null,
114 IDPais smallint not null,
115 Estado bit not null default(1)
116)
117
118go
119
120Create Table Ventas(
121 ID bigint identity(1, 1),
122 IDCliente bigint not null,
123 Fecha datetime not null default(getdate()),
124 Total money not null
125)
126
127go
128
129Create Table Calzados_x_Ventas(
130 IDVenta bigint not null ,
131 IDCalzado bigint not null,
132 IDTalle smallint not null,
133 IDColorBase smallint not null,
134 IDColor smallint not null,
135 Cantidad smallint not null
136)
137
138go
139
140Alter Table Marcas
141 Add Constraint PK_Marcas primary key(ID)
142
143go
144
145Alter Table Marcas
146 Add Constraint UQ_Descripcion_Marcas unique(Descripcion)
147
148go
149
150Alter Table TiposCalzado
151 Add Constraint PK_TiposCalzado primary key(ID)
152
153go
154
155Alter Table TiposCalzado
156 Add Constraint UQ_Nombre_TiposCalzado unique(Nombre)
157
158go
159
160Alter Table Calzados
161 Add Constraint PK_Calzados primary key(ID)
162
163go
164
165Alter Table Calzados
166 Add Constraint CHK_Precio_Calzados check(Precio > 0)
167
168go
169
170Alter Table Calzados
171 Add Constraint FK_IDMarca_Calzados foreign key(IDMarca) references Marcas(ID)
172
173go
174
175Alter Table Calzados
176 Add Constraint FK_IDTipoCalzado_Calzados foreign key(IDTipoCalzado) references TiposCalzado(ID)
177
178go
179
180Alter Table Colores
181 Add Constraint PK_Colores primary key(ID)
182
183go
184
185Alter Table Talles
186 Add Constraint PK_Talles primary key(ID)
187
188go
189
190Alter Table Talles
191 Add Constraint CHK_Numero_Talles check(Numero > 0)
192
193go
194
195Alter Table Talles
196 Add Constraint UQ_Numero_Talles unique(Numero)
197
198go
199
200Alter Table Stock
201 Add Constraint PK_Stock primary key(IDCalzado, IDTalle, IDColorBase, IDColor)
202
203go
204
205Alter Table Stock
206 Add Constraint FK_IDCalzado_Stock foreign key(IDCalzado) references Calzados(ID)
207
208go
209
210Alter Table Stock
211 Add Constraint FK_IDTalle_Stock foreign key(IDTalle) references Talles(ID)
212
213go
214
215Alter Table Stock
216 Add Constraint FK_IDColorBase_Stock foreign key(IDColorBase) references Colores(ID)
217
218go
219
220Alter Table Stock
221 Add Constraint FK_IDColor_Stock foreign key(IDColor) references Colores(ID)
222
223go
224
225Alter Table Stock
226 Add Constraint CHK_StockDisponible_Stock check(StockDisponible >= 0)
227
228go
229
230Alter Table Paises
231 Add Constraint PK_Paises primary key(ID)
232
233go
234
235Alter Table Paises
236 Add Constraint UQ_Nombre_Paises unique(Nombre)
237
238go
239
240Alter Table Usuarios
241 Add Constraint PK_Usuarios primary key(ID)
242
243go
244
245Alter Table Usuarios
246 Add Constraint UQ_NombreUsuario_Usuarios unique(NombreUsuario)
247
248go
249
250Alter Table DatosUsuarios
251 Add Constraint PK_DatosUsuarios primary key(IDUsuario)
252
253go
254
255Alter Table DatosUsuarios
256 Add Constraint FK_IDUsuario_DatosUsuarios foreign key(IDUsuario) references Usuarios(ID)
257
258go
259
260Alter Table DatosUsuarios
261 Add Constraint FK_IDPais_DatosUsuarios foreign key(IDPais) references Paises(ID)
262
263go
264
265Alter Table DatosUsuarios
266 Add Constraint CHK_Nacimiento_DatosUsuarios check(Nacimiento < getdate())
267
268go
269
270Alter Table DatosUsuarios
271 Add Constraint UQ_Dni_DatosUsuarios unique(Dni)
272
273go
274
275Alter Table DatosUsuarios
276 Add Constraint CHK_Genero_DatosUsuarios check(upper(Genero) in ('F', 'M', 'O'))
277
278go
279
280Alter Table DatosUsuarios
281 Add Constraint UQ_Email_DatosUsuarios unique(Email)
282
283go
284
285Alter Table DatosClientes
286 Add Constraint PK_DatosClientes primary key(ID)
287
288go
289
290Alter Table DatosClientes
291 Add Constraint CHK_Nacimiento_DatosClientes check(Nacimiento < getdate())
292
293go
294
295Alter Table DatosClientes
296 Add Constraint UQ_Dni_DatosClientes unique(Dni)
297
298go
299
300Alter Table DatosClientes
301 Add Constraint CHK_Genero_DatosClientes check(upper(Genero) in ('F', 'M', 'O'))
302
303go
304
305Alter Table DatosClientes
306 Add Constraint UQ_Email_DatosClientes unique(Email)
307
308go
309
310Alter Table DatosClientes
311 Add Constraint FK_IDPais_DatosClientes foreign key(IDPais) references Paises(ID)
312
313go
314
315Alter Table Ventas
316 Add Constraint PK_Ventas primary key(ID)
317
318go
319
320Alter Table Ventas
321 Add Constraint FK_IDCliente_Ventas foreign key(IDCliente) references DatosClientes(ID)
322
323go
324
325Alter Table Ventas
326 Add Constraint CHK_Total_Ventas check(Total > 0)
327
328go
329
330Alter Table Calzados_x_Ventas
331 Add Constraint PK_Calzados_x_Ventas primary key(IDVenta, IDCalzado, IDTalle, IDColorbase, IDColor)
332
333go
334
335Alter Table Calzados_x_Ventas
336 Add Constraint FK_IDVenta_Calzados_x_Ventas foreign key(IDVenta) references Ventas(ID)
337
338go
339
340Alter Table Calzados_x_Ventas
341 Add Constraint FK_IDCalzado_Calzados_x_Ventas foreign key(IDCalzado, IDTalle, IDColorBase, IDColor) references Stock(IDCalzado, IDTalle, IDColorBase, IDColor)
342
343go
344
345Alter Table Calzados_x_Ventas
346 Add Constraint CHK_Cantidad_Calzados_x_Ventas check(Cantidad > 0)