在 MySQL 中,如何从单个表加载多个表,同时保持引用完整性?
更新:“将foreign_key_checks设置为1不会触发对现有表数据的扫描。因此,当foreign_key_checks = 0时添加到表中的行将不会被验证一致性。”资料来源:5.1.4。服务器系统变量 -- foreign_key_checks - - 因此,关闭foreign_key_checks似乎不是一个选项...
需要将具有100,000多条记录的单个文件中的数据加载到MySQL上的多个表中,维护文件中定义的关系/桌子;意味着关系已经匹配。该解决方案应该适用于最新版本的 MySQL,并且可以使用 InnoDB 或 MyISAM 引擎。
我对这一切完全陌生,几乎没有自动生成 ID 和建立外键关系的经验。任何指示将不胜感激。
请参阅上面的更新说明: 我可能会补充一点,不需要在实时数据库上进行更新,这意味着可以禁用外键约束,然后执行插入,启用再次受到限制。因为据我了解,如果数据库的引用完整性出现问题,操作就会失败。
所有方法都应包括一些验证和回滚/清理策略,以防插入失败或无法维护引用完整性。
再说一次,我对此完全陌生,如果您有任何疑问或要求澄清,我会尽力提供尽可能多的信息 - 请告诉我。
谢谢!
示例数据:为了更好地说明示例,假设我正在尝试加载一个文件,其中包含员工姓名、他们过去占用的办公室以及由选项卡分隔的职位历史记录。
文件:
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)
如何使用 MySQL 将文件加载到上面的架构中 自动生成员工、办公室和 JobTitle 的 ID 并维护员工和办公室之间的关系,以及员工和职位名称?
所以在这种情况下。表格应如下所示:
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
UPDATE: "Setting foreign_key_checks to 1 does not trigger a scan of the existing table data. Therefore, rows added to the table while foreign_key_checks = 0 will not be verified for consistency." SOURCE: 5.1.4. Server System Variables -- foreign_key_checks -- As a result, it appears turning OFF foreign_key_checks is NOT an option...
Need to load data from a single file with a 100,000+ records into multiple tables on MySQL maintaining the relationships defined in the file/tables; meaning the relationships already match. The solution should work on the latest version of MySQL, and may use either the InnoDB or MyISAM engine.
I am a completely new to all this and have very little experience auto-generating IDs and establishing foreign key relationships. Any pointers would be appreciated.
See UPDATE note above: I might add that it is not a requirement to have the updates made on a live database, meaning it is OKAY to disable foreign key constraints, then execute the inserts, enable the constraints again. Since it's my understanding that if there is something wrong with the database's referential integrity, the operation will fail.
All approaches should include some from of validation and a rollback/cleanup strategy should an insert fail, or fail to maintain referential integrity.
Again, completely new to this, and doing my best to provide as much information as possible, if you have any questions, or request for clarification -- just let me know.
Thanks!
SAMPLE DATA: To better elaborate with an example, lets assume I am trying to load a file containing employee name, the offices they have occupied in the past and their Job title history separated by a tab.
File:
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
NOTE: The single table database is completely normalized (as much as a single table may be) -- and for example, in the case of "John Smith" there is only one John Smith; meaning there are no duplicates that would lead to conflicts in referential integrity.
The MyOffice
database schema has the following tables:
Employee (nId, name)
Office (nId, number)
JobTitle (nId, titleName)
Employee2Office (nEmpID, nOfficeId)
Employee2JobTitle (nEmpId, nJobTitleID)
How can I use MySQL to load the file into the schema above Auto-Generating IDs for Employee, Office and JobTitle and maintaining the relationship between the employee and offices, and employee and Job Titles?
So in this case. the tables should look like:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会将所有文件上传到临时数据库,其中包含以下表格:
Temp_Employee(nId,名称)
Temp_Office(nId,编号)
...
这些表上不会有任何限制或 FK。如果记录已上传,那么您可以为记录添加 id,检查完整性,然后将它们移动到实时数据库(禁用 fks,移动数据,再次启用 fks)
I would upload all the files onto a staging database with following tables:
Temp_Employee (nId, name)
Temp_Office (nId, number)
...
There would be not contraints or FKs on these tables. If the records are uploaded, then you can add id's for the records, check the integrity and then move them to the live database (disabling the fks, moving the data, enabling the fks again)