2

I would like to insert values in my database with this query:

INSERT INTO Users (pinConfirmed,Factor,DateUtc,LockoutEnabled,AccessCount,EmailConfirmed)
 values ('false', 'false','1/1/2018' ,'false','0','false')

But I get this error: Cannot insert the value NULL into column 'Id', table 'Users'; column does not allow nulls. INSERT fails.

But, there are already data in the DB among which Id which is not null. I checked with these two requests and I have no null for Id.

select * from Users where Id IS NULL;
select * from Users where Id = 'NULL';

I don't understand where the problem is. Need help please.

2
  • What is the table definition? If Id isn't set to produce a value (such as an incrementing integer identity) then it would need a value provided. You're not providing one, hence the error. Commented Dec 6, 2018 at 17:33
  • In fact I copy values ​​from one table to another with an insert into select. There are already values in the field Id. Here is the definition: CREATE TABLE [dbo].[Users] ([Id] NVARCHAR (128) NOT NULL CONSTRAINT [PK_dbo.Users] PRIMARY KEY CLUSTERED ([Id] ASC) ); . I can not do a self increment for values ​​values Commented Dec 6, 2018 at 17:40

3 Answers 3

5

if you go into the table designer, in the properties for the Id column you will see the Identity Specification property set like so:

enter image description here

Expand that out and set (Is Identity) to Yes. This will set the defaults for Identity Increment and Identity Seed to 1

enter image description here

Now save the table and you are all set.

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

Comments

3

According to your comment on the question above, the Id column is defined as:

[Id] NVARCHAR (128) NOT NULL 

And that's it. No default value, no identity/autoincrement, etc. Simply a character value which can not be NULL. But when you perform an insert you don't provide a value:

INSERT INTO Users (pinConfirmed,Factor,DateUtc,LockoutEnabled,AccessCount,EmailConfirmed)
values ('false', 'false','1/1/2018' ,'false','0','false')

That's why you get an error. NOT NULL means a value is required. You're not providing one.

You have a few options:

  • Provide a value for the column when you INSERT
  • Make the column generate its own value (this will depend on your RDBMS, for example in SQL Server you would make the column an IDENTITY or in MySQL you'd make it AUTOINCREMENT, though both of those are for integer columns, whereas yours is character data, so you'll need to change the data type as well)
  • Allow NULL, which of course will only work once because this is a primary key column and values must be unique.

1 Comment

ok David, I did an insertion in two times after your explanation I tried to do it in a step and it works. thanks for your help
0

ID is the PRIMARY key , it is a unique identifier of the row. It Cannot be left null. That is why you have an error.

2 Comments

There are values in the column Id.
@kst92: Not in the data you're trying to insert. Closely examine your INSERT statement. Where do you see an Id value?

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.