Creacion de tablas con valores predeterminados e indices

CREATE TABLE dbo.status_tabla
(
id int NOT NULL,
status_tabla nvarchar(50) NOT NULL
)  ON [PRIMARY]
GO
ALTER TABLE dbo.status_tabla ADD CONSTRAINT
PK_status_tabla PRIMARY KEY CLUSTERED
(
id
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
CREATE UNIQUE NONCLUSTERED INDEX IX_status_tabla ON dbo.status_tabla
(
status_tabla
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

Creacion de tablas, valores predeterminados e integridad referencial en SQL Server

CREATE TABLE dbo.presupuesto
(
id int NOT NULL,
idorden int NOT NULL,
fecha smalldatetime NOT NULL,
idusuario_captura int NOT NULL,
fechautoriza smalldatetime NULL,
idusuario_autoriza int NOT NULL,
importotal money NOT NULL,
idstatus_presupuesto int NOT NULL
)  ON [PRIMARY]
GO
ALTER TABLE dbo.presupuesto ADD CONSTRAINT
DF_presupuesto_fecha DEFAULT GETDATE() FOR fecha
GO
ALTER TABLE dbo.presupuesto ADD CONSTRAINT
DF_presupuesto_importotal DEFAULT 0 FOR importotal
GO
ALTER TABLE dbo.presupuesto ADD CONSTRAINT
DF_presupuesto_idstatus_presupuesto DEFAULT 0 FOR idstatus_presupuesto
GO
ALTER TABLE dbo.presupuesto ADD CONSTRAINT
PK_presupuesto PRIMARY KEY CLUSTERED
(
id
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.presupuesto ADD CONSTRAINT
fk_presupuesto_orden FOREIGN KEY
(
idorden
) REFERENCES dbo.orden
(
id
) ON UPDATE  NO ACTION
ON DELETE  NO ACTION

GO
ALTER TABLE dbo.presupuesto ADD CONSTRAINT
fk_presupuesto_usuario_captura FOREIGN KEY
(
idusuario_captura
) REFERENCES dbo.usuario
(
id
) ON UPDATE  NO ACTION
ON DELETE  NO ACTION

GO
ALTER TABLE dbo.presupuesto ADD CONSTRAINT
fk_presupuesto_usuario_autoriza FOREIGN KEY
(
idusuario_autoriza
) REFERENCES dbo.usuario
(
id
) ON UPDATE  NO ACTION
ON DELETE  NO ACTION

GO
ALTER TABLE dbo.presupuesto ADD CONSTRAINT
fk_presupuesto_status_presupuesto FOREIGN KEY
(
idstatus_presupuesto
) REFERENCES dbo.status_presupuesto
(
id
) ON UPDATE  NO ACTION
ON DELETE  NO ACTION

GO
ALTER TABLE dbo.presupuesto SET (LOCK_ESCALATION = TABLE)
GO

CREATE TABLE dbo.presupuestodet
(
id int NOT NULL,
numpar int NOT NULL,
idpresupuesto int NOT NULL,
cant float NOT NULL,
idproduc int NOT NULL,
idreparaclasif int NOT NULL,
idrepara int NOT NULL,
fechvigencia smalldatetime,
precio float NOT NULL,
impor float NOT NULL,
)  ON [PRIMARY]
GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
PK_status_presupuesto PRIMARY KEY CLUSTERED
(
id
) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
DF_presupuestodet_cant DEFAULT 0 FOR cant
GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
DF_presupuestodet_idproduc DEFAULT 0 FOR idproduc
GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
DF_presupuestodet_idreparaclasif DEFAULT 0 FOR idreparaclasif
GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
DF_presupuestodet_idrepara DEFAULT 0 FOR idrepara
GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
DF_presupuestodet_fechvigencia DEFAULT GETDATE() FOR fechvigencia
GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
DF_presupuestodet_precio DEFAULT 0 FOR precio
GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
DF_presupuestodet_impor DEFAULT 0 FOR impor
GO

ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
fk_presupuestodet_presupuesto FOREIGN KEY
(
idpresupuesto
) REFERENCES dbo.presupuesto
(
id
) ON UPDATE  NO ACTION
ON DELETE  NO ACTION

GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
fk_presupuestodet_produc FOREIGN KEY
(
idproduc
) REFERENCES dbo.produc
(
id
) ON UPDATE  NO ACTION
ON DELETE  NO ACTION

GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
fk_presupuestodet_reparaclasif FOREIGN KEY
(
idreparaclasif
) REFERENCES dbo.reparaclasif
(
id
) ON UPDATE  NO ACTION
ON DELETE  NO ACTION

GO
ALTER TABLE dbo.presupuestodet ADD CONSTRAINT
fk_presupuestodet_repara FOREIGN KEY
(
idrepara
) REFERENCES dbo.repara
(
id
) ON UPDATE  NO ACTION
ON DELETE  NO ACTION

GO