Auto Increment in Temporary Table - mysql

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.

Related

MYSQL (WAMP SERVER): Error 1064 when using AUTO_INCREMENT=1000 to create table column?

I have this code:
CREATE TABLE Company ( Comp_Code INT(5) NOT NULL AUTO_INCREMENT=1000, Comp_Name VARCHAR(15) NOT NULL, PRIMARY KEY (Comp_Code) );
but when i run it in MYSQL from WAMPServer it gives an 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 '=1000,
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
)' at line 2
Why is this happening? I can't seem to find any solution to this particular issuer.
AUTO_INCREMENT is column option.
Initial AUTO_INCREMENT=1000 value is table option.
CREATE TABLE Company (
Comp_Code INT(5) NOT NULL AUTO_INCREMENT, -- specify that the column is AI
Comp_Name VARCHAR(15) NOT NULL,
PRIMARY KEY (Comp_Code)
) AUTO_INCREMENT=1000; -- specify initial AI value

MySQL Workbench create table generates SQL that throws an error

I am using MySQL version 5.7.18 and MySQL Workbench is targeting version 5.6.20. However, when I try and create a table using the table generator it produces the following sql statement:
CREATE TABLE `sys`.`annotations` (
`id` INT GENERATED ALWAYS AS () VIRTUAL,
`annotation` LONGTEXT NOT NULL,
PRIMARY KEY (`id`));
But this produces the following error message:
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 ') STORED',
`annotation` LONGTEXT NOT NULL,
PRIMARY KEY (`id`))' at line 2
SQL Statement:
CREATE TABLE `sys`.`annotations` (
`id` INT GENERATED ALWAYS AS () STORED,
`annotation` LONGTEXT NOT NULL,
PRIMARY KEY (`id`))
Why would this be happening and how would I go about fixing this?
I have no idea what an empty expression means for a computed column. Nor do I understand a VIRTUAL column being a primary key. I would use:
CREATE TABLE sys.`annotations` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`annotation` LONGTEXT NOT NULL
);

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.

SQL Expression wrong

I try to create a table but I keep getting an SQL Error but I can't figure out why.
This is the error:
check the manual that corresponds to your MySQL server version for the
right syntax to use near '+491634170770 (Id INT NOT NULL PRIMARY KEY
AUTO_INCREMENT,Type VARCHAR(20),Conte' at line 1 ERROR 1064 (42000):
You have an error in your SQL syntax;
This is my statement:
CREATE TABLE +491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
I already tried to find a solution here, but my syntax seems to be correctly. I think its because of the name of the table. But using backticks like this ´+491234175789´ didn't work.
This is the backtick `:
CREATE TABLE `+491234175789` (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);
However, don't create a table name that requires backticks. It is just bad form and makes queries harder to read and write -- you are creating problems for the future. Call it something like:
CREATE TABLE t_491234175789 (
Id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
Type VARCHAR(20),
Content MEDIUMBLOB
);

what is wrong with the sql query?

CREATE TABLE 1
(text longtext(4,294,967,295),
date VARCHAR(50),
time VARCHAR(50),
id INT(11 ) NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY ( `id` )
What is wrong with the above SQL statement? I keep getting an 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 '(4,294,967,295), date VARCHAR(50), time VARCHAR(50), id INT( 11 ) NOT NULL AU' at line 3
1 is a lousy name for a table. If you use it, you need to escape the name. Also, commas aren't allow in lengths for character fields and longtext doesn't need a length anyway:
CREATE TABLE `1`
(text longtext,
date VARCHAR(50),
time VARCHAR(50),
id INT(11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY
)
And, you don't "add" a primary key. You just declare it.