Primary key and Foreign Key


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Primary key and Foreign Key

  1. #1
    Join Date
    Aug 2006
    Posts
    113

    Primary key and Foreign Key

    i want to know how to use primary key and foreign key

    pleaseeeeeee

  2. #2
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    Phil Weber
    http://www.philweber.com

    Please post questions to the forums, where others may benefit.
    I do not offer free assistance by e-mail. Thank you!

  3. #3
    Join Date
    Apr 2006
    Posts
    28
    How do you mean?

    Primary key will uniquely identify any row within your table, and the foreign key is used to relate the child table to the parent.

    Example,

    We have a v simple database containing student course information. In one table we have student data (where the primary key is studentid), and in the second table we have course data (where the courseid is pkey).

    The relationship between the 2 tables is that a student undertakes one course and a course can be taken by many students - a one to many rel.

    In order to relate the two tables, we put the courseid (foreign key) into the student table.

    ^^ does that help???...

    soz-very tired at the moment to get into any more detail!
    Last edited by evian; 08-31-2006 at 11:40 AM.

  4. #4
    Join Date
    Aug 2006
    Posts
    113

    syedwna

    means we have to write sql query to insert data into student table and one more sql query to insert data into course tabel,for ex

    student table
    stid stname stcid
    1 syewna c1


    course tabel
    cid cname
    c1 maths

    so we will be inserting c1 as the course id in both the tabels,what is the use then we can have the column 'cname' in student tabel itself no,so we can save having 2 columns of cid in both the tabels

  5. #5
    Join Date
    Apr 2006
    Posts
    28
    Quote Originally Posted by syedwna
    means we have to write sql query to insert data into student table and one more sql query to insert data into course tabel,for ex
    Yeap, u need to insert cid into both tables if cid is your primary key-how else would you know what student was doing what course???

    If each course name is unique then you could possibly use that as the primary key of the course table, and then put the course name into the child table (student table in your case) .

    You may use course id rather than the course name to avoid problems such as:

    if course name is written incorrectly and inserted into both tables - to correct this spelling mistake we have to update both tables. If we used the course id - we would just have to update the sp mistake in the course table.


    Hope that helps.

  6. #6
    Join Date
    Aug 2006
    Posts
    113

    primary key,foreign key,index

    can u explain in basic example about these keys

  7. #7
    Join Date
    Sep 2006
    Posts
    10

    hi again

    primary key enforce uniqueness for values entered in specified columns that do not allow nulls; in other words it uniquely identifies a record in our table.
    example: table employee

    Emp_ID Emp_Name Emp_Age Dept_ID
    1 Rana 24 1
    2 nana 27 1
    3 Rana 26 2

    as you can see above Emp_ID can never be NULL and can never be repeated in the table, thus each employee has its own ID
    therefore the Emp_ID field can be assigned as primary key since it uniquely identifies a record.
    while as you can see also the name and the department id could be repeated.

    As for foreign key:
    A foreign key (FK) is a column used to establish and enforce a link between the data in two tables. A link is created between two tables by adding the column that hold one table's primary key values to the other table. This column becomes a foreign key in the second table.
    or in other word its Father and child relation, always the child carries his father name and not the opposite.

    example dept table:

    dept_Id dept_name
    1 HR
    2 FF

    in this table dept_id is aprimary key since it uniquely identifies a record.
    while if u look to the dept_ID in the employee table u will notice that it can be repeated and it shows each employee to which dept he belongs


    concerning the index property, its just like the index of a book that helps in fast searching

    i guess i couldnt be as clear as possible, but im somehow in a hurry.i will check later and repost with better examples.


    regards,
    rana

Similar Threads

  1. Database Design Help needed
    By Brian Pittman in forum Database
    Replies: 2
    Last Post: 04-29-2007, 08:23 AM
  2. Replies: 0
    Last Post: 10-28-2002, 09:06 AM
  3. Primary and Foreign Keys
    By Chris in forum Database
    Replies: 3
    Last Post: 04-24-2002, 04:19 PM
  4. Replies: 0
    Last Post: 09-18-2001, 04:53 PM
  5. Creating a foreign key in MS SQL 7
    By Gonzalo in forum Database
    Replies: 1
    Last Post: 04-19-2001, 04:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center