Display collation in create table output - mysql

I'm writing a set of SQL statements in MySQL to create and modify a few tables. I need to get my output to match a document of sample output exactly (this is for school).
When I show my create table statements, all varchar columns need to look like this:
`name` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
but they weren't showing the collation. I tried changing the declaration to
name varchar COLLATE utf8_unicode_ci DEFAULT NULL,
but this caused the output to show both the charset and collation, and I need to be showing just the collation. The sample output document was created on Unix, while I am on Windows, so this could be the source of the difference, but I need to know for sure.
Is there a way I can alter my queries to show collation or is this just a Unix Windows inconsistency?

To be honest, I doubt very much that anyone intends for you to obtain output that is identical verbatem—it's more likely that they require it to be identical semantically. However, you might play around with the table's default charset/collation to see whether that makes a difference to the output obtained from SHOW CREATE TABLE:
ALTER TABLE foo CHARACTER SET utf8 COLLATE ut8_bin;
Failing that, it could be a difference between MySQL versions.

Related

Create integer only table with a default charset, bad idea?

When I create a table, even if it's using integers only, I set the default charset to utf8 (because I copy paste the code and because in case I introduce a string column in the future).
Example:
CREATE TABLE IF NOT EXISTS `articles` (
`id` smallint(6) unsigned NOT NULL,
`disabled` tinyint(1) unsigned NOT NULL DEFAULT 0,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
However, i'm wondering if it's affecting "performances" to have a default charset in a table that do not make use of it.
Tables have a character set no matter what, so no, there's no performance issue, and UTF-8 is a good default choice (but utf8 is not). But you still shouldn't do that.
It is a bad practice to add a default character set unless you need to specify one. This overrides the default character set of the database which might not be utf8. You're risking making a table with a different character set than every other table causing confusion.
Instead, make sure the server and database character set are set correctly. Then let your tables use the default, unless you have a specific reason to do otherwise.
For example, UTF-8 is a good default choice, but MySQL got UTF-8 wrong. utf8 cannot handle all of UTF-8. You should instead be using utf8mb4 (UTF-8 4-byte). The database might correctly use utf8mb4, but you're overriding that with a less capable character set.
See Specifying Character Sets and Collations and Unicode Support.
The DEFAULT CHARSET clause at the bottom of your table creation is only metadata. It is only used if you add a CHAR/VARCHAR/TEXT column and don't explicitly define the column's character set. Then the table's default character set is used.
Tables don't have any performance characteristic — they are just storage. Queries have performance.
Since your table has no columns with character sets, there can be no query against this table that is affected by the character set. Therefore the default character set has no effect.

tables sum collation is latin1_swedish_ci, but all individual tables show utf8_general_ci

This MySQL db was set up in Godaddy from installing WordPress. Things on the site were acting glitchy - swapping out theme and deactivating plugins didn't help, so I decided to take a look at the tables using phpmyAdmin.
I've never seen this before - all tables use utf8_general_ci (there are 13 tables), but at the bottom is the summary of everything and the collation shows latin1_swedish_ci, and not the utf8...
Seems like this shouldn't be. What can I do to make it all uniform, using the utf8... and not the latin1_swedish?
To help explain, SHOW CREATE TABLE gives you something like this:
CREATE TABLE `h2u` (
`c` varchar(9) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
This says that the column c is utf8 (and some utf8 collation, probably utf8_general_ci), but the default for any other columns you might add is latin1 (and some latin1 collation, probably latin1_swedish_ci).
The CHARACTER SET and COLLATION on the column is what matters.
(It is sad that 3rd party software, while trying to be helpful, sometimes obscures useful info.)
Edit
I would guess from the picture that the default for the database is latin1 / latin1_swedish_ci and the default for each table is utf8 / utf8_general_ci. But the important thing is what the setting is for each column; the image does not show that.
The character set and collation for each column is important if you are using anything other than English text. Are you?
Yes, you have observed something strange. I am trying to say that it is probably not important, and very unlikely to cause any glitches other than with non-English text.
The following would have created output similar to the image:
CREATE DATABASE wp DEFAULT CHARACTER SET latin1;
CREATE TABLE wp_users (...) DEFAULT CHARACTER SET utf8; -- overriding the 'latin1'

Changing collation on indexed columns without changing data

I am trying to change the collation on a bunch of columns. I don't want to mess up my current data, so I've been looking at doing something like in this answer:
ALTER TABLE something MODIFY name BLOB;
The next step is to convert the column to a nonbinary data type with the proper character set:
ALTER TABLE something MODIFY name VARCHAR(12) CHARACTER SET hebrew COLLATE hebrew_bin;
Or Try with this:
ALTER TABLE something MODIFY name VARCHAR(12) CHARACTER SET utf8 COLLATE utf8_unicode_ci
Unfortunately, mysql won't let me convert an indexed column to a blob.
SQL error: [1170] - BLOB/TEXT column 'baz' used in key specification without a key length
Query: ALTER TABLE foo.bar MODIFY baz blob;
Is there a way around this? Or do I need to somehow remove my indexes and rebuild them after the conversion?
Don't do the "2-step ALTER" unless the CHARACTER SET was wrong but the data was 'right'. If you want to convert both the CHARACTER SET and the data, use
ALTER TABLE something CONVERT TO CHARACTER SET ...;
See Case 5 in my blog, which, as you might guess, has other cases.
As for the error 1170 -- that had to do with some INDEX. Before I help you with that, decide whether it is relevant.

mysql 5.6: remove explicit column collate

I am running mysql 5.6.
Some of the columns in a schema that I inherited from previous developers have an explicitly specified collate clause.
All explicitly specified collate clauses are the same as the database's default collate.
Is there any way to remove the explicit column collate clauses?
There should be no functional collating differences versus my current collate, but I want the following:
to get column definitions sans collate clauses when I request a
create table statement from mysql (I want to be able to compare
table creation scripts from a code repository with create table
statements obtained from different instances of the schema on
different mysql servers; the explicit column collate clauses are
only in some instances, but not others, which would require me to
use a more complex diff than a plain text diff)
to have the collate of these columns automatically change to
whatever is the new default database collate if I ever change it
1) is much more important than 2), however, since I will probably never change the collate again.
Thanks.
Instead of using SHOW CREATE TABLE, fetch the equivalent data from information_schema tables TABLES and COLUMNS.
Meanwhile, do you have an example of the COLLATION clause being present in some cases, but not in other cases?

Sense of command collate in create table sql

I understand function of command collate (a little). It is truth that I did not test if it is possible to have tables with various collation (or even various charset) inside one DB.
But I found that (at least in phpmyadmin) when I create any DB, I set its charset and collation - and if I miss this command in CREATE TABLE ..., then automatically will be set collation set in creation of DB.
So, my question is: What is sense of presence of command collate in sql of CREATE TABLE ... if it can be missing there - and is recommended to have collate in CREATE TABLE ... or is it irrelevant?
In SQL Server if you don't specify the COLLATE it is defaulted to what ever DB is set to. Thus there is no danger in not specifying.
In MySQL behavior is the same:
The table character set and collation are used as default values for
column definitions if the column character set and collation are not
specified in individual column definitions. MySQL Reference
Collate is only used when you want to specify to non-default value. If all you are using is English character set than you have nothing to worry about it. If you store data from multiple languages than you have specify specific collation to ensure what characters are stored correctly.