Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
--====================================
CREANDO LA BASE DE DATOS
--====================================
*/
use master
go
--------------------------------------
if exists (select * from sysdatabases where name='JPSoluciones')
drop database JPSoluciones
go
create database JPSoluciones
go
use JPSoluciones
go
--TABLA REGION
---------------
--Almacena los datos las regiones políticas del país
-----------------------------------------------------
if exists (select * from sysobjects where name='Region')
drop table Region
go
create table Region
( id int identity (1,1),
idRegion as RIGHT('00'+CAST(id as varchar(2)),2) persisted not
null,
NomRegion varchar(25) not null,
Detalle varchar(50) )
go
--TABLA CLIENTEH
if exists (select * from sysobjects where name='Clienteh')
drop table Clienteh
go
create table Clienteh
( id int identity (1,1),
idCliente as 'CL'+ RIGHT('000'+CAST(id as varchar(4)),4)
persisted not null,
Dni char(8) not null,
Ruc char(11),
Nombres varchar(35) not null,
Apaterno varchar(25) not null,
Amaterno varchar(25) not null,
Sexo char(1) not null,
Fnacim date )
go
--TABLA CLIENTEM
if exists (select * from sysobjects where name='Clientem')
drop table Clientem
go
create table Clientem
( id int identity (1,1),
idCliente as 'CL'+ RIGHT('000'+CAST(id as varchar(4)),4)
persisted not null,
Dni char(8) not null,
Telefono varchar(12),
Email varchar(35),
Direccion varchar(50),
idDistrito varchar(5) )
go
‹ Otro punto en cuestión es el de los IDs o códigos de algunas tablas, que sirven como claves
primarias de las mismas. Las tablas con esta característica cuentan con un campo id entero
incremental, que nos servirá para calcular el ID que será la clave principal. Este campo
calculado se compone de dos letras iniciales, características de la tabla, y un número
consecutivo de cuatro cifras, logrado a partir del ID entero inicial.
--==================================================
CREANDO LAS RESTRICCIONES PRIMARY KEY
--=================================================
*/
--TABLA REGION
---------------
if exists (select * from sysobjects where name='pk_region')
alter table Region
drop constraint pk_region
go
alter table Region
add constraint pk_region
PRIMARY KEY(idRegion)
go
‹ Igual con los foreign key, procurando asegurar que el eventual borrado o actualización de
filas se realice en cascada para evitar conflictos.
--==============================================
CREANDO RESTRICCIONES FOREIGN KEY
--==============================================
*/
--TABLA DISTRITO
-----------------
if exists (select * from sysobjects where
name='fk_Distrito_Region')
alter table Distrito
drop constraint fk_Distrito_Region
go
alter table Distrito
add constraint fk_Distrito_Region
FOREIGN KEY(idRegion)
references Region(idRegion)
on delete cascade
on update cascade
go
‹ Por ejemplo, asegurándonos que el campo Región en la tabla del mismo nombre resulta
ser único
--TABLA REGION
---------------
if exists (select * from sysobjects where
name='uniq_region_nomregion')
alter table Region
drop constraint uniq_region_nomregion
alter table Region
add constraint uniq_region_nomregion
UNIQUE(Nomregion)
go
‹ En la tabla de Cliente nos aseguramos de que el Sexo sea sólo M o F en sus dos variantes:
if exists (select * from sysobjects where name='ch_clienteh_sexo')
alter table Clienteh
drop constraint ch_clienteh_sexo
alter table Clienteh
add constraint ch_clienteh_sexo
CHECK (Sexo like '[mMfF]')
go
‹ Y finalmente, que el tipo de comprobante sólo acepte los ingresos de “Boleta” o “Factura”
alter table Comprobante
add constraint CH_Comprobante_Tipocomp
CHECK(TipoComp in ('Boleta','Factura'))
go
(...)
insert into DetalleComp values (@idComprobante,@idprod,1,@costo,@Hora)
(...)
gDetalleComprobante:
o Recibe:
idComprobante
TipoComp
idCliente
Fecha date
idProducto
Cantidad
Subtotal
FechaHora
o Devuelve:
– –
o Operaciones:
Verifica si idComprobante existe en COMPROBANTE:
Si existe:
o Se insertan los datos pertinentes en DETALLECOMP.
Si no existe:
o Se insertan los datos de nuevo documento en
COMPROBANTE.
o Se insertan los datos pertinentes en DETALLECOMP.
VerCompporCodigo:
o Recibe:
idComprobante
o Devuelve:
o Operaciones:
MostrarTotalComprobante:
o Recibe:
idComprobante
o Devuelve:
Total
o Operaciones:
CancelarVentas:
o Recibe: idComprobante
o Devuelve:
o Operaciones:
Elimina de DETALLECOMP todos los registros con el idComprobante
ingresado.
Elimina de la tabla COMPROBANTE el registro con el idComprobante
ingresado.