Reputation: 1498
I get a SQL Sytax Error when I try to execute this code:
CREATE TABLE ? (
ID INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`UUID` VARCHAR(36) NOT NULL
);
I use a PreparedStatement to replace the ? with a String
The Error Message:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an errorin your SQL syntax; check the manual that corresponds to your MySQL serverversion for the right syntax to use near ''95f7ed55-ab3d-46f9-bffe-72bf5780a1ec' (ID INT(255) UNSIGNED AUTO_INCREMENT PRIM' at line 1
Thanks for your help!
Upvotes: 0
Views: 1416
Reputation: 11
Please try following query syntax:
CREATE TABLE table_name (ID INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,UUID VARCHAR(36) NOT NULL);
Upvotes: 1
Reputation: 457
The sign (-) character is not allowed when not using quotes. http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
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. (Exception: A reserved word that follows a period in a qualified name must be an identifier, so it need not be quoted.)
This is executed now:
CREATE TABLE 95f7ed55-ab3d-46f9-bffe-72bf5780a1ec (
ID INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`UUID` VARCHAR(36) NOT NULL
);
But the identifier on the first line needs to be quoted:
CREATE TABLE `95f7ed55-ab3d-46f9-bffe-72bf5780a1ec` (
Maybe something like this will do the trick? (edit: added "IF NOT EXISTS" from Anil Kumar's answer)
CREATE TABLE IF NOT EXISTS `?` (
ID INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`UUID` VARCHAR(36) NOT NULL
);
Upvotes: 1
Reputation: 3752
-- DROP PROCEDURE IF EXISTS createTblDynamically;
DELIMITER //
CREATE PROCEDURE createTblDynamically(tblName VARCHAR(255))
BEGIN
SET @tableName = tblName;
SET @q = CONCAT('
CREATE TABLE IF NOT EXISTS `' , @tableName, '` (
ID INT(255) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`UUID` VARCHAR(36) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8
');
PREPARE stmt FROM @q;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
Upvotes: 0
Reputation: 426
pls try this code.
CREATE TABLE `table_name`(
`ID` INT( 255 ) UNSIGNED AUTO_INCREMENT PRIMARY KEY ,
`UUID` VARCHAR( 36 ) NOT NULL
);
Note: table_name
using this symbol.
Upvotes: 0
Reputation: 27082
Put table name into backticks, it contains -
which has to be escaped.
You used single quotes ('
), which are bad in SQL.
Upvotes: 2