今天在用mysql创建一个新表的时候一直报错:
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 'INOUT varchar(20))' at line 2
一开始以为是语句代码的问题,检查不出问题才想到可能用到数据库的保留关键字INOUT了,查了一下才知道.
我想问一下,如果非要用这个INOUT 应该怎么办呢?
1
sivacohan 2015-04-30 00:20:38 +08:00 via Android
推荐你看,MySQL必知必会。
这个问题可以通过加引号解决 |
2
raincious 2015-04-30 00:28:31 +08:00
PostgreSQL里可以用双引号(")包裹字段名来使用与关键字冲突的字段名,相信MySQL也应类似,只是似乎应该使用`符号(可不是引号啊:D)。
|
3
iyaozhen 2015-04-30 00:34:25 +08:00
字段前后加`(反撇键,不是引号)
|
4
yangqi 2015-04-30 00:38:36 +08:00
|
5
raincious 2015-04-30 09:13:51 +08:00
@yangqi
你使用的版本?C9.io上的机器是5.5.40的,我做了如下测试: mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql> use test; Database changed mysql> create table `INOUT` (`KEY` INT, `IN` INT, `OUT` INT); Query OK, 0 rows affected (0.03 sec) mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | INOUT | +----------------+ 1 row in set (0.00 sec) mysql> show columns in `INOUT`; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | KEY | int(11) | YES | | NULL | | | IN | int(11) | YES | | NULL | | | OUT | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 3 rows in set (0.00 sec) |
7
raincious 2015-04-30 09:58:33 +08:00
@yangqi
你……发帖前自己做过测试么? mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql> use test; Database changed mysql> create table `INOUT` (`INOUT` INT); Query OK, 0 rows affected (0.02 sec) mysql> show columns in `INOUT`; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | INOUT | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.00 sec) |