Using a Multicolumn Primary Key : Primary Key « Constraints « SQL Server / T-SQL Tutorial






5> CREATE TABLE ClassGrades(
6>     ClassID int,
7>     StudentID int,
8>     GradeLetter varchar(2),
9>     Constraint PK_ClassGrades
10>         PRIMARY KEY(ClassID, StudentID)
11> )
12> GO
1>
2> INSERT ClassGrades VALUES(1,1,'A')
3> INSERT ClassGrades VALUES(1,2,'B-')
4> INSERT ClassGrades (ClassID, GradeLetter)VALUES(1,'C-')
5> GO

(1 rows affected)

(1 rows affected)
Msg 515, Level 16, State 2, Server J\SQLEXPRESS, Line 4
Cannot insert the value NULL into column 'StudentID', table 'master.dbo.ClassGrades'; column does not allow nulls. INSERT fails.
The statement has been terminated.
1> drop table ClassGrades;
2> GO








7.2.Primary Key
7.2.1.Primary Key Constraint
7.2.2.The PRIMARY KEY Clause
7.2.3.Define constraint name for primary key
7.2.4.PRIMARY KEY (cust_id)
7.2.5.You cannot add another primary key to table
7.2.6.NONCLUSTERED PRIMARY KEY
7.2.7.Using a Multicolumn Primary Key