• Best of luck to the class of 2024 for their HSC exams. You got this!
    Let us know your thoughts on the HSC exams here
  • YOU can help the next generation of students in the community!
    Share your trial papers and notes on our Notes & Resources page
MedVision ad

primary and foreign key?? (1 Viewer)

rach_1

New Member
Joined
Aug 11, 2005
Messages
25
Gender
Female
HSC
2005
hey, i dont understand the difference between a primary and foreign key or when they are used and added to existing databases. if someone could please explain that would be great :)
 

Lwaxana

Betazed Ambassador
Joined
Oct 16, 2002
Messages
430
Location
in a galaxy far far away
Gender
Female
HSC
N/A
Keys are fields that are used to sort and retrieve information.
Primary key is a field in which the data is unique
eg in Student Field - primary key would be StudentId
Foreign key is a field that is a Primary Key in another table
eg in Classes table which has ClassId as primary key
TeacherId could be foreign key

does that answer yr query ?
 

Kd14

Member
Joined
Oct 17, 2004
Messages
87
Gender
Male
HSC
2010
The way I remember it is that a Primary key is unique. Like an ID no.

Foreign key is a primary key that's in a field that already has its own primary key.

EG.
Family has a primary key Family ID.
Student has a primary key StudentID plus a foreign key Family ID.
 
Last edited:

jempage

New Member
Joined
Feb 7, 2005
Messages
8
Location
Sydney
Gender
Male
HSC
2005
to use Kd14's example:

Table:Family has the primary field FAMILY_ID. Each FAMILY_ID in the Family table MUST be unique.

Table:Student has the primary field STUDENT_ID, which must be unique, and the FOREIGN_KEY FAMILY_ID from the family table. This foreign key can exist MORE THAN ONE TIME in the Students table, but exists only once in the Family table.

... this would be an example of a Many(students)-to-one(family) relationship!

Wordy I know, but i hope this makes it a bit clearer.
 

^caTaHolic^

New Member
Joined
Nov 10, 2005
Messages
3
Location
Aust
Gender
Female
HSC
2006
can sum1 help me too?

im doing an assessment and we have to create a database for a music store, selling instruments, cds, dvs etc..can u help me and tell me wot tables i would need to create and the relationship between them, i relli duno wot to do and its due in a wk!! thx
 

seremify007

Junior Member
Joined
Apr 29, 2004
Messages
10,059
Location
Sydney, Australia
Gender
Male
HSC
2005
Uni Grad
2009
Haha ummm... It's hard to say =P But you could have like a table for inventory and a table for products... like, instead of saying (in the Inventory table) that you have 100 Green flutes, you could make it ItemNo. 1, Quantity 100, and then in the Products table, say that ItemNo. 1 is a Green flute. After this you could create an orders table, a customer table, etc..

Just remember- primary key is something which has to be unique. Whenever I need one, I just make the "ID" field (or I create an ID field) and make that the primary key. Foreign key is the primary key from another table.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top