简体   繁体   English

是否可以基于另一个表列T-SQL来命名约束?

[英]Is it possible to name a constraint based off of another table column T-SQL?

First, I'm working with asp.net 4.5 and this is T-SQL and MVC. 首先,我正在使用asp.net 4.5,这是T-SQL和MVC。

I can't find a clear answer online (so I'm guessing it's not possible). 我无法在网上找到明确的答案(因此,我猜这是不可能的)。 I wanted to know if you can set a constraint for a default value to be from a table's column. 我想知道是否可以从表的列中设置默认值的约束。 Such as: 如:

Table A (Users): 表A(用户):

ID | UserName |
---------------
1    Chuck
---

Table B: 表B:

ID  | List_Name |


[List_Name] NVARCHAR(128) NULL Default ([dbo].[Users] ([UserName]) + (' List')),

Or is it better to just add data-annotations to it in view and controller part? 还是在视图和控制器部分中向其添加数据注释会更好? If so, how would I do that in the controller by-chance? 如果是这样,我将如何在控制器附带机会中做到这一点?

I'm just diving into creating something and would like to know what a professional would do. 我只是想创造一些东西,并且想知道专业人士会做什么。

Sorry, in Sql Server the default must be a constant expression - use an insert trigger if you want something more complex. 抱歉,在Sql Server中,默认值必须是一个常量表达式-如果您需要更复杂的内容,请使用插入触发器。 Alternatively, when you insert the row, why not calculate the value for List_Name and use that in the insert? 或者,当您插入行时,为什么不计算List_Name的值并在插入中使用它呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM