将数据添加到相互关联的表..更简单的方法?

发布于 2024-10-25 13:07:36 字数 743 浏览 0 评论 0原文

我对 mysql 有点生疏,并试图再次跳入其中。如果这个问题太简单了,我很抱歉。

我基本上创建了一个数据模型,其中有一个名为“Master”的表,其中包含名称和 IDcode 的必填字段,然后是一个包含 IDcode 外键的“详细信息”表。

现在这就是它变得棘手的地方..我输入:

INSERT INTO Details (Name, UpdateDate) Values (name, updateDate)

我收到一个错误:在详细信息上说IDcode没有默认值..所以我添加一个然后它抱怨字段'Master_IDcode'没有默认值

它一切都有道理,但我想知道是否有任何简单的方法可以完成我想做的事情。我想将数据添加到详细信息中,如果不存在 IDcode,我想在主表中添加一个条目。问题是我必须首先将名称添加到基金主数据中..等待生成唯一的 ID(用于 IDcode),然后在输入主数据时找出该名称并将其添加到我的查询中。正如您可以想象的那样,由于我有很多表,查询可能会变得很长。

有更简单的方法吗?每次我添加一些内容时,它都会按名称搜索是否存在外键,如果不存在,它会将其添加到其链接到的所有表中?人们这样做有标准方法吗?我无法想象对于所有复杂的数据库,人们还没有找到更简单的方法。

抱歉,如果这个问题没有意义。如果需要,我可以添加更多信息。

ps这可能是一个不同的问题,但我听说过 Django for python 并且它有助于创建查询..它对我的情况有帮助吗?

预先非常感谢:-)

I am a bit rusty with mysql and trying to jump in again..So sorry if this is too easy of a question.

I basically created a data model that has a table called "Master" with required fields of a name and an IDcode and a then a "Details" table with a foreign key of IDcode.

Now here's where its getting tricky..I am entering:

INSERT INTO Details (Name, UpdateDate) Values (name, updateDate)

I get an error: saying IDcode on details doesn't have a default value..so I add one then it complains that Field 'Master_IDcode' doesn't have a default value

It all makes sense but I'm wondering if there's any easy way to do what I am trying to do. I want to add data into details and if no IDcode exists, I want to add an entry into the master table. The problem is I have to first add the name to the fund Master..wait for a unique ID to be generated(for IDcode) then figure that out and add it to my query when I enter the master data. As you can imagine the queries are going to probably get quite long since I have many tables.

Is there an easier way? where everytime I add something it searches by name if a foreign key exists and if not it adds it on all the tables that its linked to? Is there a standard way people do this? I can't imagine with all the complex databases out there people have not figured out a more easier way.

Sorry if this question doesn't make sense. I can add more information if needed.

p.s. this maybe a different question but I have heard of Django for python and that it helps creates queries..would it help my situation?

Thanks so much in advance :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

棒棒糖 2024-11-01 13:07:36

(决定扩展上面的评论并将其放入答案中)

我建议在数据库中创建一组临时表(每个数据集/文件一个)。

然后使用 LOAD DATA INFILE (或批量插入行)到这些临时表中。
确保在加载之前删除索引,并在加载数据后重新创建所需的索引。

然后,您可以对临时表进行一次传递以创建丢失的主记录。例如,假设您的临时表之一包含应用作 masterID 的国家/地区代码。您可以通过执行以下操作来添加主记录:

insert 
  into master_table(country_code)
select distinct s.country_code 
  from staging_table     s
  left join master_table m on(s.country_code = m.country_code)
 where m.country_code is null;

然后,您可以继续将行插入到“真实”表中,并知道所有详细信息行都引用有效的主记录。

如果您需要获取参考信息和数据(例如翻译一些代码),您可以通过简单的联接来完成此操作。另外,如果您想通过其他表过滤行,现在也很容易。

insert 
  into real_table_x(
          key
         ,colA
         ,colB
         ,colC
         ,computed_column_not_present_in_staging_table
        ,understandableCode
       )
  select x.key
        ,x.colA
        ,x.colB
        ,x.colC
        ,(x.colA + x.colB) / x.colC
        ,c.understandableCode
     from staging_table_x  x 
     join code_translation c on(x.strange_code = c.strange_code);

这种方法是一种非常有效的方法,并且可以很好地扩展。上述的变体通常用在数据仓库的 ETL 部分来加载大量数据。

MySQL 的一个需要注意的是它不支持哈希联接,这是一种非常适合完全联接两个表的联接机制。 MySQL 使用嵌套循环,这意味着您需要非常仔细地对连接列建立索引。
InnoDB 表在主键上具有集群功能,可以帮助提高效率。

最后一点。当数据库中有暂存数据时,可以轻松添加一些数据分析并将“坏”行放在单独的表中。然后,您可以使用 SQL 检查数据,而不必在编辑器中费力地浏览 csv 文件。

(decided to expand on the comments above and put it into an answer)

I suggest creating a set of staging tables in your database (one for each data set/file).

Then use LOAD DATA INFILE (or insert the rows in batches) into those staging tables.
Make sure you drop indexes before the load, and re-create what you need after the data is loaded.

You can then make a single pass over the staging table to create the missing master records. For example, let's say that one of your staging table contains a country code that should be used as a masterID. You could add the master record by doing something along the lines of:

insert 
  into master_table(country_code)
select distinct s.country_code 
  from staging_table     s
  left join master_table m on(s.country_code = m.country_code)
 where m.country_code is null;

Then you can proceed and insert the rows into the "real" tables, knowing that all detail rows references a valid master record.

If you need to get reference information along with the data (such as translating some code) you can do this with a simple join. Also, if you want to filter rows by some other table this is now also very easy.

insert 
  into real_table_x(
          key
         ,colA
         ,colB
         ,colC
         ,computed_column_not_present_in_staging_table
        ,understandableCode
       )
  select x.key
        ,x.colA
        ,x.colB
        ,x.colC
        ,(x.colA + x.colB) / x.colC
        ,c.understandableCode
     from staging_table_x  x 
     join code_translation c on(x.strange_code = c.strange_code);

This approach is a very efficient one and it scales very nicely. Variations of the above are commonly used in the ETL part of data warehouses to load massive amounts of data.

One caveat with MySQL is that it doesn't support hash joins, which is a join mechanism very suitable to fully join two tables. MySQL uses nested loops instead, which mean that you need to index the join columns very carefully.
InnoDB tables with their clustering feature on the primary key can help to make this a bit more efficient.

One last point. When you have the staging data inside the database, it is easy to add some analysis of the data and put aside "bad" rows in a separate table. You can then inspect the data using SQL instead of wading through csv files in yuor editor.

‘画卷フ 2024-11-01 13:07:36

我认为没有一步到位的方法可以做到这一点。

我所做的是

INSERT IGNORE (..) values (..)

向主表发出 a ,如果该行不存在,它将创建该行,或者什么都不做,然后发出 a

SELECT id FROM master where someUniqueAttribute = ..

另一个选项是存储过程/触发器,但它们在 MySQL 中仍然是相当新的我怀疑这是否有助于提高性能。

I don't think there's one-step way to do this.

What I do is issue a

INSERT IGNORE (..) values (..)

to the master table, wich will either create the row if it doesn't exist, or do nothing, and then issue a

SELECT id FROM master where someUniqueAttribute = ..

The other option would be stored procedures/triggers, but they are still pretty new in MySQL and I doubt wether this would help performance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文