简体   繁体   English

如何将SQL数据表转换为指针表?

[英]How to transform SQL table of data into table of pointers?

Let's say I have an SQL table of tuples (id, lastname, firstname, occupation) where all values are strings (ok, obviously id is a key). 假设我有一个元组(id,lastname,firstname,占领)的SQL表,其中所有值都是字符串(好的,显然id是键)。

I want to transform it to a table with tuples (id, lastid, firstid, occupid), where I only keep pointers to other tables that contain the actual values. 我想将其转换为具有元组(id,lastid,firstid,occupid)的表,在该表中,我仅保留指向包含实际值的其他表的指针。 I apologize if the example domain of names and occupations is not the best suited for this operation. 抱歉,示例名称和职业领域不是最适合此操作的。

Obviously to create the other tables that will hold the data, I need to get all last names, unique, and insert them into a new table with an auto-generated key. 显然,要创建用于保存数据的其他表,我需要获取所有唯一的姓氏,然后使用自动生成的键将它们插入到新表中。 The same with first names and occupations. 名字和职业相同。

Having done that, is there a single transformation that can generate the new table containing the pointers (er, foreign keys) ? 完成此操作后,是否可以通过单个转换生成包含指针(er,外键)的新表?

Implementation uses SQLite, if it matters. 如果重要的话,实现使用SQLite。

Assuming your tables for last/first names and occupations are Lnt, Fnt and Occ, each with just two columns, an id field and a value: 假设您的姓/名和职业表是Lnt,Fnt和Occ,每个表只有两列,一个id字段和一个值:

REPLACE INTO TheTable (last, first, occup)
SELECT Lnt.id, Fnt.id, Occ.id
FROM TheTable
JOIN Lnt ON (last=Lnt.value)
JOIN Fnt ON (first=Fnt.value)
JOIN Occ ON (occup=Occ.value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM