SQL Expression wrong - mysql

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

Related

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

SQL syntax error in this part: "int not null auto_increment,"

I create a physical model in powerdesigner
and then generate the code for mysql5, and now in
phpmyadmin Im getting an 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 CARD
Do you see why this can be happening?
Im creating my tables like this:
create table books
(
COD_BOOK int not null auto_increment,
TITLE_BOOK varchar(50),
ISBN _BOOK varchar(20),
CATEGORY_BOOK varchar(20),
primary key (COD_BOOK)
)
And the problem seems like is in this part: int not null auto_increment,
You're missing the semi-colon at the end of your create statement. This makes the first line of the create statement for the next table an error which is what that error message is trying to tell you.
create table books
(
COD_BOOK int not null auto_increment,
TITLE_BOOK varchar(50),
ISBN_BOOK varchar(20),
CATEGORY_BOOK varchar(20),
primary key (COD_BOOK)
);
The problem is the space after ISBN:
create table books
(
COD_BOOK int not null auto_increment,
TITLE_BOOK varchar(50),
ISBN_BOOK varchar(20),
CATEGORY_BOOK varchar(20),
primary key (COD_BOOK)
)
Here is a SQL Fiddle.

MySQL create Table error on phpMyAdmin

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.

Integrity constraint in MySQL to express "must have one primary attribute"

I have a 2-table schema which can be specified as follows:
CREATE TABLE MEMBERS
( membershipnumber int NOT NULL PRIMARY KEY,
firstname varchar(20) NOT NULL,
lastname varchar(20) NOT NULL,
email varchar(30) NOT NULL,
status varchar(15) NOT NULL DEFAULT 'unapproved',
);
CREATE TABLE TELEPHONENUMBERS
( telephone varchar(15) NOT NULL PRIMARY KEY,
membershipnumber INT NOT NULL REFERENCES MEMBERS(membershipnumber),
isprimary enum('0','1') NOT NULL DEFAULT '1'
);
I am trying to specify an integrity constraint to indicate that any individual member may have only one primary telephone number (i.e. telephonenumbers.isprimary='1') and any number of secondary telephone numbers.
Here is my current attempt:
ALTER TABLE MEMBERS
ADD CONSTRAINT oneprimary_ck CHECK (SELECT COUNT(isprimary)=1 FROM TELEPHONENUMBERS WHERE TELEPHONENUMBERS.membershipnumber = membershipnumber) IN '1';
However this yields the following syntax 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 'SELECT COUNT(isprimary)=0 FROM TELEPHONENUMBERS WHERE TELEPHONENUMBERS.membershipnumber=1 IN '1') IN ''
I'm assuming my approach is totally wrong, but am unsure how to proceed. Would it be better to do this using a trigger or is there a small alteration ot my approach that would work?
I am using server version: 5.5.30.
With many thanks,
Froskoy.
Does it work any better if you swap ...
ALTER TABLE MEMBERS ADD CONSTRAINT oneprimary_ck CHECK
(SELECT COUNT(isprimary)=1 FROM TELEPHONENUMBERS
WHERE TELEPHONENUMBERS.membershipnumber = membershipnumber) IN '1';
... with ...
ALTER TABLE MEMBERS ADD CONSTRAINT oneprimary_ck CHECK
(SELECT COUNT(isprimary) FROM TELEPHONENUMBERS
WHERE isprimary=1 AND TELEPHONENUMBERS.membershipnumber = membershipnumber) > 1;
... ?
Two things are wrong in your approach:
A check constraint cannot contain a subquery
Check constraints are not enforced by MySQL (not in 5.5 anyway)
What you can do, is create a trigger that raises an error. Here's an example:
https://stackoverflow.com/a/7189396/417194

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.