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


reason for this???


reason for this???

Author
Message
Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2219 Visits: 1119
I've recently run across this structure in a database that I'm migrating.

create table fktest (
dept int not null,
dps int not null,
constraint [pkfktest] primary key clustered (dept,dps),
constraint [fktester] foreign key (dept, dps) references fktest (dept,dps)
) on [Primary]



Is there a logical reason to have a foreign key that references itself when it's also the primary key in a table? Is this used for some reason? It just doesn't make sense to me.

Thoughts?
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97101 Visits: 38988
The structure is only part of the picture, you need to look at the data as well.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2219 Visits: 1119
Ok, Lynn, let me go pull some test data and post it. I can't, for the life of me, think of any reason why someone would build this structure.
Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2219 Visits: 1119
I just don't see the practical application of this foreign key. What benefit does it provide? I'll add that this is a user created database and the user is not around to answer this question any more.

Slight modification to table structure:

create table fktest (
dept varchar(10) not null,
dps varchar(10) not null,
constraint [pkfktest] primary key clustered (dept,dps),
constraint [fktester] foreign key (dept, dps) references fktest (dept,dps)
) on [Primary]

insert into fktest
VALUES('FAN', '3553'),
('FAN', '3554'),
('HAMS', '00001'),
('HAMS', '0001'),
('HAMS', '0002'),
('HAMS', '0003'),
('HAMS', '1304'),
('HAMS', '1336'),
('HAMS', '1337'),
('HAMS', '1350'),
('HAMS', '1355'),
('HAMS', '1362'),
('HAMS', '1363'),
('HAMS', '3104'),
('HAMS', '3106'),
('HAMS', '3108'),
('HAMS', '3110'),
('HAMS', '3540'),
('HAMS', '3541'),
('HAMS', '3542'),
('HAMS', '3543'),
('HAMS', '3544'),
('HAMS', '3545'),
('HAMS', '3555'),
('HAMS', '6000'),
('HAMS', '6001'),
('HAMS', '6002'),
('HAMS', '6005'),
('HAMS', '6006'),
('HAMS', '6007'),
('HAMS', '6008'),
('HAMS', '6009'),
('HAMS', '6010'),
('HAMS', '6011'),
('HAMS', '6012'),
('HAMS', '6013'),
('HAMS', '6014'),
('HAMS', '6015'),
('HAMS', '6016'),
('HAMS', '6017'),
('HAMS', '6018'),
('HAMS', '6019'),
('HAMS', '6020'),
('HAMS', '6022'),
('HAMS', '6023'),
('HAMS', '6024'),
('HAMS', '6025'),
('HAMS', '6028'),
('HAMS', '6029'),
('HAMS', '6030')

SELECT * FROM fktest
drop table fktest


Michael Valentine Jones
Michael Valentine Jones
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14883 Visits: 11848
If that is the actual table structure, there seems to be no reason for the foreign key reference, since it is the primary key referencing itself.

At least you won't get any FK constraint violations. :-)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97101 Visits: 38988
Maybe the user just wanted to see if it could be done? Other than that, based on the structure and the data I see no reason for th FK.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Erin Ramsay
Erin Ramsay
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2219 Visits: 1119
Excellent.. thank you folks.. I thought there must be some sort of fundamental flaw in my understanding.. lol
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