I can not insert the values in the table with auto_increment in the primary key

3

This is my table:

CREATE TABLE Utilizadores(
IDUtilizador INT NOT NULL AUTO_INCREMENT,
PNome VARCHAR(2000) NOT NULL,
UNome VARCHAR(2000) NOT NULL,
Email VARCHAR(2000) NOT NULL,
PalavraPasse VARCHAR(2000) NOT NULL,
TipoUtilizador VARCHAR(2000) NOT NULL,
Check(TipoUtilizador='Administrador' OR TipoUtilizador='Cliente'),
PRIMARY KEY(IDUtilizador)
);

This is my Insert:

INSERT INTO Utilizadores (IDUtilizador,PNome,UNome,Email,PalavraPasse,TipoUtilizador) VALUES ('Ruben','Figueiredo','[email protected]','RubenFigueiredo','Cliente');

The error you give me is as follows:

  

Error Code: 1136. Column count does not match value count at row 1

    
asked by anonymous 20.05.2016 / 20:10

2 answers

2

In the auto-increment column you can omit it from the field list and the VALUES class or pass null as a value.

Passing an empty string may work depending on the configuration of the NO_AUTO_VALUE_ON_ZERO if it is active, it will allow the value zero in the column and when it finds the empty string it will try to insert the record always with zero which causes the error of primary key duplication, in this case to have the correct behavior only null increments the value.

Option 1

INSERT INTO Utilizadores ( IDUtilizador,PNome,UNome,Email,PalavraPasse,TipoUtilizador) VALUES (null, 'Ruben','Figueiredo','[email protected]','RubenFigueiredo','Cliente');

Option 2

 INSERT INTO Utilizadores (PNome,UNome,Email,PalavraPasse,TipoUtilizador) VALUES ('Ruben','Figueiredo','[email protected]','RubenFigueiredo','Cliente');
    
20.05.2016 / 20:13
3

Correct Mode:

INSERT INTO Utilizadores (IDUtilizador,PNome,UNome,Email,PalavraPasse,TipoUtilizador) VALUES ('', 'Ruben','Figueiredo','[email protected]','RubenFigueiredo','Cliente');

You need to pass the ID, pass it as NULL (''), so that auto_increment works correctly. That way it will index ids

    
20.05.2016 / 20:12