Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referential integrity in a SQL DataBase

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA

I am using Delphi 6 Enterprise and Interbase6.

Is there a SQL guru out there who can please tell me how to create referential ingerity
between table PARENT, CHILD, GCHILD and GGCHILD in a SQL DataBase called PChild.GDB?

What has to be born in mind is the following.

In table PARENT field P_NO has to be Unique - as there can only be ONE "Father 1" in the
table. (i.e. P_NO can only have the value "1" ONCE.)

But in table CHILD field C_NO can NOT be unique because there will be a "child 1" for each
father. Same applying to field GC_NO in table GCHild (GrandChild). And GC_NO in table
GGChild (GreatGrandChild).

Table GCHILD needs field P_NO to relate it to P_NO in table PARENT (being it's father)
whilst also needing a field C_NO to relate it to C_NO in table CHILD (being IT'S father)
along with GC_NO.

Equally table GGCHILD should have similar referential integrity to the above through
fields P_NO, C_NO and GC_NO.

Whilst one can create referential integrity between table CHILD and PARENT as follows ....

"ALTER TABLE CHILD ADD FOREIGN KEY (P_NO) REFERENCES PARENTS (P_NO);"

.... what does one require to create the same effect bewteen the other tables? Bearing in mind
that the following won't work whilst C_NO (and G_NO) are not unique ..

ALTER TABLE GCHILD ADD FOREIGN KEY (C_NO) REFERENCES CHILD (C_NO);
ALTER TABLE GGCHILD ADD FOREIGN KEY (GC_NO) REFERENCES GCHILD (GC_NO);

CONSTRAINT CHILD_PARENT_PK PRIMARY KEY (P_NO),
CONSTRAINT GCHILD_CHILD_PK PRIMARY KEY (C_NO),
CONSTRAINT GGCHILD_GCHILD_PK PRIMARY KEY (GC_NO),

Thanks in advance.

Terry.


 
Let's start from another angle, right?

Let's drop the whole "how to create a FK" thingy.

Start with identifying your entities first:

I saw the following:
1) we have a parent
2) we have a child
3) we have a grandchild
4) we have a great grand child

Now start by aswering the following questions:

1) what defines a parent? What are it's attributes?
2) a child can have a child -> that makes it a parent. Is there a 'child' entity or is this 'being a child of someone' an attribute?


Hope this helps, Martijn Tonies
InterBase Workbench - the developer tool for InterBase and Firebird
 
It is all so simple really. :)

When I look at a parent I want to see all IT's children, grand-children and great grand-children.

And we don't want the 1st child of (say) parent 3 to be numbered 5 because parents 1 and 2 already have 2 children each on the DataBase.

One cannot use normal Delphi Objects which conveniently allow one to hook up to MasterFields etc.. Because it is too limiting in that one cannot have beyond Master/Detail/Detail. And doing it with a SQLQuery isn't that simply either.

I have developed a unique method of having Master/Detail/Detail/Detail/Detail ...etc. etc... as far as you want to go?

I have made a working model available as per my thread102-272955. I am sending you the ReadMe file on it by seperate e-mail.

Thanks for your interest

Terry
 
Hi,

If you want start numbering your children at '1' constantly, that you might want to consider a compound primary key at PARENTID, CHILDID.

On the other hand, if the key is artificial anyway, it doesn't matter that a first child is numbered '5'.

Just my 0.02 Martijn Tonies
InterBase Workbench - the developer tool for InterBase and Firebird
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top