Vous êtes sur la page 1sur 5

UNIVERSIDAD NACIONAL DE TRUJILLO

ESCUELA DE INGENIERIA DE SISTEMAS

LABORATORIO N 03
TRIGGER EN UNA BASE DE DATOS
Crear la Base de datos VENTAS, con la siguiente configuracin:
Base de datos: VENTAS
Tablas de base:
CLIENTES(ClienteID, Nombre, Email)
VENDEDORES(VendedorID, Nombre, Comision)
PRODUCTOS(ProductoID, Descripcion, Medida, PrecioUnitario, Stock )
PEDIDOS(PedidoID, ClienteID, VendedorID, Fecha )
DETALLE DE PEDIDOS(PedidoID, ProductoID, Cantidad, PrecioUnitario)
Creacin de las Tablas de Base
Tabla: CLIENTES
use VENTAS
go
create table CLIENTES
(
ClienteID int identity(1, 1) not null,
Nombre varchar(50),
Email varchar(30)
)on [primary]
go
alter table CLIENTES
With nocheck ADD
constraint [PK_Clientes]
primary key clustered
(
ClienteID
)on [primary]
go

Tabla: VENDEDORES
use VENTAS
go
create table VENDEDORES
(
Dr. LUIS BOY CHAVIL

Pgina 1

VendedorID int identity(1, 1) not null,


Nombre varchar(50),
Comision decimal(6, 2)
)on [primary]
go
alter table VENDEDORES
With nocheck ADD
constraint [PK_Vendedores]
primary key clustered
(
VendedorID
)on [primary]
go

Tabla: PRODUCTOS
use VENTAS
go
create table PRODUCTOS
(
ProductoID int identity(1, 1) not null,
Descripcion varchar(50),
Medida varchar(30),
PrecioUnitario money not null,
Stock int
)on [primary]
go
alter table PRODUCTOS
With nocheck ADD
constraint [PK_Productos]
primary key clustered
(
ProductoID
)on [primary]
Go

Tabla: PEDIDOS
use VENTAS
go
create table PEDIDOS
(
PedidoID int identity(1, 1) not null,
ClienteID int not null,
VendedorID int not null,
Dr. LUIS BOY CHAVIL

Pgina 2

Fecha datetime
)on [primary]
go
alter table PEDIDOS
With nocheck ADD
constraint [PK_Pedidos]
primary key clustered
(
PedidoID
)on [primary]
go

Tabla: [DETALLE DE PEDIDOS]


use VENTAS
go
create table [DETALLE DE PEDIDOS]
(
PedidoID int not null,
ProductoID int not null,
Cantidad int not null,
PrecioUnitario money
)on [primary]
go
alter table [DETALLE DE PEDIDOS]
With nocheck ADD
constraint [PK_DetallePedidos]
primary key clustered
(
PedidoID,
ProductoID
)on [primary]
go

Dr. LUIS BOY CHAVIL

Pgina 3

Diagrama de la base de datos VENTAS

Descripcin de las Operaciones a programar en el Trigger:


1. La Comision del Vendedor se calcula de acuerdo al monto de la venta y equivale al 0,5% de
dicho monto.
2. Verificar que la cantidad del pedido <= Stock actual
3. Actualizar el Stock en base a las ventas efectuadas
Programacin del Trigger CalculaComisiones

use VENTAS
go
create trigger CalculaComisiones
on [DETALLE DE PEDIDOS]
for insert
AS
Begin
-- Obtener la cantidad a Insertar
declare @Cantidad int
select @Cantidad=cantidad
from inserted
-- Obtener el Stock del producto
declare @Stock int
select @Stock=stock
from Productos P
join inserted i
on P.ProductoID=i.ProductoID
if @Cantidad<=@Stock
Begin
-- Actualizar el Stock
Dr. LUIS BOY CHAVIL

Pgina 4

UPDATE PRODUCTOS
set stock=stock-@Cantidad
from Productos
join inserted i
on P.ProductoID=i.ProductoID
-- Calcular la Comision del Vendedor
UPDATE VENDEDORES
set comision=comision+0.5/100*(@Cantidad*i.PrecioUnitario)
from VENDEDORES V
join PEDIDOS P
on V.VendedorID=P.VendedorID
join inserted i
on P.PedidoID=i.PedidoID
End
else
Begin
Raiserror('Error-Stock insuficiente', 16, 2)
rollback transaction
End
End

Dr. LUIS BOY CHAVIL

Pgina 5

Vous aimerez peut-être aussi