tags:

views:

138

answers:

5

i added a field in sql server 2008 and it put the column name in brackets like this [column2], but the other column names dont have brackets. what do the brackets mean?

the column is [macro-writer]

should i remove the minus sign and replace with underscore?

+5  A: 

Brackets are a form of quoting. It's only necessary if the column name contains spaces or conflicts with a reserved word, but many wizards will just add the brackets for all field names to avoid the logic for deciding whether they are necessary.

Ben Voigt
+2  A: 

Brackets allow you to delimit names in SQL Server. This allows you to do such things as use keywords [count] or include spaces [my column name].

EDIT: For your follow up question, if this is a new column and there's no risk of breaking existing code, then I'd at least recommend replacing the hyphen with an underscore. Our own internal naming standards are to use PascalCase (e.g., MacroWriter) rather than underscores.

Joe Stefanelli
+4  A: 

Columns enclosed in square brackets are usually keywords or contain special characters or spaces.

What specific column name do you have enclosed in brackets?

Bernard
[macro-writer] [macro-writer]
I__
A hyphen is considered a special character.
Bernard
+1  A: 

Brackets allow you to use characters and names which are not allowed like spaces, reserved words and names starting with numbers

invalid my column, 1column col%umn, table

valid [my column], [1column], [col%umn], [table]

of course now you can become really creative :-)

create table [table]([table] varchar(20))

insert [table] values ('table')

select [table] from [table]
SQLMenace
not to mention create table [แสดงבוקרǣ]
nonnb
you will never have a table that has a mix of hebrew and thai font
I__
Well, why not try it and see for yourself ;). Works on SQL 2005, 2008create table [แสดงבוקרǣ]( ID int not null)
nonnb
+1  A: 

See related questions with answers:

I would recommend using underscore instead of dash in identifier names.

Bill Karwin