primary and foreign keys

  • Thread starter Thread starter amod
  • Start date Start date
A

amod

hi,

i am confused between these keys. say i create two tables. Now, i
would like to know - which table i need to create primery key and in
which table foreign key, how do i relate them when i write a query.

While creating foreign key do we have to mention that this key has
primery key in some perticular table.

i have not understood the concept. pl explain

amod
 
A primary key is a way to uniquely identify a record.

A simple way to get a primary key is to add an AutoNumber to your table.
Since this gives a different value on each row, it's enough to uniquely
identify the record, e.g. just saying #45 is enough to know which record you
mean.

Every table should have a primary key. You can put a primary key in your
table even if it is the only table in your database.

In a relational database, you usually have more than 1 table. You often need
a 2nd table related to the first. For example, if you have a table of
clients (one record for each client), and a table of orders (one record for
each time someone orders something.) Over time, one client may place many
orders. Therefore in the orders table, you don't put the entire client name
and address every time. Instead you add a ClientID field, which relates to
the ClientID of the client table. This ClientID field cannot be the primary
key of the orders table, because it is not unique (e.g. Client 45 could have
several orders.) This is a foreign key field. That just means it relates to
the primary key of another table.
 
Back
Top