我正在读一本书来学习cakephp。我有这个表在mysql中创建,但我一直有错误
CREATE TABLE IF NOT EXISTS 'categories'(
'id' int(10) NOT NULL AUTO_INCREMENT,
'parent_id' int(11) NOT NULL DEFAULT '0',
'name' varchar(50) character NOT NULL,
'description' varchar(200) character NOT NULL,
'image' varchar(255) character NOT NULL,
PRIMARY KEY ('id'),
KEY 'cat_parent_id' ('parent_id'),
KEY 'cat_name' ('name')
);
INSERT INTO 'categories' ('id', 'parent_id', 'name', 'description', 'image') VALUES
(17, 0, 'Jazz', 'Everything from 1890s', ''),
(12, 0, 'Classical', 'From Medieval to Contemporary', ''),
(13, 17, 'Dizzy Gillepsie', 'The Trumpeter Master', ''),
(14, 12, 'Mozart', 'The Old Favourite', '');
第1行的错误???:
#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' int(10) NOT NULL AUTO_INCREMENT,
'parent_id' int(11) NOT NU' at line 1
答案 0 :(得分:2)
单引号用于字符串文字,而不是标识符(列名或表名)。删除它们。此外,我不确定character
应该做什么,除非您打算指定character set(CHARACTER SET <charset_name>
):
CREATE TABLE IF NOT EXISTS categories(
id int(10) NOT NULL AUTO_INCREMENT,
parent_id int(11) NOT NULL DEFAULT '0',
name varchar(50) NOT NULL,
description varchar(200) NOT NULL,
image varchar(255) NOT NULL,
PRIMARY KEY (id),
KEY cat_parent_id (parent_id),
KEY cat_name (name)
);
如果需要在MySQL中引用标识符,请使用反引号`。这有助于轻松识别哪些是表/列名称,哪些是关键字。一些开发人员还坚持在各地使用它们作为一种风格规则:
CREATE TABLE IF NOT EXISTS `categories`(
`id` int(10) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) NOT NULL DEFAULT '0',
`name` varchar(50) NOT NULL,
`description` varchar(200) NOT NULL,
`image` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `cat_parent_id` (`parent_id`),
KEY `cat_name` (`name`)
);
使用保留字作为列名称时,必须使用引号(不推荐):
CREATE TABLE IF NOT EXISTS foobar(
id int(10) NOT NULL AUTO_INCREMENT,
`table` int(11) NOT NULL,
`select` int(11) NOT NULL
);