If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Naming columns with numbers

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-14-10, 11:57
freelance_84 freelance_84 is offline
Registered User
 
Join Date: Apr 2010
Posts: 1
Naming columns with numbers

Hello,

I am pretty new to MySQL and the only previous database use I have had is Access way back in school, so apologise if this is a seriously silly question.

I have created a table named mq1. Within which I have created 11 fields/columns: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, & 10.

How do I get all the information from column named '0'?

I tried the standard querying from the tutorial books:
SELECT 0 FROM mq1 (column 0 from mq1 actually contains one entry: "hello" but this query doesn't return "hello", it returns '0'.)

If I rename column '0' to 'a' and try the query again:
SELECT a FROM mq1 (it returns "hello", which is what i want if I name the column 0)


Do I need to escape the 0 somehow as it is a number? Or is it just not possible to name columns by numbers?

Any help would be very much appreciated here. Thanks,

John
Reply With Quote
  #2 (permalink)  
Old 04-14-10, 12:07
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I'd just recreate your table with proper named fields that explain perfectly what data they hold. You can also rename fields using the alter table command.
__________________
Mike
Reply With Quote
  #3 (permalink)  
Old 04-14-10, 12:59
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 782
You should definitely follow Mike's advice about proper naming of objects/attributes. Though to answer your first question try:
SELECT "0" ....
Reply With Quote
  #4 (permalink)  
Old 04-14-10, 17:08
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Naming fields with proper names will make it easier for you to develop later on. However, that is not to say what you are looking for cannot be done. Try the following

CREATE TABLE tablename
(`0` VARCHAR(100), ....);

It also means that when you are writing your SELECT statements that you will need to include the ` as follows:

SELECT `0` FROM tablename;
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On