在MSSQL 2014中,我想使用更新语法,该语法添加具有默认值的新列。这有可能吗?
答案 0 :(得分:4)
类似以下内容:
alter table myTable add myNewColumn nvarchar(20) default 'myDefaultValue' not null
答案 1 :(得分:2)
这是一个完整的可复制示例。
CREATE TABLE employees
( employee_id INT NOT NULL,
last_name VARCHAR(50) NOT NULL,
first_name VARCHAR(50) NOT NULL,
salary MONEY
);
INSERT INTO employees
VALUES (1, 'Miller', 'Peter', 80000);
INSERT INTO employees
VALUES (2, 'Myer', 'Joana', 90000);
SELECT * FROM employees;
employee_id last_name first_name salary
1 Miller Peter 80000
2 Myer Joana 90000
ALTER TABLE employees
ADD email VARCHAR(50) DEFAULT 'info@company.com' NOT NULL;
SELECT * FROM employees;
employee_id last_name first_name salary email
1 Miller Peter 80000 info@company.com
2 Myer Joana 90000 info@company.com
答案 2 :(得分:1)
尝试一下:-
CREATE TABLE employees
( employee_id INT NOT NULL DEFAULT 0,
last_name VARCHAR(50) NOT NULL DEFAULT 'jon',
first_name VARCHAR(50) NOT NULL DEFAULT 'jona',
salary MONEY DEFAULT 0
);
ALTER TABLE #employees CONSTRAINT employees_employee_id 50 FOR employee_id ;