在下表中,我希望full_name
列将默认值作为first_name
,middle_name
&中值的串联。 last_name
列。
CREATE TABLE ms_employee
(
emp_no int unique,
first_name varchar(100) not null,
middle_name varchar(100) not null,
last_name varchar(100) not null,
full_name varchar(100)
)
以下代码会出现错误
ALTER TABLE ms_employee ADD CONSTRAINT [cons_1] DEFAULT ([first_name]+' '+[middle_name]+' '+[last_name]) FOR [emp_name]
The name "first_name" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
所以,它不能使用Constraint
!
如何使用Constraint
来实现它?或任何其他方式也将不胜感激!
答案 0 :(得分:0)
您可以将其更改为Computed列。
ALTER TABLE ms_employee DROP COLUMN [full_name];
GO
ALTER TABLE ms_employee ADD [full_name]
AS ([first_name] + ' ' + [middle_name] + ' ' + [last_name])