Where is the error in my SQL query? - sql

My SQL query is
ALTER TABLE `USERS` MODIFY `UUID` SERIAL UNSIGNED NOT NULL AUTO_INCREMENT
but I keep on getting
#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 'UNSIGNED NOT
NULL AUTO_INCREMENT' at line 1
from MySQL (version 5.1). :-(
Any pointers?

From MySQL Doc:
SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE.
That's why you don't have to add more attributes to it...

Related

SQL syntax error

I use phpMyAdmin and want to create a table. I use the visual interface for creating the table but I'm gonna post the code from "Preview SQL" option:
CREATE TABLE `baza`.`koncert` (
`koncert_id` INT(10) NOT NULL AUTO_INCREMENT ,
`koncert_naziv` VARCHAR(50) NULL ,
`koncert_lokacija` VARCHAR(50) NOT NULL ,
`koncert_datum` DATE NULL DEFAULT NULL ,
`koncert_cijena` DOUBLE(10) NOT NULL ,
`koncert_slika` VARCHAR(500) NOT NULL )
ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_croatian_ci;
And I get this error:
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL version for the right syntax to use near ')' NOT NULL, 'koncert_slika' VARCHAR(500) NOT NULL ) ENGINE=InnoDB CHARSET=ut
I tried setting the 'koncert_datum' default value to CURRENT_TIMESTAMP, but then I get an error "Invalid default value for 'koncert_datum'". I just don't understand what could possibly be wrong (and I used the phpMyAdmin visual interface to try create the table!)
According to the documentation https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html the DOUBLE type needs total digits and decimal digits. Something like
`koncert_cijena` DOUBLE(12,2) NOT NULL ,
Check the schema and ensure baza.koncert is present and try running them by removing "ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_croatian_ci" this.

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server

I was trying to run following Query on my sql server :
CREATE TABLE `e_store`.`products`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(250) NOT NULL ,
`brand_id` INT UNSIGNED NOT NULL ,
`category_id` INT UNSIGNED NOT NULL ,
`attributes` JSON NOT NULL ,
PRIMARY KEY(`id`) ,
INDEX `CATEGORY_ID`(`category_id` ASC) ,
INDEX `BRAND_ID`(`brand_id` ASC) ,
CONSTRAINT `brand_id` FOREIGN KEY(`brand_id`) REFERENCES `e_store`.`brands`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE ,
CONSTRAINT `category_id` FOREIGN KEY(`category_id`) REFERENCES `e_store`.`categories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE
);
I have already brands and categories tables on my e_store database.
But I got 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 'JSON NOT NULL ,
PRIMARY KEY(`id`) ,
INDEX `CATEGORY_ID`('category_id' ' at line 6
For those who are facing this issue similar to me:
MariaDB does not natively implement the JSON data type but it uses it as an alias for LONGTEXT for compatibility reasons. According to the documentation (https://mariadb.com/kb/en/library/json-data-type/):
JSON is an alias for LONGTEXT introduced for compatibility reasons with MySQL's JSON data type. MariaDB implements this as a LONGTEXT rather, as the JSON data type contradicts the SQL standard, and MariaDB's benchmarks indicate that performance is at least equivalent.
In order to ensure that a a valid json document is inserted, the JSON_VALID function can be used as a CHECK constraint.
So if you are having issues with the JSON data type in MariaDB, simply just change to LONGTEXT. ;-)
I think you are getting error for JSON datatype.
For Mysql 5.7 you can get help from below link.
https://dev.mysql.com/doc/refman/5.7/en/json.html
You can check vesrion using below query.
select version() as 'mysql version'
"JSON" is parsed in the server. JSON is one of the points of divergence.
MySQL 5.7 introduced the JSON datatype, which matches your syntax.
MariaDB 10.0.16 introduced a ENGINE=CONNECT table_type=JSON which does not match your attempted syntax.
You have given single quotes in your index definitions instead of backticks
Try this:
CREATE TABLE `e_store`.`products`(
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(250) NOT NULL ,
`brand_id` INT UNSIGNED NOT NULL ,
`category_id` INT UNSIGNED NOT NULL ,
`attributes` JSON NOT NULL ,
PRIMARY KEY(`id`) ,
INDEX `CATEGORY_ID`(`category_id` ASC) , -- Changed single quotes to backticks
INDEX `BRAND_ID`(`brand_id` ASC) , -- Changed single quotes to backticks
CONSTRAINT `brand_id` FOREIGN KEY(`brand_id`) REFERENCES `e_store`.`brands`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE ,
CONSTRAINT `category_id` FOREIGN KEY(`category_id`) REFERENCES `e_store`.`categories`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE
);

phpMyAdmin creating an attribute with '#" character in its name

I am trying to create the following table.
CREATE TABLE customer
(
cust# CHAR(3)NOT NULL ,
cname VARCHAR(30) NOT NULL ,
city VARCHAR(20) NOT NULL,
PRIMARY KEY (cust#)
)engine=InnoDB;
I am getting the following 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 'cname VARCHAR(30) NOT NULL , city VARCHAR(20) NOT NULL, PRIMARY KEY (cust#' at line 4
I have confirmed that the problem is with the '#' by replacing it with 'custNum'.
However, I must use the '#' sign. I know I can rename the field in the myPhpAdmin interface to cust# but I need to know how to escape it in the SQL statement.
*Edit to say I have already tried '\#'
Thanks
I was able to answer my own question and I'm posting here in case someone else has the same problem.
You have to use '`' around the name. So `cust#` did the trick.

sql: making a table structure for injections

I want to take the values from this site for the country table in my database.
The problem is that they don't provide the table structure, so I have to create one, but I cannot get it right - my phpMyAdmin keeps displaying an error when I want to inject the data into the table I created below:
#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 'NUMERIC, alpha3, name, officialName) VALUES ('004','AFG','Afghanistan','Afghan' at line 1
--
-- Table structure for table `countrytable`
--
CREATE TABLE IF NOT EXISTS `countrytable` (
`NUMERIC` int(11) NOT NULL,
`alpha3` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`officialName` varchar(255) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
I think my table structure is incorrect. How can I fix it? Thanks!
Try all varchar fields to get the data in since all fields are in quotes in the string you have.
NUMERIC is reserved word in mysql
add in back-tick or quote it -> http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html
`alpha3` should be a varchar(3) (or larger), not an int(11).

mysql compare enum

Here is a table i have
CREATE TABLE `CUSTOMERSTATUSTYPES` (
`CustomerStatusId` int(1) unsigned NOT NULL auto_increment,
`CustomerStatusName` enum('ACTIVE','SUSPEND','TERMINATE','CANCEL') default NULL,
PRIMARY KEY (`CustomerStatusId`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
When i write
GROUP BY ... WHERE cst.CustomerStatusName<>'TERMINATE' ;
i get a 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 'WHERE
cst.CustomerStatusName<>'TERMINATE''
at line 1
How do i correctly check this?
The WHERE clause must come before the GROUP BY clause.
You either need to put the WHERE clause before the GROUP BY clause, or you need to use the HAVING keyword.
See here for more info:
http://www.databasejournal.com/features/mysql/article.php/3469351/The-HAVING-and-GROUP-BY-SQL-clauses.htm