Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL syntax error 'GENERATED ALWAYS AS IDENTITY'

Tags:

mysql

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

like image 385
jj0808 Avatar asked May 06 '26 19:05

jj0808


1 Answers

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)
);
like image 169
Andomar Avatar answered May 09 '26 15:05

Andomar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!