gpt4 book ai didi

sql - 仅使用 SQL 的 MySQL INSERT

转载 作者:行者123 更新时间:2023-11-29 05:43:10 26 4
gpt4 key购买 nike

是否可以仅使用 SQL 和 MySQL 来获取下面的“输出”?

示例数据:为了更好地举例说明,假设我正在尝试加载一个文件,其中包含员工姓名、他们过去担任过的办公室以及他们的职务历史记录(用制表符分隔) .

文件:

EmployeeName<tab>OfficeHistory<tab>JobLevelHistory
John Smith<tab>501<tab>Engineer
John Smith<tab>601<tab>Senior Engineer
John Smith<tab>701<tab>Manager
Alex Button<tab>601<tab>Senior Assistant
Alex Button<tab>454<tab>Manager

注意:单表数据库是完全规范化的(尽可能多的单表)——例如,在“John Smith”的情况下,只有一个 John Smith;这意味着不存在会导致参照完整性冲突的重复项。

MyOffice 数据库架构具有下表:

Employee (nId, name)
Office (nId, number)
JobTitle (nId, titleName)
Employee2Office (nEmpID, nOfficeId)
Employee2JobTitle (nEmpId, nJobTitleID)

输出: 所以在这种情况下。表格应如下所示:

Employee
1 John Smith
2 Alex Button

Office
1 501
2 601
3 701
4 454

JobTitle
1 Engineer
2 Senior Engineer
3 Manager
4 Senior Assistant

Employee2Office
1 1
1 2
1 3
2 2
2 4

Employee2JobTitle
1 1
1 2
1 3
2 4
2 3

这是用于创建数据库和表的 MySQL DDL:

create database MyOffice2;

use MyOffice2;

CREATE TABLE Employee (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(50) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;

CREATE TABLE Office (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
office_number INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;

CREATE TABLE JobTitle (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
title CHAR(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;

CREATE TABLE Employee2JobTitle (
employee_id MEDIUMINT NOT NULL,
job_title_id MEDIUMINT NOT NULL,
FOREIGN KEY (employee_id) REFERENCES Employee(id),
FOREIGN KEY (job_title_id) REFERENCES JobTitle(id),
PRIMARY KEY (employee_id, job_title_id)
) ENGINE=InnoDB;

CREATE TABLE Employee2Office (
employee_id MEDIUMINT NOT NULL,
office_id MEDIUMINT NOT NULL,
FOREIGN KEY (employee_id) REFERENCES Employee(id),
FOREIGN KEY (office_id) REFERENCES Office(id),
PRIMARY KEY (employee_id, office_id)
) ENGINE=InnoDB;

最佳答案

您可以使用传递表,并为此使用触发器。定期或从您的调用应用程序中,在您完成此表时从中删除。

create table TmpEmp (
EmployeeName char(50) not null,
OfficeHistory int null,
JobLevelHistory char(30) null);

在此表上创建触发器

delimiter |
CREATE TRIGGER tg_TmpEmp BEFORE INSERT ON TmpEmp
FOR EACH ROW
BEGIN
IF not exists (select * from Employee where Name = NEW.EmployeeName) THEN
INSERT INTO Employee(name)
select NEW.EmployeeName;
END IF;
IF not exists (select * from Office where office_number = NEW.OfficeHistory) THEN
INSERT INTO Office(office_number)
select NEW.OfficeHistory;
END IF;
IF not exists (select * from JobTitle where title = NEW.JobLevelHistory) THEN
INSERT INTO JobTitle(title)
select NEW.JobLevelHistory;
END IF;
INSERT INTO Employee2JobTitle(employee_id,job_title_id)
select E.id, T.id
from Employee E
inner join JobTitle T on T.title = NEW.JobLevelHistory
where E.Name = NEW.EmployeeName
AND not exists (select *
from Employee2JobTitle J
where J.employee_id = E.id and J.job_title_id = T.id);
INSERT INTO Employee2Office(employee_id,office_id)
select E.id, O.id
from Employee E
inner join Office O on O.office_number = NEW.OfficeHistory
where E.Name = NEW.EmployeeName
AND not exists (select *
from Employee2Office J
where J.employee_id = E.id and J.office_id = O.id);
END; |
delimiter ;

注意:此触发器和表的好处是无论您使用的是 LOAD-FILE 还是纯插入,它都能正常工作。触发触发器并在需要的地方添加数据。

测试一下

insert tmpEmp(EmployeeName,OfficeHistory,JobLevelHistory)
select 'John Smith',501,'Engineer' union all
select 'John Smith',601,'Senior Engineer' union all
select 'John Smith',701,'Manager' union all
select 'Alex Button',601,'Senior Assistant' union all
select 'Alex Button',454,'Manager';

truncate table tmpEmp;

关于sql - 仅使用 SQL 的 MySQL INSERT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4839460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com