简体   繁体   English

如何在现有表中添加非空列,然后在该列中插入值?

[英]how to add not null column in existing table and then insert values in that column?

how to add not null column in existing table and then insert values in that column ?? 如何在现有表中添加非空列,然后在该列中插入值? in sql... 在sql ...

If you want to add a NOT NULL column you must specify a DEFAULT : 如果要添加NOT NULL列,则必须指定DEFAULT

ALTER TABLE YourTable 
    ADD SomeColumn INT NOT NULL 
    CONSTRAINT DF_YourTable_SomeColumn DEFAULT(0);

Other possibility is to add it with NULL , add your data and alter it to NOT NULL later (see ALTER TABLE) 其他可能性是将其添加为NULL ,添加您的数据并稍后将其更改为NOT NULL (请参阅ALTER TABLE)

EDIT: Your comment about "how to insert values"... 编辑:您对“如何插入值”的评论......

This depends very much in your needs. 这在很大程度上取决于您的需求。 If you want to set all rows to the same value it is: 如果要将所有行设置为相同的值,则为:

UPDATE YourTable SET SomeColumn=0;

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

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