Define Primary key and foreign key : Foreign Key « Constraints « SQL Server / T-SQL






Define Primary key and foreign key


1> CREATE TABLE Matches (
2>    Society_Group_Id int NOT NULL ,
3>    Match_Id         int IDENTITY (1, 1) NOT NULL PRIMARY KEY CLUSTERED,
4>    Points_Against   smallint NOT NULL
5> )
6> GO
1>
2> CREATE TABLE Match_Scores (
3>    Match_Id     int NOT NULL PRIMARY KEY CLUSTERED,
4>    Player_Id    int NOT NULL ,
5>    Score_Points tinyint NULL
6> )
7> GO
1>
2> ALTER TABLE Match_Scores
3>    ADD CONSTRAINT [FK_Match_Scores_Matches] FOREIGN KEY (Match_Id)
4>    REFERENCES [Matches] ([Match_Id])
5> GO
1>
2> drop table Matches
3> GO
1>
2> drop table Match_scores
3> GO
1>
           
       








Related examples in the same category

1.Table-level constraints
2.How to use foreign key constraints
3.Adding a Foreign Key to an Existing Table