Aprende a crear una base de datos en SQL Server

Aprende a crear una base de datos en SQL Server

Diseñaremos nuestra base de datos y la crearemos usando transact sql teniendo en cuenta las relaciones entre tablas.

Tenemos el siguiente diseño de nuestra bdmarket, donde iremos codificando hasta lograr tener el diseño actual. Aquí se pueden identificar las tablas como estado, empleado, producto, categoria, cliente, venta y detalleventa.

diagrama base datos bdmarket

Iniciamos nuestro Sql Server Management Studio añadimos una nueva hoja de consultas.

Empezaremos creando las tablas de la base de datos con sus respectivos atributos, indicando las claves primarias y claves foráneas.

A continuación dejamos el script.

use master
create database bdmarket
go
use bdmarket
go
create table estado
(
idestado int primary key identity(1,1),
nombre varchar(50) not null
);
create table categoria
(
idcategoria int primary key identity(1,1),
nombre varchar(70)
)
create table producto
(
idproducto int primary key identity(1,1),
nombreproducto varchar(70) not null,
stock int not null,
idcategoria int not null,
idestado int not null,
CONSTRAINT fk_productocategoria FOREIGN KEY (idcategoria)
REFERENCES categoria(idcategoria),
CONSTRAINT fk_productoestado FOREIGN KEY (idestado)
REFERENCES estado(idestado)
);
create table cliente
(
idcliente int primary key identity(1,1),
nombres varchar(50) not null,
apellidos varchar(50) not null,
celular varchar(15),
idestado int not null,
CONSTRAINT fk_clienteestado FOREIGN KEY (idestado)
REFERENCES estado(idestado)
);
create table empleado
(
idempleado int primary key identity(1,1),
nombres varchar(50) not null,
apellidos varchar(50) not null,
celular varchar(15),
idestado int not null,
CONSTRAINT fk_empleadoestado FOREIGN KEY (idestado)
REFERENCES estado(idestado)
);
create table venta
(
idventa int primary key identity(1,1),
fecha date,
idcliente int not null,
idempleado int not null,
monto decimal(10,2) not null,
idestado int not null,
CONSTRAINT fk_ventacliente FOREIGN KEY (idcliente)
REFERENCES cliente(idcliente),
CONSTRAINT fk_ventaempleado FOREIGN KEY (idempleado)
REFERENCES empleado(idempleado),
CONSTRAINT fk_ventaestado FOREIGN KEY (idestado)
REFERENCES estado(idestado)
);
create table detalleventa
(
iddetalleventa int primary key identity(1,1),
idventa int not null,
idproducto int not null,
cantidad int not null,
precio decimal(10,2),
descuento decimal(10,2) not null,
CONSTRAINT fk_detalleventa FOREIGN KEY (idventa)
REFERENCES venta(idventa),
CONSTRAINT fk_detalleventaproducto FOREIGN KEY (idproducto)
REFERENCES producto(idproducto)
);

Publicar un comentario

Guardar mi nombre, correo electrónico y sitio web en este navegador la próxima vez que comente

0 Comentarios