What can cause legit MySql INSERT INTO command to fail? [duplicate] - sql

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
I can't figure out what's causing my INSERT INTO's to fail to certain table in MySql. I can manage them to other tables. The table looks like:
CREATE TABLE IF NOT EXISTS `Match` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`match_no` int(11) NOT NULL,
`season` int(11) NOT NULL,
`hometeam` int(11) NOT NULL,
`awayteam` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `match_no` (`match_no`),
KEY `season` (`season`),
KEY `hometeam` (`hometeam`),
KEY `awayteam` (`awayteam`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And the command is
INSERT INTO Match (`match_no`, `season`, `hometeam`, `awaytem`) VALUES (1, 1, 2, 3)
All I get is:
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 'Match (match_no, season, hometeam, awaytem) VALUES (1, 1, 2, 3)' at line 1
I have checked the manual and half-a-dozen examples from the web and whatnought and tried all sorts of changes to the syntax in case there is some MySql specific oddity, but nothing seems to work.

Change awaytem to awayteam and see how it goes and use `Match` as the table: match is a reserved word.

Match is a reserved word in MySQL.
Here goes the list of MySQL reserved words
Enclose Match in back ticks as:
INSERT INTO `Match` .........
Also as Pax pointed out you've misspelt a column name.

Match is a reversed word
so,
INSERT INTO `Match`
note the same backticks you used for the fieldnames
these are not for decoration

Related

PostgreSQL CHECK Constraint with 'LIKE' fails but succeeds with 'SIMILAR TO' and/or POSIX '!~*'

I am using PostgreSQL 10.1.
I create the following table:
CREATE TABLE country
(
id smallint NOT NULL,
alpha2 character varying(2) NOT NULL,
alpha3 character varying(3) NOT NULL,
name character varying(38) NOT NULL,
CONSTRAINT country_pkey PRIMARY KEY (id),
CONSTRAINT country_alpha2_key UNIQUE (alpha2),
CONSTRAINT country_alpha3_key UNIQUE (alpha3),
CONSTRAINT country_name_key UNIQUE (name),
CONSTRAINT country_alpha2_check
CHECK ((char_length(alpha2::text)) = 2 AND
(alpha2 NOT LIKE '%[^a-zA-Z]%')),
CONSTRAINT country_alpha3_check
CHECK ((char_length(alpha3::text)) = 3 AND
(alpha3 NOT LIKE '%[^a-zA-Z]%')),
CONSTRAINT country_name_check CHECK (char_length(name::text) > 0)
);
Unfortunately, the following statement succeeds although it should not:
INSERT INTO country (id, alpha2, alpha3, name)
VALUES (1, '11', '111', 'Haiti');
If I substitute LIKE with SIMILAR TO then the above statement fails as it should.
If I substitute NOT LIKE '%[^a-zA-Z]%' with POSIX Regex !~* '[^a-zA-Z]' then the above statement does fail, too, as it should.
Is there any explanation why LIKE fails? Most of the examples I have seen use LIKE! It seems that LIKE doesn't like to work!
Tia
The explanation is obvious and is hidden in a mere technicality:
LIKE in PostgreSQL uses only two characters to form a pattern: underscore _ and percent sign %.

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

Unexpected output Dbdelta WordPress

I have looked at many other questions on here relating to this issue but none so far have been able to resolve mine.
As per the codex on dbDelta. SQL statements must:
You must put each field on its own line in your SQL statement.
You must have two spaces between the words PRIMARY KEY and the
definition of your primary key.
You must use the key word KEY rather than its synonym INDEX and you
must include at least one KEY.
You must not use any apostrophes or backticks around field names.
Field types must be all lowercase.
SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
The error message I'm getting is:
WordPress database 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 '(
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NUL' at line 1]
But to my eyes my SQL statement conforms to dbDelta's requirements. NB the variables $table_name and $charset_collate are defined earlier in the function.
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
statistics longblob NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
How about this :
$sql = "CREATE TABLE " . $table_name. " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
statistics longblob NOT NULL,
PRIMARY KEY (id)
) " . $charset_collate . ";";

Create named Column default in CREATE TABLE statement in ANSI SQL

I want to create a named default value in an ANSI compliant fashion, if possible, in a CREATE TABLE statement
If I try to add the CONSTRAINT as I would normally write it in an ALTER TABLE statement, it fails (at least in SQL SERVER, though I emphasise I am hoping to find an ANSI complaint statement as I would prefer it to work over a variety of Ado.NET DbConnections).
Example:
CREATE TABLE [dbo].[MyExample]
(
Id int NOT NULL IDENTITY (1, 1),
Name varchar(512) NOT NULL,
IsActive bit NOT NULL,
CONSTRAINT PK_MyExample PRIMARY KEY CLUSTERED (Id),
CONSTRAINT DF_MyExample_IsActive DEFAULT (1) FOR [IsActive]
)
Error:
Incorrect syntax near 'for'.
In terms of the SQL-92 Standard -- which is both ISO (I = International) and ANSI (A + American), by the way -- DEFAULT is not a constraint that may be given a name. In SQL-92 the DEFAULT can only be defined inline with the column definition and must be between the data type and the NOT NULL (if used) e.g.
CREATE TABLE T (c INTEGER DEFAULT 1 NOT NULL UNIQUE);
Note you have much non-Standard syntax in your small example:
square brackets as quoted identifiers (should be double quotes)
non-compliant data type (e.g. incorrect bit null behaviour)
abbreviated data types (e.g. int rather than INTEGER)
IDENTITY
CLUSTERED
Is it not ANSI compliant?
CREATE TABLE [dbo].[MyExample]
(
Id int NOT NULL IDENTITY (1, 1),
Name varchar(512) NOT NULL,
IsActive bit NOT NULL CONSTRAINT DF_MyExample_IsActive DEFAULT (1),
CONSTRAINT PK_MyExample PRIMARY KEY CLUSTERED (Id)
)

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