Tutorials Forums
     Tutorials Videos
        Sign Up Now For FREE
Welcome, Guest
Username Password: Remember me

Adding a Column to a Table
(1 viewing) (1) Guest
SQL SERVER HELP, SQL SERVER TUTORIALS, SQL SERVER PROGRAMMING, SQL SERVER CODE


SQL Server is a relational DMBS written for the Windows platform by Microsoft. SQL Server is a high-end and high-performance solution, for applications that interact with a database. Its use has been increasing because of the number of web applications that feature a data base back end.
  • Page:
  • 1

TOPIC: Adding a Column to a Table

Adding a Column to a Table 04 Feb 2010 14:23 #258

Introduction
To add a new column to a table, follow this formula:

ALTER TABLE TableName
ADD ColumnName Properties


Here is an example:

ALTER TABLE StaffMembers
ADD Address varchar(100) NULL
GO


When this code is executed, a new column named Address, of type varchar, with a limit of 100 characters, and that allows empty entries, would be added to the table named StaffMembers.

To use sample code, first display an empty query window and display the Templates Explorer. Expand the Table node. Under Table, drag Add Column and drop it in the query window. Delete the undesired sections of code and keep only the part that deals with adding a column. Here is an example:
--==========================================================================
-- Add column template
--
-- This template creates a table, then it adds a new column to the table.
--==========================================================================
USE <database, sysname, AdventureWorks>
GO
 
-- Add a new column to the table
ALTER TABLE <schema_name, sysname, dbo>.<table_name, sysname, sample_table>
ADD <new_column_name, sysname, column3>
<new_column_datatype,, datetime>
<new_column_nullability,, NULL>
GO
  • CE
  • OFFLINE
  • Administrator
  • Posts: 197
  • Karma: 78
CodersEngine
  • Page:
  • 1
Time to create page: 0.28 seconds