if not exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}[{objectQualifier}tblNaDevEventsCustomEventAttribute]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
 begin

	CREATE TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsCustomEventAttribute](
		[Id] [int] IDENTITY(1,1) NOT NULL,
		[ModuleId] [int] NOT NULL,
		[AttributeName] [nvarchar] (255) not null,
		[IsMandatory] [bit] not null
	 CONSTRAINT [PK_{objectQualifier}tblNaDevEventsCustomEventAttribute] PRIMARY KEY CLUSTERED 
	(
		[Id] ASC
	)
	)
 end
GO

if not exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}[{objectQualifier}tblNaDevEventsCustomEntity]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
 begin

	CREATE TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsCustomEntity](
		[EntityId] [int] IDENTITY(1,1) NOT NULL,
		[ModuleId] [int] NOT NULL,
		[EntityName] [nvarchar] (255) not null
	 CONSTRAINT [PK_{objectQualifier}tblNaDevEventsCustomEntity] PRIMARY KEY CLUSTERED 
	(
		[EntityId] ASC
	)
	)
 end
GO

if not exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}[{objectQualifier}tblNaDevEventsCustomEntityValue]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
 begin

	CREATE TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsCustomEntityValue](
		[Id] [int] IDENTITY(1,1) NOT NULL,
		[EntityId] [int] NOT NULL,
		[EntityValue] [nvarchar] (max) null
	 CONSTRAINT [PK_{objectQualifier}tblNaDevEventsCustomEntityValue] PRIMARY KEY CLUSTERED 
	(
		[Id] ASC
	)
	)
 end
GO

if not exists (SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='FK_{objectQualifier}tblNaDevEventsCustomEntityValue_tblNaDevEventsCustomEntity')

ALTER TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsCustomEntityValue]  WITH CHECK ADD  CONSTRAINT [FK_{objectQualifier}tblNaDevEventsCustomEntityValue_tblNaDevEventsCustomEntity] FOREIGN KEY([EntityId])
REFERENCES {databaseOwner}[{objectQualifier}tblNaDevEventsCustomEntity] ([EntityId])
ON DELETE CASCADE

GO

if not exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}[{objectQualifier}tblNaDevEventsEventCustomEntitySupported]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
 begin

	CREATE TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsEventCustomEntitySupported](
		[SupportedId] [int] IDENTITY(1,1) NOT NULL,
		[EventId] [int] NOT NULL,
		[EntityValueId] [int] NOT NULL
	 CONSTRAINT [PK_{objectQualifier}tblNaDevEventsEventCustomEntitySupported] PRIMARY KEY CLUSTERED 
	(
		[SupportedId] ASC
	)
	)
 end
GO

if not exists (SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='FK_{objectQualifier}tblNaDevEventsEventCustomEntitySupported_tblNaDevEventsCustomEntityValue')

ALTER TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsEventCustomEntitySupported]  WITH CHECK ADD  CONSTRAINT [FK_{objectQualifier}tblNaDevEventsEventCustomEntitySupported_tblNaDevEventsCustomEntityValue] FOREIGN KEY([EntityValueId])
REFERENCES {databaseOwner}[{objectQualifier}tblNaDevEventsCustomEntityValue] ([Id])
ON DELETE CASCADE

GO

if not exists (SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='FK_{objectQualifier}tblNaDevEventsEventCustomEntitySupported_tblNaDevEvents')

ALTER TABLE {databaseOwner}[{objectQualifier}tblNaDevEventsEventCustomEntitySupported]  WITH CHECK ADD  CONSTRAINT [FK_{objectQualifier}tblNaDevEventsEventCustomEntitySupported_tblNaDevEvents] FOREIGN KEY([EventId])
REFERENCES {databaseOwner}[{objectQualifier}tblNaDevEvents] ([EventId])
ON DELETE CASCADE

GO