Supongamos que queremos crear una base de datos para una tienda en línea que vende productos. Primero, debemos identificar las entidades relevantes para nuestra base de datos.
Aquí te presento un ejemplo de creación y diseño de una base de datos en MariaDB utilizando las tablas tb_cliente, tb_producto, tb_factura y tb_factura_detalle.
Base de datos "bdcrud"
CREATE DATABASE /*!32312 IF NOT EXISTS*/ bdcrud;
USE bdcrud;
Tabla "tb_cliente"
La tabla "tb_cliente" contiene información sobre los clientes de la tienda, incluyendo su nombre, dirección y fecha de nacimiento.
DROP TABLE IF EXISTS `tb_cliente`;
CREATE TABLE `tb_cliente` (
`IDCLIENTE` int(11) NOT NULL AUTO_INCREMENT,
`NOMBRES` varchar(100) NOT NULL,
`DIRECCION` varchar(100) DEFAULT NULL,
`FECHA_NACIMIENTO` date NOT NULL,
`FECHA_REGISTRO` datetime NOT NULL,
PRIMARY KEY (`IDCLIENTE`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb3;
INSERT INTO `tb_cliente` (`IDCLIENTE`,`NOMBRES`,`DIRECCION`,`FECHA_NACIMIENTO`,`FECHA_REGISTRO`) VALUES
(1,'Juan Perez','Av. Siempreviva 123','1990-06-15',now()),
(2,'Maria Rodriguez','Calle Falsa 123','1985-08-20',now()),
(3,'Pedro Gomez','Av. Principal 456','1995-02-10',now()),
(4,'Ana Fernandez','Calle Real 789','1982-12-01',now()),
(5,'Luisa Martinez','Av. America 321','1993-07-18',now()),
(6,'Jorge Ramirez','Calle Luna 456','1978-11-30',now()),
(7,'Sofia Sanchez','Av. Sol 987','1998-04-05',now()),
(8,'Carlos Torres','Calle Estrella 654','1987-03-22',now()),
(9,'Fernando Garcia','Av. Universitaria 321','1991-09-28',now()),
(10,'Gloria Castro','Calle Alegria 987','1975-05-12',now()),
(11,'Diego Lopez','Av. Libertad 456','1996-08-02',now()),
(12,'Valeria Jimenez','Calle Flores 789','1989-01-07',now()),
(13,'Ricardo Hernandez','Av. Rio 123','1979-10-25',now());
INSERT INTO `tb_cliente` (`IDCLIENTE`,`NOMBRES`,`DIRECCION`,`FECHA_NACIMIENTO`,`FECHA_REGISTRO`) VALUES
(14,'Silvia Morales','Calle Montaña 456','1992-03-12',now()),
(15,'Gabriel Diaz','Av. Peru 987','1984-06-22',now()),
(16,'Lucia Ortiz','Calle Paraiso 654','1976-09-08',now()),
(17,'Oscar Nieto','Av. España 321','1990-12-15',now()),
(18,'Alicia Rivera','Calle Paz 789','1983-05-28',now()),
(19,'Pablo Castro','Av. Amazonas 456','1994-02-03',now()),
(20,'Natalia Vega','Calle Luna 123','1977-11-17',now());
Tabla "tb_producto"
La tabla "tb_producto" contiene información sobre los productos que vende la tienda, incluyendo su nombre, precio y stock.
DROP TABLE IF EXISTS `tb_producto`;
CREATE TABLE `tb_producto` (
`IDPRODUCTO` int(11) NOT NULL AUTO_INCREMENT,
`NOMBRE` varchar(100) NOT NULL,
`PRECIO` decimal(10,2) NOT NULL,
`STOCK` int(11) NOT NULL,
`FECHA_REGISTRO` datetime NOT NULL,
PRIMARY KEY (`IDPRODUCTO`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8mb3;
INSERT INTO `tb_producto` (`IDPRODUCTO`,`NOMBRE`,`PRECIO`,`STOCK`,`FECHA_REGISTRO`) VALUES
(1,'Smartphone Samsung Galaxy S20','899.99',10,now()),
(2,'Laptop Dell Inspiron 15','1099.99',5,now()),
(3,'Smart TV LG 55\"','1299.99',3,now()),
(4,'Aspiradora Dyson V11','599.99',2,now()),
(5,'Camisa Polo Ralph Lauren','99.99',20,now()),
(6,'Tenis Nike Air Max','149.99',8,now()),
(7,'Muñeca Barbie Dreamhouse','199.99',4,now()),
(8,'Camisa','39.99',50,now()),
(9,'Pantalón','59.99',30,now()),
(10,'Zapatos','89.99',20,now()),
(11,'Gorra','19.99',100,now()),
(12,'Bufanda','29.99',40,now()),
(13,'Guantes','14.99',50,now()),
(14,'Chaqueta','99.99',10,now()),
(15,'Calcetines','9.99',80,now()),
(16,'Sombrero','24.99',30,now()),
(17,'Cinturón','19.99',60,now());
Tabla "tb_factura"
La tabla "tb_factura" relaciona los clientes con las facturas que han realizado y contiene información sobre la fecha y el total(monto) de la factura.
DROP TABLE IF EXISTS `tb_factura`;
CREATE TABLE `tb_factura` (
`IDFACTURA` int(11) NOT NULL AUTO_INCREMENT,
`ID_CLIENTE` int(11) NOT NULL,
`NRO_FACTURA` varchar(10) NOT NULL,
`MONTO` decimal(10,2) NOT NULL,
`FECHA_REGISTRO` datetime NOT NULL,
PRIMARY KEY (`IDFACTURA`),
UNIQUE KEY `tb_factura_un` (`NRO_FACTURA`),
KEY `tb_factura_FK` (`ID_CLIENTE`),
CONSTRAINT `tb_factura_FK` FOREIGN KEY (`ID_CLIENTE`) REFERENCES `tb_cliente` (`IDCLIENTE`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8mb3;
INSERT INTO `tb_factura` (`IDFACTURA`,`ID_CLIENTE`,`NRO_FACTURA`,`MONTO`,`FECHA_REGISTRO`) VALUES
(1,1,'FAC001',100.00,now()),
(2,2,'FAC002',200.00,now()),
(3,3,'FAC003',300.00,now()),
(4,4,'FAC004',400.00,now()),
(5,5,'FAC005',500.00,now()),
(6,1,'FAC006',150.00,now()),
(7,2,'FAC007',250.00,now()),
(8,3,'FAC008',350.00,now()),
(9,4,'FAC009',450.00,now()),
(10,5,'FAC010',550.00,now());
Tabla "tb_factura_detalle"
La tabla "tb_factura_detalle" contiene información sobre los productos que se incluyen en cada factura y la cantidad y precio de cada producto.
DROP TABLE IF EXISTS `tb_factura_detalle`;
CREATE TABLE `tb_factura_detalle` (
`IDDETALLEFACTURA` int(11) NOT NULL AUTO_INCREMENT,
`ID_FACTURA` int(11) NOT NULL,
`ID_PRODUCTO` int(11) NOT NULL,
`CANTIDAD` int(11) NOT NULL,
`PRECIO` decimal(10,2) NOT NULL,
`SUBTOTAL` decimal(10,2) NOT NULL,
`FECHA_REGISTRO` datetime NOT NULL,
PRIMARY KEY (`IDDETALLEFACTURA`),
KEY `tb_factura_detalle_FK` (`ID_PRODUCTO`),
KEY `tb_factura_detalle_FK_1` (`ID_FACTURA`),
CONSTRAINT `tb_factura_detalle_FK` FOREIGN KEY (`ID_PRODUCTO`) REFERENCES `tb_producto` (`IDPRODUCTO`),
CONSTRAINT `tb_factura_detalle_FK_1` FOREIGN KEY (`ID_FACTURA`) REFERENCES `tb_factura` (`IDFACTURA`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8mb3;
INSERT INTO `tb_factura_detalle` (`IDDETALLEFACTURA`,`ID_FACTURA`,`ID_PRODUCTO`,`CANTIDAD`,`PRECIO`,`SUBTOTAL`,`FECHA_REGISTRO`) VALUES
(1,1,1,2,'25.00','50.00',now()),
(2,1,2,1,'50.00','50.00',now()),
(3,2,3,3,'20.00','60.00',now()),
(4,2,4,2,'35.00','70.00',now()),
(5,3,5,4,'15.00','60.00',now()),
(6,3,6,1,'100.00','100.00',now()),
(7,4,7,3,'30.00','90.00',now()),
(8,4,8,2,'45.00','90.00',now()),
(9,5,9,5,'10.00','50.00',now()),
(10,5,10,2,'75.00','150.00',now()),
(11,6,1,1,'25.00','25.00',now()),
(12,6,4,3,'35.00','105.00',now()),
(13,7,6,2,'100.00','200.00',now()),
(14,7,9,1,'10.00','10.00',now()),
(15,8,2,2,'50.00','100.00',now()),
(16,8,3,1,'20.00','20.00',now()),
(17,9,5,3,'15.00','45.00',now()),
(18,9,8,4,'45.00','180.00',now());
INSERT INTO `tb_factura_detalle` (`IDDETALLEFACTURA`,`ID_FACTURA`,`ID_PRODUCTO`,`CANTIDAD`,`PRECIO`,`SUBTOTAL`,`FECHA_REGISTRO`) VALUES
(19,10,10,1,'75.00','75.00',now()),
(20,10,7,2,'30.00','60.00',now());
Diseño de la base de datos "bdcrud"
Con este diseño de base de datos, podemos almacenar y recuperar información de manera eficiente sobre los clientes que realizan compras, los productos que vendemos, las facturas que generamos y los detalles de cada factura. Además, podemos realizar consultas y generar informes útiles para la gestión de la tienda.
Para visualizar el diseño completo de las tablas creadas utilizando el modelo lógico en MariaDB, lo ideal es utilizar una herramienta de modelado de datos que te permita crear y visualizar diagramas ER (Entidad-Relación) que representen la estructura de la base de datos y las relaciones entre las tablas.
Una de las herramientas de modelado de datos más utilizadas en la industria es MySQL Workbench, que es una aplicación de escritorio gratuita y de código abierto que te permite diseñar, modelar, generar código y administrar bases de datos MySQL y MariaDB.
