If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > how to make us relationsship one to many?

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-10, 04:36
luser78 luser78 is offline
Registered User
 
Join Date: Feb 2010
Posts: 1
how to make us relationsship one to many?

users have a table and a table of city

One city uses a lot of users. (relationship is one-to-many).

Create users:
CREATE TABLE users (id SERIAL, name VARCHAR NOT NULL, city VARCHAR NOT NULL);

Create a city:
CREATE TABLE city (id SERIAL, country VARCHAR NOT NULL, city VARCHAR NOT NULL);

How to write this sql-queries to the table referred to each other?
Reply With Quote
  #2 (permalink)  
Old 02-12-10, 10:39
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 779
You would be better off with another table design. Maybe something like:

Create users:
CREATE TABLE users (user_id SERIAL, name VARCHAR NOT NULL, city_id

Create a city:
CREATE TABLE city (city_id SERIAL, country VARCHAR NOT NULL, city VARCHAR NOT NULL);

Then join based on the CITY_IDs being equal. Otherwise you would join on the City and hope that no 2 countries have a city with the same name, sorry but that does happen...

Dave
Reply With Quote
Reply

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On