Search code examples
mysqlinsertauto-increment

How to insert row with next id in mysql


I'm using mysql 8 and I'm a big noob so be polite please, I have a table with only 2 columns, ID (Primary key, Not Null, Auto Increment, Unique) and Password (Not Null, Unique)

I'd like to have something like this:

+----+------------+
| id | Password   |
+----+------------+
| 1  | dsakjhsajs |
+----+------------+
| 2  | xczkcjhczx |
+----+------------+
| 3  | treiuytreu |
+----+------------+

Is it possible to INSERT a new password without specifying the id and let the db calculate the id?

Because if I use

INSERT INTO myTable VALUES ('anotherpassword')

I obviously get an error


Solution

  • Name the columns for which you will be inserting values:

    INSERT into mytable (passwordcol) VALUES ('password1');
    

    The word "password" is a keyword, so avoid that as a column name.