Primary key Foreign key
Posted in 1993
Topics: Triggers, Constraints & Referential Integrity
>>Kevin Leung writes... >> >> >>Should all columns of the foreign key be the same as the columns in the >>corresponding primary key ? >> >>For example, table t1 has four columns c1, c2, c3 and c4. Columns c1 and >>c2 are composite pirmary key. >> >>Table t2 has four columns d1, d2, c1, d3. Columns d1 is the primary key >>and column c1 has the same domain as the column c1 in t1. >> >>Can I say column c1 in t2 the foreign key of column c1 in t1? >> So you run a small food delivery company and have planned your menu on a weekly basis with a choice of 3 meals a day and the days numbered 1,2,3... Suppose you have the following... table t1(weekly menu list) --------------------------------------------------------- | c1(number) | c2(day) | c3(menu) | c4(price) | --------------------------------------------------------- | 1 | 1 | noodles | 2.25 | | 2 | 1 | steak | 5.00 | | 3 | 1 | sausage | 1.75 | | 1 | 2 | vegetable | 3.95 | | 2 | 2 | spaghetti | 2.50 | | 3 | 2 | burgers | 2.00 | | 1 | 3 | curry | 2.75 | | ... | ... | ... | ... | | ... | ... | ... | ... | --------------------------------------------------------- You know that on the current list the menu number 1 on day 2 is vegetable. That is your primary key (numer and day). Your order department receives orders based on this list from your customer base and it happens to look like this. table t2(orders received) ------------------------------------------------------------------------- | d1(customer_id) | d2(address) | c1(number) | d3(amount) | ------------------------------------------------------------------------- | Freds shack | 42 St. | 1 | 22 | | Freds shack | 42 St. | 2 | 1 | | Freds shack | 42 St. | 3 | 8 | | Big Bros Ltd. | Orleans Road | 3 | 255 | | Big Bros Ltd. | Orleans Road | 2 | 5 | | Big Bros Ltd. | Orleans Road | 1 | 3 | ------------------------------------------------------------------------- Well that's what your foreign key looks like ! Do you think you can deliver the right food from t1 based on that ? The foreign key must relate fully to the primary key whether composite or not. Hope this helps !! MfG Tom Hogarty SNI Paderborn Germany Standard disclaimer applies !!