Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2012
    Posts
    5

    Hello Folks i have a question

    I have a question?

    If you have a composite key of 3 fields, can 2 of those 3 fields be repeated? or none of the fields have to be repeated in 2 or more rows, thank you!


    Thanks,

  2. #2
    Join Date
    Oct 2009
    Location
    221B Baker St.
    Posts
    486
    It will help if you post some data examples to clarify your question.

  3. #3
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by ybalt View Post
    If you have a composite key of 3 fields, can 2 of those 3 fields be repeated?
    yes

    it is the combination of all three that has to be unique

    so you can have this --

    AAA BBB 1
    AAA BBB 2
    PPP QQQ 9
    PPP QQQ 37
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  4. #4
    Join Date
    Oct 2012
    Posts
    5
    Thanks a lot, that was very helpful.

    I have another question, if you don't mind,

    TAKE A LOOK AT THE FOLLOWING TABLES:
    ROOM
    key_room_number **** key
    price_per_ night
    floor
    max_persons
    has_baby_bed
    has_shower
    has_bath
    has_seeview


    BOOKING
    booking_id
    start_date
    end_date
    room_number
    guest_id


    GUEST
    guest_id ****key
    first_name
    last_name
    phone
    email
    address
    city
    country

    IS THIS ALRIGHT FOR A SIMPLE HOTEL BOOKING DATABASE?

    i have a doubt in the booking table, this is an explanation to many to many relationship in a tutorial online, but i dont know which should be my key in the bookings table, is it the booking id or a composite key between the other 2 tables keys or otherwise, i will show you the website in case i didnt explaim myself very well, thank you

    http://en.tekstenuitleg.net/articles...y-to-many.html

    the example is at the bottom of the page.

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by ybalt View Post
    ... but i dont know which should be my key in the bookings tablee.
    Code:
    CREATE TABLE bookings
    ( room_number INTEGER NOT NULL REFERENCES rooms ( room_id )
    , guest_id INTEGER NOT NULL REFERENCES guests ( guest_id )
    , start_date  DATETIME NOT NULL
    , PRIMARY KEY ( room_number, guest_id, start_date )
    , end_date   DATETIME NOT NULL
    );
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Tags for this Thread

Posting Permissions

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