Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2010
    Posts
    1

    Unanswered: 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?

  2. #2
    Join Date
    Dec 2007
    Location
    Richmond, VA
    Posts
    1,328
    Provided Answers: 5
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •