Can I add a not null column without DEFAULT value

后端 未结 10 1517
Happy的楠姐
Happy的楠姐 2020-12-13 07:56

Can I add a column which is I specify as NOT NULL,I don\'t want to specify the DEFAULT value but MS-SQL 2005 says:

ALTER TABLE only allows columns to

10条回答
  •  無奈伤痛
    2020-12-13 08:45

    @Damien_The_Unbeliever's comment , Is it adding computed column? Neither question nor answer implied anything like that. In case of computed column the error states:

    "Only UNIQUE or PRIMARY KEY constraints can be created on computed columns, while CHECK, FOREIGN KEY, and NOT NULL constraints require that computed columns be persisted"

    OK, if to continue this guessing game, here is my script illustrating the adding of "NOT NULL" column in one "ALTER TABLE" step:

    CREATE TABLE TestInsertComputedColumn 
    (
        FirstName VARCHAR(100),
        LastName CHAR(50)
    );  
    
    insert into TestInsertComputedColumn(FirstName,LastName)
         select 'v', 'gv8';
    select * from TestInsertComputedColumn;
    
    ALTER TABLE TestInsertComputedColumn 
          ADD FullName As FirstName + LastName PERSISTED NOT NULL;
    
    select * from TestInsertComputedColumn;
    --drop TABLE TestInsertComputedColumn;
    

提交回复
热议问题