What is the difference between inserting NULL or '' in the auto_increment column in mysql?

Asked 2 years ago, Updated 2 years ago, 144 views

Example) member_table
no - auto increment,
id - not null,
pw - not null,
email,  
phone,  
address 

$sql1= "INSERT INTO member_table (no ,id, pw, email,phone,address) VALUES (NULL, '$id', '$pw', '$email', '$phone', '$address')";

$sql2 = "INSERT INTO member_table (no ,id, pw, email,phone,address) VALUES ('', '$id', '$pw', '$email', '$phone', '$address')";

$sql3 = "INSERT INTO member_table (id, pw, email,phone,address) VALUES ('$id', '$pw', '$email', '$phone', '$address')";

If you put data in the member_table above, as shown in $sql1 and $sql2, you can write NULL in the auto_increment column, no, or you can write it by adding a sink quota. What is the difference between the two?

And one more question, is there any difference from just using it with $sql3?

Please answer me.

mysql insert auto_increment

2022-09-22 08:34

1 Answers

MySQL automatically assigns a sequence number if you do not assign a value for the auto_increment column or if you assign a zero. Also, if the auto_increment column is NOT NULL, the sequence number is assigned even if NULL is assigned.

If you look at the case of the member_table above,

INSERT INTO member_table
(id, pw, email, phone, address)
VALUES (1, 'password', '[email protected]', '010-1111-1111', 'address');

INSERT INTO member_table
(no, id, pw, email, phone, address)
VALUES (0,1, 'password', '[email protected]', '010-1111-1111', 'address');
INSERT INTO member_table
(no, id, pw, email, phone, address)
VALUES (NULL, 1, 'password', '[email protected]', '010-1111-1111', 'address');

If you put '' (blank) in the case you mentioned, an error will occur that it is not an integer value. In general, it seems that many people do not assign values to the auto_increment column.


2022-09-22 08:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.