Error trying to insert SQL SERVER

0

I created a table in the DB with the following code

CREATE TABLE documentosDefesa
(
id_documentos_defesa int NOT NULL,
id_reclamatoria int NOT NULL,
funcao nvarchar(50) NULL
);

And now when I try to do the following insertion,:

INSERT INTO [RDO].[dbo].[documentosDefesa] (id_reclamatoria, funcao) VALUES (177,'Pedreiro')

In SQL SERVER MANAGEMENT it displays the following warning:

Can not enter NULL value in column 'id_documentos_defesa', table 'RDO.dbo.documentosDefesa'; the column does not allow nulls. INSERT failed.

I do not understand why I made this mistake, because this is not the first time I create a table and I do it!

    
asked by anonymous 10.02.2015 / 12:29

1 answer

4

The way you created your table, it is defined that the field id_documentos_defesa can not be null, and at the time of inserting, you are not specifying value for such a field (which can not be null).

Perhaps you hoped this field would be self-increasing. For this, you would have to create your table as follows:

CREATE TABLE documentosDefesa
(
    id_documentos_defesa int IDENTITY(1,1) PRIMARY KEY,
    id_reclamatoria int NOT NULL,
    funcao nvarchar(50) NULL
);

Or, change the SQL statement. for example:

INSERT INTO [RDO].[dbo].[documentosDefesa] (id_documentos_defesa, id_reclamatoria, funcao) VALUES (1, 177,'Pedreiro')

If you do not want to delete the entire table, you can only change the ID column:

ALTER TABLE documentosDefesa DROP COLUMN id_documentos_defesa;
ALTER TABLE documentosDefesa ADD id_documentos_defesa int IDENTITY(1,1);
    
10.02.2015 / 12:38