Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a table, and now I need to add a column to it. How would I go about this?
Posted
Updated 9-May-12 1:18am
v2

Use
ALTER TABLE ADD COLUMN
statement.

Refer this[^] link
 
Share this answer
 
Comments
Maciej Los 9-May-12 12:59pm    
Good answer, my 5!
nagendrathecoder 10-May-12 1:19am    
Thanks :)
Try like this

SQL
CREATE TABLE dbo.Employees
(
  EmployeeID int IDENTITY (1,1) NOT NULL PRIMARY KEY NONCLUSTERED
)
GO

ALTER TABLE dbo.Employees
ADD
    FirstName varchar(50) NULL
    ,LastName varchar(50) NULL
 
Share this answer
 
Comments
Maciej Los 9-May-12 12:59pm    
Good answer, my 5!
If you have already added data into the table, you need to ensure that the column you add takes this into account if the column you want to add is none nullable. As others have said, you need to use the ALTER TABLE command, but as I've said if it can't be NULL, you need to apply a default when you create it. A practical example would look like this:
SQL
ALTER TABLE MyTable
ADD MyColumn DATETIME DEFAULT GETDATE() WITH VALUES NOT NULL
 
Share this answer
 
Comments
Maciej Los 9-May-12 12:59pm    
Good answer, my 5!
Use ALTER TABLE[^] command
or "Modify" from popup menu.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900