CREATE TABLE MttoEstados ( 
IdEstado VARCHAR(4) NOT NULL,
Estado VARCHAR(50) NOT NULL,
NColor INT DEFAULT ((0)) NOT NULL,
Inactivo BIT DEFAULT ((0)) NOT NULL
 CONSTRAINT PK_MttoEstados PRIMARY KEY CLUSTERED (IdEstado),
 CONSTRAINT CK_MttoEstadosEstado CHECK ((len([Estado])>(0))),
 CONSTRAINT CK_MttoEstadosIdEstado CHECK ((len([IdEstado])>(0))))
CREATE TABLE MttoSistemas ( 
IdSistema VARCHAR(4) NOT NULL,
Sistema VARCHAR(50) NOT NULL,
Inactivo BIT DEFAULT ((0)) NOT NULL
 CONSTRAINT PK_MttoSistemas PRIMARY KEY CLUSTERED (IdSistema),
 CONSTRAINT CK_MttoSistemasIdSistema CHECK ((len([IdSistema])>(0))),
 CONSTRAINT CK_MttoSistemasSistema CHECK ((len([Sistema])>(0))))
CREATE TABLE MttoTipos ( 
IdMtto VARCHAR(4) NOT NULL,
TipoMtto VARCHAR(50) NOT NULL,
Inactivo BIT DEFAULT ((0)) NOT NULL
 CONSTRAINT PK_MttoTipos PRIMARY KEY CLUSTERED (IdMtto),
 CONSTRAINT CK_MttoTiposIdMtto CHECK ((len([IdMtto])>(0))),
 CONSTRAINT CK_MttoTiposTipoMtto CHECK ((len([TipoMtto])>(0))))
CREATE TABLE MttoTiposOdt ( 
IdTipoOdt VARCHAR(4) NOT NULL,
TipoOrden VARCHAR(50) NOT NULL,
Inactivo BIT DEFAULT ((0)) NOT NULL
 CONSTRAINT PK_MttoTiposOdt PRIMARY KEY CLUSTERED (IdTipoOdt),
 CONSTRAINT CK_MttoTiposOdtIdTipoOdt CHECK ((len([IdTipoOdt])>(0))),
 CONSTRAINT CK_MttoTiposOdtTipoOrden CHECK ((len([TipoOrden])>(0))))
CREATE TABLE MttoInspecciones ( 
IdInspec VARCHAR(4) NOT NULL,
Inspeccion VARCHAR(150) NOT NULL,
IdSistema VARCHAR(4) NOT NULL,
IdMtto VARCHAR(4) NOT NULL,
TipoDato INT DEFAULT ((0)) NOT NULL,
UndMed VARCHAR(50),
Inactivo BIT DEFAULT ((0)) NOT NULL
 CONSTRAINT PK_MttoInspecciones PRIMARY KEY CLUSTERED (IdInspec),
 CONSTRAINT CK_MttoInspeccionesIdInspec CHECK ((len([IdInspec])>(0))),
 CONSTRAINT CK_MttoInspeccionesIdMtto CHECK ((len([IdMtto])>(0))),
 CONSTRAINT CK_MttoInspeccionesIdSistema CHECK ((len([IdSistema])>(0))),
 CONSTRAINT CK_MttoInspeccionesInspeccion CHECK ((len([Inspeccion])>(0))))
CREATE TABLE MttoInspecRangos ( 
IdInspec VARCHAR(4) NOT NULL,
Item INT DEFAULT ((0)) NOT NULL,
Estado VARCHAR(100) NOT NULL,
ValorInicial DECIMAL(16,6) DEFAULT ((0)) NOT NULL,
ValorFinal DECIMAL(16,6) DEFAULT ((0)) NOT NULL,
NColor INT DEFAULT ((0)) NOT NULL,
ReqMtto BIT DEFAULT ((0)) NOT NULL
 CONSTRAINT PK_MttoInspecRangos PRIMARY KEY CLUSTERED (IdInspec,Item),
 CONSTRAINT CK_MttoInspecRangosEstado CHECK ((len([Estado])>(0))),
 CONSTRAINT CK_MttoInspecRangosIdInspec CHECK ((len([IdInspec])>(0))))
