I have defined my tables as follows;
CREATE TABLE IF NOT EXISTS test.notes ( id INT(10)
UNSIGNED NOT NULL AUTO_INCREMENT, clientid INT(10) NOT NULL,
userid INT(10) NOT NULL,
notes NVARCHAR(256) NULL DEFAULT NULL, createddatetime TIMESTAMP
NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id)) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8;
here the notes column i defined as navarchar, but finally it turns as varchar column. Im using 6.3 version . What is wrong here?
From the MySQL documentation, we can see that internally MySQL will just map NVARCHAR to VARCHAR with a UTF-8 character set. The documentation mentions that the following definitions are all equivalent:
VARCHAR(10) CHARACTER SET utf8
NATIONAL VARCHAR(10)
NVARCHAR(10)
NCHAR VARCHAR(10)
NATIONAL CHARACTER VARYING(10)
NATIONAL CHAR VARYING(10)
Related
I am trying to create a simple table in mysql but I am getting a syntax error.
Here is the code:
CREATE TABLE survey(
id INTEGER(15) NOT NULL,
`name` UNICODE(65) NOT NULL,
parentId INTEGER(15) NOT NULL,
createdAt TIMESTAMP(30) NOT NULL,
modifiedAt TIMESTAMP (30) NOT NULL,
surveyUrl UNICODE(100) NOT NULL,
PRIMARY KEY(id)
);
I have tried changing the UNICODE to STRING and placing the column names between marks with no luck. Thank you!
a Datatype UNICOde and the prcision 30 for timestampo are not allowed.
You can switch to following format, iff you need another character set and collation besides the default
More about collations
CREATE TABLE survey(
id BIGINT NOT NULL,
`name` varchar(65) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
parentId BIGINT NOT NULL,
createdAt TIMESTAMP(6) NOT NULL,
modifiedAt TIMESTAMP (6) NOT NULL,
surveyUrl varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
PRIMARY KEY(id)
);
I am running MySQL 8.0.17 and trying to add a default value to a column definition, specifically a JSON column.
create table `test` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `notes` json default ('[]')) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
The query executes fine, but when I look at the table structure the default value is listed as "(_utf8mb4'[]')".
This even happens if I set a default value for a VARCHAR field, but enclose the string in parentheses.
I've also tried using the MySQL JSON_ARRAY() function.
create table `test` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `notes` json default (JSON_ARRAY())) default character set utf8mb4 collate 'utf8mb4_unicode_ci'
but this puts the "(json_array())" as a string as the default.
I feel like I'm doing this write based on the documentation. Is this a bug? or am I missing something?
Thanks!
USE raimohuybrechts;
CREATE TABLE UnitBase
(
Id INT NOT NULL UNIQUE AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(255) NOT NULL UNIQUE CHARACTER SET utf8,
Description VARCHAR(1024) CHARACTER SET utf8,
ShippingCostMultiplier FLOAT,
Code VARCHAR(2) NOT NULL UNIQUE
);
Error is :
line 4: #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 utf8, Description VARCHAR(1024)
CHARACTER SET utf8, Ship' at line 4
Can't really figure out what is wrong, as I use the exact same syntax on line 3, and it doesn't give an error there.
You may want to set the character set at the table-level.
But if you need to set it at the column-level, the column-level CHARACTER SET specification is part of the data_type, so it needs to come before other modifiers such as NOT NULL, UNIQUE, etc.
This should work:
CREATE TABLE UnitBase
(
Id INT NOT NULL UNIQUE AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(255) CHARACTER SET utf8 NOT NULL UNIQUE,
Description VARCHAR(1024) CHARACTER SET utf8,
ShippingCostMultiplier FLOAT,
Code VARCHAR(2) NOT NULL UNIQUE
);
You can't set Character to UTF8 to a field/column.
The correct string is:
CREATE TABLE UnitBase
(
Id INT NOT NULL UNIQUE AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
Description VARCHAR(1024),
ShippingCostMultiplier FLOAT,
Code VARCHAR(2) NOT NULL UNIQUE
) ENGINE=InnoDB CHARACTER SET=utf8;
Is there any shorter equivalent for the following query: (I can not alter the table). Are cast operators applicable here?
select convert(old_text using utf8) as text,
convert(rev_timestamp using utf8) as ts,
convert(rev_user_text using utf8) as user from revision;
Update: The table schema is:
CREATE TABLE `revision` (
`rev_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`rev_page` INT(10) UNSIGNED NOT NULL,
`rev_text_id` INT(10) UNSIGNED NOT NULL,
`rev_comment` TINYBLOB NOT NULL,
`rev_user` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`rev_user_text` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'latin1_bin',
`rev_timestamp` BINARY(14) NOT NULL DEFAULT '\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0',
`old_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`old_text` MEDIUMBLOB NOT NULL,
`old_flags` TINYBLOB NOT NULL,
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
the best is to alter your table and set utf8 for your needed columns instead of everytime converting them
ALTER TABLE t MODIFY old_text CHAR(50) CHARACTER SET utf8;
ALTER TABLE t MODIFY rev_timestamp CHAR(50) CHARACTER SET utf8;
ALTER TABLE t MODIFY rev_user_text CHAR(50) CHARACTER SET utf8;
and then select them normal.
select old_text as text, rev_timestamp as ts, rev_user_text as user
from revision;
note: about char(50) change it to your needs.
set names utf8;
SELECT old_text AS text, rev_timestamp AS ts, rev_user_text AS user
FROM revisions;
...but this will break if you are sending DML in a different character set.
The easiest way to modify, is to modify the my.ini file in mysql character set key,
default-character-set = utf8
character_set_server = utf8
After modification, restart the mysql service, service mysql restart
I exported a DB using dbForge (v6) and the whole script has a problem with this:
USE `global-cms-content2`;
CREATE TABLE `global-cms-content2`.umbracorelationtype (
ID int(11) NOT NULL AUTO_INCREMENT,
DUAL bit(1) NOT NULL,
PARENTOBJECTTYPE char(36) NOT NULL,
CHILDOBJECTTYPE char(36) NOT NULL,
NAME varchar(255) NOT NULL,
ALIAS varchar(100) DEFAULT NULL,
PRIMARY KEY (ID)
)
ENGINE = INNODB
AUTO_INCREMENT = 2
AVG_ROW_LENGTH = 16384
CHARACTER SET utf8
COLLATE utf8_general_ci;
Error is :
1 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 'DUAL bit(1) NOT NULL,
PARENTOBJECTTYPE char(36) NOT NULL,
CHILDOBJECTTYPE ' at line 3 SQL2.sql 2 1
Same error happens even when I create the table manually using the editor.
Why is MySQL not working with its own scripts?
Any ideas?
UPDATE:
This did it!
USE `global-cms-content3`;
CREATE TABLE `global-cms-content3`.umbracorelationtype (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`DUAL` bit(1) NOT NULL,
`PARENTOBJECTTYPE` char(36) NOT NULL,
`CHILDOBJECTTYPE` char(36) NOT NULL,
`NAME` varchar(255) NOT NULL,
`ALIAS` varchar(100) DEFAULT NULL,
PRIMARY KEY (ID)
)
ENGINE = INNODB
AUTO_INCREMENT = 2
AVG_ROW_LENGTH = 16384
CHARACTER SET utf8
COLLATE utf8_general_ci;
Still not sure why the export script or the backup DB script doesn't take care of reserved keywords... anyway
The word DUAL is a keyword. See Keyword list.
Try to quote the word as below:
CREATE TABLE `global-cms-content2`.umbracorelationtype (
ID int(11) NOT NULL AUTO_INCREMENT,
`DUAL` bit(1) NOT NULL,
PARENTOBJECTTYPE char(36) NOT NULL,
CHILDOBJECTTYPE char(36) NOT NULL,
NAME varchar(255) NOT NULL,
ALIAS varchar(100) DEFAULT NULL,
PRIMARY KEY (ID)
)