SQL Syntax error - can't find the error [closed] - mysql

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
CREATE TABLE 'categories'(
'id' SMALLINT NOT NULL AUTO_INCREMENT,
'category' VARCHAR(30) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE KEY 'category' ('category')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE 'orders' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'user_id' INT UNSIGNED NOT NULL,
'transaction_id' VARCHAR(19) NOT NULL,
'payment_status' VARCHAR(15) NOT NULL,
'payment_amount' DECIMAL(6,2) UNSIGNED NOT NULL,
'payment_date_time' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
KEY 'user_id' ('user_id')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE 'pages' (
'id' MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
'category_id' SMALLINT UNSIGNED NOT NULL,
'title' VARCHAR(100) NOT NULL,
'description' TINYTEXT NOT NULL,
'content' LONGTEXT NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
KEY 'category_id' ('category_id'),
KEY 'creation_date' ('date_created')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE 'pdfs' (
'id' SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
'tmp_name' CHAR(40) NOT NULL,
'title' VARCHAR(100) NOT NULL,
'description' TINYTEXT NOT NULL,
'file_name' VARCHAR(40) NOT NULL,
'size' MEDIUMINT UNSIGNED NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
UNIQUE KEY 'tmp_name' ('tmp_name'),
KEY 'date_created' ('date_created')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE 'users' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'type' ENUM ('member','admin') NOT NULL,
'username' VARCHAR(30) NOT NULL,
'email' VARCHAR(80) NOT NULL,
'pass' VARBINARY(32) NOT NULL,
'first_name' VARCHAR(20) NOT NULL,
'last_name' VARCHAR (40) NOT NULL,
'date_expires' DATE NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
'date_modified' TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY ('id'),
UNIQUE KEY 'username' ('username'),
UNIQUE KEY 'email' ('email')
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
.............................................................................
I cannot find the SQL error here.
MySQL said:
#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 ''categories' ('id' SMALLINT NOT NULL AUTO_INCREMENT, 'category' VARCHAR(30) NOT' at line 1
Please help. I am using MySQL 4.4.x on a shared server.

You're surrounding your table and column names with ', it should be ` (backtick)
CREATE TABLE `categories`
(`id` SMALLINT NOT NULL AUTO_INCREMENT,
`category` VARCHAR(30) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `category` (`category`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Get rid of your single quotes or as #Joachim Isaksson said, use backticks `.
CREATE TABLE categories (id SMALLINT NOT NULL AUTO_INCREMENT, category VARCHAR(30) NOT NULL, PRIMARY KEY (id), UNIQUE KEY(category) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
Also next time, simplify your problem. You have a problem with the first CREATE TABLE statement. There is no need to print everything else. Also some formatting would be nice too :-).

Related

#1072 - Key column 'Name' doesn't exist in table

CREATE TABLE IF NOT EXISTS ban (
UID int(4) NOT NULL,
AdminUID int(4) NOT NULL,
Grund varchar(50) NOT NULL,
Datum varchar(50) NOT NULL,
IP varchar(50) NOT NULL DEFAULT '0',
Serial varchar(50) NOT NULL,
Eintragsdatum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
STime int(11) NOT NULL DEFAULT '0'
,
PRIMARY KEY (UID),
KEY Name (Name),
KEY IP (IP),
KEY Serial (Serial),
KEY STime (STime)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Error:
1072 - Key column 'Name' doesn't exist in table
Where is my fault?
Remove KEY Name (Name) in the code as there is not Name field in the table.
CREATE TABLE IF NOT EXISTS ban (
UID int(4) NOT NULL,
AdminUID int(4) NOT NULL,
Grund varchar(50) NOT NULL,
Datum varchar(50) NOT NULL,
IP varchar(50) NOT NULL DEFAULT '0',
Serial varchar(50) NOT NULL,
Eintragsdatum timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
STime int(11) NOT NULL DEFAULT '0' , PRIMARY KEY (UID), KEY IP (IP), KEY Serial (Serial), KEY STime (STime) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

#1064 You have an error in your SQL syntax;

I'm getting the following error when I try to create a TABLE in SQL.
The version of phpMyAdmin I'm using it 4.2.7.1 which says it's up to date.
Can anyone help? I can't spot an error? I have looked in similar questions relating to error 1064, but no joy
CREATE TABLE 'categories' (
'id' SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
'category' VARCHAR(45) NOT NULL,
PRIMARY KEY ('id'),
UNIQUE INDEX 'category_UNIQUE' ('category' ASC)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE 'orders' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'users_id' INT UNSIGNED NOT NULL,
'transaction_id' VARCHAR(45) NOT NULL,
'payment_status' VARCHAR(45) NOT NULL,
'payment_amount' INT UNSIGNED NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
INDEX 'date_created' ('date_created' ASC),
INDEX 'transaction_id' ('transaction_id' ASC),
CONSTRAINT 'fk_orders_users1' FOREIGN KEY ('id')
REFERENCES 'users' ('id')
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE 'pages' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'categories_id' SMALLINT UNSIGNED NOT NULL,
'title' VARCHAR(100) NOT NULL,
'description' TINYTEXT NOT NULL,
'content' LONGTEXT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
INDEX 'date_created' ('date_created' ASC),
INDEX 'fk_pages_categories_idx' ('categories_id')
REFERENCES 'categories' ('id')
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE 'pdfs' (
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'title' VARCHAR (100) NOT NULL,
'description' TINYTEXT NOT NULL,
'tmp_name' CHAR(63) NOT NULL,
'file_name' VARCHAR(100) NOT NULL,
'size' MEDIUMINT UNSIGNED NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY ('id')
UNIQUE INDEX 'tmp_name_UNIQUE' ('tmp_name' ASC),
INDEX 'date_created' ('date_created' ASC)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE ('users')
'id' INT UNSIGNED NOT NULL AUTO_INCREMENT,
'type' ENUM('member','admin') NOT NULL DEFAULT 'member'
'username' VARCHAR(45) NOT NULL,
'email' VARCHAR(80) NOT NULL,
'pass' VARCHAR(255) NOT NULL,
'first_name' VARCHAR (45) NOT NULL,
'last_name' VARCHAR (45) NOT NULL,
'date_created' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
'date_expires' DATE NOT NULL,
'date_modified' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY ('id'),
UNIQUE INDEX 'username_UNIQUE' ('username' ASC),
UNIQUE INDEX 'email_UNIQUE' ('email' ASC),
INDEX 'login' ('email' ASC, 'pass' ASC)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
MySQL said: Documentation
#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 ''categories' (
'id' SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
'catego' at line 1
Remove single quote characters from field names
You need to use back-ticks ` (next to the 1 key on my keyboard) instead of ' quotes. This works for me:
CREATE TABLE `categories` (
`id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`category` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `category_UNIQUE` (`category` ASC)
) ENGINE = InnoDB DEFAULT CHARSET=utf8;
Replace ' characters that surround table names and field names by `.
There are some more syntax errors that will appear then - for example there are unneeded parentheses around 'users' table.
Also when you create 'orders' table, it has a foreign key on users, but users are declared later.

How to create a table with composite foreign key

I created the master table with the composite primary key.
parent table structure is as follows:
CREATE TABLE `taskcategory` (
`SiteID` int(10) unsigned NOT NULL DEFAULT 1,
`TaskID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`TaskName` varchar(45) DEFAULT '',
`TaskDescription` varchar(45) DEFAULT '',
`IsInbuild` int(11) DEFAULT '1',
PRIMARY KEY (`TaskID`,`SiteID`)
);
when i am trying to create the table with foreign key with the above parent table reference i am getting 'can't create table error no 150' error . help me to do that.
child table structure as follows:
CREATE TABLE taskdetails (`SiteID` int(10) unsigned NOT NULL DEFAULT '1',
`TaskID` int(10) unsigned NOT NULL DEFAULT '0',
`SubtaskID` int(10) unsigned NOT NULL,
`ScriptName` varchar(255) DEFAULT '',
`FunctionName` varchar(255) DEFAULT '',
`ButtonName` varchar(255) DEFAULT '',
`IsInbuild` int(10) unsigned DEFAULT '1',
`Description` varchar(255) DEFAULT '',
PRIMARY KEY (`SubtaskID`,`TaskID`,`SiteID`),
INDEX (siteid, taskid),
FOREIGN KEY (siteid, taskid)
REFERENCES taskcategory(siteid, taskid)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=INNODB;
help me to resolve it.
From the manual:
InnoDB requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan. In the
referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order. Such an
index is created on the referencing table automatically if it does not
exist. This index might be silently dropped later, if you create
another index that can be used to enforce the foreign key constraint.
index_name, if given, is used as described previously.
So when you add an index in the parent table it works (yes, I tested it):
CREATE TABLE `taskcategory` (
`SiteID` int(10) unsigned NOT NULL DEFAULT 1,
`TaskID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`TaskName` varchar(45) DEFAULT '',
`TaskDescription` varchar(45) DEFAULT '',
`IsInbuild` int(11) DEFAULT '1',
PRIMARY KEY (`TaskID`,`SiteID`)
, INDEX (SiteID, TaskID)
) ENGINE=INNODB;
CREATE TABLE taskdetails (`SiteID` int(10) unsigned NOT NULL DEFAULT '1',
`TaskID` int(10) unsigned NOT NULL DEFAULT '0',
`SubtaskID` int(10) unsigned NOT NULL,
`ScriptName` varchar(255) DEFAULT '',
`FunctionName` varchar(255) DEFAULT '',
`ButtonName` varchar(255) DEFAULT '',
`IsInbuild` int(10) unsigned DEFAULT '1',
`Description` varchar(255) DEFAULT '',
PRIMARY KEY (`SubtaskID`,`TaskID`,`SiteID`)
,INDEX (SiteID, TaskID)
,FOREIGN KEY (SiteID, TaskID)
REFERENCES taskcategory(SiteID, TaskID)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=INNODB;
You have a primary key on those columns already (which means there's an implicit index), but the order of the columns is important!
Your table definition of taskcategory lacks the ENGINE=InnoDB clause, and probably this is not your system's default. Foreign key relations can only be set up between InnoDB tables.

Mysql Table Key

Can anyone tell me what is the meaning of KEY fk_pickup_method (pickup_method_id),
KEY fk_deliv_method (delivery_method_id) lines. As pickup_method and deliv_method are not the tables. So what is the use of these lines.
CREATE TABLE `test` (
`idTest` int(11) NOT NULL AUTO_INCREMENT,
`Name` mediumtext NOT NULL,
`email` varchar(45) NOT NULL,
`password` varchar(45) NOT NULL,
`firstname` varchar(45) NOT NULL,
`lastname` varchar(45) NOT NULL,
`phone` bigint(20) unsigned NOT NULL,
`address_street` varchar(128) NOT NULL,
`address_apt` varchar(45) DEFAULT NULL,
`address_city` varchar(128) NOT NULL,
`address_state` varchar(2) NOT NULL,
`address_zip` int(11) NOT NULL,
`fax` bigint(20) unsigned DEFAULT NULL,
`account_balance` float NOT NULL DEFAULT '0',
`delivery_radius` float DEFAULT NULL,
`pickup_method_id` int(11) NOT NULL DEFAULT '0',
`delivery_method_id` int(11) NOT NULL,
`is_deleted` tinyint(1) NOT NULL DEFAULT '0',
`lat` float NOT NULL DEFAULT '0',
`lng` float NOT NULL DEFAULT '0',
`timezone` varchar(45) NOT NULL,
PRIMARY KEY (`idTest`),
UNIQUE KEY `phone_UNIQUE` (`phone`),
KEY `fk_pickup_method` (`pickup_method_id`),
KEY `fk_deliv_method` (`delivery_method_id`)
)
They're INDEXes on the columns in the ()'s. But the fields are not constrained to being UNIQUE.
Look for {INDEX|KEY} in this MySQL document link.
Line
KEY fk_pickup_method (pickup_method_id)
defines an index named fk_pickup_method on table column pickup_method_id.
When you run EXPLAIN, under possible_keys column you'll see the name of the index.
The usual practice is to call the key as the column it indexes. That's the default behaviour if you don't specify the key name.
These are indexes on the fields pickup_method_id and delivery_method_id.
How mysql uses indexes : http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html
The name fk_ points to the intention of the creator to create a foreign key. But this is not a foreign key, and won't care about referential integrity for you.
Foreign keys in mysql (innodb) : http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
My guess is that these were intended to be FOREIGN KEY constraints and reference other tables.
InnoDB engine creates an index (if there isn't one) when a FOREIGN KEY constraint is defined.
MyISAM engine ignores FOREIGN KEY constraints but it still creates the index. Example:
CREATE TABLE test
( test_id int NOT NULL AUTO_INCREMENT,
delivery_method_id int NOT NULL,
PRIMARY KEY (test_id),
FOREIGN KEY fk_deliv_method (delivery_method_id)
REFERENCES delivery_method(delivery_method_id)
) ENGINE=MyISAM
DEFAULT CHARSET=utf8 ;
And then:
SHOW CREATE TABLE test ;
CREATE TABLE `test` (
`test_id` int(11) NOT NULL AUTO_INCREMENT,
`delivery_method_id` int(11) NOT NULL,
PRIMARY KEY (`test_id`),
KEY `fk_deliv_method` (`delivery_method_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8

phpMyadmin - Error #1064

When trying to create this table in my db, I get an error:
#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 'unsigned NOT NULL default '0',
PRIMARY KEY (reminder_id), KEY
reminder_id (rem' at line 5
CREATE TABLE reminder_events (
reminder_id bigint(20) unsigned NOT NULL auto_increment,
reminder_name varchar(255) NOT NULL default '',
reminder_desc text,
reminder_date varchar(8) unsigned NOT NULL default '0',
PRIMARY KEY (reminder_id),
KEY reminder_id (reminder_id)
) TYPE=MyISAM;
Can anyone see what I'm doing wrong?
The problem is that you're trying to define a text field (varchar) as being unsigned, which is something that can only apply to a numerical field.
i.e.: "reminder_date varchar(8) unsigned NOT NULL default '0'," makes no sense.
However, as the field is called "reminder_date", I'm guessing you're attempting to store a date, in which case you really want to use MySQLs DATE field type.
e.g.: "reminder_date DATE NOT NULL,"
Additionally, if you're going to want to search on any of these fields, you should also add some indexes to speed up the searches. So, if you wanted to be able to search on the name and date, you could use:
CREATE TABLE reminder_events (
reminder_id bigint(20) unsigned NOT NULL auto_increment,
reminder_name varchar(255) NOT NULL DEFAULT '',
reminder_desc text,
reminder_date DATE NOT NULL,
PRIMARY KEY (reminder_id),
KEY reminder_id (reminder_id),
KEY reminder_name (reminder_name),
KEY reminder_date (reminder_date)
) TYPE=MyISAM;
Just remove unsigned.
CREATE TABLE reminder_events (
reminder_id bigint(20) unsigned NOT NULL auto_increment,
reminder_name varchar(255) NOT NULL default '',
reminder_desc text,
reminder_date varchar(8) NOT NULL default '0',
PRIMARY KEY (reminder_id),
KEY reminder_id (reminder_id)
) TYPE=MyISAM;
Write ENGINE instead of TYPE
CREATE TABLE reminder_events (
reminder_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
reminder_name VARCHAR(255) NOT NULL DEFAULT '',
reminder_desc TEXT,
reminder_date VARCHAR(8) NOT NULL DEFAULT '0',
PRIMARY KEY (reminder_id),
KEY reminder_id (reminder_id)
) ENGINE=MYISAM;