I can't tell what is wrong with this query.
create table roles
(
id int unsigned not null auto_increment
,name varchar(32) not null
,phone varchar(256) not null
,primary key (id)
)
engine=innodb
default charset=utf8;
When I try to run it, I get:
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 'unsigned not null auto_increment ,role_id int unsigned ' at
line 3
Using MySQL 5.7
Thanks for the advice, tadman
Turns out I had a tab between "int unsigned" instead of a space. Replaced the tab with a space and it worked just fine.
That was driving me crazy for almost 2 hours.
Related
I ran into a new problem today, when I was dealing with a mysql query that works on 10.1.19-MariaDB localhost, but not on MySQL 5.7.21-0ubuntu0.16.04.1-log:
CREATE TABLE testing (
pageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
position SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (pageid),
UNIQUE position (position)
) ENGINE=InnoDB CHARSET=utf8
In MySQL 5.6 it works without any hickups (fiddle), however, in MySQL 5.7.21 (fiddle) it throws:
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 'position (position)) ENGINE=InnoDB CHARSET=utf8' at line 1
I figured out a solution by replacing UNIQUE position (position) with UNIQUE (position).
But I am wondering, what the underlying problem is, why it works with the other db system, and I am not sure if my solution is correct.
MySQL doesn't allow you to create an index with the same name of the column
The following code works for MySQL (fiddle)
CREATE TABLE testing (
pageid INT UNSIGNED NOT NULL AUTO_INCREMENT,
position SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (pageid),
UNIQUE idx_position (position)
) ENGINE=InnoDB CHARSET=utf8
That's why it is so important to wrap field and table names in backticks in MySQL (and their derivatives):
Try this (same query and index names, only added backticks):
CREATE TABLE `testing` (
`pageid` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`position` SMALLINT UNSIGNED NOT NULL,
PRIMARY KEY (`pageid`),
UNIQUE `position` (`position`)
) ENGINE=InnoDB CHARSET=utf8;
I am trying to create a table, but it gives me this error:
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 touse near ' INT NOT NULL AUTO_INCREMENT
Here is my code:
CREATE TABLE bowling_scores (
PlayerID, INT NOT NULL AUTO_INCREMENT
, LName VARCHAR(100) NOT NULL
, FName VARCHAR(100) NOT NULL
, Game1 INT NULL
, Game2 INT NULL
, Game3 INT NULL
, Game4 INT NULL
, PRIMARY KEY (PlayerID)
);
Here's a tip about syntax error messages: the message tells you where the error is.
check the manual that corresponds to your MySQL server version for the right syntax to use near ' INT NOT NULL AUTO_INCREMENT
This is telling you that MySQL became confused because it wasn't expecting to see that text. This usually means you made a mistake at that point, or immediately before it.
This can help you narrow down your search in the SQL statement to the point where it went wrong.
The error message also tells you what to do: check the manual. You should help yourself become better at SQL by reading documentation and examples.
I've been using MySQL for nearly 20 years, but I still refer to the reference docs every day. In fact, it's because I am experienced with MySQL that I know this is the right thing to do.
All SQL statements have a reference page in the manual. Here's the page for CREATE TABLE: https://dev.mysql.com/doc/refman/5.7/en/create-table.html
The MySQL manual is large, so it's easy to think "I don't know where the right page is." Gradually you can learn how the manual is organized. Pay attention to the hierarchy of links on the left column. When in doubt, just use Google for phrases like "mysql create table syntax".
You should be able to answer simple syntax errors for yourself instead of posting to Stack Overflow. You'll get your answer more quickly!
This is an obvious error:
PlayerID, INT NOT NULL AUTO_INCREMENT
You have an extraneous comma. It should be:
PlayerID INT NOT NULL AUTO_INCREMENT
Stop putting commas on the next line of your SQL statements as well!
This should work. You had a comma after PlayerID
CREATE TABLE bowling_scores (
PlayerID INT NOT NULL AUTO_INCREMENT,
LName VARCHAR(100) NOT NULL,
FName VARCHAR(100) NOT NULL,
Game1 INT NULL,
Game2 INT NULL,
Game3 INT NULL,
Game4 INT NULL,
PRIMARY KEY (PlayerID)
);
You should be writing something like:
creat table Bowling_score(Playerid int, Firstname varchar(100), Lastname varchar(100), Game1 int, Game2 int, Game3 int);
I'm trying to get this to work but I don't understand how SQL works outside of the basics. Can you find the error in this?
CREATE TABLE lil_urls (
id varchar(255) NOT NULL default '',
url text,
date timestamp(14) NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
I'm currently getting this error in PhPMyAdmin:
#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 '(14) NOT NULL,
PRIMARY KEY (id)' at line 4
Thanks for your help!
Correct syntax is
CREATE TABLE lil_urls (
id varchar(255) NOT NULL default '',
url text,
date timestamp NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
TYPE is no longer used and you need to use as ENGINE
And no need to specify length for timestamp
Creating a database for a class in PHP and MYSQL. Running into an error when creating the database, phpMyAdmin gives me a useless error message. (Software that gives a better error message would be awesome BTW)
phpMyAdmin error:
#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 customers ( customer_id INT UNSIGNED NOT NULL auto increment, fir' at line 1
USE isys288_gottfrk
CREATE TABLE customers ( customer_id INT UNSIGNED NOT NULL
auto increment, first_name carchar(20) NOT NULL, last_name VARCHAR(40) NOT NULL,
PRIMARY KEY (customer_id), INDEX full_name (last_name, first_name) ) engine =
innodb;
There were several errors in your code.
USE isys288_gottfrk;
CREATE TABLE customers
(
customer_id INT UNSIGNED NOT NULL auto_increment,
first_name varchar(20) NOT NULL,
last_name VARCHAR(40) NOT NULL,
PRIMARY KEY (customer_id),
INDEX full_name (last_name, first_name)
) engine = innodb;
I suggest you use a DB tool like MySQL Workbench to generate your tables. Then you can see the problems right away.
Juergen d edited your question for formatting, this might have helped you as well: Your initial statement was all on one line, so the error was flagged in that one line. If you edited it on multi lines (like in your question, or better in his answer) you might have gotten a better message, e.g. one that flags that the line containing carchar(20) contains a problem (it's even better the way he has it in his answer, but it correctly uses varchar(20), not flagging a problem)
Yes, it would be nice if error messages would immediately flag the problem, but sometimes it's already better when the input is formatted in a more human-readable way. I've tested the same with a multiline statement, mysql stated
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 'carchar(20)
Try this:
CREATE TABLE customers (
customer_id INT UNSIGNED NOT NULL AUTO_INCREMENT
, first_name VARCHAR (20) NOT NULL
, last_name VARCHAR (40) NOT NULL
, PRIMARY KEY (customer_id)
, INDEX full_name (last_name, first_name)
) ENGINE = INNODB ;
You can use some GUI clients like SQLyog for this.
create table foo(
id UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
barbaz VARCHAR(50) CHARACTER SET utf8
)
[Edit]: OK I had accidentally removed the INT and I didn't realize it (these statements are generated automatically).[/Edit]
This gives me this error:
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 'UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
barbaz VARCHAR(50) CHARACTER SET '
Yeah, I'm using Maria DB as it replaces mysql in Fedora, but supposedly it's exactly the same syntax as Mysql.
Thanks for your help.
UNSIGNED is not a type. You need to specify the type (most likely INT):
id INT UNSIGNED ...
Try this:
CREATE TABLE `foo`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`barbaz` VARCHAR(50) CHARSET utf8,
PRIMARY KEY (`id`)
);