我有时会遇到奇怪的SQL错误,它告诉我检查MySQL版本,例如
mysql> CREATE TABLE city (
-> id int NOT NULL IDENTITY(1, 1),
-> city_name char(128) NOT NULL,
-> lat decimal(9,6) NOT NULL,
-> long decimal(9,6) NOT NULL,
-> country_id int NOT NULL,
-> CONSTRAINT city_pk PRIMARY KEY (id)
-> );
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 'IDENTITY(1, 1),
city_name char(128) NOT NULL,
lat decimal(9,6) NOT NUL' at line 2
所以我做了:
mysql> show variables like '%version%';
+--------------------------+-------------------------------+
| Variable_name | Value |
+--------------------------+-------------------------------+
| admin_tls_version | TLSv1,TLSv1.1,TLSv1.2,TLSv1.3 |
| immediate_server_version | 999999 |
| innodb_version | 8.0.27 |
| original_server_version | 999999 |
| protocol_version | 10 |
| replica_type_conversions | |
| slave_type_conversions | |
| tls_version | TLSv1,TLSv1.1,TLSv1.2,TLSv1.3 |
| version | 8.0.27-0ubuntu0.20.04.1 |
| version_comment | (Ubuntu) |
| version_compile_machine | x86_64 |
| version_compile_os | Linux |
| version_compile_zlib | 1.2.11 |
+--------------------------+-------------------------------+
错误消息的其余部分告诉我检查手册,但我不确定该检查哪个。这个输出告诉我的是,我正在使用8.0.27版本的某个未指定的东西。那么,根据课本,我应该怎么做才能发现原来的语法错误呢?
MySQL不支持IDENTITY()
。这是Microsoft SQL Server的东西(和Sybase)。
您希望使用AUTO_INCREMENT
执行类似的功能,为主键分配单调递增的整数。
阅读这里:https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html