Mysql - insert into a table with an auto-increment column only

Suppose we have table A with one column, id (which is the primary key)

How do we insert a new row into a table without specifying an identifier?

I tried this

INSERT INTO A (`id`) VALUES (NULL) 

and does not work

Edit: I forgot to mention this id, the primary key has an auto_increment and NOT NULL attribute.

Edit 2: Exact error while executing the request above

 Column 'id' cannot be null 
+6
source share
4 answers

Once the "id" as auto-increment resolves (if the ID is an integer), you can simply do:

 INSERT INTO A (id) values (null) 

and 'id' will continue to increase each time.

+10
source

only works if you use the primary key auto_increment (PK), since each PK must have a unique but not empty value.

 drop table if exists A; create table A ( id int unsigned not null auto_increment primary key ) engine=innodb; insert into A (id) values (null),(null); mysql> select * from A order by id; +----+ | id | +----+ | 1 | | 2 | +----+ 2 rows in set (0.00 sec) 
+2
source

Try this without the `` .. As in:

INSERT IN A (sid) VALUES (NULL); // I used sid instead of id ...

worked great for me ..

Also, when creating table A, specify a unique (sid) ... iee

create table A (sid int (3) not null auto_increment unique (sid));

0
source

I had a similar problem, then I noticed that I did not apply the changes when I changed id to primary key + not null + auto incremental.

0
source

Source: https://habr.com/ru/post/887806/


All Articles