如何将具有默认值的列添加到SQL Server 2000/SQL Server 2005中的现有表中?
1> 小智..:
句法:
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
WITH VALUES
例:
ALTER TABLE SomeTable
ADD SomeCol Bit NULL --Or NOT NULL.
CONSTRAINT D_SomeTable_SomeCol --When Omitted a Default-Constraint Name is autogenerated.
DEFAULT (0)--Optional Default-Constraint.
WITH VALUES --Add if Column is Nullable and you want the Default Value for Existing Records.
ALTER TABLE MyTable
ADD MyNewColumn INT NOT NULL DEFAULT 0
8> JerryOL..:
使用:
-- Add a column with a default DateTime
-- to capture when each record is added.
ALTER TABLE myTableName
ADD RecordAddedDate smalldatetime NULL DEFAULT(GetDate())
GO
9> Gabriel L...:
如果要添加多个列,可以这样做,例如:
ALTER TABLE YourTable
ADD Column1 INT NOT NULL DEFAULT 0,
Column2 INT NOT NULL DEFAULT 1,
Column3 VARCHAR(50) DEFAULT 'Hello'
GO
-------------------------------------------------------------------------
-- Drop COLUMN
-- Name of Column: Column_EmployeeName
-- Name of Table: table_Emplyee
--------------------------------------------------------------------------
IF EXISTS (
SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'table_Emplyee'
AND COLUMN_NAME = 'Column_EmployeeName'
)
BEGIN
IF EXISTS ( SELECT 1
FROM sys.default_constraints
WHERE object_id = OBJECT_ID('[dbo].[DF_table_Emplyee_Column_EmployeeName]')
AND parent_object_id = OBJECT_ID('[dbo].[table_Emplyee]')
)
BEGIN
------ DROP Contraint
ALTER TABLE [dbo].[table_Emplyee] DROP CONSTRAINT [DF_table_Emplyee_Column_EmployeeName]
PRINT '[DF_table_Emplyee_Column_EmployeeName] was dropped'
END
-- ----- DROP Column -----------------------------------------------------------------
ALTER TABLE [dbo].table_Emplyee
DROP COLUMN Column_EmployeeName
PRINT 'Column Column_EmployeeName in images table was dropped'
END
--------------------------------------------------------------------------
-- ADD COLUMN Column_EmployeeName IN table_Emplyee table
--------------------------------------------------------------------------
IF NOT EXISTS (
SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'table_Emplyee'
AND COLUMN_NAME = 'Column_EmployeeName'
)
BEGIN
----- ADD Column & Contraint
ALTER TABLE dbo.table_Emplyee
ADD Column_EmployeeName BIT NOT NULL
CONSTRAINT [DF_table_Emplyee_Column_EmployeeName] DEFAULT (0)
PRINT 'Column [DF_table_Emplyee_Column_EmployeeName] in table_Emplyee table was Added'
PRINT 'Contraint [DF_table_Emplyee_Column_EmployeeName] was Added'
END
GO
这两种方法是使用默认值将列添加到现有数据库表.
14> Christo..:
或者,您可以添加默认值而无需明确命名约束:
ALTER TABLE [schema].[tablename] ADD DEFAULT ((0)) FOR [columnname]
如果在创建此约束时遇到现有默认约束的问题,则可以通过以下方式删除它们:
alter table [schema].[tablename] drop constraint [constraintname]
15> Tony L...:
这也可以在SSMS GUI中完成.我在下面显示默认日期,但当然默认值可以是任何值.
将您的表放在设计视图中(右键单击object explorer-> Design中的表)
向表中添加一列(如果已存在,则单击要更新的列)
在下面的列属性中,在默认值或绑定字段中输入(getdate())或abc或0任何值,如下图所示:
16> Benjamin Aut..:
ALTER TABLE ADD ColumnName {Column_Type} Constraint
MSDN文章ALTER TABLE(Transact-SQL)具有所有alter table语法.
17> 小智..:
例:
ALTER TABLE [Employees] ADD Seniority int not null default 0 GO
18> 小智..:
例:
ALTER TABLE tes
ADD ssd NUMBER DEFAULT '0';
19> Laxmi..:
首先创建一个名为student的表:
CREATE TABLE STUDENT (STUDENT_ID INT NOT NULL)
添加一列:
ALTER TABLE STUDENT
ADD STUDENT_NAME INT NOT NULL DEFAULT(0)
SELECT *
FROM STUDENT
将创建该表,并使用默认值将列添加到现有表中.
20> Jakir Hossai..:
试试这个
ALTER TABLE Product
ADD ProductID INT NOT NULL DEFAULT(1)
GO
21> 小智..:
SQL Server +更改表+添加列+默认值uniqueidentifier
ALTER TABLE Product
ADD ReferenceID uniqueidentifier not null
default (cast(cast(0 as binary) as uniqueidentifier))
22> Jeevan Ghart..:
IF NOT EXISTS (
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME ='TABLENAME' AND COLUMN_NAME = 'COLUMNNAME'
)
BEGIN
ALTER TABLE TABLENAME ADD COLUMNNAME Nvarchar(MAX) Not Null default
END
ALTER TABLE {schemaName}.{tableName}
ADD {columnName} {datatype} NULL
CONSTRAINT {constraintName} DEFAULT {DefaultValue}
UPDATE {schemaName}.{tableName}
SET {columnName} = {DefaultValue}
WHERE {columName} IS NULL
ALTER TABLE {schemaName}.{tableName}
ALTER COLUMN {columnName} {datatype} NOT NULL
WHILE 1=1
BEGIN
UPDATE TOP (1000000) {schemaName}.{tableName}
SET {columnName} = {DefaultValue}
WHERE {columName} IS NULL
IF @@ROWCOUNT < 1000000
BREAK;
END
24> wild coder..:
--Adding Value with Default Value
ALTER TABLE TestTable
ADD ThirdCol INT NOT NULL DEFAULT(0)
GO
这并没有为多年前已有的答案增加任何价值.
25> Chirag Thaka..:
向表中添加新列:
ALTER TABLE [table]
ADD Column1 Datatype
例如,
ALTER TABLE [test]
ADD ID Int
如果用户想要使其自动递增,则:
ALTER TABLE [test]
ADD ID Int IDENTITY(1,1) NOT NULL
26> 小智..:
这可以通过以下代码完成.
CREATE TABLE TestTable
(FirstCol INT NOT NULL)
GO
------------------------------
-- Option 1
------------------------------
-- Adding New Column
ALTER TABLE TestTable
ADD SecondCol INT
GO
-- Updating it with Default
UPDATE TestTable
SET SecondCol = 0
GO
-- Alter
ALTER TABLE TestTable
ALTER COLUMN SecondCol INT NOT NULL
GO
27> 小智..:
这适用于SQL Server:
ALTER TABLE TableName
ADD ColumnName (type) -- NULL OR NOT NULL
DEFAULT (default value)
WITH VALUES
例:
ALTER TABLE Activities
ADD status int NOT NULL DEFAULT (0)
WITH VALUES
如果要添加约束,则:
ALTER TABLE Table_1
ADD row3 int NOT NULL
CONSTRAINT CONSTRAINT_NAME DEFAULT (0)
WITH VALUES
与几年前已经存在的答案相比,这没有任何价值。
28> Sandeep Kuma..:
ALTER TABLE tbl_table ADD int_column int NOT NULL DEFAULT(0)
从此查询中,您可以添加一个数据类型为整数的列,其默认值为0.
29> usefulBee..:
如果默认值为Null,则:
在SQL Server中,打开目标表的树
右键单击"列"==> New Column
键入列名称,Select Type然后选中"允许空值"复选框
从菜单栏中单击 Save
完成!
30> gdmanandamoh..:
好吧,我现在对我以前的答案进行了一些修改.我注意到没有提到的答案IF NOT EXISTS.所以我将提供一个新的解决方案,因为我遇到了一些改变表格的问题.
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.columns WHERE table_name = 'TaskSheet' AND column_name = 'IsBilledToClient')
BEGIN
ALTER TABLE dbo.TaskSheet ADD
IsBilledToClient bit NOT NULL DEFAULT ((1))
END
GO