Hi
I have 2 tables
1. Student Master
Student ID Primary , Name , Address
2. Student Detail
ID Primary (Identity), Student ID, Join Date , Marks
How i can relate 2 tables so that Student ID in table 1 must exists.
Thanks
Hi
I have 2 tables
1. Student Master
Student ID Primary , Name , Address
2. Student Detail
ID Primary (Identity), Student ID, Join Date , Marks
How i can relate 2 tables so that Student ID in table 1 must exists.
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit MohantyPosted Apr 5, 2023, 4:16 AM
You can use a foreign key in the "Student Detail" table that references the "Student ID" primary key in the "Student Master" table. This way, you can ensure that a student ID in the "Student Detail" table must exist in the "Student Master" table.
Vishal YelvePosted Apr 5, 2023, 6:32 AM
Hi Ramco,
As per Amit suggestion you need to add foreign key in StudentDetail table. which refernce to StudentId of Student Master table.
Also refer below links
https://csharp-video-tutorials.blogspot.com/2012/08/creating-and-working-with-tables-part-3.html
https://www.youtube.com/watch?v=TpKcAmaaBts
https://www.youtube.com/watch?v=JLeaM8pK8dE
https://www.guru99.com/sql-server-foreign-key.html
Amit MohantyPosted Apr 5, 2023, 4:47 AM
If your tables are already exists the you can alter the table to add foregin key.
Ramco RamcoPosted Apr 5, 2023, 4:29 AM
Hi Amit
Can u pls guide how to do thru GUI
Thanks
Pasang TamangPosted Apr 5, 2023, 4:11 AM
Hi Rmaco,
You have to use foreign key in your second table. In Student Detail table column Student ID should be foreign key. You can get detail idea about foreign key here https://www.w3schools.com/sql/sql_foreignkey.asp.
Regards,
Pasang