I have a problem understanding how the foreign key concept works, i know the theory but the practice cause me problems. For eg I have the table
client(client_id(pk),first_name,last_name, user_id(fk)) and
user(user_id(pk),cient_id(fk),password,e-mail)
and i want to make a form that adds a new client. When adding in client table an user_id, in user table the user_id is filled automatically? or should i add it?
hope it makes sense what i've explained.

Violeta PopaPosted Mar 19, 2013, 7:35 AM
Pradip PandeyPosted Mar 19, 2013, 6:46 AM
Please send your code then will better help you.
Hope it will help.
Violeta PopaPosted Mar 19, 2013, 5:04 AM
Pradip PandeyPosted Mar 19, 2013, 4:58 AM
This is the logical concept of sql server or any database like oracle, mysql etc. It doesn't fill the record into the tables although you can insert the values into the respective tables by using the triggers. It makes the database reliable and consistent.
Hope it will help.
Jignesh TrivediPosted Mar 19, 2013, 12:02 AM
hi
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.Primary key field from one table and insert it into the other table where it becomes a foreign key ie. Foreign Key is a column or a combination of columns whose values match a Primary Key in a different table.
The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign Key in the second table. If a table has a primary key defined on any field(s) then you can not have two records having the same value of that field(s)
Defining FOREIGN KEY is not mean that it automatically fill the data. it just create list of Possible values and restrict user to enter wrong value.
hope this will help you.
Vishal GilbilePosted Mar 18, 2013, 10:30 PM
Creating a foreign key doesn't mean that your value will be automatically processed and saved in your table's foreign key column. It just ensures that the value which is going to be inserted in your table's foreign key column needs to be exists in the parent table's primary key column. Creating Primary foreign key relationship between the table helps us to ensure that the data is reliable and consistent, and at any point of time we can easily get the data by joining these two table based on primary and foreign key column's.
Now coming to your point, you yourself will have to pass the foreign key column value from your front end. Talking from sql point of view, if you want to automatically add the foreign key column value what you can do is create an insert trigger on your parent table and create a procedure to fetch the data from magic tables(from here you will get the last primary key value which was inserted) and through procedure try to add the records in your foreign key table.
Hope that solves your problem.
With Regards,
Vishal Gilbile.