I was trying to get my column to be in set type on MySQL, but I got an error:
Syntax error near 'COLLATE 'latin1_swedish_ci' NOT NULL FIRST, COMMENT='' REMOVE PARTITIONING' at line 2
ALTER TABLE `Recipe`
CHANGE `Ingredients` `Ingredients` set COLLATE 'latin1_swedish_ci' NOT NULL FIRST,
COMMENT=''
REMOVE PARTITIONING;
Please help and appreciated.
COLLATE should be after CHARACTER SET, which is table level, not column level.
ALTER TABLE `Recipe`
CHANGE `Ingredients` `Ingredients` INT NOT NULL FIRST,
COMMENT=''
CHARACTER SET 'latin1' COLLATE 'latin1_swedish_ci'
REMOVE PARTITIONING;
SQLFidele demo:
http://sqlfiddle.com/#!9/d2f62a
Related
I am forward engineering a MySQL database from an EER diagram in MySQL workbench, and am being shown the following error on the execution of the statement below:
ERROR: Error 1115: Unknown character set: 'DEFAULT'
CREATE TABLE IF NOT EXISTS `recipes_database_2`.`cleaned_string` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`string` VARCHAR(511) CHARACTER SET 'DEFAULT' NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `string_UNIQUE` (`string` ASC) VISIBLE)
ENGINE = InnoDB
AUTO_INCREMENT = 1
DEFAULT CHARACTER SET = DEFAULT
What can I change to fix this? I want the character set to be the one I set as the default for the database.
Just don't specify the CHARACTER SET at all, then the
database character set and collation are used
as one can read from the documentation.
Mysql - phpmyadmin
What's wrong with this query?
ALTER TABLE `invoices`
CHANGE `status` `status` ENUM('paid','due','canceled','partial','cheque')
CHARACTER SET `utf8` COLLATE `utf8_general_ci` NOT NULL DEFAULT `due`;
I am getting this error:
Missing comma before start of a new alter operation. (near "CHARACTER SET") and not able to run this query on live server. Query was generated when I changed the table structure on my localhost during development.
I also tried below query mentioned in many threads but it also give same above error:
ALTER TABLE `invoices` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
Am not sure why. I also faced the same issue.
Though it appears like:
When removed both CHARACTER and COLLATE part from SQL, parser works fine.
i.e
Before
ALTER TABLE `place` CHANGE COLUMN `plc_location_type` `gplc_location_type` ENUM('LOCATION') CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL;
After
ALTER TABLE `place` CHANGE COLUMN `plc_location_type` `gplc_location_type` ENUM('LOCATION') NOT NULL;
REFERENCE:
https://github.com/phpmyadmin/sql-parser/issues/229
Good luck!
ALTER TABLE table_name
CHANGE old_column_name1 new_col_name1 Data Type,
CHANGE old_column_name2 new_col_name2 Data Type,
CHANGE old_column_name3 new_col_name3 Data Type;
What is wrong with this?
ALTER TABLE `groups`
ADD COLUMN `forum_enabled` enum('0','1') CHARACTER SET latin1 COLLATE
latin1_swedish_ci NOT NULL DEFAULT '0' AFTER `admindeco`;
ALTER TABLE `user_badges`
ADD UNIQUE INDEX `user_id, badge_id`;
ALTER TABLE `furniture`
ADD COLUMN`behaviour_data` int(11) NOT NULL DEFAULT 0 AFTER
`interaction_type`;
ALTER TABLE `users`
MODIFY COLUMN `rank_vip` int(1) NULL DEFAULT 1 AFTER `rank`;
ALTER TABLE `server_settings`
CHANGE COLUMN `variable` `key` varchar(255) CHARACTER SET latin1 COLLATE
latin1_swedish_ci NOT NULL DEFAULT 'server.variable' FIRST ;
ALTER TABLE `catalog_deals`
DROP COLUMN `page_id`,
DROP COLUMN `cost_credits`,
DROP COLUMN `cost_pixels`,
MODIFY COLUMN `items` text CHARACTER SET latin1 COLLATE
latin1_swedish_ci
NOT NULL AFTER `id`,
ADD COLUMN `room_id` int(11) NOT NULL AFTER `name`;
SET FOREIGN_KEY_CHECKS=0;
I am getting following error:
SQL query: ALTER TABLE groups ADD COLUMN forum_enabled ENUM( '0', '1' ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL DEFAULT '0' AFTER admindeco ; MySQL said: Documentation #1060 - Duplicate column name 'forum_enabled'
From the error message, it seems like the column you are trying to add in the first statement already exists in the table.
A simple solution is to DROP the column before you add it, like :
ALTER TABLE `groups`
DROP COLUMN `forum_enabled`;
This can be combined with the ADD command in a single statement, like :
ALTER TABLE `groups`
DROP COLUMN `forum_enabled`,
ADD COLUMN `forum_enabled` enum('0','1') ...
;
It is also possible to dynamically check if a column exists, then drop it if neede, like :
IF EXISTS (
SELECT 1
FROM information_schema.columns
WHERE
table_name = 'groups'
AND column_name = 'forum_enabled'
AND table_schema = DATABASE()
) THEN
ALTER TABLE `groups`
DROP COLUMN `forum_enabled`;
END IF;
I have the following UTF-8 table:
CREATE TABLE `table` (
`id` int(11) NOT NULL DEFAULT '0',
`description` mediumtext,
`description_info` mediumtext,
`rights` mediumtext,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
inside a UTF-8 database:
CREATE DATABASE `DB` /*!40100 DEFAULT CHARACTER SET utf8 */
in this table there are rows with LATIN1 characters, like: è or ò
My question is: how can I convert those characters?
THX!
Use a CONVERT:
SELECT
description,
CONVERT(description USING utg8)
FROM
`table`
I have found a solution here: http://www.percona.com/blog/2013/10/16/utf8-data-on-latin1-tables-converting-to-utf8-without-downtime-or-double-encoding/
Those are the steps I have applied to convert latin1 text inside the 'description' column to UTF8:
ALTER TABLE table CONVERT TO CHARACTER SET latin1 -- this is because my table is in UTF8
ALTER TABLE table CHANGE description description BLOB;
ALTER TABLE table CONVERT TO CHARACTER SET utf8, CHANGE description description mediumtext;
I don't know if this is the most effective method to this... but it works!
I got the following error while trying to alter a column's data type and setting a new default value:
ALTER TABLE foobar_data ALTER COLUMN col VARCHAR(255) NOT NULL SET DEFAULT '{}';
ERROR 1064 (42000): 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 'VARCHAR(255) NOT NULL SET DEFAULT '{}'' at line 1
ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
A second possibility which does the same (thanks to juergen_d):
ALTER TABLE foobar_data CHANGE COLUMN col col VARCHAR(255) NOT NULL DEFAULT '{}';
As a follow up, if you just want to set a default, pretty sure you can use the ALTER .. SET syntax. Just don't put all the other stuff in there. If you're gonna put the rest of the column definition in, use the MODIFY or CHANGE syntax as per the accepted answer.
Anyway, the ALTER syntax for setting a column default, (since that's what I was looking for when I came here):
ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT 'literal';
For which 'literal' could also be a number (e.g. ...SET DEFAULT 0). I haven't tried it with ...SET DEFAULT CURRENT_TIMESTAMP but why not eh?
If you want to add a default value for the already created column,
this works for me:
ALTER TABLE Persons
ALTER credit SET DEFAULT 0.0;
For DEFAULT CURRENT_TIMESTAMP:
ALTER TABLE tablename
CHANGE COLUMN columnname1 columname1 DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CHANGE COLUMN columnname2 columname2 DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
Please note double columnname declaration
Removing DEFAULT CURRENT_TIMESTAMP:
ALTER TABLE tablename
ALTER COLUMN columnname1 DROP DEFAULT,
ALTER COLUMN columnname2 DROPT DEFAULT;
In case the above does not work for you (i.e.: you are working with new SQL or Azure) try the following:
1) drop existing column constraint (if any):
ALTER TABLE [table_name] DROP CONSTRAINT DF_my_constraint
2) create a new one:
ALTER TABLE [table_name] ADD CONSTRAINT DF_my_constraint DEFAULT getdate() FOR column_name;
Accepted Answer works good.
In case of Invalid use of NULL value error, on NULL values, update all null values to default value in that column and then try to do the alter.
UPDATE foobar_data SET col = '{}' WHERE col IS NULL;
ALTER TABLE foobar_data MODIFY COLUMN col VARCHAR(255) NOT NULL DEFAULT '{}';
Try this
ALTER TABLE `table_name` CHANGE `column_name` `column_name` data_type NULL DEFAULT '';
like this
ALTER TABLE `drivers_meta` CHANGE `driving_license` `driving_license` VARCHAR(30) NULL DEFAULT '';
Use this approach:-
ALTER TABLE foobar_data
CHANGE COLUMN col VARCHAR(255) NOT NULL SET DEFAULT '{}';