[英]In SQL, How to add values after add a new column in the existing table?
I created a table and inserted 3 rows.我创建了一个表并插入了 3 行。 Then I added a new column using
alter
.然后我使用
alter
添加了一个新列。 How can I add values to the column without using any null values?如何在不使用任何空值的情况下向列添加值?
Two solutions. 两种解决方案
this: 这个:
ALTER TABLE YourTable
ADD YourNewColumn INT NOT NULL
DEFAULT 10
WITH VALUES;
null
values first. null
值的列。 Then update all rows to enter the values you want. Like so: 像这样:
ALTER TABLE YourTable
ADD YourNewColumn INT NULL;
UPDATE YourTable SET YourNewColumn = 10; -- Or some more complex expression
Then, if you need to, alter the column to make it not null
: 然后,如果需要,请更改列以使其
not null
:
ALTER TABLE YourTable ALTER COLUMN YourNewColumn NOT NULL;
我认为下面的SQL对你有用
update table_name set newly_added_column_name = value;
Why don't you use UPDATE statement: 为什么不使用UPDATE语句:
UPDATE tablename SET column=value <WHERE ...>
WHERE is optional. WHERE是可选的。 For instance in T-SQL for table:
例如在T-SQL for table中:
I can update column NewTestColumn by this statement: 我可以通过以下语句更新列NewTestColumn:
UPDATE [dbo].[Table] SET [NewTestColumn] = 'Some value'
Suppose you have a Employee table with these columns Employee_ID, Emp_Name,Emp_Email initially. 假设您最初有一个Employee表,其中包含Employee_ID,Emp_Name,Emp_Email这些列。 Later you decide to add Emp_Department column to this table.
稍后您决定将Emp_Department列添加到此表中。 To enter values to this column, you can use the following query :
要为此列输入值,可以使用以下查询:
Update *Table_Name* set *NewlyAddedColumnName*=Value where *Columname(primary key column)*=value
Example update TblEmployee set Emp_Department='Marketing' where Emp_ID='101'
示例
update TblEmployee set Emp_Department='Marketing' where Emp_ID='101'
update table_name
set new_column=value
假设emp是表,Comm是新列,则触发下面的查询。
update emp set Comm=5000
Update table table_name set column_name = value where 'condition';
我更喜欢使用p.key
列来获得最佳结果。
For Microsoft SQL :对于 Microsoft SQL:
UPDATE TABLE_NAME SET COLUMN_NAME=10;更新 TABLE_NAME SET COLUMN_NAME=10;
here 10 means it will set all values by default to 10此处 10 表示默认情况下会将所有值设置为 10
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.