我创建了一个包含主键并启用AUTO_INCREMENT
的表,如何使用MYSQL AUTO_INCREMENT
?
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)
);
以下是我使用过的查询,我已尝试使用“”和“1”作为第一个值,但它不起作用。
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');
答案 0 :(得分:118)
如果您希望自动神奇地分配自动增量字段,则将其设置为NULL或0 ...
答案 1 :(得分:55)
为了利用列的自动递增功能,在插入行时不要为该列提供值。数据库将为您提供一个值。
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'
);
答案 2 :(得分:9)
default
关键字对我有用:
mysql> insert into user_table (user_id, ip, partial_ip, source, user_edit_date, username) values
(default, '39.48.49.126', null, 'user signup page', now(), 'newUser');
---
Query OK, 1 row affected (0.00 sec)
我正在运行mysql --version
5.1.66:
mysql Ver 14.14 Distrib **5.1.66**, for debian-linux-gnu (x86_64) using readline 6.1
答案 3 :(得分:7)
答案 4 :(得分:0)
我在这里看到三种可能性,它们可以帮助您插入表格而不造成一团糟,但是“指定” 为 AUTO_INCREMENT 列提供了一个值,因为您正在提供所有值都可以执行以下任一操作。
第一种方法(提供 NULL ):
INSERT INTO test.authors VALUES (
NULL,'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'
);
第二种方法(提供'' {简单的引号/撇号},尽管会警告您)
INSERT INTO test.authors 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'
);
第三种方法(提供默认):
INSERT INTO test.authors VALUES (
default,'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'
);
只要插入的所有值与创建表时定义的顺序相同,这些示例中的任何一个在插入表中就足够了。
答案 5 :(得分:0)
我使用类似这样的东西在我的 SQL 请求中只输入值。 我的案例列太多了,我很懒。
insert into my_table select max(id)+1, valueA, valueB, valueC.... from my_table;