Close

Results 1 to 3 of 3

Thread: SQL

  1. #1
    DF VIP Member Nikki's Avatar
    Join Date
    Dec 2002
    Location
    Walsall
    Posts
    12,413
    Thanks
    559
    Thanked:        148
    Karma Level
    899

    Default SQL

    i am doing constraints at the moment. am i correct in saying, each table has a primary key, and the link between the primary keys in sql are called foreign keys? as am i reading on i get a different thought for some reason

  2. #2
    DF VIP Member /dev/null's Avatar
    Join Date
    Feb 2004
    Location
    Behind You
    Posts
    2,952
    Thanks
    0
    Thanked:        0
    Karma Level
    451

    Default Re: SQL

    Yes, that is basically correct. Basically, if you want to connect two tables, say "customer" and "order" and you link them in order via the primary key in customer (say customerID) then in the order table, this is a foreign key.
    A primary key is a unique identifier in a table (that is, it is a field which you can uniquely identify a record by), so yes, every table should have one to avoid data duplication and ambiguities (what if two customers could have the same customerID - who would you be referring to?!?!).
    Bear in mind that you can have composite keys too, which are basically a primary key but made from the composition of two (or more) fields in a table, rather than just one.

    Hope this helps and explains it - if not, I'll try and explain better (with a better example, lol!)

  3. #3
    DF VIP Member Q-Ball's Avatar
    Join Date
    Mar 2005
    Location
    In a House
    Posts
    1,576
    Thanks
    56
    Thanked:        2
    Karma Level
    342

    Default Re: SQL

    A foreign key is a primary key in another table.
    E.g You have a customer table and the primary key is customer id.
    You also have a order table and the primary key is order id. If the order table had customer id in it, that would be a foreign key, and should be used to join onto the customer table.

    so yes, you are correct with your interpretation.
    If I'm not back in five minutes... wait longer!

Social Networking Bookmarks

Posting Permissions

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