CREATE TABLE MttoConceptos ( 
IdConc VARCHAR(4) NOT NULL,
Concepto VARCHAR(150) NOT NULL,
IdSistema VARCHAR(4) NOT NULL,
IdMtto VARCHAR(4) NOT NULL,
TipoControl INT DEFAULT ((0)) NOT NULL,
Intervalo_km INT DEFAULT ((0)) NOT NULL,
Intervalo_dias INT DEFAULT ((0)) NOT NULL,
Req_Parte BIT DEFAULT ((0)) NOT NULL,
CodigoMO VARCHAR(16),
Inactivo BIT DEFAULT ((0)) NOT NULL,
FechaCrea SMALLDATETIME NOT NULL,
IdUsuario VARCHAR(11) NOT NULL
 CONSTRAINT PK_MttoConceptos PRIMARY KEY CLUSTERED (IdConc),
 CONSTRAINT CK_MttoConceptosConcepto CHECK ((len([Concepto])>(0))),
 CONSTRAINT CK_MttoConceptosIdConc CHECK ((len([IdConc])>(0))),
 CONSTRAINT CK_MttoConceptosIdMtto CHECK ((len([IdMtto])>(0))),
 CONSTRAINT CK_MttoConceptosIdSistema CHECK ((len([IdSistema])>(0))),
 CONSTRAINT CK_MttoConceptosIdUsuario CHECK ((len([IdUsuario])>(0))))
CREATE TABLE VehPartes ( 
IdVehiculo VARCHAR(10) NOT NULL,
NumParte VARCHAR(20) NOT NULL,
DescripParte VARCHAR(250) NOT NULL,
IdSistema VARCHAR(4) NOT NULL,
TipoControl INT DEFAULT ((0)) NOT NULL,
Intervalo_km INT DEFAULT ((0)) NOT NULL,
Intervalo_dias INT DEFAULT ((0)) NOT NULL,
kmActual INT DEFAULT ((0)) NOT NULL,
kmCambio INT DEFAULT ((0)) NOT NULL,
FechaCambio SMALLDATETIME,
CdProdRep VARCHAR(16),
TipoUnidad CHAR(1) DEFAULT ('V') NOT NULL,
TipoParte VARCHAR(10) DEFAULT ('ORIGINAL') NOT NULL,
IdEstado VARCHAR(4) NOT NULL,
Inactivo BIT DEFAULT ((0)) NOT NULL,
FechaRetiro SMALLDATETIME,
FechaCrea SMALLDATETIME NOT NULL,
IdUsuario VARCHAR(11) NOT NULL
 CONSTRAINT PK_VehPartes PRIMARY KEY CLUSTERED (IdVehiculo,NumParte),
 CONSTRAINT CK_VehPartesDescripParte CHECK ((len([DescripParte])>(0))),
 CONSTRAINT CK_VehPartesIdEstado CHECK ((len([IdEstado])>(0))),
 CONSTRAINT CK_VehPartesIdSistema CHECK ((len([IdSistema])>(0))),
 CONSTRAINT CK_VehPartesIdUsuario CHECK ((len([IdUsuario])>(0))),
 CONSTRAINT CK_VehPartesIdVehiculo CHECK ((len([IdVehiculo])>(0))),
 CONSTRAINT CK_VehPartesNumParte CHECK ((len([NumParte])>(0))),
 CONSTRAINT CK_VehPartesTipoParte CHECK ((len([TipoParte])>(0))),
 CONSTRAINT CK_VehPartesTipoUnidad CHECK ((len([TipoUnidad])>(0))))
