자동 증분 기본 키를 사용하여 MySQL에 데이터를 삽입하는 방법은 무엇입니까?
기본 키를 사용하여 테이블을 만들고 활성화했습니다 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');
열의 자동 증가 기능을 활용하려면 행을 삽입 할 때 해당 열에 값을 제공하지 마십시오. 데이터베이스가 값을 제공합니다.
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'
);
자동 증가 필드를 NULL 또는 0으로 설정하면 자동으로 자동 할당됩니다.
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
이 게시물 확인
그것에 따르면
No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign NULL or 0 to the column to generate sequence numbers.
I see three possibilities here that will help you insert into your table without making a complete mess but "specifying" a value for the AUTO_INCREMENT column, since you are supplying all the values you can do either one of the following options.
First approach (Supplying 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'
);
Second approach (Supplying '' {Simple quotes / apostrophes} although it will give you a warning):
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'
);
Third approach (Supplying default):
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'
);
Either one of these examples should suffice when inserting into that table as long as you include all the values in the same order as you defined them when creating the table.
'IT Share you' 카테고리의 다른 글
PhpMyAdmin의 왼쪽 메뉴에 모든 테이블을 나열하는 방법은 무엇입니까? (0) | 2020.11.12 |
---|---|
System.Windows.Interactivity를 프로젝트에 추가하는 방법은 무엇입니까? (0) | 2020.11.12 |
Android에서 하이퍼 링크 텍스트보기 만들기 (0) | 2020.11.12 |
jQuery DataTables를 사용할 때 첫 번째 열에서 자동 정렬 비활성화 (0) | 2020.11.12 |
MySQL Workbench가 쿼리 결과를 표시하지 않음 (0) | 2020.11.12 |