mysql字段是关键字_MySql 表的一个字段的字段名和关键字重名

今天遇到一个问题就是表的一个字段的字段名和关键字重名,我先创建了一个uses数据库

mysql> create database users;

Query OK, 1 row affected (0.00 sec)

mysql> use users

Database changed

mysql> show tables;

Empty set (0.00 sec)

然后新建了一个userinfo表

mysql> CREATE TABLE userinfo(

-> id INT NOT NULL AUTO_INCREMENT,

-> `name` VARCHAR(45) NOT NULL,

-> `password` VARCHAR(45) NOT NULL,

-> remark VARCHAR(100) NOT NULL,

-> PRIMARY KEY(id)

-> );

Query OK, 0 rows affected (0.06 sec)

mysql> show tables;

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

| Tables_in_users |

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

|

userinfo |

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

1 row in set (0.00 sec)

mysql> DESCRIBE userinfo;

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

| Field |

Type | Null | Key | Default |

Extra |

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

|

id |

int(11) | NO | PRI |

NULL |

auto_increment |

|

name | varchar(45) |

NO | | NULL | |

| password | varchar(45) |

NO | | NULL | |

| remark | varchar(100) |

NO | | NULL | |

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

4 rows in set (0.01 sec)

接着我想给表里面插入一条记录,如下所示:

mysql> INSERT INTO

userinfo('name','password',remark) VALUES

('Tom','12345','good');

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 ''name','password',remark) VALUES

('Tom','12345','good')' at line 1

这时候问题出现了,以前用MSSQL Server时,用中括号就可以解决。

mysql> INSERT INTO

userinfo([name],[password],remark) VALUES

('Johnson','12345','good');

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 '[name],[password],remark) VALUES

('Johnson','12345','good')' at line 1

最后经过把name和password两边的单引号改成反引号(数字键1旁边ESC下面的那个键)就好了

mysql> INSERT INTO

userinfo(`name`,`password`,remark) VALUES

('Tom','12345','good');

Query OK, 1 row affected (0.03 sec)

mysql> SELECT * FROM userinfo;

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

| id | name |

password | remark |

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

| 1 |

Tom | 12345 |

good |

| 2 | Johnson |

12345 |

good |

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

2 rows in set (0.00 sec)

发现和MSSQL Server不一样,MSSQL Server用中括号就可以解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值