Error inserting: duplicate entry '0' for primary key

0

I have a person table in my database, which I define as follows:

CREATE TABLE 'persona' (
 'documento' bigint(20) NOT NULL,
 'nombres' varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 'apellidos' varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 'telefono' varchar(23) COLLATE utf8_unicode_ci NOT NULL,
 'direccion' varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 'username' varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
 PRIMARY KEY ('documento'),
 KEY 'username' ('username')
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

And when I try to do a insert like this:

INSERT INTO 'persona' ('documento', 'nombres', 'apellidos', 'telefono', 'direccion', 'username') 
VALUES ('88668', 'theth', 'thetn', '2256', 'thrther', 'juancho') 

I get the following error message:

  

Error # 1062 - Duplicate entry '0' for key 'PRIMARY'

Why does that happen and how can I solve it?

    
asked by JUAN CAMILO RUIZ RAMIREZ 28.04.2018 в 04:06
source

2 answers

0

If it's a bigint, it should not have quotes:

INSERT INTO 'persona' 
       ('documento', 'nombres', 'apellidos', 'telefono',  'direccion', 'username') 
VALUES (      88668,   'theth',     'thetn',     '2256',    'thrther', 'juancho' )
    
answered by 28.04.2018 в 09:46
0

When you have numeric fields (not text fields in which you save numbers) in the database do not pass values to them in the query in quotes, pass them directly without them.

That is, instead of ('1', 'asdf', '2', 'qwerty') use this format: (1, 'asdf', 2, 'qwerty').

From my experience I would tell you that this is why you are failing, and if it is not for this, it is likely that you will also have this fault if you do not change it.

    
answered by 07.11.2018 в 22:16