I have two tables. The first table is a list of database names and backup files. The second table is the relationship of said data bases, There're primary, attachment, common and formulary databases and they are used together as groups. Some primary databases may share other databases (attachment, common and formulary) with a working group.
Example databases 'sales' and 'marketing' are primary databases and they share attachment, common and formulary databases. Another primary database, 'distribution', shares common with the other two but has its own attachment and formulary. And there are other primary databases that have their own relations ships with the supporting databases.
Each column in DatabaseRelationships must exist in the database x ref table. But SQL seems to prevent this. How do I make this happen?
CREATE TABLE [dbo].[DatabaseNameToFileNameXref](
[DatabaseName] [nvarchar](128) NOT NULL,
[FileName] [nvarchar](2048) NOT NULL,
CONSTRAINT [PK_DatabaseNameToFileNameXref] PRIMARY KEY CLUSTERED
(
[DatabaseName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[DatabaseRelationships](
[PrimaryDatabaseName] [nvarchar](128) NOT NULL,
[AttachmentDatabaseName] [nvarchar](128) NOT NULL,
[CommonDatabaseName] [nvarchar](128) NOT NULL,
[FormularyDatabaseName] [nvarchar](128) NOT NULL,
CONSTRAINT [DatabaseRelationships_PK] PRIMARY KEY CLUSTERED
(
[PrimaryDatabaseName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
It has been suggested that this question is related to the "Polymorphic Association" problem. However, I do not believe this is the case. Each column is a DatabaseBase, the link back to the xref table doesn't determine the type. The type of database is determined by the DatabaseRelationship table: Primary, Attachment, etc.
The actual error message is related to the "On Update Cascade" for FK, which yeilds: '..may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION' I now see that I can only have one such FK back to the x-ref table; when I attempt to create a second FK with the same on-update setting, then I get the error. Otherwise, with out the on-update, it works.
So the actual question is, how do I add the cascade feature? Is there some other mechanism that I may use?
Using this short hand, DatabaseRelationship as DR and DatabaseNameToFileNameXref as X, the FK's are from DR.PrimaryDatabase to X.DatabaseName, and from DR.AttachmentDatabase to X.DatabaseName, and from DR.CommonDatabaseName to X.DatabaseName, and lastly DR.FormularyDatabaseName to X.DatabaseName. The X table provides the filename for restoring databases. The DR table describes how the databases are related to each other. If I change the name of a database, with out the cascade, I have to create a new entry in the X table for the new name, then update each row in the DR table that is affected and then delete the old row from the X table, which creates more opportunity for mistakes.