MySQL Workbench Error 1064 when creating table - mysql

I am trying to use MySQL Workbench 8.0.3 with MariaDB but I get the following error:
Executing:
CREATE TABLE `mydb`.`customer` (
`ID` INT UNIQUE UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NOT NULL,
`Email` VARCHAR(255) CHARACTER SET 'utf8' NOT NULL,
`Street` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_czech_ci' NOT NULL,
`City` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_czech_ci' NOT NULL,
PRIMARY KEY (`ID`));
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 'UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NOT NULL,
`Email` VARC' at line 2
SQL Statement:
CREATE TABLE `mydb`.`customer` (
`ID` INT UNIQUE UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(255) NOT NULL,
`Email` VARCHAR(255) CHARACTER SET 'utf8' NOT NULL,
`Street` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_czech_ci' NOT NULL,
`City` VARCHAR(255) CHARACTER SET 'utf8' COLLATE 'utf8_czech_ci' NOT NULL,
PRIMARY KEY (`ID`))
Operation failed: There was an error while applying the SQL script to the database.
The code you see above has been generated using the "Create Table" Workbench feature.

UNSIGNED and INT go together, so this works:
`ID` INT UNSIGNED UNIQUE NOT NULL AUTO_INCREMENT,
This is shorter:
`ID` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
A primary key is already non-NULL and unique. There is no need to declare those properties twice.

I solved it by using int(10) instead of int
ID INT(10)
not
ID INT

Related

MySQL error #1064 on create table with DOUBLE column

I'm struggling to code this table as it's giving me a #1064 error, and this corresponds to a few potential issues. If someone could point out where I've made a mistake that would be great.
Here is an image of the code I've typed, and the error I've received:
The code:
CREATE TABLE `RENRMyLUoX`.`movie`(
`mID` INT(20) NOT NULL,
`title` VARCHAR(50) NULL DEFAULT NULL,
`relYear` YEAR(4) NULL DEFAULT NULL,
`category` VARCHAR(50) NULL DEFAULT NULL,
`runTime` INT(20) NOT NULL,
`studioName` VARCHAR(50) NULL DEFAULT NULL,
`description` VARCHAR(50) NULL DEFAULT NULL,
`rating` DOUBLE(20) NULL DEFAULT NULL,
PRIMARY KEY(`mID`(20))
) ENGINE = InnoDB CHARSET = latin1 COLLATE latin1_bin;
The DOUBLE datatype should be specified as either DOUBLE or DOUBLE(m, d). I don't understand what (20) is supposed to do... you can simply omit it:
rating DOUBLE NULL DEFAULT NULL,
TRY BELOW CODE
CREATE TABLE RENRMyLUoX.movie(
mID INT(20) NOT NULL,
title VARCHAR(50) NULL DEFAULT NULL,
relYear YEAR(4) NULL DEFAULT NULL,
category VARCHAR(50) NULL DEFAULT NULL,
runTime INT(20) NOT NULL,
studioName VARCHAR(50) NULL DEFAULT NULL,
description VARCHAR(50) NULL DEFAULT NULL,
rating DOUBLE(20) NULL DEFAULT NULL)ENGINE = InnoDB CHARSET = latin1 COLLATE latin1_bin;
ALTER TABLE RENRMyLUoX.movie
ADD PRIMARY KEY (mID)
ALTER TABLE RENRMyLUoX.movie
MODIFY mID int(20) NOT NULL AUTO_INCREMENT;
COMMIT;

MySQL query for login form not working in phpmyadmin sql query runner

