创建应通过连接表中其他列中的值自动生成的列
我在 SQL 数据库中得到一个包含四列的表:ID、Comp_Id、Cont_Id 和 Comp_Cont_Id。 ID、Comp_Id、Cont_Id 列中的值是手动输入的,Comp_Cont_Id 列中的值将通过连接 ID、Comp_Id、Cont_Id 列中的值自动生成。
例如,如果我们分别在 (ID, Comp_Id, Cont_Id) 中输入 (201, 301, 401),则表的 Comp_Cont_Id 列中应自动生成值 201_301_401。
有办法实现吗?如果是这样请告诉我。
I got a table in a SQL database with four columns: ID, Comp_Id, Cont_Id and Comp_Cont_Id. The values into ID, Comp_Id, Cont_Id columns are entered manually and the value in the column Comp_Cont_Id is to be generated automatically by concatenating the values in ID, Comp_Id, Cont_Id columns.
For example if we enter (201, 301, 401) into (ID, Comp_Id, Cont_Id) respectively, the value 201_301_401 should be auto-generated in the Comp_Cont_Id column of the table.
Is there a way to achieve it? If so please tell me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MS SQL 支持计算列,语法在此类似问题中给出。
MS SQL supports computed columns, the syntax is given in this similar question.
您使用 PHP 或 ASP.NET 输入值吗?如果用户直接键入表,您可以尝试使用触发器来设置最后一个值。
然而,Comp_Cont_Id 是所谓的派生字段。它的价值取决于其他领域。派生字段的示例有记录总数、平均价格等。最好动态地生成该字段,而不是存储它。当然,也有例外,例如当性能很重要时。
Are you using PHP or ASP.NET to enter the values? If the user is directly keying into the table, you can try using a trigger to set the last value.
However, Comp_Cont_Id is what is called a derived field. Its value depends on other fields. Examples of derived fields are the total number of records, average of prices and etc. It is better to come up with the field on the fly, dynamically, rather than to store it. Of course, there are exception to this, such as when performance is important.