Ahmer Mehmood
Ahmer Mehmood

Reputation: 23

Composite primary key in sql server

I am trying to create a composite primary key in Sql server. The syntax I have tried is:

create table Installment_details
(
 constraint P_key primary key(Account_No,month),
 Account_No int not null,
     foreign key (Account_No) references Account_details(Account_no) on delete cascade,
 Month char(15) not null,
 D@te date,
 Receipt_no varchar(15),
 Amount_received int,
 Amount_left int,
 Amount_receiver char(50),
)

As far as I know it should create column with column name P_key for primary key but whenever I make a entry in table it doesn't show this column.

enter image description here

Upvotes: 0

Views: 176

Answers (2)

Sachin Sah
Sachin Sah

Reputation: 36

I think you are getting it wrong P_key in your code is constraint's name not a column name. Also composite key is not a column, it is used when you don't have a column with unique values. So you take combination of two or more column as primary key so that we can uniquely identify a row.

Upvotes: 1

Nicolas Fleitas
Nicolas Fleitas

Reputation: 118

You are confused about the terms you're using. It's not the same a Primary Key and a Column. For example, you're creating a Primary Key based on two existing columns, and the name P_Key it's the name of the Primary Key, which is the way SQL SERVER (in this case) can identify a row in the Table (it cannot be two rows with the same values on those two columns). I hope this clarifies a little bit the issue.

Upvotes: 1

Related Questions