0

What is the problem in this code?

CREATE TABLE pharmacy_night
(
    [Id_day] INT NOT NULL ,
    [Id_pharmacy] INT,
    constraint pk_pharmacy_night primary key (Id_day, Id_pharmacy),
    constraint fk_pharmacy_night_days foreign key (Id_day) references days(Id_day),
    constraint fk_pharmacy_night_pharmacy foreign key (Id_pharmacy) references pharmacy (Id_pharmacy)


)
peterh
  • 11,875
  • 18
  • 85
  • 108

1 Answers1

1

Try this for composite key

create table abc (
  ID int,
  number int,
  constraint PK_abc primary key (ID, number)
);

And try this for Primary key

create table abc
(
ID int primary key,
number int
)
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
  • it doesn't work, when i try to update i receive errors –  May 15 '15 at 04:46
  • What error? Explain your business case please... – Amnesh Goel May 15 '15 at 04:46
  • Update cannot proceed due to validation errors. Please correct the following errors and try again. SQL71508 :: The model already has an element that has the same name dbo.abc. SQL71501 :: Primary Key: [dbo].[PK_abc] has an unresolved reference to object [dbo].[abc]. SQL71501 :: Primary Key: [dbo].[PK_abc] has an unresolved reference to object [dbo].[abc]. SQL71508 :: The model already has an element that has the same name dbo.abc.number. SQL71508 :: The model already has an element that has the same name dbo.abc.ID. –  May 15 '15 at 04:49
  • This error is not helping other than getting that error is coming form some server side code. What is your insert or update query with data please? Check this [SQL FIlddle](http://sqlfiddle.com/#!9/1061a/1) – Amnesh Goel May 15 '15 at 04:56
  • what do you mean in insert query?just i want to create a table with a composite key –  May 15 '15 at 04:59
  • it'is ok.. I solve the problem.. –  May 15 '15 at 05:01
  • Okay, so why don't you drop the existing table if any with the same name and references.. Could you fire a select * from to check if that exist.. or check if any reference exist with that name.. – Amnesh Goel May 15 '15 at 05:01