73 lines
2.4 KiB
SQL
73 lines
2.4 KiB
SQL
-- Crear la base de datos solo si no existe
|
|
CREATE DATABASE IF NOT EXISTS `suitecoffee`;
|
|
|
|
USE `suitecoffee`;
|
|
|
|
-- Crear tabla de categorías
|
|
CREATE TABLE IF NOT EXISTS categorias (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
nombre VARCHAR(100) NOT NULL UNIQUE
|
|
);
|
|
|
|
-- Crear tabla de productos
|
|
CREATE TABLE IF NOT EXISTS productos (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
nombre VARCHAR(100) NOT NULL,
|
|
precio DECIMAL(10,2) NOT NULL,
|
|
categoria_id INT,
|
|
FOREIGN KEY (categoria_id) REFERENCES categorias(id)
|
|
);
|
|
|
|
-- Crear tabla de mesas
|
|
CREATE TABLE IF NOT EXISTS mesas (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
numero INT NOT NULL UNIQUE
|
|
);
|
|
|
|
-- Crear tabla de comandas con productos en JSON
|
|
CREATE TABLE IF NOT EXISTS comandas (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
mesa_id INT NOT NULL,
|
|
productos JSON NOT NULL, -- Array de productos con cantidad y precio
|
|
fecha DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
total DECIMAL(10,2),
|
|
FOREIGN KEY (mesa_id) REFERENCES mesas(id)
|
|
);
|
|
|
|
-- Insertar categoría 'Café' en la tabla categorias
|
|
INSERT INTO categorias (nombre) VALUES ('cafe');
|
|
|
|
-- Insertar mesa '1, 2 y 3' en la tabla mesas
|
|
INSERT INTO mesas (numero)
|
|
VALUES
|
|
(1),
|
|
(2),
|
|
(3);
|
|
|
|
-- Insertar cappuccino en la tabla productos, asociándolo con la categoría 'Café'
|
|
INSERT INTO productos (nombre, precio, categoria_id)
|
|
VALUES
|
|
('Cappuccino', 200.00, (SELECT id FROM categorias WHERE nombre = 'Café')),
|
|
('Latte', 200.00, (SELECT id FROM categorias WHERE nombre = 'Café')),
|
|
('Espresso', 120.00, (SELECT id FROM categorias WHERE nombre = 'Café'));
|
|
('Frappe', 290.00, (SELECT id FROM categorias WHERE nombre = 'Café'));
|
|
|
|
-- Insertar una comanda en la tabla comandas para la mesa 1
|
|
INSERT INTO comandas (mesa_id, productos, total)
|
|
VALUES
|
|
(
|
|
2, -- mesa_id
|
|
JSON_ARRAY(
|
|
JSON_OBJECT('producto_id', (SELECT id FROM productos WHERE nombre = 'Expresso'), 'cantidad', 2, 'precio_unitario', 111.00),
|
|
JSON_OBJECT('producto_id', (SELECT id FROM productos WHERE nombre = 'Latte'), 'cantidad', 1, 'precio_unitario', 666.00)
|
|
),
|
|
208457935.00 -- total (2 Cappuccinos * 200 + 1 Latte * 220)
|
|
),
|
|
(
|
|
3, -- mesa_id
|
|
JSON_ARRAY(
|
|
JSON_OBJECT('producto_id', (SELECT id FROM productos WHERE nombre = 'Cappuccino'), 'cantidad', 2, 'precio_unitario', 444.00),
|
|
JSON_OBJECT('producto_id', (SELECT id FROM productos WHERE nombre = 'Frappe'), 'cantidad', 4, 'precio_unitario', 222.00)
|
|
),
|
|
93826.00 -- total (2 Cappuccinos * 200 + 1 Latte * 220)
|
|
); |