Search code examples
mysqlsqlinsertprimary-keyauto-increment

Auto_Increment not working on insert query


Need help how to solve this problem... I have created a users table which has following columns

Create table users
(
  uid int(10) PRIMARY KEY AUTO_INCREMENT,
  uname varchar(50),
  password varchar(50),
  email varchar(50)
);

when i insert values with uid it executes successfully :

Insert into users values(1,'ABC','Helloworld','ABC@gmail.com');

but when i try without uid

Insert into users values('SDC','Helloworld','SDC@gmail.com');

it does not execute successfully and gives an error

ERROR 1136 (21S01): Column count doesn't match value count at row 1

my uid has AUTO_INCREMENT so it should automatically increase..


Solution

  • Of course auto_increment is working correctly. You just need to learn best practices about using insert. Always list all the columns (unless you really, really know what you are doing):

    Insert into users (uname, password, email)
        values('SDC', 'Helloworld', 'SDC@gmail.com');
    

    The id column will be auto-incremented. If you don't list the columns, then MySQL expects values for all columns, including the auto-incremented one.