IF COL_LENGTH('dbo.Productos', 'Meses_Garantia') IS NULL
ALTER TABLE Productos ADD Meses_Garantia SMALLINT


IF COL_LENGTH('dbo.Productos', 'Flag_Garantia_Ext') IS NULL
ALTER TABLE Productos ADD Flag_Garantia_Ext TINYINT



IF COL_LENGTH('dbo.Proveedores', 'Meses_Garantia') IS NULL
ALTER TABLE Proveedores ADD Meses_Garantia SMALLINT


IF COL_LENGTH('dbo.Prod_Familias', 'Meses_Garantia') IS NULL
ALTER TABLE Prod_Familias ADD Meses_Garantia SMALLINT




IF NOT EXISTS (SELECT * FROM sysobjects WHERE Name='Prod_Familias_Garantias' AND Xtype='U')
BEGIN
	CREATE TABLE Prod_Familias_Garantias (
			Id_Familia_Garan  	INT IDENTITY(1,1) NOT NULL,
			Id_Familia			INT NOT NULL,
			Meses_Garantia		SMALLINT,
			Descripcion			VARCHAR(255),
			Coef				DECIMAL(8,5),
			Id_Producto			VARCHAR(14),
			CONSTRAINT PK_PFG_PorID PRIMARY KEY (Id_Familia_Garan)
			)

	CREATE INDEX IX_PFG_PorFam ON Prod_Familias_Garantias (Id_Familia,Meses_Garantia)

	CREATE INDEX IX_PFG_PorProdGran ON Prod_Familias_Garantias (Id_Producto)

	ALTER TABLE Prod_Familias_Garantias ADD CONSTRAINT FK_PFG_Prod_Familias FOREIGN KEY (Id_Familia)
		REFERENCES Prod_Familias (Id_Familia)

	ALTER TABLE Prod_Familias_Garantias ADD CONSTRAINT FK_PFG_Productos FOREIGN KEY (Id_Producto)
		REFERENCES Productos (Id_Producto)
END
