Character Set utf8 giving syntax error - mysql

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;

Related

Cannot create table in MYSQL with UNICODE and INTEGER types, ERROR: 1064

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

Why this IF NOT EXISTS statement is not ok?

I'm trying to create a table but it keeps giving me error and I'm not able to figure out the problem even after checking the manual
That's the code:
CREATE TABLE IF NOT EXISTS table1
(ID BIGINT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_cI NOT NULL,
VALUE INT(11) NOT NULL,
ATTACHMENT TEXT NULL DEFAULT NULL)
I solved by using a different word from VALUE. It is already defined as keyword

mysql workbench converts the nvarchar columns to varchar?

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)

MySQL Default Value as Expression is Wrong

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!

Enum values and Error 1064

I have a problem with this error.
here is the code to create the table
CREATE TABLE `qa`.`question`(
`id` INT(9) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(256) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`password` VARCHAR(256) CHARACTER SET utf16 COLLATE utf16_general_ci NOT NULL,
`email` VARCHAR(512) NOT NULL,
`phone` INT(11) NOT NULL,
`content` TEXT NOT NULL,
`status` ENUM(0) NOT NULL,
PRIMARY KEY(`id`)
) ENGINE = InnoDB CHARSET = utf8 COLLATE utf8_general_ci;
I see the same question , but none of them occur in my code .
I think about default value too , but I receive this:
Invalid value for id (error #1067)
I can't reproduce the error you got. I got a different 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 '0) NOT NULL,
This is because the elements of an ENUM must be strings, not numbers. I changed it to:
`status` ENUM('OK', 'FAIL') NOT NULL,
and the table creation succeeded.
The error you got would happen if you tried to assign a default value to the id column, because it's also declared to use AUTO_INCREMENT. See #1067 - Invalid default value for 'bonusid' how can i fix this error?