Re: Re: MySQL Group?

From: Date: Thu, 20 Mar 2008 03:16:37 +0000
Subject: Re: Re: MySQL Group?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-271872@lists.php.net to get a copy of this message
Thanks for getting me started. (Sorry, I'm a top quoter.) Below is some working code for the archives. What I've learned so far is that : 1) what I'm referring to in person has to be a key. 2) if I want to refer to more than one field from person in shopping, I have to use unique keys. I'm still discovering what keys are for. John DROP TABLE IF EXISTS person; CREATE TABLE person ( person_id int(11) NOT NULL auto_increment, name varchar(255) default NULL, email varchar(255) default NULL, PRIMARY KEY (person_id), KEY email (email), KEY name (name) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; INSERT INTO person( person_id, name, email ) VALUES ( 1, 'Name', 'email@address.com' ) ; INSERT INTO person( person_id, name, email ) VALUES ( 2, 'second Name', 'email@anotheraddress.com' ) ; DROP TABLE IF EXISTS shopping; CREATE TABLE IF NOT EXISTS shopping ( shopping_id int(11) NOT NULL, email varchar(255) default NULL, name varchar(255) default NULL, PRIMARY KEY (shopping_id), UNIQUE KEY email (email), UNIQUE KEY name (name), FOREIGN KEY (email) REFERENCES person (email), FOREIGN KEY (name) REFERENCES person (name) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
You end up with something like this: create table person ( person_id int primary key, name varchar(255), email varchar(255) ) engine=innodb; create table shopping ( shopping_id int primary key, person_id int, foreign key (person_id) references person(person_id) ) engine=innodb; insert into person(person_id,name,email) values(1,'Name','email@address.com'); insert into shopping(shopping_id, person_id) values(1, 1);


« previous php.general (#271872) next »