Why am I getting an insert blob error when using MySql v8.0 instead of v5.1? - mysql

I created the following table:
CREATE TABLE `player__main` (
`row` varbinary(256) NOT NULL,
`schema_id` int NOT NULL,
`version` int NOT NULL,
`format` tinyint NOT NULL,
`avro` mediumblob NOT NULL,
PRIMARY KEY (`row`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Then, I run my script below on MySql v5.17:
insert into player__main (row, schema_id, version, format, avro) VALUES (x'61646D696E',11,1,0,x'0A61646D69');
Query OK, 1 row affected (0.05 sec)
However, when I execute the code below in MySql v8.0:
insert into player__main (row, schema_id, version, format, avro) VALUES (x'61646D696E',11,1,0,x'0A61646D69');
I get 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 'row, schema_id, version, format, avro) VALUES
(x'61646D696E',11,1,0,x'0A61646D69' at line 1
It's the same query but it returns different results depending of the version of MySql I am using.
How can I fix this?

I think the real problem here is row is a reserved keyword in 8:
ROW (R); became reserved in 8.0.2
You'll need to rename that column, or escape it going forward:
INSERT INTO player_main (`row`, ...)

Related

Failed generated stored procedure

This is the code
CREATE TABLE `church` (
`ID` int(10) UNSIGNED NOT NULL,
`StudentID` int(11) NOT NULL,
`semesterID` int(11) NOT NULL,
`attendedWed` int(11) NOT NULL,
`attendedFri` int(11) NOT NULL,
`attendedSabM` int(11) NOT NULL,
`attendedSabE` int(11) NOT NULL,
`ChurchScore` double(10,2) GENERATED ALWAYS AS ((((((`attendedWed` + `attendedFri`) + `attendedSabM`) + `attendedSabE`) * 100) / 60)) STORED
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
This is the error
Operation failed: There was an error while applying the SQL script to
the database. Executing: ALTER TABLE citizenshipgroup3.church
CHANGE COLUMN ChurchScore ChurchScore DOUBLE(10,2) NULL DEFAULT
attendedWed ;
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 'attendedWed' at line 2 SQL Statement: ALTER TABLE
citizenshipgroup3.church CHANGE COLUMN ChurchScore
ChurchScore DOUBLE(10,2) NULL DEFAULT attendedWed
Your syntax is fine. The problem is that MySQL does not support generated columns until 5.7. You are presumably using an earlier version.
Probably the simplest solution is to use a view for the calculation.
The error appears at the time of ALTER TABLE, when you are trying to modify the column ChurchScore setting the DEFAULT value to an expression involving another Column attendedWed.
Also, your error message seems to be originating from MariaDB, not MySQL.
From Mariadb Documentation:
From MariaDB 10.2.1 you can use most functions in DEFAULT. Expressions
should have parentheses around them. If you use a non deterministic
function in DEFAULT then all inserts to the table will be replicated
in row mode. You can even refer to earlier columns in the DEFAULT
expression:
CREATE TABLE t1 (a int DEFAULT (1+1), b int DEFAULT (a+1));
CREATE TABLE t2 (a bigint primary key DEFAULT UUID_SHORT());
So you need to ensure couple of things:
Upgrade your MariaDB version to 10.2.1 and above. Preferably, upgrade to current latest version (it is 10.3+ right now).
You need to use parentheses around the expression specified in the DEFAULT clause.
So the ALTER TABLE statement would look like:
ALTER TABLE `citizenshipgroup3`.`church`
CHANGE COLUMN `ChurchScore` `ChurchScore` DOUBLE(10,2) NULL
DEFAULT (attendedWed) ;

Error with datetime insertion

I'm trying to insert a datetime in my table, but I can't figure why it isn't working. I'm running the following query:
DROP TABLE IF EXISTS dynamic_pricing ;
CREATE TABLE dynamic_pricing(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
time_start TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
time_stop TIMESTAMP NOT NULL,
change_ratio REAL NOT NULL
-- day_of_week INT(1)
);
INSERT INTO dynamic_pricing(id,time_start,time_stop,change_ratio)
VALUES(1,DATETIME('2000-01-01 00:00:00'),DATETIME('2000-01-01 00:00:02'),2);
I tried using this, only to get
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 '('2000-01-01 00:00:00'),DATETIME('2000-01-01
00:00:02'),2)' at line 2
What's wrong with this query?
The problem is that your value is defined as DATETIME() but you could use
INSERT INTO dynamic_pricing VALUES ('2017-06-03 21:30:03' )
instead.
Given that you are defaulting two values, you don't need to insert them. I would suggest:
INSERT INTO dynamic_pricing(time_stop, change_ratio)
VALUES('2000-01-01 00:00:02', 2);
Then, the DATETIME() is unnecessary (is it even a function?). Here is a SQL Fiddle example.
INSERT INTO dynamic_pricing(id,time_start,time_stop,change_ratio)
VALUES(1,'2000-01-01 00:00:00','2000-01-01 00:00:02',2);

Running SQL script gives syntax error

I have written an sql script with the following contents :
CREATE DATABASE IF NOT EXISTS stock_trading;
USE stock_trading;
CREATE TABLE IF NOT EXISTS transactions(
user_name VARCHAR(30) NOT NULL,
passwrd BINARY(64) NOT NULL,
balance_cash BIGINT NOT NULL DEFAULT 100000,
PRIMARY KEY (user_name,passwrd),
)ENGINE=InnoDB;
and every time I try to run it in the SQL command prompt it keeps giving away the error as:
ERROR 1064 (42000) at line 5 in file: 'db_script.sql': 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 ')ENGINE=InnoDB' at line 5
the script appears to be correct but I don't know why it keeps giving this error.
Additional information:
Operating System : Arch Linux
Database : MariaDB
You have a stray comma at the end of your table definition:
PRIMARY KEY (user_name,passwrd),
^^^ remove this
Your full table definition:
CREATE TABLE IF NOT EXISTS transactions(
user_name VARCHAR(30) NOT NULL,
passwrd BINARY(64) NOT NULL,
balance_cash BIGINT NOT NULL DEFAULT 100000,
PRIMARY KEY (user_name, passwrd)
) ENGINE=InnoDB;

MySQL inserting timestamp value causes command to fail

I am having problems inserting into a timestamp field.
Here is my schema:
CREATE TABLE `sponsorlog` (
`updated` timestamp,
`user` varchar(200) NOT NULL,
`company` varchar(200) NOT NULL,
`change` varchar(200) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Here is the insert command:
insert into sponsorlog (user, company, change, updated)
values ('attaskadmin#llts.com', '3M', 'Sponsor Jon Bove added', '2015-08-10 17:43:32');
And the error is the typical useless MySQL error. It seems to be pointing at the date/time value but I can't see anything wrong with it. Any ideas?
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 'change, updated) values ('attaskadmin#llts.com',
'3M', 'Sponsor Jon Bove added',' at line 1
change is a reserved word and needs to be quoted. In MySQL the identifier quote character is the backtick: `
insert into sponsorlog (user, company, `change`, updated)
values ('attaskadmin#llts.com', '3M', 'Sponsor Jon Bove added', '2015-08-10 17:43:32');
Also user is a non-reserved keyword, so to be consistent you might want to use quotes for all columns.
If an identifier contains special characters or is a reserved word,
you must quote it whenever you refer to it
Reference

Unknown syntax error in MySQL statement

I am using below CREATE TABLE statement
CREATE TABLE IF NOT EXISTS users (
`uuid` varchar(36) NOT NULL,
`json` varchar(MAX) NOT NULL,
PRIMARY KEY (`uuid`)
);
However I keep 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
'MAX) NOT NULL,
PRIMARY KEY (uuid)
)' at line 3
Makes no sense to me.
MAX is not suported for this use, it is reserved for the MAX function. Use the equivalent number instead, check this out: Equivalent of varchar(max) in MySQL?
This will work for you. MAX is reserved keyword. Specify exact number of varchar instead of max. However, varchar(MAX) will work in SQL SERVER 2005+.
CREATE TABLE IF NOT EXISTS users (
uuid varchar(36) NOT NULL,
json varchar(21808) NOT NULL,
PRIMARY KEY (uuid)
);
FIDDLE
MAX() is a function in MySql,So if you want to declare the size to the max.please refer the below example.
CREATE TABLE IF NOT EXISTS users (
`uuid` varchar(36) NOT NULL,
`json` varchar(65535) NOT NULL,
PRIMARY KEY (`uuid`)
);
and if you calculate that 21845*3 = 65535, which wouldn't have worked anyway. Whereas 21844*3 = 65532, which does work.
mysql> CREATE TABLE foo ( v VARCHAR(21844) ) CHARSET=utf8;
Query OK, 0 rows affected (0.32 sec)