How to Create Relation ship
Primary Key
Follow the step below to create a primary key.
How to set Auto Increment for Primary key (optional). If you want to auto increment the primary key then you can set 'Is Identity' to 'Yes'. For this follow the step as below:
How to create Foreign Key (fk)
Assume the two tables (tblFaculty & tblStudent)
Since FacID from tblFaculty is FK with FacID in tblStudent.
Here's how we create the foreign key:
Open your table that contains FK(foreign key). In our case we have FacID in tblStudent where we have to set FK. Open table tblStudent in design (right click the table and click on desing)
Now Click on the Relationship (if your relationship is still disabled then click on any column of table in design).
Once you click on 'Relationship' new window will popup. Click on the Add button and follow the step as below:
Now the new window will popup. Select the Primary Key Table and Column as shown below
Then click on OK and save it. Now you are good to have a foreign key from tblStudent referencing tblFaculty.
Create Foreign Key using script
Write this script and Execute it.
ALTER TABLE tblStudent ADD CONSTRAINT FK_tblStudent_tblFaculty FOREIGN KEY(FacID) REFERENCES tblFaculty(FacID)
Primary Key
Follow the step below to create a primary key.
How to set Auto Increment for Primary key (optional). If you want to auto increment the primary key then you can set 'Is Identity' to 'Yes'. For this follow the step as below:
How to create Foreign Key (fk)
Assume the two tables (tblFaculty & tblStudent)
Since FacID from tblFaculty is FK with FacID in tblStudent.
Here's how we create the foreign key:
Open your table that contains FK(foreign key). In our case we have FacID in tblStudent where we have to set FK. Open table tblStudent in design (right click the table and click on desing)
Now Click on the Relationship (if your relationship is still disabled then click on any column of table in design).
Once you click on 'Relationship' new window will popup. Click on the Add button and follow the step as below:
Now the new window will popup. Select the Primary Key Table and Column as shown below
Then click on OK and save it. Now you are good to have a foreign key from tblStudent referencing tblFaculty.
Create Foreign Key using script
Write this script and Execute it.
ALTER TABLE tblStudent ADD CONSTRAINT FK_tblStudent_tblFaculty FOREIGN KEY(FacID) REFERENCES tblFaculty(FacID)
No comments:
Post a Comment