Blkrazor50
Blkrazor50

Reputation: 21

sql commands to add a field to a table

I am trying to use SQL commands to add a field called Birthday to a Customers Table. My command is

ALTER TABLE Customers ADD COLUMN Birthday

I keep getting a syntax error in the field definition. What am I doing wrong?

Upvotes: 2

Views: 231

Answers (5)

ziad mansour
ziad mansour

Reputation: 349

alter TABLE Customers 
ADD Birthday DATETIME
GO
update Customers set Birthday = GETDATE()
GO

alter TABLE Customers
alter column Birthday DATETIME NOT NULL
GO

alter table Customers add constraint df_Customers_note default (getDATE()) for Birthday
GO

Upvotes: 0

James
James

Reputation: 3928

You need to specify a type for the Birthday field. The syntax may depend on what the type is and which version of SQL you are using (you haven't specified either). Assuming you're using Microsoft SQL, and given that it's a birthday (and you don't need a time), and there are likely customers for whom you won't have a birthday, I'd recommend:

ALTER TABLE Customers 
ADD COLUMN Birthday DATE CONSTRAINT Customers_Birthday_Default DEFAULT NULL

Upvotes: 0

japzdivino
japzdivino

Reputation: 1746

Additional column Birthday must have it's datatype, such as DATE, DATETIME etc.

ALTER TABLE Customers 
ADD COLUMN Birthday DATETIME

Upvotes: 1

KrishnaDhungana
KrishnaDhungana

Reputation: 2684

You need to specity type for your column. Assuming Birthday of type DATETIME and NOT NULL, The syntax for adding column is:

ALTER TABLE  Customers
ADD Birthday DATETIME NOT NULL

Upvotes: 1

dieuvn3b
dieuvn3b

Reputation: 6114

The query need datatype for birthday, Ex:

 ALTER TABLE Customers ADD COLUMN Birthday datetime

Upvotes: 2

Related Questions