MySQL链接表——视图、外键还是触发器?

发布于 2024-10-06 14:04:21 字数 224 浏览 0 评论 0原文

如果“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 技术交流群。

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

发布评论

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

评论(5

指尖上得阳光 2024-10-13 14:04:21

视图可能有效,具体取决于现有系统施加的限制,并且这些由视图强加。它保存重复数据:

CREATE TABLE web_users (username VARCHAR(255), password VARCHAR(255));

CREATE VIEW forum_users AS SELECT username, password FROM web_users;

INSERT INTO web_users VALUES ('user1', 'password1');

SELECT * FROM forum_users;
+----------+-----------+
| username | password  |
+----------+-----------+
| user1    | password1 |
+----------+-----------+

INSERT INTO forum_users VALUES ('user2', 'password2');

SELECT * FROM forum_users;
+----------+-----------+
| username | password  |
+----------+-----------+
| user1    | password1 |
| user2    | password2 |
+----------+-----------+

SELECT * FROM web_users;
+----------+-----------+
| username | password  |
+----------+-----------+
| user1    | password1 |
| user2    | password2 |
+----------+-----------+

A view may work, depending on the constraints imposed by your existing system, and those imposed by views. It saves duplicating data:

CREATE TABLE web_users (username VARCHAR(255), password VARCHAR(255));

CREATE VIEW forum_users AS SELECT username, password FROM web_users;

INSERT INTO web_users VALUES ('user1', 'password1');

SELECT * FROM forum_users;
+----------+-----------+
| username | password  |
+----------+-----------+
| user1    | password1 |
+----------+-----------+

INSERT INTO forum_users VALUES ('user2', 'password2');

SELECT * FROM forum_users;
+----------+-----------+
| username | password  |
+----------+-----------+
| user1    | password1 |
| user2    | password2 |
+----------+-----------+

SELECT * FROM web_users;
+----------+-----------+
| username | password  |
+----------+-----------+
| user1    | password1 |
| user2    | password2 |
+----------+-----------+
御守 2024-10-13 14:04:21

这可以通过触发器来完成。

真正的问题是为什么您希望在数据库中包含重复的信息。

This can be accomplished with triggers.

The real question is why would you want to have duplicate information in the database.

逐鹿 2024-10-13 14:04:21

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 work

details: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

However using natural key as foreign key has some drawbacks,
this question has been discussed before: How to choose my primary key?

阳光下慵懒的猫 2024-10-13 14:04:21

实现此类要求的有效方法是通过数据库触发器

An effective way to implement such a requirement is via a database trigger.

妥活 2024-10-13 14:04:21

在mysql中使用存储引擎作为INNODB并在其中建立索引

use storage engine as INNODB in mysql and do the indexing in it

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