Most of the time many new SQL developer do not able to create the primary key and Foreign Key relationship. In this post I am going to show this steps and it very simple to understand.
We have to create to table first for Primary Key and second for Foreign Key.
--Step1- First table creating here. With Primary Key on RuleID column.
create TABLE dbo.Table1_Rule
RuleID int primary key,
Status bit ,
- Step2 - Now we are creating second table with getting reference for Foreign Key.
create TABLE dbo.Table2_Condition
RuleConditionID int ,
RuleID int references dbo.Table1_Rule(RuleID ),
AppliedContent bit ,
--Step 3 - Then we are appling Foreign Key in second(dbo.Table2_Condition) table on RuleID column.
ALTER TABLE dbo.Table2_Condition
ADD FOREIGN KEY (RuleID) REFERENCES dbo.Table1_Rule(RuleID);