
Tipos de datos definidos por el usuario
Los tipos de datos definidos por el usuario permiten facilitar la estandarización de los tipos de las columnas en un diseño de base de datos.
En este ejemplo se crea una base de datos para luego crear un diseño con una Factura, los productos y el Detalle de la factura
Ejemplo
Creamos la base de datos y la abrimos.
xp_create_subdir ‘C:\Bases’
go
Create database ERP
on Primary (name=’ERP01′, filename=’C:\Bases\ERP01.mdf’),
filegroup VENTAS
(name=’ERP02′, filename=’C:\Bases\ERP02.ndf’),
filegroup RRHH
(name=’ERP03′, filename=’C:\Bases\ERP03.ndf’)
log on
(name=’ERP04′, filename=’C:\Bases\ERP04.ldf’)
go
use ERP
go
Tipos de datos definidos por el usuario
Para crear se utiliza
Create type NombreTipoDato from TipoDatosSQL
Para eliminar el tipo de dato (no lo debe estar usando ninguna tabla)
Drop type NombreTipoDato
Tipos de datos Codigo5, TextoLargo100, Texto200, NumeroGrande
Create type Codigo5 from nchar(5) not null
Create type TextoLargo100 from nvarchar(100)
Create type Texto200 from nvarchar(200)
Create type NumeroGrande from Numeric(17,2)
go
Lista de los tipos de datos
select * from sys.types where is_user_defined = 1
go
Crear el tipo NumeroGrande, primero ver si el tipo no existe.
if not exists
(select * from sys.types where is_user_defined = 1 and name=’NumeroGrande’)
Begin
Execute(‘Create type NumeroGrande from Numeric(17,2)’)
End
go

Tablas: Facturas, dos campos PK, Campos Calculados (Ver Campos Calculados)
Create table Facturas
(
FacturasNumeroSerie Codigo5,
FacturasNumeroFactura nchar(7) not null,
FacturasFechaEmision DateTime,
FacturasPorcentajeIGV Numeric(4,2),
FacturasMontoSinIGV NumeroGrande,
FacturasMontoIGV As (FacturasPorcentajeIGV* FacturasMontoSinIGV),
FacturasMontoTotalFactura As
(FacturasMontoSinIGV + FacturasPorcentajeIGV* FacturasMontoSinIGV),
constraint FacturasPK Primary key
(FacturasNumeroSerie, FacturasNumeroFactura)
)
go
Tabla Productos
Create table Productos
(
ProductosCodigo Codigo5,
ProductosDescripcion Texto200,
ProductosPrecio Numeric(9,2),
constraint ProductosPK Primary key (ProductosCodigo)
)
go
Detalle de factura
Create table FacturasDetalle
(
FacturasNumeroSerie Codigo5,
FacturasNumeroFactura nchar(7) not null,
ProductosCodigo Codigo5,
FacturasDetalleCantidadVendida Numeric(9,2),
FacturasDetallePrecioVenta Numeric(9,2),
FacturasDetalleValorVenta As
(FacturasDetalleCantidadVendida * FacturasDetallePrecioVenta),
constraint FacturasDetallePK Primary key
(FacturasNumeroSerie,FacturasNumeroFactura,ProductosCodigo),
constraint FacturasDetalleConFacturasFK Foreign key
(FacturasNumeroSerie,FacturasNumeroFactura)
references Facturas(FacturasNumeroSerie,FacturasNumeroFactura),
constraint FacturasDetalleConProductos Foreign key (ProductosCodigo)
references Productos(ProductosCodigo)
)
go
Para borrar el tipo de dato definido por el usuario llamado Codigo5
Drop type Codigo5
go
Mens. 3732, Nivel 16, Estado 1, Línea 110 No se puede quitar el tipo ‘Codigo5’ porque el objeto ‘Facturas’ hace referencia a él.
Puede que haya otros objetos que hagan referencia a este tipo.
Modificar tablas para poder eliminar el tipo de datos Codigo5
— Cambiar de Codigo5 a nchar(5) not null
Alter table Facturas
alter column FacturasNumeroSerie nchar(5) not null
go
En Productos
Alter table Productos
alter column ProductosCodigo nchar(5) not null
go
Eliminar el tipo TextoLargo100
Drop type TextoLargo100
go