PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > mysql.general > relational tables
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
relational tables

Réponse
 
LinkBack Outils de la discussion
Vieux 20/03/2008, 02h29   #1
John Taylor-Johnston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut relational tables

I want to make a relational link from `data` to `shopping` so when I
insert a new record in `shopping`, I will see the contents of
`data`.`name` and `data`.`email` as drop-down menus in `shopping`.

This is InnoDB so I should be able to do this by SQL, right?

Thanks,
John


DROP TABLE IF EXISTS `data`;
CREATE TABLE `data` (
`id` int(5) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

INSERT INTO `data` VALUES(1, 'Allen, Carolyn', 'nobody@jtjohnston.net');
INSERT INTO `data` VALUES(2, 'Atwood, Margaret', 'someone@jtjohnston.net');

DROP TABLE IF EXISTS `shopping`;
CREATE TABLE `shopping` (
`id` int(5) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
`address` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

  Réponse avec citation
Vieux 20/03/2008, 07h59   #2
Sebastian Mendel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: relational tables

John Taylor-Johnston schrieb:
> I want to make a relational link from `data` to `shopping` so when I
> insert a new record in `shopping`, I will see the contents of
> `data`.`name` and `data`.`email` as drop-down menus in `shopping`.
>
> This is InnoDB so I should be able to do this by SQL, right?


where do you want to see this drop-downs?

MySQL is an database server, and i m not aware of any place where it would
display any drop-downs, possible you speak of some sort of GUI, like MySQL
Admin or phpMyAdmin?


--
Sebastian
  Réponse avec citation
Vieux 20/03/2008, 21h16   #3
John Taylor-Johnston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: relational tables

Sorry, I'm a top quoter. This is what I want to do. I'm still told there
re problems with my keys.

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;


Sebastian Mendel wrote:
>>
>> This is InnoDB so I should be able to do this by SQL, right?

>
> where do you want to see this drop-downs?

  Réponse avec citation
Vieux 20/03/2008, 21h51   #4
Jason Trebilcock
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut RE: relational tables

See below...

> -----Original Message-----
> From: John Taylor-Johnston [mailto:John.Taylor-
> Johnston@cegepsherbrooke.qc.ca]
> Sent: Thursday, March 20, 2008 2:17 PM
> To: Sebastian Mendel; mysql@lists.mysql.com
> Subject: Re: relational tables
>
> 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;
>


Something doesn't smell right with the 'shopping' table. Rather than using
what appears to be the same values (aside from the person_id as opposed to
the shopping_id), wouldn't it be more effective to have the shopping table
have 'shopping_id' and 'person_id' fields? With that approach, you could
get away from having the same data in two tables. But, you'll have to start
building ways to look up the person_id based on name and email values in
order to populate the shopping table.

Took a flyer at an updated version of the above (not knowing what your
intent is):

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 ( null, 'Name',
'email@address.com' ) ; -- updated the key value to allow for being
created automatically
INSERT INTO `person`( `person_id`, `name`, `email` ) VALUES ( null,
'second Name', 'email@anotheraddress.com' ) ; -- same thing here

DROP TABLE IF EXISTS `shopping`;
CREATE TABLE IF NOT EXISTS `shopping` (
`shopping_id` int(11) NOT NULL,
`person_id` int(11) NOT NULL,
PRIMARY KEY (`shopping_id`),
FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

The syntax of the above might not be 100%...but it looks to be close enough.

No virus found in this outgoing message.
Checked by AVG.
Version: 7.5.519 / Virus Database: 269.21.7/1335 - Release Date: 3/19/2008
9:54 AM


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h31.


Édité par : vBulletin® version 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10648 seconds with 12 queries