skip to Main Content

Field | Type | Null | Key | Default | Extra

| Student_id | int | NO | PRI | NULL | auto_increment

As I need when I insert data in table its auto increment automatically insert value but when i try this query insert into student_info values("Harry",75,89,50,56);
I am getting this error as
ERROR 1136 (21S01): Column count doesn’t match value count at row 1

And when I write query like this

insert into student_info values(1,"Harry",75,89,50,56);

The data get inserted

I dont want to insert Student_id by manually as I have declared this column as AUTO_INCREMENT

Mysql Version 8.0

3

Answers


  1. You just need to specify the list of columns explicitly in your query and omit the auto-incremented one. You haven’t specified which columns you have in your table, but it will be something like:

    INSERT INTO student_info(Name, Column2, Column3, Column4, Column5) 
    VALUES("Harry",75,89,50,56);
    
    Login or Signup to reply.
  2. You need to write you request like that :

    INSERT INTO student_info(Student_id, Name, Col1, Col2, Col3, Col4) 
    VALUES('',"Harry",75,89,50,56);
    

    As you need to pass value to your id autoincrement, pass "empty" value, it will generate the new value by itself.

    Login or Signup to reply.
  3. Specify the columns in the INSERT operation. For example:

    INSERT INTO student_info (Name, SomeValue, AnotherValue, AnotherColumn, AndAnotherColumn) 
    VALUES ("Harry",75,89,50,56)
    

    The database engine isn’t going to infer what you want based on whether the primary key is AUTOINCREMENT or not. It’s just looking for the number of values provided and the number of columns in the table. If those don’t match, you need to specify the columns you want to use.

    As an aside, you really should always specify the columns in these operations. Otherwise as soon as you change any table schema then operations which should still work won’t.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search