SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Schema design for immutable FK data vs user created FK data


Schema design for immutable FK data vs user created FK data

Author
Message
cppprogrammer
cppprogrammer
SSC Journeyman
SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)

Group: General Forum Members
Points: 91 Visits: 130
Hi all,

Just after recommendations on the best way to design a foreign key table that, for example, defines a type, which contains immutable data in addition to types that are user defined.

E.g. without any sort of protection:

CREATE TABLE [dbo].[MyTestFK]
(
[MyTestFKID] [int] IDENTITY(1,1) NOT NULL,
[Type] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_MyTestFK] PRIMARY KEY CLUSTERED
(
[MyTestFKID] ASC
)
)
GO

CREATE TABLE [dbo].[MyTestData]
(
[MyTestDataID] [int] IDENTITY(1,1) NOT NULL,
[MyTestFKID] [int] NOT NULL,
CONSTRAINT [PK_MyTestData] PRIMARY KEY CLUSTERED
(
[MyTestDataID] ASC
)
)
GO
ALTER TABLE [dbo].[MyTestData]
ADD CONSTRAINT [FK_MyTestData_MyTestFK] FOREIGN KEY([MyTestFKID])
REFERENCES [dbo].[MyTestFK] ([MyTestFKID])
GO
INSERT INTO [dbo].[MyTestFK] ([Type])
VALUES (N'ImmutableType1')

INSERT INTO [dbo].[MyTestFK] ([Type])
VALUES (N'UserType1')
GO

Basically, one proposal (which I am not convinced is the way to go) is to structurally separate immutable and user types into separate tables. Is there a "best practices" for this scenario?

Thanks,
SC
andrew gothard
andrew gothard
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1087 Visits: 5791
If the only difference is that some rows may have certain data elements updated and others can't, I'd say they belong in the same table (there are bound to be exceptions, but I can't think of an example).
Say you have a business rule that says element X is one of those which can (or can't) be renamed? Then someone high up changes their mind on that If they're all in one table, you have a flag which identifies this. You change it. Job done.
Now, think about the pain you've got if you have two tables and you have to switch the data from one to the other, updating all dependencies to keep referential integrity.
That could get painful by comparison.
Basically, if you see a fk which references two tables at the same time ... how does that work? It doesn't, if it's in one table it can't be in the other, so all your inserts to the child table fail.

If anyone replies to this with "Well you could triggers .. ", please just shoot them and do us all a favour. They're just trying to do something stupid to allow themselves to do something stupid.
If they suggest dummies in each table. Shoot them twice.

hth

Edit for clarity on one point

I'm a DBA.
I'm not paid to solve problems. I'm paid to prevent them.
cppprogrammer
cppprogrammer
SSC Journeyman
SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)

Group: General Forum Members
Points: 91 Visits: 130
andrew gothard (2/12/2014)
Basically, if you see a fk which references two tables at the same time ... how does that work? It doesn't, if it's in one table it can't be in the other, so all your inserts to the child table fail.

Yes I agree there. I think they were looking at applying a constraint rather than a FK but my opinion is that you'd want a single FK in a relational database rather than some custom key constraint.

andrew gothard (2/12/2014)If anyone replies to this with "Well you could triggers .. ", please just shoot them and do us all a favour. They're just trying to do something stupid to allow themselves to do something stupid.

Yes agreed there too.

It hasn't been done yet so let's see how it pans out.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search