kanayaki
kanayaki

Reputation:

Mysql create table query problems?

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

Upvotes: 0

Views: 250

Answers (3)

Zed
Zed

Reputation: 57658

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

PS: To concatenate elsewhere, use concat().

Upvotes: 0

knittl
knittl

Reputation: 265231

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

Upvotes: 1

Svetlozar Angelov
Svetlozar Angelov

Reputation: 21660

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.

Upvotes: 3

Related Questions