SQL syntax error 'GENERATED ALWAYS AS IDENTITY' - 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

There are seval things wrong:
identity is SQL Server syntax, MySQL uses auto_increment
<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)
);

Related

MySQL giving me syntax error referring to characters that don't exist? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
DROP DATABASE IF EXISTS `company_db`;
CREATE DATABASE `company_db`;
USE `company_db`;
CREATE TABLE department(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
department_name VARCHAR (30) NOT NULL;
);
CREATE TABLE roles(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
title VARCHAR (30) NOT NULL,
salary DECIMAL,
department_id INT;
);
CREATE TABLE employee(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
first_name VARCHAR (30) NOT NULL,
last_name VARCHAR (30) NOT NULL,
role_id INT NOT NULL,
manager_id INT;
);
Here's my code above. When I try to run this schema file I get the following errors.
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 '' at line 3
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 ')' at line 1
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 '' at line 5
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 ')' at line 1
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 '' at line 6
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 ')' at line 1
What are they talking about? There isn't even a ')' in line 1. Is there something I'm misunderstanding about this? Thanks in advance!
You have an abundance of ;. This terminates each statement, it should not be present within the create:
CREATE TABLE department(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
department_name VARCHAR (30) NOT NULL
);
CREATE TABLE roles(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
title VARCHAR (30) NOT NULL,
salary DECIMAL,
department_id INT
);
CREATE TABLE employee(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
first_name VARCHAR (30) NOT NULL,
last_name VARCHAR (30) NOT NULL,
role_id INT NOT NULL,
manager_id INT
);
In each create statement, you have “;” after the last column… this is interpreted as the end of the SQL statement which is causing a syntax error. Just remove them.
The line numbers are within each statement, and you have extra semicolons. So your:
CREATE TABLE department(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
department_name VARCHAR (30) NOT NULL;
);
is two statements. The first:
CREATE TABLE department(
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
department_name VARCHAR (30) NOT NULL
complains about a syntax error at '' on line 3 (the end, where it is expecting a , or ) and sees neither).
And the second:
);
complains about a syntax error at ')' on line 1 (for obvious reasons).

What is the syntax error in this simple SQL query for creating a table?

I am trying out IntelliJ's DataGrip to do some SQL work on a MariaDB database.
Somehow i cannot execute the query that was automatically created by DataGrip itself....
Can you help me find the error in it ?
create table currencyIndex
(
id int auto_increment,
isoCode VARCHAR2(3) not null,
isoCodeNumeric SMALLINT not null,
currencyName VARCHAR2(100) not null,
countryName VARCHAR2(100) not null,
constraint currencyIndex_pk
primary key (id)
);
The error is
[42000][1064] (conn=246) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(3) not null,
[42000][1064] You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(3) not null,
isoCodeNumeric SMALLINT not null,
currencyName VARCHAR2(100) ...' at line 4.
I tried to validate the query with an online validator and it seems fine... Any suggestions ?
MariaDB and MySQL do not have a VARCHAR2 type (but Oracle does). Use plain VARCHAR and the error should go away:
CREATE TABLE currencyIndex (
id INT AUTO_INCREMENT,
isoCode VARCHAR(3) NOT NULL,
isoCodeNumeric SMALLINT NOT NULL,
currencyName VARCHAR(100) NOT NULL,
countryName VARCHAR(100) NOT NULL,
CONSTRAINT currencyIndex_pk PRIMARY KEY (id)
);
Demo

mysql error referring to Null

$sql1= "CREATE TABLE bookapp(
id int NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
pnumber int(11),
emailID VARCHAR(50),
reg_date datetime()
PRIMARY KEY(id)
)";
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 ''', NULL)' at line 1
You can use the following to create the table:
CREATE TABLE bookapp(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
pnumber INT(11),
emailID VARCHAR(50),
reg_date DATETIME,
PRIMARY KEY(id)
);
Explanation:
( and ) on DATETIME is invalid. DATETIME doesn't need a length.
You have to seperate the column definitions with ,. You forgot this after column reg_date.
Hint (to your error message):
Your given CREATE TABLE throws another error message, that is different to yours:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') PRIMARY KEY(id) )' at line 6

Create table error with MySQL

I try to create a table in MySQL:
mysql> create table order ( ID varchar(30) not null,
-> Cname varchar(100) not null,
-> name varchar(30),
-> Type varchar(30),
-> primary key(ID, Cname));
but an error occured:
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 'order ( ID varchar(30) not null, Cname
varchar(100) not null, name varchar(30), ' at line 1
I have checked for thousand time and I still find no error here.
Can anyone help me?
Its is because of the table name order.
There is an order by reserved word. Change the table name and it will work fine.
If you want order as table name use back ticks around the table name. It will workfine

Creating table on php having error on sql syntax

The program that I want to build is to create a table in mysql using php. The table name is the value entered in a textbox from a form of a previous page.
I am having a problem stating:
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 '2014( ID_Number INT NOT NULL, PRIMARY KEY (ID_Number), First_Name VARCH' at line 1'
CREATE TABLE $filename(
ID_Number INT NOT NULL,
PRIMARY KEY (ID_Number),
First_Name VARCHAR(50),
Middle_Name VARCHAR(50),
Last_Name VARCHAR(50),
Year INT,
Course VARCHAR(50),
Position VARCHAR(50),
Party VARCHAR(50),
Picture VARCHAR(50),
vote INT,
)
You need to remove the comma after the last column in your create statement
vote INT,
(Error creating table: 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 '2014( ID_Number INT NOT NULL, PRIMARY KEY (ID_Number), First_Name VARCH' at line 1')
Besides other possible errors (as suggested in other answers), above error message returned specifically because you were trying to create a table named 2014 which is not allowed.
"Identifiers may begin with a digit but unless quoted may not consist solely of digits". [MySql Manual : 9.2 Schema Object Names]
You can escape digits only table name by using backticks :
CREATE TABLE `2014`
....
String concatenation is wrong. Try this -
$sql = "CREATE TABLE $filename(".
"ID_Number INT NOT NULL,".
"PRIMARY KEY (ID_Number),".
"First_Name VARCHAR(50),".
"Middle_Name VARCHAR(50),".
"Last_Name VARCHAR(50),".
"Year INT,".
"Course VARCHAR(50),".
"Position VARCHAR(50),".
"Party VARCHAR(50),".
"Picture VARCHAR(50),".
"vote INT".
")";