What's wrong with this very simple mysql create table statement? - mysql

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

Related

Table cannot be created in mysql -Error 1064

I am trying to create a table in MySQL with the query
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
rank TINYINT NOT NULL,
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, rank),
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
but seems like it is throwing error everytime I made updates too. I don't know what is going wrong with it.
Error coming up 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 'rank TINYINT NOT NULL, groupName
VARCHAR at line 3
MySQL 8.0.2 added support for the window rank function, making it a reserverd word.
You could escape it using backticks (`):
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
`rank` TINYINT NOT NULL, -- Here
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, `rank`), -- And here
INDEX ofRosterGroup_rosterid_idx (rosterID)
);
But it may be a better idea to just use a name that isn't a reserved word, such as rosterRank instead of rank:
CREATE TABLE ofRosterGroups (
rosterID BIGINT NOT NULL,
rosterRank TINYINT NOT NULL, -- Here
groupName VARCHAR(255) NOT NULL,
PRIMARY KEY (rosterID, rosterRank), -- And here
INDEX ofRosterGroup_rosterid_idx (rosterID)
);

Why is the MySQL notation "UNIQUE field (field)" causing an error in MySQL 5.7.21?

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;

Error at "unsigned" when creating table in MySQL?

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.

MySQL ERROR 1064 (42000) when creating a table

I've looked at previous posts but I am struggling to figure out where i am going wrong.
I am trying to create a table using this code:
mysql> CREATE TABLE Engine
-> (
-> ID VARCHAR(255) UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
-> Name VARCHAR(255) NOT NULL,
-> Displacement VARCHAR(10) NOT NULL,
-> Code VARCHAR(10) NOT NULL,
-> PowerOutput VARCHAR(10),
-> MadeAt VARCHAR(255) NOT NULL,
-> PRIMARY KEY (ID),
-> FOREIGN KEY (MadeAt)
-> );
However I am repeatedly getting 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 to use near 'UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
Name VARCHAR(255) NOT NULL,
Displace' at line 3
Any help would be much appreciated.
Try to change
ID VARCHAR(255) UNASSSIGNED NOT NULL AUTO_INCREMENT UNIQUE
into:
ID INT NOT NULL AUTO_INCREMENT
UNASSIGNED should be UNSIGNED, but it is not necessary to define this, because AUTO_INCREMENT will start at 1 by default when not defined otherwise. UNIQUE is redundant as well, because the ID will increment and the numbers will be unique anyway.
In most cases VARCHAR is not recommended to use as PRIMARY KEY.

Auto Increment in Temporary Table

I'm trying to create a Temp Table that has a key field that auto increments. But I keep running into a syntax error.
Here's what I'm trying:
CREATE TEMPORARY TABLE
RETURN_ARTISTS
(KEY INT(11) NOT NULL AUTO_INCREMENT,
ARTIST_1_KEY INT(11),
ARTIST_2_KEY INT(11));
INSERT INTO RETURN_ARTISTS (1,1);
DELETE TABLE RETURN_ARTISTS;
And here's the error I keep getting:
Err] 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 'INT(11) NOT NULL AUTO_INCREMENT,
ARTIST_1_KEY INT(11),
ARTIST_2_KEY INT(' at line 3
First of all, key is a reserved word, escape it with `
Secondly, when using auto_increment column, that column must be defined as a key.
CREATE TEMPORARY TABLE
`RETURN_ARTISTS`
(`KEY` INT(11) NOT NULL AUTO_INCREMENT,
ARTIST_1_KEY INT(11),
ARTIST_2_KEY INT(11),
KEY (`KEY`));
key is a reserved word, so you'll have to escape it:
(`KEY` INT(11) NOT NULL AUTO_INCREMENT,
^---^--- escapes
or, preferably, use a different field name.