Wednesday, March 26, 2014

MSSQL: Add a new column to table

ALTER TABLE <table name> 
ADD <new column name> <data type> NOT NULL
GO

ALTER TABLE <table name> 
ADD CONSTRAINT <constraint name> DEFAULT <default value> FOR <new column name>
GO
Add foreign Key
ALTER TABLE <Table_Name> ADD FOREIGN KEY(<ColumnName>) REFERENCES <Primary_Table_Name>(<Primary_column_Name>)
ALTER TABLE dbo.AssetInventory ADD FOREIGN KEY(RegionID) REFERENCES Region(RegionID)

Example:

ALTER TABLE dbo.AssetInventory ADD ASSET_PARENT int NULL

No comments:

Post a Comment