SQL Server 2008:
DECLARE @MyTable TABLE( PersonID INT NOT NULL, Person2ID INT NOT NULL, Description NVARCHAR(100), CONSTRAINT PK PRIMARY KEY CLUSTERED (PersonID, Person2ID) );
Gives:
Msg 156, Level 15, State 1, Line 5 Incorrect syntax near the keyword 'CONSTRAINT'.
Is there any way to have compound Primary key in Table valued variables?
Primary keys must contain unique values. A primary key column cannot have NULL values. A table can have only one primary key, which may consist of single or multiple fields. When multiple fields are used as a primary key, they are called a composite key.
Table variables allow us to create the following constraints: Primary Key. Unique.
Good practice for primary keys in tables Prefer a numeric type because numeric types are stored in a much more compact format than character formats.
What Is a Composite Key in SQL? A composite key in SQL can be defined as a combination of multiple columns, and these columns are used to identify all the rows that are involved uniquely. Even though a single column can't identify any row uniquely, a combination of over one column can uniquely identify any record.
You can define a composite primary key like this:
DECLARE @MyTable TABLE ( PersonID INT NOT NULL, Person2ID INT NOT NULL, Description NVARCHAR(100), PRIMARY KEY (PersonID, Person2ID) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With