How to insert data with auto-increment columns (fields) in MySQL?
P粉465287592
P粉465287592 2023-08-29 11:47:56
0
2
511
<p>I created a table with a primary key and enabled <code>AUTO_INCRMENT</code>:</p> <pre class="brush:php;toolbar:false;">CREATE TABLE IF NOT EXISTS test.authors ( hostcheck_id INT PRIMARY KEY AUTO_INCREMENT, instance_id INT, host_object_id INT, check_type INT, is_raw_check INT, current_check_attempt INT, max_check_attempts INT, state INT, state_type INT, start_time datetime, start_time_usec INT, end_time datetime, end_time_usec INT, command_object_id INT, command_args VARCHAR(25), command_line VARCHAR(100), timeout int, early_timeout INT, execution_time DEC(18,5), latency DEC(18,3), return_code INT, output VARCHAR(50), long_output VARCHAR(50), perfdata VARCHAR(50) );</pre> <p>Then, with the following query, I tried using "" and "1" as the first value, but it didn't work: </p> <pre class="brush:php;toolbar:false;">INSERT INTO test.authors VALUES ('1','1','67','0','0','1','10 ','0','1', '2012-01-03 12:50:49','108929','2012-01-03 12:50:59','198963','21','', '/usr/local/nagios/libexec/check_ping 5','30','0','4.04159','0.102','1', 'PING WARNING -DUPLICATES FOUND! Packet loss = 0%, RTA = 2.86 ms','', 'rta=2.860000m=0%;80;100;0');</pre> <p>So, how to insert auto-increment column (field) data into <strong>MySQL</strong>? </p>
P粉465287592
P粉465287592

reply all(2)
P粉403549616

If you want the auto-increment field to be magically assigned automatically, set it to NULL or 0...

P粉738046172

To take advantage of a column's auto-increment feature, do not provide a value for the column when inserting the row. The database will provide you with a value.

INSERT INTO test.authors (
   instance_id,host_object_id,check_type,is_raw_check,
   current_check_attempt,max_check_attempts,state,state_type,
   start_time,start_time_usec,end_time,end_time_usec,command_object_id,
   command_args,command_line,timeout,early_timeout,execution_time,
   latency,return_code,output,long_output,perfdata
) VALUES (
   '1','67','0','0','1','10','0','1','2012-01-03 12:50:49','108929',
   '2012-01-03 12:50:59','198963','21','',
   '/usr/local/nagios/libexec/check_ping  5','30','0','4.04159',
   '0.102','1','PING WARNING -DUPLICATES FOUND! Packet loss = 0%, RTA = 2.86 ms',
   '','rta=2.860000m=0%;80;100;0'
);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template