日期:2014-05-19  浏览次数:20496 次

创建一个不为空的字段(自认为有点难度)
条件:
    1.数据库里面存在一张表,表中至少有一个不为空的字段,
    2.表中无数据
现在想通过程序或查询分析新建一个字段:如
创建了一张表create   table   ABC(id   int   not   null),然后再添加字段
alter   table   ABC   add   code   varchar   not   null
这样写在程序里或是在查询分析器里执行是要出错了,我不想给它加默认值:
如这样     alter   table   ABC   add   code   varchar   not   null   defalut( ' ')

为什么在企业管理器里面这样创建一张表,在表里面加一个不为空字段,然后保存后退出,再进入添加一个不为空的字段不会报错,想知道企业管理器里面是怎样做到的

------解决方案--------------------
可以這樣,先加入列,使其可以為null,然後修改為不能為null

create table ABC(id int not null)
alter table ABC add code varchar(10) null
alter table ABC Alter Column code varchar(10) null
Select * From ABC
Drop Table ABC