The primary key was created when the database was created, but the relationship between the tables was not linked, and it was not possible to build the database diagram only if the primary key did not have a foreign key.
The foreign key is built on the premise that the key must be the primary key of the other table. Steps to build a foreign key:
The first step is to open the designer for the foreign key table you want to build, and right-click to select Relationships.
Then the foreign key relationship form pops up, we select Add, and then click the Small button after table and column specification,
The other form will pop up and let's select the primary key table and column, then click OK after selecting it.
Then we expand the Insert and update specifications, there are four options for updating the rule and deleting the rule, namely "do Nothing," "Cascade," "Set to NULL," "Set default value."
The default does not take any action. If you do not do anything, when we delete or update data from the primary key table, we tell the user that the operation cannot be deleted or updated.
"cascading" means that when we delete or update data from the primary key table, all rows of related data involved in the foreign key table are deleted or updated.
"Set NULL" means that when we delete or update data from the primary key table, the value of the foreign key column in the Foreign key table is set to NULL, provided that the column is allowed to be empty.
"Set default" means that if we define a default value for a foreign key column, the value of the foreign key column in the Foreign key table is set to the default value defined when we delete or update the data in the primary key table.
Of course we can create it with code, and when we create the database table T--card, just add a sentence, "Foreign key (Studentno) references t_student (Studentno)". If we have already created a change chart, then how to implement it in code, it is also very simple saying "add constraint cmpkey (foreign key name) foreign key (Studentno) references t_student (Studentno)".
SQL server2008 How to create a foreign key