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

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.

Related

Can mysql charset for table and column be different?

Does it makes sense to have two different charset for table and a single column in the same table ? or will it create problem, especially for the below mentioned example ?
For example,
Table charset - latin1
Column C1 charset - utf8mb4
Tables don't have a charset anyway, the only thing they have is a default charset. The only thing that has an actual "physical" charset are columns, because they're the only thing that actually stores data. The way it works is that if you're not setting an explicit charset for a column, the table's default is used. And if the table doesn't have a default, the database's default is used. And if that doesn't have a default, the server's default is used.

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'

How to store weird chars in the correct way in a MySQL DB

I need to store some data in a MySQL table, but I got some problems with theese kind of characters: "ā" "æ" "ō" "ĕ" (and so on)
Till now I had theese data stored in a SQLlite database and it was great because all was good, but now I'm trying to export it in a MySQL DB, but the strange chars are stored not in the good way, even if I tried different char encode. (UTF-8, UTF-16, latin blah blah)
Does anyone know the correct way to do so?
thanks a lot!!
utf8 needs to be established in about 4 places.
The column(s) in the database -- Use SHOW CREATE TABLE to verify that they are explicitly set to utf8, or defaulted from the table definition. (It is not enough to change the database default.)
The connection between the client and the server. See SET NAMES utf8.
The bytes you have.
If you are displaying the text in a web page, check the <meta> tag.
Either you can switch to BLOB datatype, or if you insist on using TEXT/VARCHAR/CHAR then you need to change charset of your table and database as shown below.
CREATE DATABASE mydbname
CHARACTER SET utf8
COLLATE utf8_general_ci;
USE mydbname;
CREATE TABLE `mytable` (
`data` varchar(200) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
If you already have a database with utf8 charset and collation set to utf8_general_ci then you can simply alter your table as mentioned below:
ALTER TABLE `mytable` CHANGE `data` `data` VARCHAR(100)
CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;

Display collation in create table output

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.

How to change the default collation of a table?

create table check2(f1 varchar(20),f2 varchar(20));
creates a table with the default collation latin1_general_ci;
alter table check2 collate latin1_general_cs;
show full columns from check2;
shows the individual collation of the columns as 'latin1_general_ci'.
Then what is the effect of the alter table command?
To change the default character set and collation of a table including those of existing columns (note the convert to clause):
alter table <some_table> convert to character set utf8mb4 collate utf8mb4_unicode_ci;
Edited the answer, thanks to the prompting of some comments:
Should avoid recommending utf8. It's almost never what you want, and often leads to unexpected messes. The utf8 character set is not fully compatible with UTF-8. The utf8mb4 character set is what you want if you want UTF-8. – Rich Remer Mar 28 '18 at 23:41
and
That seems quite important, glad I read the comments and thanks #RichRemer . Nikki , I think you should edit that in your answer considering how many views this gets. See here https://dev.mysql.com/doc/refman/8.0/en/charset-unicode-utf8.html and here What is the difference between utf8mb4 and utf8 charsets in MySQL? – Paulpro Mar 12 at 17:46
MySQL has 4 levels of collation: server, database, table, column.
If you change the collation of the server, database or table, you don't change the setting for each column, but you change the default collations.
E.g if you change the default collation of a database, each new table you create in that database will use that collation, and if you change the default collation of a table, each column you create in that table will get that collation.
It sets the default collation for the table; if you create a new column, that should be collated with latin_general_ci -- I think. Try specifying the collation for the individual column and see if that works. MySQL has some really bizarre behavior in regards to the way it handles this.
may need to change the SCHEMA not only table
ALTER SCHEMA `<database name>` DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci ;
as Rich said - utf8mb4
(mariaDB 10)