Why is this MySQL CREATE TABLE statement failing? - mysql

This create statement is failing, and I can't see the reason. MySQL reports:
Error Code: 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 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci, objObjectType VARCHAR(100) ' at line 3*
CREATE TABLE `my_object_attribute_map` (
`objID` INT(10) UNSIGNED auto_increment NOT NULL primary key,
`objForeignKey` VARCHAR(100) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`objObjectType` VARCHAR(100) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`objTypeName` VARCHAR(100) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`objLabel` VARCHAR(100) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`objValue` VARCHAR(100) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci,
`objDateCreated` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`objCreatorID` CHAR(32) NOT NULL CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci
);

The not null constraint goes after the character set and collation:
CREATE TABLE `my_object_attribute_map` (
`objID` INT(10) UNSIGNED auto_increment NOT NULL primary key,
`objForeignKey` VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
`objObjectType` VARCHAR(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
...
);

The syntax of column definitions has some warts; see https://dev.mysql.com/doc/refman/8.0/en/create-table.html and https://dev.mysql.com/doc/refman/8.0/en/string-type-syntax.html.
Basically, the CHARACTER SET is part of the data type, and must come before the NOT NULL, which is an extra attribute that applies to any type. COLLATE I believe in principle is also part of the data type, and should also come before NOT NULL, but if you do specify it afterwards it still works (or is ignored, for non-string types), presumably for backwards compatibility.
Since the collation set also determines the character set, you could just leave character set out and specify COLLATE wherever you want.

Related

Shopware import Database runs on "GENERATED ALWAYS AS" error

So i exported a shopware database using phpmyadmin with MySQL and get an "GENERATED ALWAYS AS" error on the order table when importing to a new database.
After a short time I get this error and can't find any solution to this.
I even deleted all orders to be sure there is no malformed order in my table.
This is what phpmyadmin returns:
SQL-Befehl:
CREATE TABLE `order` ( `id` binary(16) NOT NULL, `version_id` binary(16) NOT NULL, `state_id` binary(16) NOT NULL, `auto_increment` bigint(20) UNSIGNED NOT NULL, `order_number` varchar(64) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `currency_id` binary(16) NOT NULL, `language_id` binary(16) NOT NULL, `currency_factor` double DEFAULT NULL, `sales_channel_id` binary(16) NOT NULL, `billing_address_id` binary(16) NOT NULL, `billing_address_version_id` binary(16) NOT NULL, `price` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`price`)), `order_date_time` datetime(3) NOT NULL, `order_date` date GENERATED ALWAYS AS (cast(`order_date_time` as date)) STORED, `amount_total` double GENERATED ALWAYS AS (json_unquote(json_extract(`price`,'$.totalPrice'))) VIRTUAL, `amount_net` double GENERATED ALWAYS AS (json_unquote(json_extract(`price`,'$.netPrice'))) VIRTUAL, `position_price` double GENERATED ALWAYS AS (json_unquote(json_extract(`price`,'$.positionPrice'))) VIRTUAL, `tax_status` varchar(255) COLLATE utf8mb4_unicode_ci GENERATED ALWAYS AS (json_unquote(json_extract(`price`,'$.taxStatus'))) VIRTUAL, `shipping_costs` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`shipping_costs`)), `shipping_total` double GENERATED ALWAYS AS (json_unquote(json_extract(`shipping_costs`,'$.totalPrice'))) VIRTUAL, `deep_link_code` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `custom_fields` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`custom_fields`)), `affiliate_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `campaign_code` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `customer_comment` longtext COLLATE utf8mb4_unicode_ci DEFAULT NULL, `created_at` datetime(3) NOT NULL, `updated_at` datetime(3) DEFAULT NULL, `item_rounding` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`item_rounding`)), `total_rounding` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`total_rounding`)), `rule_ids` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`rule_ids`)), `created_by_id` binary(16) DEFAULT NULL, `updated_by_id` binary(16) DEFAULT NULL )
MySQL meldet: Dokumentation
#1064 - Fehler in der SQL-Syntax. Bitte die korrekte Syntax im Handbuch nachschlagen bei 'GENERATED ALWAYS AS (json_unquote(json_extract(`price`,'$.taxStatus'))) VIRTU...' in Zeile 19
Using this line of code via SSH on my dump, made it possible to Import it afterwards:
sed -i 's/COLLATE.*GENERATED ALWAYS/GENERATED ALWAYS/g' dump.sql
You can look into https://sw-cli.fos.gg/commands/project/#shopware-cli-project-dump-database and use that for the dump creation. It's a replacement for mysqldump and considers this all issues.
Otherwise, you need the same mysqldump/mysql cli binary as the server. Importing mostly works only in the same database type (mysql/mariadb)

MYSQL Stored Procedure - Concat Row Value (continued)

Update / Answer
To fix this issue, I added the keyword BINARY to each side of the comparison operator. This generated the expected results. Also note that I set the variable to empty string. Setting this to NULL did not resolve the issue.
SET #previousstate = '';
SELECT
if(**BINARY** #previousstate != **BINARY** frm.fi_details_temp.PortfolioCode,
End Update / Answer
This is somewhat related to MYSQL Stored Procedure - Concat Row Value. That thread was based on Mysql 5.7.
The following statement worked under MySQL 5.7. I am in the process of trying to get it to work in MySql 8. I had to update the SET statement to 'SET #previousstate = NULL;' otherwise I get "Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '<>'". This error was way above my understanding. I am just a marketing guy working in a small firm. So that is why I set it to NULL.
With that said, after updating the SET statement the result set returns but the if() statement is not working like it did under 5.7. The very first row returned should have the first column of data marked with '*' at the beginning and end, and as the data in the column changes it should mark it too.
So it should look like this:
*brownfdn*
brownfdn
*brownfmi*
brownfmi
But I am getting this:
brownfdn
brownfdn
brownfmi
brownfmi
Do you have any thoughts about what I am doing wrong? I appreciate your help!! Getting to MySql 8 has been a struggle.
5.7
CREATE table fi_details_temp AS (
SELECT
frm.fi_details.PortfolioCode,
frm.fi_details.MaturityDate_Final
From
frm.fi_details
Where
frm.fi_details.manager = 'Bartz' And
frm.fi_details.PortfolioCode Like ('Crown%')
Group By
frm.fi_details.PortfolioCode,
frm.fi_details.MaturityDate_Final
Order By
frm.fi_details.PortfolioCode;
SET #previousstate = '';
SELECT
if(#previousstate != frm.fi_details_temp.PortfolioCode, concat('*',#previousstate:= frm.fi_details_temp.PortfolioCode,'*'), frm.fi_details_temp.PortfolioCode) as 'Portfolio Code',
frm.fi_details_temp.MaturityDate_Final as 'Maturity Date'
From frm.fi_details_temp;
DROP TABLE IF EXISTS fi_details_temp;
END
*** Create Statements - the above was a trimmed down version ***
CREATE TABLE `fi_details` (
`id` int NOT NULL AUTO_INCREMENT,
`RecordDate` datetime DEFAULT CURRENT_TIMESTAMP,
`PortfolioCode` varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`manager` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`RunDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`RunDate_Final` date DEFAULT NULL,
`AsOfDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`AsOfDate_Final` date DEFAULT NULL,
`Symbol` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`Coupon` double DEFAULT NULL,
`MaturityDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`MaturityDate_Final` date DEFAULT NULL,
`ParValue` double DEFAULT NULL,
`MarketValue` double DEFAULT NULL,
`AdjustedUnitCost` double DEFAULT NULL,
`TotalAdjustedCost` double DEFAULT NULL,
`SPRating` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`MoodyRating` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`YieldCost` double DEFAULT NULL,
`YieldMarket` double DEFAULT NULL,
`YieldCall` double DEFAULT NULL,
`YieldPut` double DEFAULT NULL,
`YieldWorst` double DEFAULT NULL,
`WorstDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`WorstDate_Final` date DEFAULT NULL,
`CallDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`CallDate_Final` date DEFAULT NULL,
`CallPrice` double DEFAULT NULL,
`PutDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`PutDate_Final` date DEFAULT NULL,
`PutPrice` double DEFAULT NULL,
`FirstCouponDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`FirstCouponDate_Final` date DEFAULT NULL,
`LastCouponDate` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`LastCouponDate_Final` date DEFAULT NULL,
`Freq` int DEFAULT NULL,
`BondCalendarCode` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`SecTypeCode` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`Security` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_fi_details_MaturityDate_Final` (`MaturityDate_Final`),
KEY `idx_fi_details_PortfolioCode` (`PortfolioCode`),
KEY `idx_fi_details_PortfolioCode_MaturityDate_Final` (`PortfolioCode`,`MaturityDate_Final`),
KEY `idx_fi_details_manager_MaturityDate_Final` (`manager`,`MaturityDate_Final`),
KEY `idx_fi_details_MaturityDate_Final_manager` (`MaturityDate_Final`,`manager`)
) ENGINE=InnoDB AUTO_INCREMENT=234850 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE DEFINER=`root`#`localhost` PROCEDURE `fi_details_Worst_Date_Hartz_Brown`()
BEGIN
DROP TABLE IF EXISTS fi_details_temp;
CREATE table fi_details_temp AS (
SELECT
frm.fi_details.PortfolioCode,
frm.fi_details.MaturityDate_Final,
frm.fi_details.YieldWorst,
frm.fi_details.WorstDate_Final,
frm.fi_details.YieldMarket,
frm.fi_details.Symbol,
frm.fi_details.Security,
frm.fi_details.Coupon,
frm.fi_details.ParValue,
frm.fi_details.MarketValue,
frm.fi_details.TotalAdjustedCost,
frm.fi_details.AdjustedUnitCost,
frm.fi_details.YieldCost,
frm.fi_details.YieldCall,
frm.fi_details.YieldPut,
frm.fi_details.CallDate_Final
From
frm.fi_details
Where
frm.fi_details.manager = 'Hartz' And
frm.fi_details.PortfolioCode Like ('Brown%')
Group By
frm.fi_details.PortfolioCode,
frm.fi_details.MaturityDate_Final,
frm.fi_details.YieldWorst,
frm.fi_details.WorstDate_Final,
frm.fi_details.YieldMarket,
frm.fi_details.Symbol,
frm.fi_details.Security,
frm.fi_details.Coupon,
frm.fi_details.ParValue,
frm.fi_details.MarketValue,
frm.fi_details.TotalAdjustedCost,
frm.fi_details.AdjustedUnitCost,
frm.fi_details.YieldCost,
frm.fi_details.YieldCall,
frm.fi_details.YieldPut,
frm.fi_details.CallDate_Final
Order By
frm.fi_details.PortfolioCode,
frm.fi_details.WorstDate_Final );
SET #previousstate = NULL;
SELECT
if(#previousstate != frm.fi_details_temp.PortfolioCode, concat('*',#previousstate:= frm.fi_details_temp.PortfolioCode,'*'), frm.fi_details_temp.PortfolioCode) as 'Portfolio Code',
frm.fi_details_temp.MaturityDate_Final as 'Maturity Date',
frm.fi_details_temp.YieldWorst as 'Worst Yield',
frm.fi_details_temp.WorstDate_Final as 'Worst Date',
frm.fi_details_temp.YieldMarket as 'Yield to Market',
frm.fi_details_temp.Symbol,
frm.fi_details_temp.Security as 'Description',
frm.fi_details_temp.Coupon,
frm.fi_details_temp.ParValue as 'Par Value',
frm.fi_details_temp.MarketValue as 'Market Value xAI',
frm.fi_details_temp.TotalAdjustedCost as 'Adjusted Cost',
frm.fi_details_temp.AdjustedUnitCost as 'Unit Adjusted Cost',
frm.fi_details_temp.YieldCost as 'Yield to Cost',
frm.fi_details_temp.YieldCall as 'Yield to Call',
frm.fi_details_temp.YieldPut as 'Yield to Put',
frm.fi_details_temp.CallDate_Final as 'Next Call Date'
From frm.fi_details_temp;
DROP TABLE IF EXISTS fi_details_temp;
END

MySQL: character set utf8 giving error with datetime

I am new to MySQL and wants to create this table after reading tutorial I wrote this command but on MySQL Workbench it shows error for 4 line created_at attribute:
CREATE TABLE tweetMelbourne (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`geo_type` VARCHAR(8) CHARACTER SET utf8,
`created_at` DATETIME CHARACTER SET utf8 NOT NULL ,
`geo_coordinates_latitude` decimal(12,9) DEFAULT NULL,
`geo_coordinates_longitude` decimal(12,9) DEFAULT NULL,
`place_full_name` VARCHAR(35) CHARACTER SET utf8,
`place_country` VARCHAR(15) CHARACTER SET utf8,
`place_type` VARCHAR(18) CHARACTER SET utf8,
`place_bounding_box_type` VARCHAR(10) CHARACTER SET utf8,
`place_bounding_box_coordinates_NE_lat` decimal(12,9) DEFAULT NULL,
`place_bounding_box_coordinates_NE_long` decimal(12,9) DEFAULT NULL,
`place_bounding_box_coordinates_SW_lat` decimal(12,9) DEFAULT NULL,
`place_bounding_box_coordinates_SW_long` decimal(12,9) DEFAULT NULL,
`place_country_code` VARCHAR(5) CHARACTER SET utf8,
`place_name` VARCHAR(17) CHARACTER SET utf8,
`text` VARCHAR(140) CHARACTER SET utf8,
`user_id` INT,
`user_verified` VARCHAR(5) CHARACTER SET utf8,
`user_followers_count` INT,
`user_listed_count` INT,
`user_friends_count` INT,
`user_location` VARCHAR(30) CHARACTER SET utf8,
`user_following` VARCHAR(5) CHARACTER SET utf8,
`user_geo_enabled` VARCHAR(5) CHARACTER SET utf8,
`user_lang` VARCHAR(5) CHARACTER SET utf8,
PRIMARY KEY (id)
);
Error is :
Error Code: 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 'CHARACTER SET utf8
have your tried with removing 'CHARACTER SET utf8'
Thank you guys it worked. I used DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci at the end. So, I think no need to define character set for datetime because its made up of numbers and doesn't include character.

Not inserting Turkish characters in SQL table. Displays 'xxx??xxx' when selected

I am trying to add Turkish names on my table but then when displayed it gives me ? instead of any of them. Any help what I am missing here? This is my table:
CREATE TABLE IF NOT EXISTS `offerings` (
`dep` varchar(5) CHARACTER SET utf16 COLLATE utf16_turkish_ci DEFAULT NULL,
`grade` varchar(4) CHARACTER SET utf16 COLLATE utf16_turkish_ci DEFAULT NULL,
`section` varchar(3) CHARACTER SET utf16 COLLATE utf16_turkish_ci DEFAULT NULL,
`name` varchar(100) CHARACTER SET utf16 COLLATE utf16_turkish_ci DEFAULT NULL,
`teacher` varchar(50) CHARACTER SET utf16 COLLATE utf16_turkish_ci DEFAULT NULL,
`quota` varchar(2) CHARACTER SET utf16 COLLATE utf16_turkish_ci DEFAULT NULL,
`lec1` varchar(35) CHARACTER SET utf16 COLLATE utf16_turkish_ci DEFAULT NULL,
`lec2` varchar(35) CHARACTER SET utf16 COLLATE utf16_turkish_ci DEFAULT NULL,
`lec3` varchar(35) DEFAULT NULL,
`lec4` varchar(35) DEFAULT NULL,
`lec5` varchar(35) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16;
As suggested from the answer I choose here is the solution to the problem for whoever googles this topic. Special thanks to all who contributed in the solution of my problem.
CREATE TABLE IF NOT EXISTS `offerings` (
`dep` varchar(5) NOT NULL,
`grade` varchar(4) COLLATE utf8_unicode_ci NOT NULL,
`section` varchar(3) COLLATE utf8_unicode_ci NOT NULL,
`name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`teacher` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`quota` varchar(2) COLLATE utf8_unicode_ci,
`lec1` varchar(35) DEFAULT NULL,
`lec2` varchar(35) DEFAULT NULL,
`lec3` varchar(35) DEFAULT NULL,
`lec4` varchar(35) DEFAULT NULL,
`lec5` varchar(35) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
(Beginnings of an answer...)
Please don't use utf16; there is virtually no reason for such in a MySQL table.
So, assuming you switch to utf8, let's see if we can get rid of the ? problems.
utf8 needs to be established in about 4 places.
The column(s) in the database -- Use SHOW CREATE TABLE to verify that they are explicitly set to utf8, or defaulted from the table definition. (It is not enough to change the database default.)
The connection between the client and the server. See SET NAMES utf8.
The bytes you have. (This is probably the case.)
If you are displaying the text in a web page, check the <meta> tag.
What probably happened:
you had utf8-encoded data (good)
SET NAMES latin1 was in effect (default, but wrong)
the column was declared CHARACTER SET latin1 (default, but wrong)
Since the CHARACTER SET disagrees with what you have shown, the problem is possibly more complex. Please provide
SELECT col, HEX(col) FROM tbl WHERE ...
for some simple cell with Turkish characters. With this, I may be able to figure out what happened.
Also, Reference notes on encodings.
VARCHARs are character strings, while NVARCHARS are Unicode character strings. NVARCHARS require more bits per character to store, but have a greater range. Try updating your data types. This should fix your problem.
EDIT This answer is wrong. The OP clearly asked for a MySQL solution, but the above applies only to SQL Server.

error in creating mysql table

I get an error when i execute this script:
create table orbeon_form_definition_attach (
created timestamp(6),
last_modified_time timestamp(6),
last_modified_by varchar(255) CHARACTER SET utf8 COLLATE utf8_bin,
app varchar(255) CHARACTER SET utf8 COLLATE utf8_bin,
form varchar(255) CHARACTER SET utf8 COLLATE utf8_bin,
form_version int not null,
deleted char(1) COLLATE utf8_bin NOT NULL,
file_name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin,
file_content longblob
) engine = InnoDB;
The error is : Error Code: 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 (6), last_modified_time timestamp(6), last_modified_by varchar(255) at line 2 0.000 sec
any idea how to solve this??
datetime or timestamp does not have a length
create table orbeon_form_definition_attach
(
created datetime,
last_modified_time datetime,
last_modified_by varchar(255) CHARACTER SET utf8 COLLATE utf8_bin,
app varchar(255) CHARACTER SET utf8 COLLATE utf8_bin,
form varchar(255) CHARACTER SET utf8 COLLATE utf8_bin,
form_version int not null,
deleted char(1) COLLATE utf8_bin NOT NULL,
file_name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin,
file_content longblob
) engine = InnoDB;