List:General Discussion« Previous MessageNext Message »
From:Peter Brawley Date:October 22 2002 1:47am
Subject:Re: how do you define a relationship?
View as plain text  
> Unfortunately this goes well beyond the scope of my original question.
> Although the inputs that you all have provided thus far have been a very
> good read, I still don't grasp the concept of defining a relationship.  Is
> it simply referring to a primary key in a field other than the one where
the
> key was defined?  Or is there some other way to define a key that I don't
> see?  All I'm looking for is a simple example, syntax excerpt, etc...that
> shows me (only the "slow learner" level) exactly how to define a
> relationship.

> Unfortunately this goes well beyond the scope of my original question.
> Although the inputs that you all have provided thus far have been a very
> good read, I still don't grasp the concept of defining a relationship.  Is
> it simply referring to a primary key in a field other than the one where
the
> key was defined?  Or is there some other way to define a key that I don't
> see?  All I'm looking for is a simple example, syntax excerpt, etc...that
> shows me (only the "slow learner" level) exactly how to define a
> relationship.

CREATE TABLE parent (
 parent_id INT UNSIGNED NOT NULL PRIMARY KEY
) TYPE=innodb;

CREATE TABLE child (
 child_id INT UNSIGNED NOT NULL PRIMARY KEY,
 parent_id INT UNSIGNED NOT NULL,
 INDEX ( parent_id ),
 FOREIGN KEY ( parent_id ) REFERENCES parent( parent_id )
)  TYPE=innodb;

CREATE TABLE grandchild  (
 grandchild_id INT UNSIGNED NOT NULL PRIMARY KEY,
 child_id INT UNSIGNED NOT NULL,
 INDEX ( child_id ),
 FOREIGN KEY ( child_id ) REFERENCES child( child_id )
) TYPE=innodb;

To see the relationships at work, try to insert into grandchild a child_id
that doesn't exist in child, or try to insert into child a parent_id that
doesn't exist in parent.

hth

PB

-----

filter fodder: mysql


Thread
how do you define a relationship?Randy Hammons20 Oct
  • Re: how do you define a relationship?Richard Clarke20 Oct
  • RE: how do you define a relationship?bwarehouse21 Oct
    • RE: how do you define a relationship?Arthur Fuller21 Oct
      • Re: how do you define a relationship?Michael T. Babcock21 Oct
RE: how do you define a relationship?Jan Steinman21 Oct
Re: how do you define a relationship?Michael T. Babcock21 Oct
RE: how do you define a relationship?Hammons Randy G SSgt 612 ACOMS/SCXX22 Oct
  • Re: how do you define a relationship?Peter Brawley22 Oct
  • RE: how do you define a relationship?Arthur Fuller23 Oct
    • re: RE: how do you define a relationship?Egor Egorov24 Oct
  • Re: how do you define a relationship?Michael T. Babcock8 Nov