CREATE TABLE `users`(
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(75) NOT NULL, `password` VARCHAR(255) NOT NULL,
`email` VARCHAR(100) NOT NULL,
PRIMARY KEY `id`
UNIQUE KEY `email` (`email`)
)ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 1;
That's my code for the query I'm trying to run in the phpmyadmin sql query runner to create a table for my users. When I try running the code I get 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 'UNIQUE KEY `email` (`email`)
)ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE...' at line 7
Can someone please help me fix the syntax.
here is the right syntax:
missing comma after pk , missing parentheses defining PK, and wrong syntax for unique key
CREATE TABLE `users`(
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(75) NOT NULL,
`password` VARCHAR(255) NOT NULL,
`email` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`) ,
UNIQUE KEY (`email`)
)ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 1;
The above answer works but I did:
'''
CREATE TABLE users(
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(75) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE KEY,
)ENGINE = INNODB DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci AUTO_INCREMENT = 1;
'''
and it works as well, you can choose whichever. Thanks for the response eshirvana!

Error trying to create table in MySQL via terminal

I am trying to create a table in my database via terminal.
Here is my syntax:
CREATE TABLE `users` (
PRIMARY KEY(id) NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHAR NOT NULL,
`gender` VARCHAR NOT NULL,
`fav_color` VARCHAR NOT NULL,
`birthdate` DATE NOT NULL
);
I am 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 'NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR NOT NULL,
`first_name` VARCHA' at line 2
What am i dong wrong here?
Besides the issues that Jens and Marc pointed out, You have to declare the length of your Varchar fields in order for this statement to work, like so:
CREATE TABLE `test`.`users` (
`id` INT NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR(45) NOT NULL,
`first_name` VARCHAR(45) NOT NULL,
`gender` VARCHAR(45) NOT NULL,
`fav_color` VARCHAR(45) NOT NULL,
`birthdate` DATE NOT NULL,
PRIMARY KEY (`id`));
the syntax of your create statement is wrong:
the correct one is this:
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT,
`last_name` VARCHAR(255) NOT NULL,
`first_name` VARCHAR(255) NOT NULL,
`gender` VARCHAR(255) NOT NULL,
`fav_color` VARCHAR(255) NOT NULL,
`birthdate` DATE NOT NULL,
PRIMARY KEY(`id`)
);
For more Information see the offical documentation.
it should be
id int primary key auto_increment not null
You're trying to define a primary key on a field that doesn't exist. Keys cannot be "not null" and definitely cannot be "auto_increment".

SQL create table syntax

I do not undertstand what is the problem with this:
CREATE TABLE 'posts' (
'IdPost' int(11) NOT NULL AUTO_INCREMENT,
'IdUser' int(11) NOT NULL,
'name' varchar(45) CHARACTER SET latin1 NOT NULL,
'title' varchar(100) CHARACTER SET latin1 NOT NULL,
'type' tinyint(4) NOT NULL,
'address' varchar(30) CHARACTER SET latin1 NOT NULL,
'lat' varchar(10) NOT NULL,
'lon' varchar(10) NOT NULL,
'url' varchar(30) NOT NULL,
PRIMARY KEY ('IdPost')
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
I am getting this error on my server:
#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 ''posts' ( 'IdPost' int(11) NOT NULL AUTO_INCREMENT, 'IdUser' int(11) NOT NUL' at line 1
What is the problem?
Do not use the quotes for your table name. Also remove all the quotes from your column name too.
DEMO SQL FIDDLE
On a side note:-
From here:-
Quoting is useful when an identifier is an SQL reserved word or
contains spaces or other special characters. Quoting an identifier
also enables it to be entirely numeric, something not true of unquoted
identifiers. To include an identifier quote character within a quoted
identifier, double it.
Don't put your table or column names in quotes. You have to use backticks for reserved words which is not the case for this table.
CREATE TABLE posts
(
IdPost int(11) NOT NULL AUTO_INCREMENT,
IdUser int(11) NOT NULL,
name varchar(45) CHARACTER SET latin1 NOT NULL,
title varchar(100) CHARACTER SET latin1 NOT NULL,
type tinyint(4) NOT NULL,
address varchar(30) CHARACTER SET latin1 NOT NULL,
lat varchar(10) NOT NULL,
lon varchar(10) NOT NULL,
url varchar(30) NOT NULL,
PRIMARY KEY (IdPost)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
This can be an alternative:
CREATE TABLE `posts` (
`IdPost` int(11) NOT NULL AUTO_INCREMENT,
`IdUser` int(11) NOT NULL,
`name` varchar(45) CHARACTER SET latin1 NOT NULL,
`title` varchar(100) CHARACTER SET latin1 NOT NULL,
`type` tinyint(4) NOT NULL,
`address` varchar(30) CHARACTER SET latin1 NOT NULL,
`lat` varchar(10) NOT NULL,
`lon` varchar(10) NOT NULL,
`url` varchar(30) NOT NULL,
PRIMARY KEY (`IdPost`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
I would suggest to use back tilts instead of quotes

my sql syntax error

I exported a DB using dbForge (v6) and the whole script has a problem with this:
USE `global-cms-content2`;
CREATE TABLE `global-cms-content2`.umbracorelationtype (
ID int(11) NOT NULL AUTO_INCREMENT,
DUAL bit(1) NOT NULL,
PARENTOBJECTTYPE char(36) NOT NULL,
CHILDOBJECTTYPE char(36) NOT NULL,
NAME varchar(255) NOT NULL,
ALIAS varchar(100) DEFAULT NULL,
PRIMARY KEY (ID)
)
ENGINE = INNODB
AUTO_INCREMENT = 2
AVG_ROW_LENGTH = 16384
CHARACTER SET utf8
COLLATE utf8_general_ci;
Error is :
1 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 'DUAL bit(1) NOT NULL,
PARENTOBJECTTYPE char(36) NOT NULL,
CHILDOBJECTTYPE ' at line 3 SQL2.sql 2 1
Same error happens even when I create the table manually using the editor.
Why is MySQL not working with its own scripts?
Any ideas?
UPDATE:
This did it!
USE `global-cms-content3`;
CREATE TABLE `global-cms-content3`.umbracorelationtype (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`DUAL` bit(1) NOT NULL,
`PARENTOBJECTTYPE` char(36) NOT NULL,
`CHILDOBJECTTYPE` char(36) NOT NULL,
`NAME` varchar(255) NOT NULL,
`ALIAS` varchar(100) DEFAULT NULL,
PRIMARY KEY (ID)
)
ENGINE = INNODB
AUTO_INCREMENT = 2
AVG_ROW_LENGTH = 16384
CHARACTER SET utf8
COLLATE utf8_general_ci;
Still not sure why the export script or the backup DB script doesn't take care of reserved keywords... anyway
The word DUAL is a keyword. See Keyword list.
Try to quote the word as below:
CREATE TABLE `global-cms-content2`.umbracorelationtype (
ID int(11) NOT NULL AUTO_INCREMENT,
`DUAL` bit(1) NOT NULL,
PARENTOBJECTTYPE char(36) NOT NULL,
CHILDOBJECTTYPE char(36) NOT NULL,
NAME varchar(255) NOT NULL,
ALIAS varchar(100) DEFAULT NULL,
PRIMARY KEY (ID)
)