MySQL链接表——视图、外键还是触发器?
如果“link”
不是这个词的确切术语,我深表歉意。
我有一个包含用户信息的表(用户和密码)。我需要相同的用户:密码对出现在同一数据库的另一个表中。第二个表需要始终复制第一个表 - 如果从第一个表中删除或添加条目 - 更改必须立即应用于第二个表。
请,我需要一个如何完成此操作的示例,因为我不知道在谷歌上搜索的术语。
我只需要两列重复,而不是整个表。
I apologize if "link"
is not the exact term for this in advance.
I have a table with user info, (user&password). I need the same user:password pair to appear in another table in the same database. the second table needs to copy the first one at all times - if an entry is deleted from the first one, or added - the changes must apply to the second table instantly.
Please, I need an example of how this can be done, as I do not know the terminology to search on google..
I only need two columns to be a duplicate, not the entire table.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
视图可能有效,具体取决于现有系统施加的限制,并且这些由视图强加。它保存重复数据:
A view may work, depending on the constraints imposed by your existing system, and those imposed by views. It saves duplicating data:
这可以通过触发器来完成。
真正的问题是为什么您希望在数据库中包含重复的信息。
This can be accomplished with triggers.
The real question is why would you want to have duplicate information in the database.
mysql中的术语称为
外键
。您需要一个
innodb存储引擎
才能使其工作详细信息:http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
但是使用
自然键 作为
外键
有一些缺点,这个问题之前已经讨论过:如何选择我的主键?
The term in mysql is called
foreign key
.You would need a
innodb storage engine
for this to workdetails: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
However using
natural key
asforeign key
has some drawbacks,this question has been discussed before: How to choose my primary key?
实现此类要求的有效方法是通过数据库触发器。
An effective way to implement such a requirement is via a database trigger.
在mysql中使用
存储引擎作为INNODB
并在其中建立索引use
storage engine as INNODB in mysql
and do the indexing in it