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

本文详细解析了在MySQL中创建带有外键约束的表格时常见的语法错误,对比了Oracle与MySQL在外键定义上的差异,并提供了正确的外键定义方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL建表语句:

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_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),
-> 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外键的时候,外键名称不可以添加单引号和双引号。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值