0

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

10
  • 1
    No, P_Key is not a column, it's the name of the primary key constraint. Commented Jan 17, 2019 at 15:30
  • 1
    You created the Composite key using those two fields (AccountNo, month). Commented Jan 17, 2019 at 15:31
  • isn't that name supposed to be shown in table? Commented Jan 17, 2019 at 15:32
  • 1
    No, that is only the name of the Key you created! Fields are only the ones which shows up on table. Commented Jan 17, 2019 at 15:33
  • Column names != constraint names. You never see constraint names unless you dig into system views or use Management Studio. You'll find P_key under Keys, but nowhere else. To refer to the primary key in foreign key constraints, you will also always need to name the columns involved ((Account_No, month)), never the constraint name itself. Commented Jan 17, 2019 at 15:34

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.