在SQL中,如何在现有表中添加新列后添加值? [英] In SQL, How to add values after add a new column in the existing table?

查看:218
本文介绍了在SQL中,如何在现有表中添加新列后添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个表格并插入了3行。然后,我使用 alter 添加了一个新列。如何在不使用任何空值的情况下将值添加到列中?

I created a table and inserted 3 rows. Then I added a new column using alter. How can I add values to the column without using any null values?

推荐答案

两种解决方案。


  1. 为列提供默认值。此值最初将用于所有现有行。确切的语法取决于您的数据库,但是通常看起来像..

this:

ALTER TABLE YourTable
ADD YourNewColumn INT NOT NULL
DEFAULT 10 
WITH VALUES;




  1. 使用 null 值。然后更新所有行以输入所需的值。

  1. Add the column with null values first. Then update all rows to enter the values you want.

像这样:

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:

ALTER TABLE YourTable ALTER COLUMN YourNewColumn NOT NULL;

这篇关于在SQL中,如何在现有表中添加新列后添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