views:

90

answers:

3

I want to create a table, which name something like :

name_thisyear_thisweekofyear, e.g -> name_2009_40

I tried this query:

CREATE TABLE IF NOT EXISTS name_YEAR(NOW())_WEEKOFYEAR(NOW())(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, phone_no int, created datetime, deleted datetime)

It doesnt work. Please help

Error Message:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOW())_WEEKOFYEAR(NOW())(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, userid int,' at line 1

+3  A: 

You can't do that... You must specify table name, a function can't be executed there

An identifier may be quoted or unquoted. If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it. The set of alphanumeric characters from the current character set, “_”, and “$” are not special.

Svetlozar Angelov
so, how to create a table with that name format? name_'year'_'week'??
As knittl said generate the name in the language you use and pass it to mysql...
Svetlozar Angelov
Is this the only way??
+1  A: 

this can't work, because parantheses are not allowed in table names (unless you escape them properly using backticks)

mysql sees your query as:

CREATE TABLE IF NOT EXISTS
name_YEAR(
    NOW()      # this is causing an error already
)
/*GARBAGE: */_WEEKOFYEAR(NOW())(id int NOT NULL AUTO_INCREMENT PRIMARY KEY, phone_no int, created datetime, deleted datetime)

i don't know if it's possible at all, to create parts of table names dynamically and concatenating the parts together in the end.

the easy way would be to calculate the name in php and create the query out of php

knittl
I tried backslash, cant work. How to make it works? tbale name "name_year_week"?
create the table name in another language!
knittl
@knittl: is this the only way??
as far as i know: yes, and all the others share the same opinion …sql wasn't designed to allow such things
knittl
Thanks knittl. I am new to mysql :)
i'm wondering why you want to do such a thing at all :)
knittl
A: 

You can't do that. Table and column names must be given as an indentifier.

PS: To concatenate elsewhere, use concat().

Zed
not quite correct, tables names must—if necessary—quoted by backticks (\`), not by quotation marks (") like strings. but yes, they must be fixed text
knittl
thanks, fixed.
Zed