Mega Code Archive

 
Categories / MSSQL Tutorial / Constraints
 

Using a Multicolumn Primary Key

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