SQL Syntax error. Suddenly broke? - mysql

When trying to create this table, it doesn't want to work properly though it worked minutes before? Assume that $username is equal to "Test" for brevity.
The error I'm receiving is:
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 '( id(10) INT AUTO_INCREMENT, PRIMARY KEY(id), message
varchar(250), ' at line 2
This is the code I'm currently using:
$sql = "CREATE TABLE {$username}
(
id(10) INT AUTO_INCREMENT,
PRIMARY KEY(id),
message varchar(250),
sender varchar(100)
)";

You have a SQL syntax error.
Instead of id(10) INT AUTO_INCREMENT
It should be id INT(10) AUTO_INCREMENT

Related

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

Where is the syntax error in this piece of sql code I wrote in themysql workbench?

This is a very simple piece of code using which I am trying to create a table of an entity Bank.
create table bank (
banknum integer(5) NOT NULL,
bankname varchar(20) NOT NULL,
primary key(banknum));
However, executing this gives an error:
Error Code: 1064 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 'create table bank ( banknum integer(5) NOT NULL,
bankname varchar(20) NOT NULL, ' at line 1
I do not understand where this syntax error is occurring.
create table bank (
banknum INT(5) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
bankname VARCHAR(20) NOT NULL);

MySQL error 1064 for 3rd table in file

I am having a very strange issue with creating a table in mySQL. Every time I run the file I get this error:
"ERROR 1064 (42000) at line 36 in file: 'QStoreDB.sql': 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 ')' at line 8"
For this section of code:
CREATE TABLE customers
(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(45) NOT NULL,
address1 VARCHAR(160) NOT NULL,
address2 VARCHAR(160) NOT NULL,
cart INT,
);
This is the third table in my file. The other 2 are created with no issue using the same format. I have tried removing all comments, as you can see I have removed all of my constraints but even when I input the data in its most basic form (ie whats writen above this paragraph) it still doesn't run. I have also tried putting my field names in `` to no avail.
I am at a complete loss and scouring the internet has not helped me so I'm hoping some one here has experienced this as can point me in the right direction.
Whilst defining PRIMARY KEY you do not have to write INT NOT NULL
CREATE TABLE customers
(
id PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(45) NOT NULL,
address1 VARCHAR(160) NOT NULL,
address2 VARCHAR(160) NOT NULL,
cart INT
);
And there shouldn't be a comma at the end.

SQL syntax error 'GENERATED ALWAYS AS IDENTITY'

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)
);

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