MySQL: character set utf8 giving error with datetime - mysql

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.

Related

How to solve Unhandled exception error when importing csv file in MysqlWorkbench?

When I imported an .csv file using import wizard of MySQL Workbench (utf8), the following error popped out.
Unhandled exception: 'ascii' codec can't decode byte 0xc2 in position 444: ordinal not in range(128)
I'm new to SQL so I don't know how to fix this. Please help me.
I do not know how your table was created, but when I create it using:
CREATE TABLE `office_supplies` (
`Order ID` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`Order Date` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`Ship Mode` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`Region` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`Product ID` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`Category` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`Sub-Category` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`Product Name` varchar(245) CHARACTER SET utf8 DEFAULT NULL,
`Sales` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`Quantity` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`Discount` varchar(45) CHARACTER SET utf8 DEFAULT NULL,
`Profit` varchar(45) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
(which is, of course, not want you want, but .... 😉)
I can import your CSV:
Order ID,Order Date,Ship Mode,Region,Product ID,Category,Sub-Category,Product Name,Sales,Quantity,Discount,Profit,
CA-2015-110870,12/12/2015,First Class,West,TEC-AC-10002926,Technology,Accessories,Logitech Wireless Marathon Mouse M705,299.94,6,0,
CA-2015-110870,12/12/2015,First Class,West,OFF-SU-10001225,Office Supplies,Supplies,Staple remover,25.76,7,0,
CA-2014-143210,01/12/2014,First Class,East,TEC-PH-10004434,Technology,Phones,Cisco IP Phone 7961G VoIP phone - Dark gray,271.9,2,0,
using:
LOAD DATA
INFILE 'D:/MySQL Server 8.0/Uploads/office_supplies.csv'
INTO TABLE office_supplies
COLUMNS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
IGNORE 1 LINES
Note: I only had to add one ',' at the end of line 1.
More info on the LOAD DATA ...
I found out why there is an error. The column Product_name of my raw data contains values with special characters. Simply remove the special characters solves the problem. You can watch how to remove special characters here

Why is this MySQL CREATE TABLE statement failing?

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.

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.

View Japanese Characters in MySQL Workbench

I already tried This solution which says
ALTER TABLE title
CHARACTER SET utf8
COLLATE utf8_unicode_ci;
Ok here are some screen shots which might help you.
Update
here's what happens when i insert Japanese characters.
Update 2
Show create table gives this
CREATE TABLE `productInfo` (
`pID` int(11) NOT NULL AUTO_INCREMENT,
`pOperation` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`year` year(4) DEFAULT NULL,
`season` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
`pName` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`category` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`margin1` text CHARACTER SET latin1,
`margin2` text CHARACTER SET latin1,
PRIMARY KEY (`pID`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
just see that
DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
But now see that the query
SELECT character_set_name, collation_name
FROM information_schema.columns
WHERE table_schema = 'trac_data'
AND table_name = 'productInfo'
AND column_name = 'pOperation';
gives
character_set_name collation_name
'latin1' 'latin1_swedish_ci'
Thats weird !
Update 3
SELECT hex(pOperation),pOperation FROM trac_data.productInfo;
gave 3F3F3F3F3F which is hex code for actual '?' and not any japanese character so that means no japanese characters are being stored
You have a mix of charsets in your table structure. The table itself uses utf8, but the column in question uses latin 1. You have it defined that way. As long as you have an own charset for your column you can change the table's or the schema's column a thousand times. It won't have any effect on your column. So, instead change the column's charset to either default (to use that of the table) or make it using utf8 explicitely.
When you alter the column's charset existing data will be converted (if possible). Your wrong input however stays wrong, so you have to fill the data again.
Ok i found the cause
CREATE TABLE `productInfo` (
`pID` int(11) NOT NULL AUTO_INCREMENT,
`pOperation` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`year` year(4) DEFAULT NULL,
`season` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
`pName` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`category` varchar(40) CHARACTER SET latin1 DEFAULT NULL,
`margin1` text CHARACTER SET latin1,
`margin2` text CHARACTER SET latin1,
PRIMARY KEY (`pID`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
I noticed how in front of each column SET latin1 was present.
So I Just changed to sjis and problem solved.
You have to set the database collation to UTF-8, not only the table collation :
Here is the SQL script result :

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;