Go Back   SitePoint Forums > Forum Index > Program Your Site > Databases > MySQL
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Nov 21, 2009, 12:32   #1
r937
SQL Consultant
silver trophybronze trophy
SitePoint Award Recipient
 
r937's Avatar
 
Join Date: Jul 2002
Location: Toronto, Canada
Posts: 31,026
here is my suggestion:
Code:
CREATE TABLE restaurants
( id  INTEGER NOT NULL PRIMARY KEY
, name VARCHAR(255) NOT NULL
, image VARCHAR(100)
);
CREATE TABLE foodtypes
( id  INTEGER NOT NULL PRIMARY KEY
, foodtype VARCHAR(37) NOT NULL
);
CREATE TABLE restaurant_foodtypes
( restaurants_id INTEGER NOT NULL
, foodtypes_id INTEGER NOT NULL 
, FOREIGN KEY ( restaurants_id ) REFERENCES restaurants ( id )
, FOREIGN KEY ( foodtypes_id ) REFERENCES foodtypes ( id )
, PRIMARY KEY ( restaurants_id, foodtypes_id )
, INDEX reversi ( foodtypes_id, restaurants_id )
);
i have changed several names in the interest of clarity

the restaurant_foodtypes table is called a many-to-many or association or relationship table
r937 is offline   Reply With Quote
Old Nov 22, 2009, 04:29   #2
co.ador
SitePoint Guru
 
co.ador's Avatar
 
Join Date: Apr 2009
Posts: 917
I am trying to import to mysql the format of the restaurant_foodtypes table into phpmyadmin in mysql 5.1 and it throw this error

Quote:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FOREIGN KEY ( foodtypes_id) REFERENCES foodtypes (id)
PRIMARY KEY (`restauran' at line 12
I don't know how to structure the syntax for FOREIGN KEY and REFERENCES in this mysql version. I tried to do it like you did but it didn't work.

The mysql dump of the table restaurant_foodtypes in the mysql version in my computer dumps it as below without the FOREING KEY and REFERENCES you have add to it.


- phpMyAdmin SQL Dump
-- version 3.1.3.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 22, 2009 at 06:07 AM
-- Server version: 5.1.33
-- PHP Version: 5.2.9-2

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `menu`
--

-- --------------------------------------------------------

--
-- Table structure for table `restaurants_foodtypes`
--

CREATE TABLE IF NOT EXISTS `restaurants_foodtypes` (
`restaurants_id` int(1) NOT NULL,
`foodtypes_id` int(1) NOT NULL,
PRIMARY KEY (`restaurants_id`,`foodtypes_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Dumping data for table `restaurants_foodtypes`
--
co.ador is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 16:42.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved