IF NOT EXISTS(
	SELECT 
		1
	FROM 
		 SYS.TABLES 
	WHERE 
		NAME = 'EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA' )
BEGIN
	
SET ANSI_NULLS ON

SET QUOTED_IDENTIFIER ON

CREATE TABLE [dbo].[EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA](
	[CD_EMP] [int] NOT NULL,
	[CD_TBL_COMIS] [int] NOT NULL,
	[CD_ARV_MERC_FAMILIA] [int] NOT NULL,
	[PERC_COMIS] [MONEY] NOT NULL,
 CONSTRAINT [PK_EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA] PRIMARY KEY CLUSTERED 
(
	[CD_EMP] ASC,
	[CD_TBL_COMIS] ASC,
	[CD_ARV_MERC_FAMILIA] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]


ALTER TABLE [dbo].[EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA]  WITH CHECK ADD  CONSTRAINT [FK_EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA_EST_ARV_MERC_FAMILIA] FOREIGN KEY([CD_EMP], [CD_ARV_MERC_FAMILIA])
REFERENCES [dbo].[EST_ARV_MERC_FAMILIA] ([CD_EMP], [CD_ARV_MERC_FAMILIA])
ON UPDATE CASCADE

ALTER TABLE [dbo].[EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA] CHECK CONSTRAINT [FK_EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA_EST_ARV_MERC_FAMILIA]

ALTER TABLE [dbo].[EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA]  WITH CHECK ADD  CONSTRAINT [FK_EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA_EST_PROD_TBL_DESC] FOREIGN KEY([CD_EMP], [CD_TBL_COMIS])
REFERENCES [dbo].[EST_PROD_TBL_COMIS] ([CD_EMP], [CD_TBL_COMIS])
ON UPDATE CASCADE
ON DELETE CASCADE

ALTER TABLE [dbo].[EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA] CHECK CONSTRAINT [FK_EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA_EST_PROD_TBL_DESC]

ALTER TABLE [dbo].[EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA] ADD  CONSTRAINT [DF_EST_PROD_TBL_COMIS_EST_ARV_MERC_FAMILIA_PERC_COMIS]  DEFAULT ((0)) FOR [PERC_COMIS]

END

