问题描述
我有一个employees表,我想添加第三列,其名称和名字的串联是"FullName".如何做到这一点而又不会丢失前两列中任何一列的任何数据?
I have an employees table and I want to add a third column valued as the concatenation of the first and last name called "FullName". How can I accomplish that without losing any data from either of the first two columns?
推荐答案
快速序言:此答案基于该问题与SQL Server有关的最初不正确的标记.我不再知道它在Oracle SQL Developer上的有效性.
ALTER TABLE Employees ADD FullName AS (FirstName + ' ' + LastName)
尽管实际上我建议您在SELECT
中执行该操作.这在某种程度上是个人喜好,但与存储额外的计算列相比,我倾向于认为在您的最终查询中执行操作会更干净,更具可读性且更易于维护.
Although in practice I'd advise that you do that operation in your SELECT
. That's somewhat personal preference, but I tend to think doing things in your end queries is a bit cleaner, more readable, and easier to maintain than storing extra, calculated columns.
最终找到了答案,并由OP对此帖子进行了评论.以下是适用于Oracle Sql数据库的语法.
This was eventually found as the answer, and listed by the OP as a comment on this post. The following is appropriate syntax for Oracle Sql Database.
ALTER TABLE emps MODIFY (FULL_NAME VARCHAR2(50) GENERATED ALWAYS AS (first_name || ' ' || last_name) VIRTUAL);
这篇关于添加一个代表其他两个Varchar列的串联的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!