[英]Joining mysql tables vs updating multiple tables
Lets say i got two tables in mysql. 可以说我在mysql中有两个表。
1. person (id, name, lastname) - Image 1.人(身份证,姓名,姓氏)- 图片
2. someothertable (id, name, lastname, action, quantity) - image 2. someothertable(ID,名称,姓氏,操作,数量)- 图片
I wanted to ask, if its really bad practice, to update both tables at once? 我想问一下,如果真的是不好的做法,一次更新两个表吗? For example if someone updates the last name of Robert Jackson to "Smith" then do 2 queries:
例如,如果有人将Robert Jackson的姓氏更新为“ Smith”,则执行2个查询:
mysql_query("UPDATE person SET lastname = '$lastname' WHERE id = '$id'");
mysql_query("UPDATE someothertable SET lastname = '$lastname' WHERE name = '$name' AND lastname = '$oldlastname'");
Assuming for now, you wont meet 2 same names and surnames (its just an example). 假设现在,您将不会遇到2个相同的名称和姓氏(仅是示例)。 Is it strongly recommended, to join those two tables when displaying data from tables, and change last name only in
person
table? 强烈建议在显示表中的数据时将这两个表连接在一起,并仅在
person
表中更改姓氏吗?
I didn't have need to use join
before (never had databases big enough), and I just started to wonder if there is another way to do this (than 2 queries). 我以前不需要使用
join
(数据库没有足够大的空间),而且我刚刚开始怀疑是否还有另一种方法(超过2个查询)。 Using join
will require some code changing, but i am ready to do it, if its right thing to do. 使用
join
将需要一些代码更改,但是,如果它做对了,我已经准备好做。
Using a join
is not a function of how big your databases are, it's about normalization and data integrity. 使用
join
不是数据库大小的函数,它是关于规范化和数据完整性的。 The reason you would have lastname
in only one table is so that there's no need to worry about keeping values in sync. 您只在一个表中使用
lastname
的原因是,这样就不必担心保持值同步。 In your example, if those calls are in a single transaction, then they should stay in sync. 在您的示例中,如果这些调用在单个事务中,则它们应保持同步。 Unless one of them is changed somewhere else, or manually in the database.
除非其中之一在其他地方更改,或者在数据库中手动更改。
So an option for you would be to have these tables: 因此,您可以选择以下表格:
person (id, name, lastname)
someothertable (id, person_id, action, quantity)
One option would be to make someothertable
have a foreign key constraint on the lastname
field in Person
. 一种选择是使
someothertable
对Person
的lastname
字段具有外键约束。 You could apply an update trigger so it would automatically cascade. 您可以应用更新触发器,以便它会自动级联。
Here is an example below: 下面是一个示例:
Alter table someothertable add constraint foreign key (lastname) references Person (lastname) on delete cascade on update cascade;
A generic version of that can be seen below: 可以看到下面的通用版本:
Alter table [table-name] add constraint foreign key (field-in-current-table) references [other-table-name] (field-in-other-table) on delete cascade on update cascade;
This can be applied to any field in any table. 可以将其应用于任何表中的任何字段。 You can then set the triggers to be appropriate for you.
然后,您可以将触发器设置为适合您的触发器。 Here is a reference link.
这是参考链接。
Have you considered normalization? 您是否考虑过标准化?
Another option would be to assign each person
in the Person
table a uniqueID (ie PersonID
). 另一个选择是为
Person
表中的每个person
分配一个uniqueID(即PersonID
)。 Now in all your other tables you where you reference a person
you reference them by the unique id. 现在,在所有其他表格中,您引用的
person
通过唯一ID引用的。 This adds many advantages: 这增加了许多优点:
1) It keeps the data normalized 2) It maintains data integrity 3) No need for updates, triggers, or cascades 4) A change would only be required in one place 1)保持数据规范化2)保持数据完整性3)不需要更新,触发器或级联4)仅需在一个位置进行更改
Hope this helps. 希望这可以帮助。 Best of luck!
祝你好运!
除了使用2更新,您还可以使用trigger: 此处的教程
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.