CREATE TABLE tm_MttoDetalle ( 
tmNumero VARCHAR(5) NOT NULL,
tmItem INT DEFAULT ((0)) NOT NULL,
tmFecha SMALLDATETIME NOT NULL,
tmIdConc VARCHAR(4) NOT NULL,
tmDescripcion VARCHAR(1000) NOT NULL,
tmCantidad DECIMAL(14,4) DEFAULT ((0)) NOT NULL,
tmVrUnitario DECIMAL(16,6) DEFAULT ((0)) NOT NULL,
tmUndMed VARCHAR(50),
tmCdMtto VARCHAR(4),
tmIdEstado VARCHAR(4) NOT NULL,
tmIdOperario VARCHAR(16) NOT NULL,
tmNumVehic VARCHAR(10),
tmNumParte VARCHAR(20),
tmReqRep BIT DEFAULT ((0)) NOT NULL,
tmReqMO BIT DEFAULT ((0)) NOT NULL
 CONSTRAINT PK_tm_MttoDetalle PRIMARY KEY CLUSTERED (tmNumero,tmItem),
 CONSTRAINT CK_tm_MttoDetalletmDescripcion CHECK ((len([tmDescripcion])>(0))),
 CONSTRAINT CK_tm_MttoDetalletmIdConc CHECK ((len([tmIdConc])>(0))),
 CONSTRAINT CK_tm_MttoDetalletmIdEstado CHECK ((len([tmIdEstado])>(0))),
 CONSTRAINT CK_tm_MttoDetalletmIdOperario CHECK ((len([tmIdOperario])>(0))),
 CONSTRAINT CK_tm_MttoDetalletmNumero CHECK ((len([tmNumero])>(0))))
CREATE TABLE Trn_MttoOrden ( 
TipDoc VARCHAR(3) DEFAULT ('ODT') NOT NULL,
NumOrden INT DEFAULT ((0)) NOT NULL,
IdCia CHAR(2) DEFAULT ('01') NOT NULL,
Fecha SMALLDATETIME NOT NULL,
FechaOrden SMALLDATETIME NOT NULL,
FechaVence SMALLDATETIME,
IdTipoOdt VARCHAR(4) NOT NULL,
IdCenSer VARCHAR(4) NOT NULL,
IdVehiculo VARCHAR(10) NOT NULL,
nRemolque VARCHAR(10) DEFAULT ('0') NOT NULL,
IdConductor VARCHAR(16) NOT NULL,
IdOperario VARCHAR(16) NOT NULL,
IdSupervisor VARCHAR(16) NOT NULL,
Modalidad VARCHAR(10) DEFAULT ('INTERNA') NOT NULL,
kmVehiculo INT DEFAULT ((0)) NOT NULL,
kmRemolque INT DEFAULT ((0)) NOT NULL,
VrServicios MONEY DEFAULT ((0)) NOT NULL,
VrRepuestos MONEY DEFAULT ((0)) NOT NULL,
VrManoObra MONEY DEFAULT ((0)) NOT NULL,
VrOtros MONEY DEFAULT ((0)) NOT NULL,
FechaCierre SMALLDATETIME,
NitCenSer VARCHAR(16),
CdEstadoVeh VARCHAR(4),
IdEstado VARCHAR(4) NOT NULL,
Anulado BIT DEFAULT ((0)) NOT NULL,
NumDev INT DEFAULT ((0)) NOT NULL,
FecDev SMALLDATETIME,
Observacion VARCHAR(2000),
OrigenAdd VARCHAR(10) NOT NULL,
TimeSys SMALLDATETIME NOT NULL,
FecUpdate SMALLDATETIME,
IdCiaCrea CHAR(2) DEFAULT ('01') NOT NULL,
IdUsuario VARCHAR(11) NOT NULL
 CONSTRAINT PK_Trn_MttoOrden PRIMARY KEY CLUSTERED (TipDoc,NumOrden,IdCia),
 CONSTRAINT CK_Trn_MttoOrdenIdCenSer CHECK ((len([IdCenSer])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenIdCia CHECK ((len([IdCia])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenIdCiaCrea CHECK ((len([IdCiaCrea])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenIdConductor CHECK ((len([IdConductor])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenIdEstado CHECK ((len([IdEstado])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenIdOperario CHECK ((len([IdOperario])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenIdSupervisor CHECK ((len([IdSupervisor])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenIdTipoOdt CHECK ((len([IdTipoOdt])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenIdUsuario CHECK ((len([IdUsuario])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenIdVehiculo CHECK ((len([IdVehiculo])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenModalidad CHECK ((len([Modalidad])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenOrigenAdd CHECK ((len([OrigenAdd])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenTipDoc CHECK ((len([TipDoc])>(0))))
CREATE TABLE Trn_MttoOrdenDet ( 
TipOdt VARCHAR(3) DEFAULT ('ODT') NOT NULL,
NumOrden INT DEFAULT ((0)) NOT NULL,
IdCia CHAR(2) DEFAULT ('01') NOT NULL,
Item INT DEFAULT ((0)) NOT NULL,
FechaServ SMALLDATETIME NOT NULL,
IdConc VARCHAR(4) NOT NULL,
Descripcion VARCHAR(1000) NOT NULL,
Cantidad DECIMAL(14,4) DEFAULT ((0)) NOT NULL,
VrUnitario DECIMAL(16,6) DEFAULT ((0)) NOT NULL,
UndMed VARCHAR(50),
CdMtto VARCHAR(4),
IdEstado VARCHAR(4) NOT NULL,
IdOperario VARCHAR(16) NOT NULL,
NumVehic VARCHAR(10),
NumParte VARCHAR(20),
Req_Rep BIT DEFAULT ((0)) NOT NULL,
Req_MO BIT DEFAULT ((0)) NOT NULL,
FechaCrea SMALLDATETIME NOT NULL,
IdUsuario VARCHAR(11) NOT NULL
 CONSTRAINT PK_Trn_MttoOrdenDet PRIMARY KEY CLUSTERED (TipOdt,NumOrden,IdCia,Item),
 CONSTRAINT CK_Trn_MttoOrdenDetDescripcion CHECK ((len([Descripcion])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenDetIdConc CHECK ((len([IdConc])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenDetIdEstado CHECK ((len([IdEstado])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenDetIdOperario CHECK ((len([IdOperario])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenDetIdUsuario CHECK ((len([IdUsuario])>(0))))
CREATE TABLE Trn_MttoOrdenReq ( 
TipReq VARCHAR(3) DEFAULT ('RQM') NOT NULL,
Requisicion INT DEFAULT ((0)) NOT NULL,
IdCia CHAR(2) DEFAULT ('01') NOT NULL,
Item INT DEFAULT ((0)) NOT NULL,
TipoItem VARCHAR(10) DEFAULT ('REPUESTO') NOT NULL,
IdProducto VARCHAR(16) NOT NULL,
Descripcion VARCHAR(250) NOT NULL,
Cantidad DECIMAL(14,4) DEFAULT ((0)) NOT NULL,
IdUnd VARCHAR(4) NOT NULL,
VrUnitario DECIMAL(16,6) DEFAULT ((0)) NOT NULL,
CdBodega VARCHAR(4),
TipOdt VARCHAR(3) DEFAULT ('ODT') NOT NULL,
NumOrden INT DEFAULT ((0)) NOT NULL,
IdCiaOdt CHAR(2) DEFAULT ('00') NOT NULL,
EstadoReq INT DEFAULT ((0)) NOT NULL,
IdOperario VARCHAR(16) NOT NULL,
CdCenServ VARCHAR(4),
NitTercero VARCHAR(16),
NumVehic VARCHAR(10),
NumParte VARCHAR(20),
TipSal VARCHAR(3) DEFAULT ('0') NOT NULL,
NumSalida INT DEFAULT ((0)) NOT NULL,
IdCiaSal CHAR(2) DEFAULT ('00') NOT NULL,
CantSalida DECIMAL(14,4) DEFAULT ((0)) NOT NULL,
Observacion VARCHAR(1000),
FechaCrea SMALLDATETIME NOT NULL,
IdUsuario VARCHAR(11) NOT NULL
 CONSTRAINT PK_Trn_MttoOrdenReq PRIMARY KEY CLUSTERED (TipReq,Requisicion,IdCia,Item),
 CONSTRAINT CK_Trn_MttoOrdenReqDescripcion CHECK ((len([Descripcion])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenReqIdCia CHECK ((len([IdCia])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenReqIdCiaSal CHECK ((len([IdCiaSal])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenReqIdOperario CHECK ((len([IdOperario])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenReqIdProducto CHECK ((len([IdProducto])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenReqIdUnd CHECK ((len([IdUnd])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenReqIdUsuario CHECK ((len([IdUsuario])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenReqTipOdt CHECK ((len([TipOdt])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenReqTipoItem CHECK ((len([TipoItem])>(0))),
 CONSTRAINT CK_Trn_MttoOrdenReqTipSal CHECK ((len([TipSal])>(0))))
CREATE TABLE Trn_MttoDiagn ( 
TipOdt VARCHAR(3) DEFAULT ('ODT') NOT NULL,
NumOrden INT DEFAULT ((0)) NOT NULL,
IdCia CHAR(2) DEFAULT ('01') NOT NULL,
NumDiagn INT DEFAULT ((0)) NOT NULL,
FechaDiagn SMALLDATETIME NOT NULL,
TipoUnidad CHAR(1) DEFAULT ('V') NOT NULL,
NumVehic VARCHAR(10) NOT NULL,
IdOperario VARCHAR(16) NOT NULL,
FechaCrea SMALLDATETIME NOT NULL,
IdUsuario VARCHAR(11) NOT NULL
 CONSTRAINT PK_Trn_MttoDiagn PRIMARY KEY CLUSTERED (TipOdt,NumOrden,IdCia,NumDiagn),
 CONSTRAINT CK_Trn_MttoDiagnIdCia CHECK ((len([IdCia])>(0))),
 CONSTRAINT CK_Trn_MttoDiagnIdOperario CHECK ((len([IdOperario])>(0))),
 CONSTRAINT CK_Trn_MttoDiagnIdUsuario CHECK ((len([IdUsuario])>(0))),
 CONSTRAINT CK_Trn_MttoDiagnNumVehic CHECK ((len([NumVehic])>(0))),
 CONSTRAINT CK_Trn_MttoDiagnTipOdt CHECK ((len([TipOdt])>(0))),
 CONSTRAINT CK_Trn_MttoDiagnTipoUnidad CHECK ((len([TipoUnidad])>(0))))
CREATE TABLE Trn_MttoDiagnDet ( 
TipOdt VARCHAR(3) DEFAULT ('ODT') NOT NULL,
NumOrden INT DEFAULT ((0)) NOT NULL,
IdCia CHAR(2) DEFAULT ('01') NOT NULL,
NumDiagn INT DEFAULT ((0)) NOT NULL,
Item INT DEFAULT ((0)) NOT NULL,
IdInspec VARCHAR(4) NOT NULL,
ValorInspec VARCHAR(100) NOT NULL,
UndMedida VARCHAR(50),
NumColor INT DEFAULT ((0)) NOT NULL,
Req_Mtto BIT DEFAULT ((0)) NOT NULL,
Intervalo VARCHAR(100),
Observacion VARCHAR(500)
 CONSTRAINT PK_Trn_MttoDiagnDet PRIMARY KEY CLUSTERED (TipOdt,NumOrden,IdCia,NumDiagn,Item),
 CONSTRAINT CK_Trn_MttoDiagnDetIdCia CHECK ((len([IdCia])>(0))),
 CONSTRAINT CK_Trn_MttoDiagnDetIdInspec CHECK ((len([IdInspec])>(0))),
 CONSTRAINT CK_Trn_MttoDiagnDetTipOdt CHECK ((len([TipOdt])>(0))),
 CONSTRAINT CK_Trn_MttoDiagnDetValorInspec CHECK ((len([ValorInspec])>(0))))
GO
ALTER TABLE tm_ReqDetalle ADD tmCantSalida DECIMAL(14,4),tmIdOperario VARCHAR(16),tmCdCenServ VARCHAR(4)
,tmNitTercero VARCHAR(16),tmNumVehic VARCHAR(10),tmNumParte VARCHAR(20),tmEstado INT DEFAULT(0),tmObservacion VARCHAR(1000)
GO
ALTER TABLE MttoInspecciones ADD 
 CONSTRAINT FK_MttoInspecciones_MttoSistemas FOREIGN KEY (IdSistema) REFERENCES [dbo].[MttoSistemas] (IdSistema),
 CONSTRAINT FK_MttoInspecciones_MttoTipos FOREIGN KEY (IdMtto) REFERENCES [dbo].[MttoTipos] (IdMtto)
ALTER TABLE MttoInspecRangos ADD 
 CONSTRAINT FK_MttoInspecRangos_MttoInspecciones FOREIGN KEY (IdInspec) REFERENCES [dbo].[MttoInspecciones] (IdInspec)
ALTER TABLE MttoConceptos ADD 
 CONSTRAINT FK_MttoConceptos_adm_Usuarios FOREIGN KEY (IdUsuario) REFERENCES [dbo].[adm_Usuarios] (IdUsuario),
 CONSTRAINT FK_MttoConceptos_MttoSistemas FOREIGN KEY (IdSistema) REFERENCES [dbo].[MttoSistemas] (IdSistema),
 CONSTRAINT FK_MttoConceptos_MttoTipos FOREIGN KEY (IdMtto) REFERENCES [dbo].[MttoTipos] (IdMtto)
ALTER TABLE VehPartes ADD 
 CONSTRAINT FK_VehPartes_adm_Usuarios FOREIGN KEY (IdUsuario) REFERENCES [dbo].[adm_Usuarios] (IdUsuario),
 CONSTRAINT FK_VehPartes_EstadoSer FOREIGN KEY (IdEstado) REFERENCES [dbo].[EstadoSer] (IdEstado),
 CONSTRAINT FK_VehPartes_MttoSistemas FOREIGN KEY (IdSistema) REFERENCES [dbo].[MttoSistemas] (IdSistema)
ALTER TABLE Trn_MttoOrden ADD 
 CONSTRAINT FK_Trn_MttoOrden_adm_Usuarios FOREIGN KEY (IdUsuario) REFERENCES [dbo].[adm_Usuarios] (IdUsuario),
 CONSTRAINT FK_Trn_MttoOrden_CentrosServ FOREIGN KEY (IdCenSer) REFERENCES [dbo].[CentrosServ] (IdCenSer),
 CONSTRAINT FK_Trn_MttoOrden_Companias FOREIGN KEY (IdCia) REFERENCES [dbo].[Companias] (IdCia),
 CONSTRAINT FK_Trn_MttoOrden_Companias1 FOREIGN KEY (IdCiaCrea) REFERENCES [dbo].[Companias] (IdCia),
 CONSTRAINT FK_Trn_MttoOrden_EstadoDoc FOREIGN KEY (IdEstado) REFERENCES [dbo].[EstadoDoc] (IdEstado),
 CONSTRAINT FK_Trn_MttoOrden_MttoTiposOdt FOREIGN KEY (IdTipoOdt) REFERENCES [dbo].[MttoTiposOdt] (IdTipoOdt),
 CONSTRAINT FK_Trn_MttoOrden_Sys_TiposDoc FOREIGN KEY (TipDoc) REFERENCES [dbo].[Sys_TiposDoc] (IdDoc),
 CONSTRAINT FK_Trn_MttoOrden_Terceros FOREIGN KEY (IdConductor) REFERENCES [dbo].[Terceros] (IdTercero),
 CONSTRAINT FK_Trn_MttoOrden_Terceros1 FOREIGN KEY (IdOperario) REFERENCES [dbo].[Terceros] (IdTercero),
 CONSTRAINT FK_Trn_MttoOrden_Terceros2 FOREIGN KEY (IdSupervisor) REFERENCES [dbo].[Terceros] (IdTercero)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenFecha ON Trn_MttoOrden(Fecha)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenFechaOrden ON Trn_MttoOrden(FechaOrden)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenIdVehiculo ON Trn_MttoOrden(IdVehiculo)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenIdConductor ON Trn_MttoOrden(IdConductor)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenIdOperario ON Trn_MttoOrden(IdOperario)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenIdCenSer ON Trn_MttoOrden(IdCenSer)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdennRemolque ON Trn_MttoOrden(nRemolque)
ALTER TABLE Trn_MttoOrdenDet ADD 
 CONSTRAINT FK_Trn_MttoOrdenDet_adm_Usuarios FOREIGN KEY (IdUsuario) REFERENCES [dbo].[adm_Usuarios] (IdUsuario),
 CONSTRAINT FK_Trn_MttoOrdenDet_MttoConceptos FOREIGN KEY (IdConc) REFERENCES [dbo].[MttoConceptos] (IdConc),
 CONSTRAINT FK_Trn_MttoOrdenDet_MttoEstados FOREIGN KEY (IdEstado) REFERENCES [dbo].[MttoEstados] (IdEstado),
 CONSTRAINT FK_Trn_MttoOrdenDet_Terceros FOREIGN KEY (IdOperario) REFERENCES [dbo].[Terceros] (IdTercero),
 CONSTRAINT FK_Trn_MttoOrdenDet_Trn_MttoOrden FOREIGN KEY (TipOdt,NumOrden,IdCia) REFERENCES [dbo].[Trn_MttoOrden] (TipDoc,NumOrden,IdCia)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenDetFechaServ ON Trn_MttoOrdenDet(FechaServ)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenDetIdConc ON Trn_MttoOrdenDet(IdConc)
ALTER TABLE Trn_MttoOrdenReq ADD 
 CONSTRAINT FK_Trn_MttoOrdenReq_adm_Usuarios FOREIGN KEY (IdUsuario) REFERENCES [dbo].[adm_Usuarios] (IdUsuario),
 CONSTRAINT FK_Trn_MttoOrdenReq_ProdMcias FOREIGN KEY (IdProducto) REFERENCES [dbo].[ProdMcias] (IdProducto),
 CONSTRAINT FK_Trn_MttoOrdenReq_Terceros FOREIGN KEY (IdOperario) REFERENCES [dbo].[Terceros] (IdTercero),
 CONSTRAINT FK_Trn_MttoOrdenReq_Trn_MttoOrden FOREIGN KEY (TipOdt,NumOrden,IdCiaOdt) REFERENCES [dbo].[Trn_MttoOrden] (TipDoc,NumOrden,IdCia),
 CONSTRAINT FK_Trn_MttoOrdenReq_UndMed FOREIGN KEY (IdUnd) REFERENCES [dbo].[UndMed] (IdUnd)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenReqIdProducto ON Trn_MttoOrdenReq(IdProducto)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenReqNumSalida ON Trn_MttoOrdenReq(TipSal,NumSalida,IdCiaSal)
 CREATE NONCLUSTERED INDEX IX_Trn_MttoOrdenReqNumOrden ON Trn_MttoOrdenReq(TipOdt,NumOrden,IdCiaOdt)
ALTER TABLE Trn_MttoDiagn ADD 
 CONSTRAINT FK_Trn_MttoDiagn_adm_Usuarios FOREIGN KEY (IdUsuario) REFERENCES [dbo].[adm_Usuarios] (IdUsuario),
 CONSTRAINT FK_Trn_MttoDiagn_Terceros FOREIGN KEY (IdOperario) REFERENCES [dbo].[Terceros] (IdTercero),
 CONSTRAINT FK_Trn_MttoDiagn_Trn_MttoOrden FOREIGN KEY (TipOdt,NumOrden,IdCia) REFERENCES [dbo].[Trn_MttoOrden] (TipDoc,NumOrden,IdCia)
ALTER TABLE Trn_MttoDiagnDet ADD 
 CONSTRAINT FK_Trn_MttoDiagnDet_MttoInspecciones FOREIGN KEY (IdInspec) REFERENCES [dbo].[MttoInspecciones] (IdInspec),
 CONSTRAINT FK_Trn_MttoDiagnDet_Trn_MttoDiagn FOREIGN KEY (TipOdt,NumOrden,IdCia,NumDiagn) REFERENCES [dbo].[Trn_MttoDiagn] (TipOdt,NumOrden,IdCia,NumDiagn)
