CREATE TABLE DRIVER (
Driver_No int (4) not null unique,
Driver_Name varchar (55),
Joined_Date date,
Branch_No int (2) not null,
PRIMARY KEY (Driver_No)
);
CREATE TABLE BRANCH (
Branch_No int (4) not null unique,
Branch_Name VARCHAR (45),
Office_No VARCHAR (20),
Contact_Person VARCHAR (45),
PRIMARY KEY (Branch_No)
);
CREATE TABLE VEHICLE (
Vehicle_No int (6) not null unique,
Plat_Number varchar (7),
Model varchar (30),
Year year,
Last_Fleet_Maintenance date,
PRIMARY KEY (Vehicle_No)
);
CREATE TABLE FLEET (
Fleet_No int (7) not null unique,
Driver_No int (4),
Vehicle_No int (6),
Fleet_Date date,
Fleet_Type int (2),
Fleet_Status varchar (6),
PRIMARY KEY (Fleet_No)
);
CREATE TABLE FLEET_DETAILS(
Fleet_No int (7),
Job_id VARCHAR (20),
Quantity INT (10),
Charge_amount DECIMAL (9,2),
PRIMARY KEY (Fleet_no, Job_id)
);
CREATE TABLE JOB(
Job_id varchar(10) not null unique,
Description varchar(50),
Service_Charge decimal(9,2),
Part_Charge decimal(9,2),
Unit varchar(10),
PRIMARY KEY (Job_id)
);
CREATE TABLE BILLING(
Billing_No int(10) not null unique,
Fleet_No int(8),
Payment_Type varchar(50),
Payment_Date date,
Payment_Amount decimal,
PRIMARY KEY (Billing_No)
);
ALTER TABLE driver
ADD FOREIGN KEY (Branch_No) REFERENCES branch(Branch_No);
ALTER TABLE fleet
ADD FOREIGN KEY (Driver_No) REFERENCES driver(Driver_No);
ALTER TABLE fleet
ADD FOREIGN KEY (Vehicle_No) REFERENCES vehicle(Vehicle_No);
ALTER TABLE fleet_details
ADD FOREIGN KEY (Fleet_No) REFERENCES fleet(Fleet_No);
ALTER TABLE fleet_details
ADD FOREIGN KEY (Job_id) REFERENCES job(Job_id);
ALTER TABLE billing
ADD FOREIGN KEY (Fleet_No) REFERENCES fleet (Fleet_No);
ALTER TABLE driver AUTO_INCREMENT=1001;
ALTER TABLE vehicle AUTO_INCREMENT=200001;
ALTER TABLE fleet AUTO_INCREMENT= 9000001;
ALTER TABLE fleet
ALTER fleet_type SET DEFAULT 01;
INSERT INTO driver (Driver_No, Driver_Name, Joined_Date, Branch_No)
VALUES ('1001', 'Mohd Hashim Ali', '2015-09-12', '10'),
('1002', 'Hamzah Razali', '2015-10-14', '11'),
('1003', 'Arif Fadillah', '2015-10-15', '12'),
('1004', 'Wan Haizan Wan Omar', '2015-11-02', '10'),
('1005', 'Badrulhisham Mokhtar' ,'2016-01-15', '11');
INSERT INTO branch (Branch_no ,Branch_name,Office_no,Contact_person)
VALUES ('10', 'Kuala Terengganu', '09-6671020', 'Amri Aziz'),
('11', 'Kemaman', '09-6951234', 'Sharifah Syed Ali'),
('12', 'Besut', '09-6973490', 'Rohaiza Rahmad');
INSERT INTO vehicle (Vehicle_No, Plat_Number, Model, Year,
Last_Fleet_Maintenance)
VALUES ('200001', 'TBS1234', 'MAN SE', '2015', '2016-01-17'),
('200002', 'TBS2021', 'MAN SE', '2015', '2016-01-04'),
('200003', 'TBU5912', 'Hino Motors', '2015', '2016-01-15'),
('200004', 'TBU9890', 'Isuzu Motors', '2016', '2016-01-15'),
('200005', 'TBV2000', 'Hino Motors', '2016', '2016-02-26'),
('200006', 'TBW7878', 'Hino Motors', '2016', '2015-12-02');
INSERT INTO fleet (Fleet_No, Driver_No, Vehicle_No, Fleet_Date, Fleet_Type,
Fleet_Status)
VALUES ('9000001', '1001', '200001', '2015-10-10', '01', 'Closed'),
('9000002', '1002', '200002', '2015-11-05', '01', 'Closed'),
('9000003', '1004', '200003', '2015-11-17', '01', 'Closed'),
('9000004', '1001', '200001', '2015-11-21', '02', 'WIP'),
('9000005', '1003', '200004', '2015-11-25', '01', 'Closed'),
('9000006', '1005', '200005', '2015-12-14', '01', 'Closed'),
('9000007', '1001', '200006', '2015-12-16', '01', 'Closed'),
('9000008', '1004', '200002', '2016-01-04', '01', 'Closed'),
('9000009', '1005', '200003', '2016-01-15', '01', 'WIP'),
('9000010', '1003', '200004', '2016-01-15', '02', 'WIP'),
('9000011', '1001', '200001', '2016-01-17', '01', 'Open'),
('9000012', '1002', '200005', '2016-02-26', '01', 'Open');
当我想插入数据以获取舰队详细信息,但出现错误时指出外键约束失败错误代码:1452。无法添加或更新子行:外键约束失败(fms
。{{ 1}},约束fleet_details
外键(fleet_details_ibfk_2
)参考Job_id
(job
))
job_id
答案 0 :(得分:1)
您必须在插入BRANCH
之前插入DRIVERS
,否则您将没有匹配的外键。
JOB
也需要插入FLEET DETAILS
您在FLEET DETAILS
'AirC – 01'
而不是'AirC–01'
上输入了错误的键
答案 1 :(得分:0)
如果您相信自己的插入语句尊重那些 FOREIGN KEYS ,则可以查看脚本,将所有 FOREIGN KEYS 声明移至代码末尾。这是您必须移至脚本末尾的代码:
ALTER TABLE driver
ADD FOREIGN KEY (Branch_No) REFERENCES branch(Branch_No);
ALTER TABLE fleet
ADD FOREIGN KEY (Driver_No) REFERENCES driver(Driver_No);
ALTER TABLE fleet
ADD FOREIGN KEY (Vehicle_No) REFERENCES vehicle(Vehicle_No);
ALTER TABLE fleet_details
ADD FOREIGN KEY (Fleet_No) REFERENCES fleet(Fleet_No);
ALTER TABLE fleet_details
ADD FOREIGN KEY (Job_id) REFERENCES job(Job_id);
ALTER TABLE billing
ADD FOREIGN KEY (Fleet_No) REFERENCES fleet (Fleet_No);
另一种忽略 FOREIGN KEYS 限制的方法是在每个INSERT语句上使用句子 DISABLE / ENABLE KEYS ,您可以在下一行看到示例:
/*!40000 ALTER TABLE `driver` DISABLE KEYS */;
INSERT INTO driver (Driver_No, Driver_Name, Joined_Date, Branch_No)
VALUES ('1001', 'Mohd Hashim Ali', '2015-09-12', '10'),
('1002', 'Hamzah Razali', '2015-10-14', '11'),
('1003', 'Arif Fadillah', '2015-10-15', '12'),
('1004', 'Wan Haizan Wan Omar', '2015-11-02', '10'),
('1005', 'Badrulhisham Mokhtar' ,'2016-01-15', '11');
/*!40000 ALTER TABLE `driver` ENABLE KEYS */;
但是,如果您想控制要插入的数据不违反 FOREIGN KEYS 限制,则需要以有序方式进行插入,即首先将数据插入到被另一个表引用(如果表A在 FOREIGN KEY 限制中引用了表B,则应首先插入表B中的数据)。