2017-05-10 1 views
0
에 대한 다중 행 삽입

나는 다음과 같은 테이블이 있습니다SQL - FK

| order_details | CREATE TABLE `order_details` (
    `order_id` int(11) NOT NULL, 
    `client_id` int(11) NOT NULL, 
    `product_id` varchar(10) NOT NULL, 
    `serial` int(11) NOT NULL, 
    `detail_id` int(11) NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY (`detail_id`), 
    KEY `order_id` (`order_id`), 
    KEY `client_id` (`client_id`), 
    CONSTRAINT `order_details_ibfk_1` FOREIGN KEY (`order_id`) REFERENCES `orders` (`order_id`), 
    CONSTRAINT `order_details_ibfk_2` FOREIGN KEY (`client_id`) REFERENCES `clients` (`client_id`) 
) ENGINE=InnoDB AUTO_INCREMENT=57 DEFAULT CHARSET=utf8 | 

내가 CLIENT_ID 컬럼의 값 아래로 여러 행을 삽입하려고 : 나는를 실행하려고

insert into order_details 
    (`client_id`) 
    values 
    (1), 
    (1), 
    (1), 
    (1), 
    (2), 
    (3), 
    (14), 
    (4), 
    (5), 
    (5), 
    (5), 
    (5), 
    (7), 
    ... 
    (12), 
    (13); 

엄격 모드를 끈 상태에서 삽입하면 외래 키 제약 조건 오류 (Err : 1452)가 발생합니다. 그러나 그것은 client_id (err msg 참조)와는 달리 order_id 열에 삽입하려고 시도하는 것 같습니다. 이 오류의 원인은 무엇이고 insert를 client_id 열로 리디렉션 할 수 있습니까?

오류 메시지 :

추가 또는 자식 행을 업데이트 할 수 없습니다 : 외래 키 제약 조건 (. CONSTRAINT order_details_ibfk_1 FOREIGN KEY (order_id) 참고 문헌 orders (order_id) dborder_details)

에게 실패를

답변

1

당신은 삽입의 모든 null 이외의 열을 나열해야합니다

insert into order_details 
    (`client_id`, [other columns here]) 
values 
(1, [other values here]), 
+0

완벽한을, 감사합니다. 만약 내가 이미 그 필드에 값을 가지고 그들을 무시하고 싶지 않아 그냥 null을 통과해야합니까? –

+0

@RayFoo 어떻게 그 필드에 값을 가질 수 있습니까? 이들은 당신이 만들고있는 새로운 행입니다. – Barmar

+0

또 다른 옵션은'CREATE TABLE' 문에'DEFAULT' 값을 선언하는 것입니다. – Barmar

0

오류 문은 매우 간단합니다 ... null 값을 not null 열에 삽입하려고합니다. 아래의 것들. insert 문에 결함이 있습니다.

order_id int(11) NOT NULL 
    `product_id` varchar(10) NOT NULL, 
    `serial` int(11) NOT NULL,