您的位置:首页 > 数据库

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your

2015-04-28 10:47 585 查看
mysql> create table country(  

    ->      country_id smallint unsigned not null auto_increment,  

    ->      country varchar(50) not null,  

    ->       last_update timestamp not null default current_timestamp on update current_timestamp,  

    ->       primary key(country_id)  

    ->      )engine=innodb default charset=utf8;  

Query OK, 0 rows affected (0.29 sec)  

  

mysql> create table city(  

    ->      city_id smallint unsigned not null auto_inc
4000
rement,  

    ->      city varchar(50) not null,  

    ->      country_id smallint unsigned not null,  

    ->      last_update timestamp not null default current_timestamp on update current_timestamp,  

    ->      primary key(city_id),  

    ->      key idx_fk_country_id(country_id),  

    ->      constraint 'fk_city_country' foreign key(country_id) references country(country_id) on delete restrict on update cascade  

    -> )  

    ->     engine=innodb default charset=utf8;  

ERROR 1064 (42000): 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 ''fk_city_country' foreign key(country_id) references country(country_id) on dele' at line 8  

经过测试,问题出在constraint 'fk_city_country' ,这貌似是oracle的写法,MySQL中不需要这个.

看去掉之后,执行成功.

 

mysql>   

mysql> create table city(  

    ->      city_id smallint unsigned not null auto_increment,  

    ->      city varchar(50) not null,  

    ->      country_id smallint unsigned not null,  

    ->      last_update timestamp not null default current_timestamp on update current_timestamp,  

    ->      primary key(city_id),  

    ->      key idx_fk_country_id(country_id),  

    ->      foreign key(country_id) references country(country_id) on delete restrict on update cascade  

    -> )  

    ->     engine=innodb default charset=utf8;  

Query OK, 0 rows affected (0.20 sec)  

  

mysql> show create table city;  

+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  

| Table | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |  

+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  

| city  | CREATE TABLE `city` (  

  `city_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,  

  `city` varchar(50) NOT NULL,  

  `country_id` smallint(5) unsigned NOT NULL,  

  `last_update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  

  PRIMARY KEY (`city_id`),  

  KEY `idx_fk_country_id` (`country_id`),  

  CONSTRAINT `city_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `country` (`country_id`) ON UPDATE CASCADE  

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |  

+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  

1 row in set (0.03 sec)  

  

mysql>   

 看到这里,发现create table 语句中可以定义CONSTRAINT `city_ibfk_1`啊,仔细想想错误的create 语句与执行成功之后的show create语句之间的差别不就是

CONSTRAINT `city_ibfk_1` 和 CONSTRAINT 'city_ibfk_1'  一个'一个`,看到这里恍然大悟,`是分隔符加在外键名称上是可以的,而且定义外键名字不需要加单引号和双引号,直接命名即可,所以加了'的会报错!

不信再试试看,不加`也不加'试试看效果:

 

mysql> create table city4(  

    ->      city_id smallint unsigned not null auto_increment,  

    ->      city varchar(50) not null,  

    ->      country_id smallint unsigned not null,  

    ->      last_update timestamp not null default current_timestamp on update current_timestamp,  

    ->      primary key(city_id),  

    ->      key idx_fk_country_id4(country_id),  

    ->      CONSTRAINT city1_ibfk_4 foreign key(country_id) references country(country_id) on update cascade  

    -> )  

    ->     engine=innodb default charset=utf8;  

Query OK, 0 rows affected (0.13 sec)  

看到了,搞定成功了,所以在define外键的时候,外键名称不可以添加单引号和双引号。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