1

I imported an excel file with the data I need into Sql Server. Now I want to add a primary key using this statement:

alter table Tickets add constraint idTicket primary key (TOC);;

But I get this error:

Msg 8111, Level 16, State 1, Line 1 Cannot define PRIMARY KEY constraint on nullable column in table 'Tickets'. Msg 1750, Level 16, State 0, Line 1 Could not create constraint or index. See previous errors.

I looked but couldn't find a solution here. How can I fix this?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 2
    Per the message, a primary key must be non-`NULL` and the column must therefore also be defined as such. `TOC` will need to be altered to be non-`NULL` (the statement for this depends on the type of `TOC`). – Jeroen Mostert Jul 24 '19 at 14:19
  • Just usually means one of your columns (idTicket) allows nulls which cannot be for a primary key, just go to the table design and uncheck the allow nulls box on the column you wish to make the primary key. – K.Madden Jul 24 '19 at 14:20

1 Answers1

1
ALTER TABLE Tickets
ALTER TOC INT NOT NULL
GO

Then do your query to add the primary key

K.Madden
  • 353
  • 1
  • 16