Click here to Skip to main content
15,904,655 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i enter the value in one table has primary key,, and another table foreign key ,,how the value store to the database automatically of foreign as i enter the value of primary key
Posted

Every table in a relational database model must have a field designated as primary key. The values in this primary key field cannot be repeated in the table so as to ensure the uniqueness of each record in the table, such as the matriculation number of a student in a table containing particular of students.
As a result of normalization, a typical relational database will have many tables. Each table (students) will be linked (related) to another table (say modules),
For example: on one hand, the students table may consist of student_id, student_name, and other related fields where student_id is the primary key.
On other hand, the modules table may consist of module_id, module_name, student_id (foreign key), module_grade, and so on., module_id and student_id make up a composite primary key for this table. In addition, student_id is also a foreign key linked to the primary key in students table.
To print the result of a student that includes student_name, module_name, grade and so on, we will have to join this 2 table using the student_id (primary key) in the students table and the student_id (foreign key) in the modules table.
Every student_id must exist in the primary key table (students) first, before it can be inserted into the foreign table (modules). Can you imagine having a module record belonging to a student whose record does not exist in the students table.
Read more: Relational_Database_Design[^]
 
Share this answer
 
You can't: the system doesn't know what row you are talking about.

When you create an element in the Primary key table, you can then use that row info to create (or update) a row in the foreign key table - but only you know which row should be updated, or what other information should be used to create a new row.

You might want to look at creating a Stored Procedure to do both actions in one operation: http://technet.microsoft.com/en-us/library/ms345415.aspx[^] should help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900