Why would this MySQL query give an error? [duplicate] - mysql

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
I can't for the life of me figure out what's wrong with this query:
CREATE TABLE IF NOT EXISTS 'plaintext' (
'id' INT NOT NULL AUTO_INCREMENT,
'name' VARCHAR NOT NULL,
'text' TEXT NOT NULL,
PRIMARY KEY ('id')
)
I am running MySQL version 5.7.14 on WAMP. I am getting this error:
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' INT NOT
NULL AUTO_INCREMENT, 'name' VARCHAR NOT NULL, 'text' TEX' at line 2
When trying out the query in phpmyadmin it told me that a symbol name and column definition was expected near 'id' on line 2, and there is an unexpected beginning of a statement near 'id' at line 5.
No clue what this even means to be completely honest with you, I'm quite new to MySQL.
Thanks in advance!
David

I think that the issue is that you are not providing a size for your VARCHAR
CREATE TABLE IF NOT EXISTS 'plaintext' (
'id' INT NOT NULL AUTO_INCREMENT,
'name' VARCHAR(100) NOT NULL,
'text' TEXT NOT NULL,
PRIMARY KEY ('id')
)

Related

MySql gives an error for no reason at all? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed last year.
I am writing an mysql program (using the command line editor):
CREATE TABLE FLIGHTS (
FL_NO VARCHAR(10) NOT NULL PRIMARY KEY,
STARTING VARCHAR(20) NOT NULL,
ENDING VARCHAR(20) NOT NULL,
NO_FLIGTHS INT NOT NULL,
NO_STOPS INT NOT NULL
);
this is somehow gives an error which is surprising. any idea?
Error message:
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 'STARTING VARCHAR(20) NOT NULL,
ENDING VARCHAR(20) NOT NULL,
NO_FLIGHTS INTEGER N' at line 3
STARTING and ENDING are keywords, and MySql does not accept keywords as column names, changing them should help...

1064 You have an error in your SQL syntax [duplicate]

This question already has answers here:
How can I fix MySQL error #1064?
(3 answers)
Closed 2 years ago.
I have had a problem since working with the new MYSQL version 8.0.18.
Always get the same error message:
SQLSTATE [42000]: Syntax error or access violation: 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 'function) VALUES (?,?,?,?,?)
DROP TABLE IF EXISTS bugtracker1_product_status;
CREATE TABLE bugtracker1_product_status (
statusID INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY,
productID INT(10),
statusTitle VARCHAR(255) NOT NULL DEFAULT '',
type ENUM('bug','suggestion') NOT NULL DEFAULT 'bug',
function ENUM('duplicate','solved','outstanding') NOT NULL DEFAULT 'outstanding',
cssClassName VARCHAR(255) NOT NULL DEFAULT '',
showOrder INT(10) NOT NULL DEFAULT 0,
KEY (productID)
);
Where is the problem?
function ENUM('duplicate','solved','outstanding') NOT NULL DEFAULT 'outstanding',
It works in the MariaDB
Thanks for your help!
Thank you for your support, but unfortunately this does not lead to success. The error remains.
`function` ENUM('duplicate','solved','outstanding') NOT NULL DEFAULT 'outstanding',
Could not prepare statement 'INSERT INTO bugtracker1_product_status (productID, statusTitle, cssClassName, type, function) VALUES (?,?,?,?,?)'
Is there no alternative?
FUNCTION (R); became reserved in 8.0.1
function is a reserved keyword in MySQL. You need to escape it with backticks:
`function` ENUM...
The MySQL docs state that it became reserved in version 8.0.1

Receiving error in an attempt to define my table [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 3 years ago.
I'm working on a group project and I'm trying to drop my database, but I'm receiving the same error, no matter how I try to manipulate what I wrote. Hopefully someone can help me understand where I went wrong.
For the
type VARCHAR NOT NULL,
description VARCHAR,
image VARCHAR,
I've tried adding in "DEFAULT NULL" and adding in numbers next to VARCHAR. I keep getting the same error message. I left it as what I put above because with my API, I don't want to restrict the amount of characters I'll be receiving when I call on it.
CREATE DATABASE recycleThis_db;
USE recycleThis_db;
CREATE TABLE materials (
id INT(11) NOT NULL AUTO_INCREMENT,
type VARCHAR NOT NULL,
description VARCHAR,
image VARCHAR,
materialID INT(11) NOT NULL,
PRIMARY KEY (id)
);
I keep getting this error message:
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 'NOT NULL,
description VARCHAR,
image VARCHAR,
materialID INT NOT NULL' at line 3
I believe VARCHAR requires a length specifier e.g. VARCHAR(10) or VARCHAR(250)
CREATE TABLE `materials`
( `id` INT(11) NOT NULL AUTO_INCREMENT
, `type` VARCHAR(80) NOT NULL
, `description` VARCHAR(255)
, ...
Reference: https://dev.mysql.com/doc/refman/8.0/en/char.html
excerpt:
The CHAR and VARCHAR types are declared with a length that indicates the maximum number of characters you want to store. For example, CHAR(30) can hold up to 30 characters.

How to fix syntax error when escaping table name? [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
CREATE TABLE 'test'.'sensor' (
'id' INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
'time' TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
'value' VARCHAR( 10 ) NOT NULL
);
This is my code which I entered in phpMyAdmin. And when I pressed go I got the following error:
#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 ''test'.'sensor' (
'id' INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
'time' TIMES' at line 1
I have tried changing some statements but couldn't get the error.
MySQL uses backticks to escape identifiers, single and double quotes for strings.
In this case you should do:
CREATE TABLE `test`.`sensor` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
`time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`value` VARCHAR( 10 ) NOT NULL
);
It's worth noting that backticks are only strictly required when your name conflicts with a reserved keyword and even then many are only relevant in a specific context. Terms like ORDER or SELECT always need to be escaped, so using them for columns or table names is best avoided. Likewise, TIME is also a column type, so you may want to pick a different name.
Additionally the TIMESTAMP column type is quite limited, values can only exist in the range of 1970 to 2038, so using it is not recommended. The DATETIME type by comparison has a range of years 1000 to 9999, more than adequate for most needs. There's a few other quirks of TIMESTAMP worth keeping in mind, too, like automatic UTC conversion.
Check that which version of php and MySQL u are using i have face this problem but solved after research . this query is working on MySQL 5.5 or newer
and MariaDB 5.5 or newer
CREATE TABLE Ghee(
country_code char(1) NOT NULL default '',
description varchar(10) NOT NULL default '',
PRIMARY KEY (country_code)
)

Why am I getting a syntax error when trying to create a table? [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 1 year ago.
I am trying to create a new table using the following:
CREATE TABLE onsale (
id INT AUTO_INCREMENT,
name VARCHAR(255),
desc TEXT,
image_file VARCHAR(255)
);
However, I keep getting an error:
ERROR 1064: (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB version for the right syntax to use near: 'desc TEXT, image_file VARCHAR(255))' at line 1
I don't see what is wrong with my syntax. It seems okay to me (obviously).
Can you tell me what I am doing wrong?
Thank you for your help.
The name you used for your column desc is a reserved word. You also need primary key (id):
CREATE TABLE onsale (
id INT AUTO_INCREMENT,
name VARCHAR(255),
description TEXT,
image_file VARCHAR(255),
primary key (id)
);