/*
 * $Id: migpost75.sql 84480 2007-07-24 06:34:27Z pca $
 *
 * Finish migrating a ManageSoft 7.5 database into ManageSoft 7.5 format.
 *
 * Use migprep.sql first, then schema.sql, then this, and finally
 * install Managesoft 7.5 which will create the stored procedures.
 *
 * Called after building the new tables, this script:
 * - migrates data from old tables
 *
 * Copyright 1995-2003 ManageSoft Corporation Ltd ABN 40 052 412 156. All rights reserved.
 */

USE ManageSoft
GO

/*
 * Check that we are upgrading from a 7.2 database...
 */
DECLARE @schema_version char(10)
SET @schema_version = (SELECT Value FROM DatabaseConfiguration
		WHERE Property = 'SchemaVersion')

IF (@schema_version = '7.2')
BEGIN
	PRINT 'Completing migration to ManageSoft 7.5 database information...'
END
ELSE IF (@schema_version = '7.5')
BEGIN
	RAISERROR ('Your ManageSoft database has already been migrated to 7.5', 16, 1)
	RAISERROR ('For more information on migrating your database, refer to the ManageSoft Upgrade Guide.', 16, 1)
END
ELSE
BEGIN
	RAISERROR ('This script can only migrate database information from ManageSoft 7.2, not from any previous versions.', 16, 1)
	RAISERROR ('Please migrate any previous versions of your ManageSoft database first.', 16, 1)
	RAISERROR ('For more information on migrating your database, refer to the ManageSoft Upgrade Guide.', 16, 1)
END
GO

--===========================================================================
--		Update the ManageSoft tables
--===========================================================================

IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_OperatingSystem_Category'
)
BEGIN
	PRINT 'Create FK_OperatingSystem_Category constraint'

	ALTER TABLE OperatingSystem
		ADD CONSTRAINT FK_OperatingSystem_Category
		FOREIGN KEY(Category)
		REFERENCES OperatingSystem(OperatingSystemID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_OperatingSystem_Category constraint'
GO
-- Every Organization belongs to a Domain
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Organization_Domain'
)
BEGIN
	PRINT 'Create FK_Organization_Domain constraint'

	ALTER TABLE Organization
		ADD CONSTRAINT FK_Organization_Domain
		FOREIGN KEY(DomainID)
		REFERENCES Domain(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Organization_Domain constraint'
GO
-- Every Domain is an Organization
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Domain_ID'
)
BEGIN
	PRINT 'Create FK_Domain_ID constraint'

	ALTER TABLE Domain
		ADD CONSTRAINT FK_Domain_ID
		FOREIGN KEY(OrganizationID)
		REFERENCES Organization(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Domain_ID constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_User_OU'
)
BEGIN
	PRINT 'Create FK_User_OU constraint'

	ALTER TABLE [User]
		ADD CONSTRAINT FK_User_OU
		FOREIGN KEY(UserOUID)
		REFERENCES Organization(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_User_OU constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Computer_OU'
)
BEGIN
	PRINT 'Create FK_Computer_OU constraint'

	ALTER TABLE Computer
		ADD CONSTRAINT FK_Computer_OU
		FOREIGN KEY(ComputerOUID)
		REFERENCES Organization(OrganizationID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Computer_OU constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Computer_OperatingSystem'
)
BEGIN
	PRINT 'Create FK_Computer_OperatingSystem constraint'

	ALTER TABLE Computer
		ADD CONSTRAINT FK_Computer_OperatingSystem
		FOREIGN KEY(OperatingSystemID)
		REFERENCES OperatingSystem(OperatingSystemID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Computer_OperatingSystem constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ADSDOU_Domain'
)
BEGIN
	PRINT 'Create FK_ADSDOU_Domain constraint'

	ALTER TABLE ADSDOU
		ADD CONSTRAINT FK_ADSDOU_Domain
		FOREIGN KEY(DomainID)
		REFERENCES Domain(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_ADSDOU_Domain constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ADComputer_Domain'
)
BEGIN
	PRINT 'Create FK_ADComputer_Domain constraint'

	ALTER TABLE ADComputer
		ADD CONSTRAINT FK_ADComputer_Domain
		FOREIGN KEY(DomainID)
		REFERENCES Domain(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_ADComputer_Domain constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ADUser_Domain'
)
BEGIN
	PRINT 'Create FK_ADUser_Domain constraint'

	ALTER TABLE ADUser
		ADD CONSTRAINT FK_ADUser_Domain
		FOREIGN KEY(DomainID)
		REFERENCES Domain(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_ADUser_Domain constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Group_Organization'
)
BEGIN
	PRINT 'Create FK_Group_Organization constraint'

	ALTER TABLE [Group]
		ADD CONSTRAINT FK_Group_Organization
		FOREIGN KEY(GroupOUID)
		REFERENCES Organization(OrganizationID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Group_Organization constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Member_TargetType'
)
BEGIN
	PRINT 'Create FK_Member_TargetType constraint'

	ALTER TABLE Member
		ADD CONSTRAINT FK_Member_TargetType
		FOREIGN KEY(TargetTypeID)
		REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Member_TargetType constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Member_Group'
)
BEGIN
	PRINT 'Create FK_Member_Group constraint'

	ALTER TABLE Member
		ADD CONSTRAINT FK_Member_Group
		FOREIGN KEY(GroupID)
		REFERENCES [Group](GroupID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Member_Group constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_TransitiveMember_TargetType'
)
BEGIN
	PRINT 'Create FK_TransitiveMember_TargetType constraint'

	ALTER TABLE TransitiveMember
		ADD CONSTRAINT FK_TransitiveMember_TargetType
		FOREIGN KEY(TargetTypeID)
		REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_TransitiveMember_TargetType constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_TransitiveMember_Group'
)
BEGIN
	PRINT 'Create FK_TransitiveMember_Group constraint'

	ALTER TABLE TransitiveMember
		ADD CONSTRAINT FK_TransitiveMember_Group
		FOREIGN KEY(GroupID)
		REFERENCES [Group](GroupID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_TransitiveMember_Group constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackagePath_ParentPath'
)
BEGIN
	PRINT 'Create FK_PackagePath_ParentPath constraint'

	ALTER TABLE PackagePath
		ADD CONSTRAINT FK_PackagePath_ParentPath
		FOREIGN KEY(ParentPathID)
		REFERENCES PackagePath(PackagePathID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_PackagePath_ParentPath constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackagePath_PackagePathType'
)
BEGIN
	PRINT 'Create FK_PackagePath_PackagePathType constraint'

	ALTER TABLE PackagePath
		ADD CONSTRAINT FK_PackagePath_PackagePathType
		FOREIGN KEY(PackagePathTypeID)
		REFERENCES PackagePathType(PackagePathTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_PackagePath_PackagePathType constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageVersion_PackageFamily'
)
BEGIN
	PRINT 'Create FK_PackageVersion_PackageFamily constraint'

	ALTER TABLE PackageVersion
		ADD CONSTRAINT FK_PackageVersion_PackageFamily
		FOREIGN KEY(PackageFamilyID)
		REFERENCES PackageFamily(PackageFamilyID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_PackageVersion_PackageFamily constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageVersion_PackagePath'
)
BEGIN
	PRINT 'Create FK_PackageVersion_PackagePath constraint'

	ALTER TABLE PackageVersion
		ADD CONSTRAINT FK_PackageVersion_PackagePath
		FOREIGN KEY(PackagePathID)
		REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_PackageVersion_PackagePath constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageProvides_PackageFamily'
)
BEGIN
	PRINT 'Create FK_PackageProvides_PackageFamily constraint'

	ALTER TABLE PackageProvides
		ADD CONSTRAINT FK_PackageProvides_PackageFamily
		FOREIGN KEY(PackageFamilyID)
		REFERENCES PackageFamily(PackageFamilyID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_PackageProvides_PackageFamily constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageProvides_PackageVersion'
)
BEGIN
	PRINT 'Create FK_PackageProvides_PackageVersion constraint'

	ALTER TABLE PackageProvides
		ADD CONSTRAINT FK_PackageProvides_PackageVersion
		FOREIGN KEY(PackageVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_PackageProvides_PackageVersion constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageRequires_PackageVersion'
)
BEGIN
	PRINT 'Create FK_PackageRequires_PackageVersion constraint'

	ALTER TABLE PackageRequires
		ADD CONSTRAINT FK_PackageRequires_PackageVersion
		FOREIGN KEY(PackageVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_PackageRequires_PackageVersion constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_MediaType'
)
BEGIN
	PRINT 'Create FK_MediaType constraint'

	ALTER TABLE Media
		ADD CONSTRAINT FK_MediaType
		FOREIGN KEY(MediaTypeID)
		REFERENCES MediaType(MediaTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_MediaType constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_MCPV_MediaID'
)
BEGIN
	PRINT 'Create FK_MCPV_MediaID constraint'

	ALTER TABLE MediaContainsPackageVersion
		ADD CONSTRAINT FK_MCPV_MediaID
		FOREIGN KEY(MediaID)
		REFERENCES Media(MediaID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_MCPV_MediaID constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_MCPV_PackageVersion'
)
BEGIN
	PRINT 'Create FK_MCPV_PackageVersion constraint'

	ALTER TABLE MediaContainsPackageVersion
		ADD CONSTRAINT FK_MCPV_PackageVersion
		FOREIGN KEY(PackageVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_MCPV_PackageVersion constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_MCPP_Media'
)
BEGIN
	PRINT 'Create FK_MCPP_Media constraint'

	ALTER TABLE MediaContainsPackagePath
		ADD CONSTRAINT FK_MCPP_Media
		FOREIGN KEY(MediaID)
		REFERENCES Media(MediaID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_MCPP_Media constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_MCPP_PackagePath'
)
BEGIN
	PRINT 'Create FK_MCPP_PackagePath constraint'

	ALTER TABLE MediaContainsPackagePath
		ADD CONSTRAINT FK_MCPP_PackagePath
		FOREIGN KEY(PackagePathID)
		REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_MCPP_PackagePath constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Installation_Computer'
)
BEGIN
	PRINT 'Create FK_Installation_Computer constraint'

	ALTER TABLE Installation
		ADD CONSTRAINT FK_Installation_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Installation_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Installation_Organization'
)
BEGIN
	PRINT 'Create FK_Installation_Organization constraint'

	ALTER TABLE Installation
		ADD CONSTRAINT FK_Installation_Organization
		FOREIGN KEY(OrganizationID)
		REFERENCES Organization(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Installation_Organization constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Installation_PackageVersion'
)
BEGIN
	PRINT 'Create FK_Installation_PackageVersion constraint'

	ALTER TABLE Installation
		ADD CONSTRAINT FK_Installation_PackageVersion
		FOREIGN KEY(PackageVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Installation_PackageVersion constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Installation_User'
)
BEGIN
	PRINT 'Create FK_Installation_User constraint'

	ALTER TABLE Installation
		ADD CONSTRAINT FK_Installation_User
		FOREIGN KEY(UserID)
		REFERENCES [User](UserID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Installation_User constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_InstallationHistory_Computer'
)
BEGIN
	PRINT 'Create FK_InstallationHistory_Computer constraint'

	ALTER TABLE InstallationHistory
		ADD CONSTRAINT FK_InstallationHistory_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_InstallationHistory_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_InstallationHistory_PackageVersion'
)
BEGIN
	PRINT 'Create FK_InstallationHistory_PackageVersion constraint'

	ALTER TABLE InstallationHistory
		ADD CONSTRAINT FK_InstallationHistory_PackageVersion
		FOREIGN KEY(PackageVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_InstallationHistory_PackageVersion constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_InstallationHistory_User'
)
BEGIN
	PRINT 'Create FK_InstallationHistory_User constraint'

	ALTER TABLE InstallationHistory
		ADD CONSTRAINT FK_InstallationHistory_User
		FOREIGN KEY(UserID)
		REFERENCES [User](UserID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_InstallationHistory_User constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_InventoryReport_Computer'
)
BEGIN
	PRINT 'Create FK_InventoryReport_Computer constraint'

	ALTER TABLE InventoryReport
		ADD CONSTRAINT FK_InventoryReport_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_InventoryReport_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_InventoryReport_User'
)
BEGIN
	PRINT 'Create FK_InventoryReport_User constraint'

	ALTER TABLE InventoryReport
		ADD CONSTRAINT FK_InventoryReport_User
		FOREIGN KEY(UserID)
		REFERENCES [User](UserID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_InventoryReport_User constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareOccurrence_Computer'
)
BEGIN
	PRINT 'Create FK_SoftwareOccurrence_Computer constraint'

	ALTER TABLE SoftwareOccurrence
		ADD CONSTRAINT FK_SoftwareOccurrence_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareOccurrence_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareOccurrence_ComputerOU'
)
BEGIN
	PRINT 'Create FK_SoftwareOccurrence_ComputerOU constraint'

	ALTER TABLE SoftwareOccurrence
		ADD CONSTRAINT FK_SoftwareOccurrence_ComputerOU
		FOREIGN KEY(ComputerOUID)
		REFERENCES Organization(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_SoftwareOccurrence_ComputerOU constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareVersion_Package'
)
BEGIN
	PRINT 'Create FK_SoftwareVersion_Package constraint'

	ALTER TABLE SoftwareOccurrence
		ADD CONSTRAINT FK_SoftwareVersion_Package
		FOREIGN KEY(PackagePathID)
		REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareVersion_Package constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareOccurrence_Software'
)
BEGIN
	PRINT 'Create FK_SoftwareOccurrence_Software constraint'

	ALTER TABLE SoftwareOccurrence
		ADD CONSTRAINT FK_SoftwareOccurrence_Software
		FOREIGN KEY(SoftwareID)
		REFERENCES SoftwareVersion(SoftwareID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareOccurrence_Software constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareOccurrence_User'
)
BEGIN
	PRINT 'Create FK_SoftwareOccurrence_User constraint'

	ALTER TABLE SoftwareOccurrence
		ADD CONSTRAINT FK_SoftwareOccurrence_User
		FOREIGN KEY(UserID)
		REFERENCES [User](UserID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareOccurrence_User constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareOccurrence_UserOU'
)
BEGIN
	PRINT 'Create FK_SoftwareOccurrence_UserOU constraint'

	ALTER TABLE SoftwareOccurrence
		ADD CONSTRAINT FK_SoftwareOccurrence_UserOU
		FOREIGN KEY(UserOUID)
		REFERENCES Organization(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_SoftwareOccurrence_UserOU constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareFile_Computer'
)
BEGIN
	PRINT 'Create FK_SoftwareFile_Computer constraint'

	ALTER TABLE SoftwareFile
		ADD CONSTRAINT FK_SoftwareFile_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareFile_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareFile_ComputerOU'
)
BEGIN
	PRINT 'Create FK_SoftwareFile_ComputerOU constraint'

	ALTER TABLE SoftwareFile
		ADD CONSTRAINT FK_SoftwareFile_ComputerOU
		FOREIGN KEY(ComputerOUID)
		REFERENCES Organization(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_SoftwareFile_ComputerOU constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareFile_Software'
)
BEGIN
	PRINT 'Create FK_SoftwareFile_Software constraint'

	ALTER TABLE SoftwareFile
		ADD CONSTRAINT FK_SoftwareFile_Software
		FOREIGN KEY(SoftwareID)
		REFERENCES SoftwareVersion(SoftwareID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareFile_Software constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_UnknownFile_Computer'
)
BEGIN
	PRINT 'Create FK_UnknownFile_Computer constraint'

	ALTER TABLE UnknownFile
		ADD CONSTRAINT FK_UnknownFile_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_UnknownFile_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_HardwareClass_Superclass'
)
BEGIN
	PRINT 'Create FK_HardwareClass_Superclass constraint'

	ALTER TABLE HardwareClass
		ADD CONSTRAINT FK_HardwareClass_Superclass
		FOREIGN KEY(SuperClassID)
		REFERENCES HardwareClass(HardwareClassID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_HardwareClass_Superclass constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_HardwareObject_Computer'
)
BEGIN
	PRINT 'Create FK_HardwareObject_Computer constraint'

	ALTER TABLE HardwareObject
		ADD CONSTRAINT FK_HardwareObject_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_HardwareObject_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_HardwareObject_ComputerOU'
)
BEGIN
	PRINT 'Create FK_HardwareObject_ComputerOU constraint'

	ALTER TABLE HardwareObject
		ADD CONSTRAINT FK_HardwareObject_ComputerOU
		FOREIGN KEY(ComputerOUID)
		REFERENCES Organization(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_HardwareObject_ComputerOU constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_HardwareObject_Class'
)
BEGIN
	PRINT 'Create FK_HardwareObject_Class constraint'

	ALTER TABLE HardwareObject
		ADD CONSTRAINT FK_HardwareObject_Class
		FOREIGN KEY(HardwareClassID)
		REFERENCES HardwareClass(HardwareClassID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_HardwareObject_Class constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_HardwareValue_HardwareObject'
)
BEGIN
	PRINT 'Create FK_HardwareValue_HardwareObject constraint'

	ALTER TABLE HardwareValue
		ADD CONSTRAINT FK_HardwareValue_HardwareObject
		FOREIGN KEY(HardwareObjectID)
		REFERENCES HardwareObject(HardwareObjectID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_HardwareValue_HardwareObject constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_HardwareValue_HardwareProperty'
)
BEGIN
	PRINT 'Create FK_HardwareValue_HardwareProperty constraint'

	ALTER TABLE HardwareValue
		ADD CONSTRAINT FK_HardwareValue_HardwareProperty
		FOREIGN KEY(HardwarePropertyID)
		REFERENCES HardwareProperty(HardwarePropertyID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_HardwareValue_HardwareProperty constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_DistributedPackage_Confirmed'
)
BEGIN
	PRINT 'Create FK_DistributedPackage_Confirmed constraint'

	ALTER TABLE DistributedPackage
		ADD CONSTRAINT FK_DistributedPackage_Confirmed
		FOREIGN KEY(ConfirmedVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_DistributedPackage_Confirmed constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_DistributedPackage_Requested'
)
BEGIN
	PRINT 'Create FK_DistributedPackage_Requested constraint'

	ALTER TABLE DistributedPackage
		ADD CONSTRAINT FK_DistributedPackage_Requested
		FOREIGN KEY(RequestedVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_DistributedPackage_Requested constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_DistributedPackage_ServerUID'
)
BEGIN
	PRINT 'Create FK_DistributedPackage_ServerUID constraint'

	ALTER TABLE DistributedPackage
		ADD CONSTRAINT FK_DistributedPackage_ServerUID
		FOREIGN KEY(ServerUID)
		REFERENCES DistributionServer(ServerUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_DistributedPackage_ServerUID constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_DistributionGroupMember_GroupUID'
)
BEGIN
	PRINT 'Create FK_DistributionGroupMember_GroupUID constraint'

	ALTER TABLE DistributionGroupMember
		ADD CONSTRAINT FK_DistributionGroupMember_GroupUID
		FOREIGN KEY(GroupUID)
		REFERENCES DistributionGroup(GroupUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_DistributionGroupMember_GroupUID constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_DistributionGroupMember_TargetType'
)
BEGIN
	PRINT 'Create FK_DistributionGroupMember_TargetType constraint'

	ALTER TABLE DistributionGroupMember
		ADD CONSTRAINT FK_DistributionGroupMember_TargetType
		FOREIGN KEY(MemberType)
		REFERENCES DistributionServerType(DistributionServerTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_DistributionGroupMember_TargetType constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareProduct_Model'
)
BEGIN
	PRINT 'Create FK_SoftwareProduct_Model constraint'

	ALTER TABLE SoftwareProduct
		ADD CONSTRAINT FK_SoftwareProduct_Model
		FOREIGN KEY(ModelID)
		REFERENCES LicenseModel(ModelID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareProduct_Model constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareProduct_Publisher'
)
BEGIN
	PRINT 'Create FK_SoftwareProduct_Publisher constraint'

	ALTER TABLE SoftwareProduct
		ADD CONSTRAINT FK_SoftwareProduct_Publisher
		FOREIGN KEY(PublisherID)
		REFERENCES SoftwarePublisher(PublisherID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareProduct_Publisher constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareProduct_TrackedBy'
)
BEGIN
	PRINT 'Create FK_SoftwareProduct_TrackedBy constraint'

	ALTER TABLE SoftwareProduct
		ADD CONSTRAINT FK_SoftwareProduct_TrackedBy
		FOREIGN KEY(TrackedByID)
		REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_SoftwareProduct_TrackedBy constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_LicenseAllocation_OrganizationID'
)
BEGIN
	PRINT 'Create FK_LicenseAllocation_OrganizationID constraint'

	ALTER TABLE LicenseAllocation
		ADD CONSTRAINT FK_LicenseAllocation_OrganizationID
		FOREIGN KEY(OrganizationID)
		REFERENCES Organization(OrganizationID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_LicenseAllocation_OrganizationID constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_LicenseAllocation_SoftwareProductID'
)
BEGIN
	PRINT 'Create FK_LicenseAllocation_SoftwareProductID constraint'

	ALTER TABLE LicenseAllocation
		ADD CONSTRAINT FK_LicenseAllocation_SoftwareProductID
		FOREIGN KEY(SoftwareProductID)
		REFERENCES SoftwareProduct(SoftwareProductID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_LicenseAllocation_SoftwareProductID constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_LicensePurchase_SoftwareReseller'
)
BEGIN
	PRINT 'Create FK_LicensePurchase_SoftwareReseller constraint'

	ALTER TABLE LicensePurchase
		ADD CONSTRAINT FK_LicensePurchase_SoftwareReseller
		FOREIGN KEY(ResellerID)
		REFERENCES SoftwareReseller(ResellerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_LicensePurchase_SoftwareReseller constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_LicensePurchase_Organization'
)
BEGIN
	PRINT 'Create FK_LicensePurchase_Organization constraint'

	ALTER TABLE LicensePurchase
		ADD CONSTRAINT FK_LicensePurchase_Organization
		FOREIGN KEY(OrganizationID)
		REFERENCES Organization(OrganizationID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_LicensePurchase_Organization constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_LicensePurchase'
)
BEGIN
	PRINT 'Create FK_LicensePurchase constraint'

	ALTER TABLE LicensePurchase
		ADD CONSTRAINT FK_LicensePurchase
		FOREIGN KEY(SoftwareProductID)
		REFERENCES SoftwareProduct(SoftwareProductID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_LicensePurchase constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ProductContains_SoftwareProductID'
)
BEGIN
	PRINT 'Create FK_ProductContains_SoftwareProductID constraint'

	ALTER TABLE ProductContainsSoftware
		ADD CONSTRAINT FK_ProductContains_SoftwareProductID
		FOREIGN KEY(SoftwareProductID)
		REFERENCES SoftwareProduct(SoftwareProductID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_ProductContains_SoftwareProductID constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ProductContains_SoftwareVersionID'
)
BEGIN
	PRINT 'Create FK_ProductContains_SoftwareVersionID constraint'

	ALTER TABLE ProductContainsSoftware
		ADD CONSTRAINT FK_ProductContains_SoftwareVersionID
		FOREIGN KEY(SoftwareVersionID)
		REFERENCES SoftwareVersion(SoftwareID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_ProductContains_SoftwareVersionID constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Policy_Domain'
)
BEGIN
	PRINT 'Create FK_Policy_Domain constraint'

	ALTER TABLE Policy
		ADD CONSTRAINT FK_Policy_Domain
		FOREIGN KEY(DomainID)
		REFERENCES Domain(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Policy_Domain constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Policy_AccessGroup'
)
BEGIN
	PRINT 'Create FK_Policy_AccessGroup constraint'

	ALTER TABLE Policy
		ADD CONSTRAINT FK_Policy_AccessGroup
		FOREIGN KEY(AccessGroupID)
		REFERENCES [Group](GroupID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Policy_AccessGroup constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Allocation_AccessGroup'
)
BEGIN
	PRINT 'Create FK_Allocation_AccessGroup constraint'

	ALTER TABLE PackageAllocation
		ADD CONSTRAINT FK_Allocation_AccessGroup
		FOREIGN KEY(AccessGroupID)
		REFERENCES [Group](GroupID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Allocation_AccessGroup constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Allocation_Policy'
)
BEGIN
	PRINT 'Create FK_Allocation_Policy constraint'

	ALTER TABLE PackageAllocation
		ADD CONSTRAINT FK_Allocation_Policy
		FOREIGN KEY(PolicyGUID)
		REFERENCES Policy(GUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Allocation_Policy constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageAllocation_TargetType'
)
BEGIN
	PRINT 'Create FK_PackageAllocation_TargetType constraint'

	ALTER TABLE PackageAllocation
		ADD CONSTRAINT FK_PackageAllocation_TargetType
		FOREIGN KEY(TargetTypeID)
		REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_PackageAllocation_TargetType constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Allocation_PackagePath'
)
BEGIN
	PRINT 'Create FK_Allocation_PackagePath constraint'

	ALTER TABLE PackageAllocation
		ADD CONSTRAINT FK_Allocation_PackagePath
		FOREIGN KEY(PackagePathID)
		REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Allocation_PackagePath constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Allocation_Details'
)
BEGIN
	PRINT 'Create FK_Allocation_Details constraint'

	ALTER TABLE PackageAllocation
		ADD CONSTRAINT FK_Allocation_Details
		FOREIGN KEY(AllocationDetailsID)
		REFERENCES AllocationDetails(AllocationDetailsID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Allocation_Details constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PolicyApplies_Domain'
)
BEGIN
	PRINT 'Create FK_PolicyApplies_Domain constraint'

	ALTER TABLE PolicyApplies
		ADD CONSTRAINT FK_PolicyApplies_Domain
		FOREIGN KEY(DomainID)
		REFERENCES Domain(OrganizationID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_PolicyApplies_Domain constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PolicyApplies_TargetType'
)
BEGIN
	PRINT 'Create FK_PolicyApplies_TargetType constraint'

	ALTER TABLE PolicyApplies
		ADD CONSTRAINT FK_PolicyApplies_TargetType
		FOREIGN KEY(TargetTypeID)
		REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_PolicyApplies_TargetType constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PolicyApplies_Policy'
)
BEGIN
	PRINT 'Create FK_PolicyApplies_Policy constraint'

	ALTER TABLE PolicyApplies
		ADD CONSTRAINT FK_PolicyApplies_Policy
		FOREIGN KEY(GUID)
		REFERENCES Policy(GUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_PolicyApplies_Policy constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_TargetedPackage_PackagePath'
)
BEGIN
	PRINT 'Create FK_TargetedPackage_PackagePath constraint'

	ALTER TABLE TargetedPackage
		ADD CONSTRAINT FK_TargetedPackage_PackagePath
		FOREIGN KEY(PackagePathID)
		REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_TargetedPackage_PackagePath constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_TargetedPackage_Policy'
)
BEGIN
	PRINT 'Create FK_TargetedPackage_Policy constraint'

	ALTER TABLE TargetedPackage
		ADD CONSTRAINT FK_TargetedPackage_Policy
		FOREIGN KEY(PolicyGUID)
		REFERENCES Policy(GUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_TargetedPackage_Policy constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_TargetedPackage_AllocationDetails'
)
BEGIN
	PRINT 'Create FK_TargetedPackage_AllocationDetails constraint'

	ALTER TABLE TargetedPackage
		ADD CONSTRAINT FK_TargetedPackage_AllocationDetails
		FOREIGN KEY(AllocationDetailsID)
		REFERENCES AllocationDetails(AllocationDetailsID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_TargetedPackage_AllocationDetails constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Target_OU'
)
BEGIN
	PRINT 'Create FK_Target_OU constraint'

	ALTER TABLE TargetedPackage
		ADD CONSTRAINT FK_Target_OU
		FOREIGN KEY(TargetOUID)
		REFERENCES Organization(OrganizationID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Target_OU constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_TargetedPackage_TargetType'
)
BEGIN
	PRINT 'Create FK_TargetedPackage_TargetType constraint'

	ALTER TABLE TargetedPackage
		ADD CONSTRAINT FK_TargetedPackage_TargetType
		FOREIGN KEY(TargetTypeID)
		REFERENCES TargetType(TargetTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_TargetedPackage_TargetType constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_NetworkDevice_OperatingSystem'
)
BEGIN
	PRINT 'Create FK_NetworkDevice_OperatingSystem constraint'

	ALTER TABLE NetworkDevice
		ADD CONSTRAINT FK_NetworkDevice_OperatingSystem
		FOREIGN KEY(OperatingSystemID)
		REFERENCES OperatingSystem(OperatingSystemID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_NetworkDevice_OperatingSystem constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_NetworkDeviceDiscoveredBy_Account'
)
BEGIN
	PRINT 'Create FK_NetworkDeviceDiscoveredBy_Account constraint'

	ALTER TABLE NetworkDeviceDiscoveredBy
		ADD CONSTRAINT FK_NetworkDeviceDiscoveredBy_Account
		FOREIGN KEY(AccountID)
		REFERENCES AdministrationAccount(AccountID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_NetworkDeviceDiscoveredBy_Account constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_NetworkDeviceDiscoveredBy_AccountOverride'
)
BEGIN
	PRINT 'Create FK_NetworkDeviceDiscoveredBy_AccountOverride constraint'

	ALTER TABLE NetworkDeviceDiscoveredBy
		ADD CONSTRAINT FK_NetworkDeviceDiscoveredBy_AccountOverride
		FOREIGN KEY(AccountIDOverride)
		REFERENCES AdministrationAccount(AccountID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_NetworkDeviceDiscoveredBy_AccountOverride constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_NetworkDeviceDiscoveredBy_Device'
)
BEGIN
	PRINT 'Create FK_NetworkDeviceDiscoveredBy_Device constraint'

	ALTER TABLE NetworkDeviceDiscoveredBy
		ADD CONSTRAINT FK_NetworkDeviceDiscoveredBy_Device
		FOREIGN KEY(DeviceID)
		REFERENCES NetworkDevice(DeviceID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_NetworkDeviceDiscoveredBy_Device constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_NetworkDeviceDiscoveredBy_Server'
)
BEGIN
	PRINT 'Create FK_NetworkDeviceDiscoveredBy_Server constraint'

	ALTER TABLE NetworkDeviceDiscoveredBy
		ADD CONSTRAINT FK_NetworkDeviceDiscoveredBy_Server
		FOREIGN KEY(ServerUID)
		REFERENCES DistributionServer(ServerUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_NetworkDeviceDiscoveredBy_Server constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Task_PackagePath'
)
BEGIN
	PRINT 'Create FK_Task_PackagePath constraint'

	ALTER TABLE Task
		ADD CONSTRAINT FK_Task_PackagePath
		FOREIGN KEY(PackagePathID)
		REFERENCES PackagePath(PackagePathID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Task_PackagePath constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Task_TaskType'
)
BEGIN
	PRINT 'Create FK_Task_TaskType constraint'

	ALTER TABLE Task
		ADD CONSTRAINT FK_Task_TaskType
		FOREIGN KEY(TaskTypeID)
		REFERENCES TaskType(TaskTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Task_TaskType constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Job_Server'
)
BEGIN
	PRINT 'Create FK_Job_Server constraint'

	ALTER TABLE Job
		ADD CONSTRAINT FK_Job_Server
		FOREIGN KEY(ServerUID)
		REFERENCES DistributionServer(ServerUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Job_Server constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Job_Task'
)
BEGIN
	PRINT 'Create FK_Job_Task constraint'

	ALTER TABLE Job
		ADD CONSTRAINT FK_Job_Task
		FOREIGN KEY(TaskID)
		REFERENCES Task(TaskID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Job_Task constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Action_ActionState'
)
BEGIN
	PRINT 'Create FK_Action_ActionState constraint'

	ALTER TABLE Action
		ADD CONSTRAINT FK_Action_ActionState
		FOREIGN KEY(ActionStateID)
		REFERENCES ActionState(ActionStateID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Action_ActionState constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Action_Job'
)
BEGIN
	PRINT 'Create FK_Action_Job constraint'

	ALTER TABLE Action
		ADD CONSTRAINT FK_Action_Job
		FOREIGN KEY(JobUID)
		REFERENCES Job(JobUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Action_Job constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Action_Server'
)
BEGIN
	PRINT 'Create FK_Action_Server constraint'

	ALTER TABLE Action
		ADD CONSTRAINT FK_Action_Server
		FOREIGN KEY(ServerUID)
		REFERENCES DistributionServer(ServerUID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Action_Server constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Action_Task'
)
BEGIN
	PRINT 'Create FK_Action_Task constraint'

	ALTER TABLE Action
		ADD CONSTRAINT FK_Action_Task
		FOREIGN KEY(TaskID)
		REFERENCES Task(TaskID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Action_Task constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ActionApplies_Action'
)
BEGIN
	PRINT 'Create FK_ActionApplies_Action constraint'

	ALTER TABLE ActionApplies
		ADD CONSTRAINT FK_ActionApplies_Action
		FOREIGN KEY(ActionUID)
		REFERENCES Action(ActionUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_ActionApplies_Action constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ActionApplies_ActionState'
)
BEGIN
	PRINT 'Create FK_ActionApplies_ActionState constraint'

	ALTER TABLE ActionApplies
		ADD CONSTRAINT FK_ActionApplies_ActionState
		FOREIGN KEY(ActionStateID)
		REFERENCES ActionState(ActionStateID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_ActionApplies_ActionState constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ActionApplies_Computer'
)
BEGIN
	PRINT 'Create FK_ActionApplies_Computer constraint'

	ALTER TABLE ActionApplies
		ADD CONSTRAINT FK_ActionApplies_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_ActionApplies_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ActionApplies_DeviceID'
)
BEGIN
	PRINT 'Create FK_ActionApplies_DeviceID constraint'

	ALTER TABLE ActionApplies
		ADD CONSTRAINT FK_ActionApplies_DeviceID
		FOREIGN KEY(DeviceID)
		REFERENCES NetworkDevice(DeviceID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_ActionApplies_DeviceID constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareUsagePerWeek_Computer'
)
BEGIN
	PRINT 'Create FK_SoftwareUsagePerWeek_Computer constraint'

	ALTER TABLE SoftwareUsagePerWeek
		ADD CONSTRAINT FK_SoftwareUsagePerWeek_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareUsagePerWeek_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareUsagePerWeek_Software'
)
BEGIN
	PRINT 'Create FK_SoftwareUsagePerWeek_Software constraint'

	ALTER TABLE SoftwareUsagePerWeek
		ADD CONSTRAINT FK_SoftwareUsagePerWeek_Software
		FOREIGN KEY(SoftwareID)
		REFERENCES SoftwareVersion(SoftwareID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareUsagePerWeek_Software constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_SoftwareUsagePerWeek_User'
)
BEGIN
	PRINT 'Create FK_SoftwareUsagePerWeek_User constraint'

	ALTER TABLE SoftwareUsagePerWeek
		ADD CONSTRAINT FK_SoftwareUsagePerWeek_User
		FOREIGN KEY(UserID)
		REFERENCES [User](UserID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_SoftwareUsagePerWeek_User constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ComputerUsage_Computer'
)
BEGIN
	PRINT 'Create FK_ComputerUsage_Computer constraint'

	ALTER TABLE ComputerUsage
		ADD CONSTRAINT FK_ComputerUsage_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_ComputerUsage_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_ComputerUsage_User'
)
BEGIN
	PRINT 'Create FK_ComputerUsage_User constraint'

	ALTER TABLE ComputerUsage
		ADD CONSTRAINT FK_ComputerUsage_User
		FOREIGN KEY(UserID)
		REFERENCES [User](UserID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_ComputerUsage_User constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_WOLDistJob_Server'
)
BEGIN
	PRINT 'Create FK_WOLDistJob_Server constraint'

	ALTER TABLE WakeOnLANDistributionJob
		ADD CONSTRAINT FK_WOLDistJob_Server
		FOREIGN KEY(ServerUID)
		REFERENCES DistributionServer(ServerUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_WOLDistJob_Server constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_WOLDistJob_Task'
)
BEGIN
	PRINT 'Create FK_WOLDistJob_Task constraint'

	ALTER TABLE WakeOnLANDistributionJob
		ADD CONSTRAINT FK_WOLDistJob_Task
		FOREIGN KEY(TaskUID)
		REFERENCES WakeOnLANTask(TaskUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_WOLDistJob_Task constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_WOLStatus_Computer'
)
BEGIN
	PRINT 'Create FK_WOLStatus_Computer constraint'

	ALTER TABLE WakeOnLANStatus
		ADD CONSTRAINT FK_WOLStatus_Computer
		FOREIGN KEY(ComputerID)
		REFERENCES Computer(ComputerID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_WOLStatus_Computer constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_WOLStatus_DistJob'
)
BEGIN
	PRINT 'Create FK_WOLStatus_DistJob constraint'

	ALTER TABLE WakeOnLANStatus
		ADD CONSTRAINT FK_WOLStatus_DistJob
		FOREIGN KEY(DistJobUID)
		REFERENCES WakeOnLANDistributionJob(DistJobUID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_WOLStatus_DistJob constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_FileNameMap_Media'
)
BEGIN
	PRINT 'Create FK_FileNameMap_Media constraint'

	ALTER TABLE FileNameMap
		ADD CONSTRAINT FK_FileNameMap_Media
		FOREIGN KEY(MediaID)
		REFERENCES Media(MediaID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_FileNameMap_Media constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Locale_Country'
)
BEGIN
	PRINT 'Create FK_Locale_Country constraint'

	ALTER TABLE Locale
		ADD CONSTRAINT FK_Locale_Country
		FOREIGN KEY(CountryCode)
		REFERENCES Country(CountryCode) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Locale_Country constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Locale_Language'
)
BEGIN
	PRINT 'Create FK_Locale_Language constraint'

	ALTER TABLE Locale
		ADD CONSTRAINT FK_Locale_Language
		FOREIGN KEY(LangCode3)
		REFERENCES Language(LangCode3) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Locale_Language constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageVersionLocale_PackageVersion'
)
BEGIN
	PRINT 'Create FK_PackageVersionLocale_PackageVersion constraint'

	ALTER TABLE PackageVersionLocale
		ADD CONSTRAINT FK_PackageVersionLocale_PackageVersion
		FOREIGN KEY(PackageVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_PackageVersionLocale_PackageVersion constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageVersionLocale_Locale'
)
BEGIN
	PRINT 'Create FK_PackageVersionLocale_Locale constraint'

	ALTER TABLE PackageVersionLocale
		ADD CONSTRAINT FK_PackageVersionLocale_Locale
		FOREIGN KEY(LocaleCode)
		REFERENCES Locale(LocaleCode) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_PackageVersionLocale_Locale constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageVersionArchitecture_PackageVersion'
)
BEGIN
	PRINT 'Create FK_PackageVersionArchitecture_PackageVersion constraint'

	ALTER TABLE PackageVersionArchitecture
		ADD CONSTRAINT FK_PackageVersionArchitecture_PackageVersion
		FOREIGN KEY(PackageVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_PackageVersionArchitecture_PackageVersion constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageVersionArchitecture_Architecture'
)
BEGIN
	PRINT 'Create FK_PackageVersionArchitecture_Architecture constraint'

	ALTER TABLE PackageVersionArchitecture
		ADD CONSTRAINT FK_PackageVersionArchitecture_Architecture
		FOREIGN KEY(ArchitectureID)
		REFERENCES Architecture(ArchitectureID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_PackageVersionArchitecture_Architecture constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_PackageVersionEnvironment_PackageVersion'
)
BEGIN
	PRINT 'Create FK_PackageVersionEnvironment_PackageVersion constraint'

	ALTER TABLE PackageVersionEnvironment
		ADD CONSTRAINT FK_PackageVersionEnvironment_PackageVersion
		FOREIGN KEY(PackageVersionID)
		REFERENCES PackageVersion(PackageVersionID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_PackageVersionEnvironment_PackageVersion constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Resource_PartitionType'
)
BEGIN
	PRINT 'Create FK_Resource_PartitionType constraint'

	ALTER TABLE Resource
		ADD CONSTRAINT FK_Resource_PartitionType
		FOREIGN KEY(PartitionTypeID)
		REFERENCES PartitionType(PartitionTypeID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Resource_PartitionType constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Right_Resource'
)
BEGIN
	PRINT 'Create FK_Right_Resource constraint'

	ALTER TABLE [Right]
		ADD CONSTRAINT FK_Right_Resource
		FOREIGN KEY(ResourceID)
		REFERENCES Resource(ResourceID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Right_Resource constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Right_ActionClass'
)
BEGIN
	PRINT 'Create FK_Right_ActionClass constraint'

	ALTER TABLE [Right]
		ADD CONSTRAINT FK_Right_ActionClass
		FOREIGN KEY(ActionClassID)
		REFERENCES ActionClass(ActionClassID) ON DELETE NO ACTION
END
ELSE
	PRINT 'Using existing FK_Right_ActionClass constraint'
GO
IF NOT EXISTS (
	SELECT *
	FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
	WHERE CONSTRAINT_NAME = 'FK_Right_Group'
)
BEGIN
	PRINT 'Create FK_Right_Group constraint'

	ALTER TABLE [Right]
		ADD CONSTRAINT FK_Right_Group
		FOREIGN KEY(GroupID)
		REFERENCES [Group](GroupID) ON DELETE CASCADE
END
ELSE
	PRINT 'Using existing FK_Right_Group constraint'
GO

--===========================================================================
--		Update the DatabaseConfiguration table
--
--		NOTE: do this last
--===========================================================================
PRINT 'Recording successful migration'
UPDATE DatabaseConfiguration SET Value = '7.5'
	WHERE Property = 'SchemaVersion'
GO

