SQL 跨行同步列
我发誓我记得学习如何执行此操作,但我不记得找到它的关键字,基本上,我希望表上有一列,当修改同一个表上的另一列时,该列会自动更新。我希望更新基本上是通过一个简短的 SQL 脚本根据同一个表中的其他值获得的值。
最大的问题是使其在对其获取值的列进行修改时自动更新,类似于 Excel 电子表格的更新方式。
I swear I remember learning how to do this, but I can't remember the keyword to find it, basically, I want to have a column on my table that automatically updates when another column, on the same table, is modified. I want the updating to basically be a value obtained through a short SQL script, based on other values in the same table.
The biggest problem is making it automatically update upon modifications to the columns it's value is obtained from, similar to how an Excel Spreadsheet would update.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先使用计算列,然后如果计算列无法执行此操作,则使用触发器。就像本例中的 Age 列一样。
Use a COMPUTED COLUMN first, and then a trigger if a computed column can't do it. Like the Age column in this example.
您在寻找“触发”关键字吗?
http://dev.mysql.com/doc/refman/5.0 /en/create-trigger.html
它基本上就是你所描述的
Are you looking for the "trigger" keyword?
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
It basically does what you described
您可能需要研究使用数据库触发器来完成此操作。不同的 RDBMS 有不同的创建方法,因此请检查 RDBMS 文档。
You may want to investigate using a Database Trigger to accomplish this. Different RDBMSes have different ways of creating them, so check your RDBMS documentation.
您可能正在考虑自引用外键
You may be thinking of a self-referencing foreign key
我相信您正在寻找数据库触发器。
MySQL: http://dev.mysql.com/doc/refman/5.0 /en/triggers.html
Sql Server:http://msdn.microsoft.com/en-us/magazine/cc164047.aspx
(无论如何,希望这能让您开始!:))
本质上,您将创建一个触发器INSERT 和 UPDATE,以确保值保持相同。
I believe you're looking for a database trigger.
MySQL: http://dev.mysql.com/doc/refman/5.0/en/triggers.html
Sql Server: http://msdn.microsoft.com/en-us/magazine/cc164047.aspx
(hopefully that gets you started, anyway! :) )
Essentially, you'll create a trigger for INSERT and UPDATE, to make sure the value is kept the same.