all groups > sql server programming > november 2003 >
You're in the

sql server programming

group:

Multiple Foreign Keys



Multiple Foreign Keys CT
11/19/2003 10:50:22 PM
sql server programming: I have a table with a primary key column of pageIDs. I want
some pages to be parents of other pages. So I have another
table with two columns, parentPageID and childPageID
(together forming a primary key). I wanted each of these
columns to be foreign keys of pageID. However, using SQL
Enterprise Manager, I can only make one of them a foreign
key. Is there a way around that?

Thanks,
Re: Multiple Foreign Keys anonymous NO[at]SPAM discussions.microsoft.com
11/20/2003 1:55:06 AM
Cool, I'll try that. Thanks!

[quoted text, click to view]
Re: Multiple Foreign Keys David Portas
11/20/2003 7:41:28 AM
The adjacency list model of a hierarchy typically uses a single table like
this:

CREATE TABLE Sometable (pageid INTEGER PRIMARY KEY, parent_pageid INTEGER
NULL REFERENCES Sometable (pageid))

Entities at the root of the tree have NULL in the parent column.

You should be aware that things become difficult in an adjacency list if the
tree is of undefined depth. There isn't a set-based solution for extracting
a sub-tree from a given parent node, which is a common requirement. You can
traverse the subtree iteratively or you can consider alternative models such
as Nested Sets:

http://www.intelligententerprise.com/001020/celko1_1.shtml

--
David Portas
------------
Please reply only to the newsgroup
--

Re: Multiple Foreign Keys Jacco Schalkwijk
11/20/2003 10:53:01 AM
Flattened trees is a good model that makes working with adjacency lists
easier:

http://www.sqlmag.com/Articles/Index.cfm?ArticleID=8826


--
Jacco Schalkwijk
SQL Server MVP


[quoted text, click to view]

AddThis Social Bookmark Button