Why this IF NOT EXISTS statement is not ok? - mysql

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

Related

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!

Character Set utf8 giving syntax error

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;

MySQL castr operator

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

Is a master table with only one column necessary in this mysql example?

I have a mysql DB with a table that holds version information for multiple other tables. In order to link to the same family of versions I have a version_master table that holds a primary key to the family of versions that the link refers to. I was wondering if there was a more elegant solution without the need for a version_master table.
CREATE TABLE IF NOT EXISTS `version` (
`version_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`version_master_id` int(10) unsigned NOT NULL,
`major` int(10) unsigned NOT NULL DEFAULT '0',
`minor` int(10) unsigned NOT NULL DEFAULT '0',
`patch` int(10) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`version_id`));
CREATE TABLE IF NOT EXISTS `version_master` (
`version_master_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY);
CREATE TABLE IF NOT EXISTS `needs_versions` (
`needs_versions_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`name` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`version_master_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`needs_versions_id`));
In this example you can certainly eliminate the version_master table and use the combination of version_id and version_master_id fields as an index. I think you can just drop it, because nothing seem to refer to it with a foreign key.
However, having version_master would be a good idea if you had additional information associated with each family of versions.
Also, you are trying to make a primary key out of the undefined column offer_type_id. It is not clear whether you can logically merge needs_versions with version_master or not. The name itself is not very descriptive. I would recommend not to use verbs in table names.
The other common way to do this is to use SEQUENCEs.
But MySQL does not seem to support them, at least the MySQL manual contains a section on how to simulate sequences using a one row, one column table:
Create a table to hold the sequence
counter and initialize it:
CREATE TABLE sequence (id INT NOT NULL);
INSERT INTO sequence VALUES (0);
Use the table to generate sequence
numbers like this:
UPDATE sequence SET id=LAST_INSERT_ID(id+1);
SELECT LAST_INSERT_ID();