
SET DEFAULT- used together with ON DELETE and ON UPDATE.The child will be set to null after the parent data has been updated or deleted. SET NULL- used together with ON DELETE and ON UPDATE.The child data will either be deleted or updated after the parent data has been deleted or updated. CASCADE- used together with ON DELETE and ON UPDATE.
Create db in sql server management studio update#
It means that nothing will happen to the child data after the update or deletion of the parent data.

It specifies what happens to the child data after update on the parent data. It specifies what happens to the child data after deletion of the parent data. parent_column1, parent_column2, … parent_column3- the columns making up the primary key of parent table.parentTable- the name of parent table whose key is to be referenced in the child table.child_column1, child_column2…child_column_n- the name of chidTable columns to reference the primary key in parentTable.fkey_name- the name of the foreign key constraint to be created.column_1, column_2- the columns to be added to the table.childTable is the name of the table that is to be created.Here is a description of the above parameters: REFERENCES parentTable (parent_column1, parent_column2. ‘Course_ID’ and ‘Course_Strength’ as two columns for child table Course_Strength_TSQL.’ However, ‘Course_ID’ shall be Foreign Key.īelow is the syntax to create a table with FOREIGN KEYįOREIGN KEY (child_column1, child_column2. Parent Table: Reconsider, we have an existing Parent table with table name as ‘Course.’Ĭourse_ID and Course_name are two columns with Course_Id as Primary Key.Ĭhild Table: We need to create the second table as the child table with the name as ‘Course_Strength_TSQL.’ T-SQL: Create a Parent-child table using T-SQL Result: We have set Parent-child relationship between ‘Course’ and ‘Course_strength.’ Step 8) Give the Table name as ‘Course_Strength’ and click on OK. ‘Foreign Key Table’- Select ‘Course_Id’ column as ‘Foreign Key table’ column. Step 6) ‘Primary Key Table’ – Select ‘Course_Id’ column as ‘Primary Key table’ column. Step 5) Select ‘Primary Key Table’ as ‘COURSE’ and the new table now being created as ‘Foreign Key Table’ from the drop down. Step 4) In ‘Table and Column Spec’ click on ‘…’ icon Step 3) In ‘Foreign Key Relationship,’ Click ‘Add’

Step 2) Enter two column name as ‘Course_ID’ and ‘Course_Strength.’ Right click on ‘Course_Id’ Column. Step 1) Right Click on Tables>New> Table… However, ‘Course_ID’ shall be Foreign Key. ‘Course_ID’ and ‘Course_Strength’ as two columns. Parent Table: Say, we have an existing Parent table as ‘Course.’ Course_ID and Course_name are two columns with Course_Id as Primary Key.Ĭhild Table: We need to create the second table as a child table. We can Create a Foreign Key in SQL server in 2 ways: The Below Foreign Key in SQL example with diagram summarizes all the above points for FOREIGN KEY How Foreign Key Works
