SQL syntax error 'GENERATED ALWAYS AS IDENTITY'

前提是你 提交于 2020-01-15 11:57:06

问题


I am trying to upload this file to netbeans however I am unable to create the authors table and continue to receive the following error:

CREATE TABLE authors (
   authorID INT NOT NULL GENERATED ALWAYS AS IDENTITY,
   firstName varchar (20) NOT NULL,
   lastName varchar (30) NOT NULL,
   PRIMARY KEY (authorID)
);

INSERT INTO authors (firstName, lastName)
VALUES 
   ('Paul','Deitel'), 
   ('Harvey','Deitel'),
   ('Abbey','Deitel'), 
   ('Dan','Quirk'),
   ('Michael','Morgano');

[Warning, Error code 1,064, SQLState 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 'GENERATED ALWAYS AS IDENTITY, firstName varchar (20) NOT NULL, lastName va' at line 2

[Exception, Error code 1,064, SQLState 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 'GENERATED ALWAYS AS IDENTITY, firstName varchar (20) NOT NULL, lastName va' at line 2 Line 2, column 1

[Warning, Error code 1,146, SQLState 42S02] Table 'books.authors' doesn't exist

I have been searching all over the web for hours and have yet to find an answer! I am new to SQL so I apologize if this is a duplicate question. I am using MYSQL version 5.7.18


回答1:


There are seval things wrong:

  1. identity is SQL Server syntax, MySQL uses auto_increment
  2. <name> <type> generated always as <calculation> applies to calculated columns.

Try:

CREATE TABLE authors (
   authorID INT NOT NULL AUTO_INCREMENT,
   firstName varchar (20) NOT NULL,
   lastName varchar (30) NOT NULL,
   PRIMARY KEY (authorID)
);


来源:https://stackoverflow.com/questions/43965114/sql-syntax-error-generated-always-as-identity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!