determine relationship between two tables?

Reply

Join Date: Nov 2008
Posts: 2
Reputation: olive08 is an unknown quantity at this point 
Solved Threads: 0
olive08 olive08 is offline Offline
Newbie Poster

determine relationship between two tables?

 
0
  #1
Nov 23rd, 2008
Hello everyone:

I am designing a database for a web application. The idea is user should able to
pick a restaurant by enter restaurant name, category(American, Chinese) etc

I currently have two tables: user table and restaurant table.
I have adminID as a primary key in user table.
I used adminID as a foreign key in restaurant table.

I thought the relationship between these two tables is one to many. For each adminID in user table there are many corresponding records in restaurant table, but each restaurant can have only one adminID in the user table.

However, i feel that the restaurant table should only hold information about the restaurant. It is not holding restaurant(s) picked by the user, I feel like there should be 3rd table to hold uid and res_id that connect user table and restaurant table.

Please help me to clarify the relationship between these two tables. Should I add 3rd table in this case? I appreciate your help!

create table command:
create table User(
username varchar(255) not null,
adminID int(11) not null auto_increment,
password varchar(255) not null,
fname varchar(31),
lname varchar(31),
email varchar(40),
primary key (adminID)
)ENGINE=INNODB;

#table restaurant
create table restaurant(
res_id int(11) not null,
uid int(11) not null,
index (uid),
name varchar(40),
address varchar(80),
city varchar(30),
state char(2),
zip varchar(15),
menu varchar(50),
category varchar(50),
primary key (res_id),
foreign key (uid) references adminUser (uid)
)ENGINE=INNODB;
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 306
Reputation: timothybard is an unknown quantity at this point 
Solved Threads: 26
timothybard's Avatar
timothybard timothybard is offline Offline
Posting Whiz

Re: determine relationship between two tables?

 
0
  #2
Nov 23rd, 2008
Yes, a third table would be appropriate in this situation. I would call the table something like "rating" and have attributes like this: ID(PK), UserID(FK), RestaurantID(FK), Rating, DateRated, etc...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: olive08 is an unknown quantity at this point 
Solved Threads: 0
olive08 olive08 is offline Offline
Newbie Poster

Re: determine relationship between two tables?

 
0
  #3
Nov 25th, 2008
Originally Posted by timothybard View Post
Yes, a third table would be appropriate in this situation. I would call the table something like "rating" and have attributes like this: ID(PK), UserID(FK), RestaurantID(FK), Rating, DateRated, etc...
Hello timothybard:
Thanks for the reply.
I am confused about what relationship shall I use.
please give me some suggestions. ThankdS

1. for a many-to-many relationship, i created a 3rd table: user_restaurants
user_restaurants table serve as a link between Users and restaurants.
a user can pick several restaurants, one restaurant can be selected by several users.

   #table Users
 create table Users(
 username varchar(255) not null,
 user_id int(11) not null auto_increment,
 password varchar(255) not null,
 fname varchar(31),
 lname varchar(31),
 email varchar(40),
 primary key (adminID)
 )ENGINE=INNODB;
 
 #table restaurants
 #
 #should I use uid as a foreign key
 create table restaurants(
 res_id int(11) not null auto_increment,
 uid int(11) not null,
 index (uid),
 name varchar(40),
 address varchar(80),
 city varchar(30),
 state char(2),
 zip varchar(15),
 menu varchar(50),
 category varchar(50),
 primary key (res_id),
 )ENGINE=INNODB;
 
 #
 #
 create table user_restaurants(
 userID int(11) not null,
 resID  int(11) not null auto_increment,
 foreign key (userID) references Users (user_id),
 foreign key (resID) references restaurants (res_id),
 )

2. for a many-to-one relationship.
2a) I created res_id as a foreign key refers back to restaurants.

in many to one relation, one user can only select one restaurant
but one restaurant can be selected by multiple users.

      #table Users
 create table Users(
 username varchar(255) not null,
 user_id int(11) not null auto_increment,
 resID int(11) not null auto_increment,
 password varchar(255) not null,
 fname varchar(31),
 lname varchar(31),
 email varchar(40),
 primary key (adminID)
 foreign key (resID) references restaurants (res_id)
 )ENGINE=INNODB;
 
 #table restaurants
 create table restaurants(
 res_id int(11) not null auto_increment,
 uid int(11) not null,
 index (uid),
 name varchar(40),
 address varchar(80),
 city varchar(30),
 state char(2),
 zip varchar(15),
 menu varchar(50),
 category varchar(50),
 primary key (res_id),
 
 )ENGINE=INNODB;
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 306
Reputation: timothybard is an unknown quantity at this point 
Solved Threads: 26
timothybard's Avatar
timothybard timothybard is offline Offline
Posting Whiz

Re: determine relationship between two tables?

 
0
  #4
Nov 25th, 2008
With the rating entity I mentioned, there is a one-to-many relationship from rating to user and a one-to-many rating from rating to restaurant. This will allow each user to rate many restaurants and each restaurant to be rated by many users.

There is a many-to-many relationship from users to restaurants with ratings being the cross reference table between the two.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Database Design Forum
Thread Tools Search this Thread



Tag cloud for Database Design
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC