How to set the character set when creating a table in MySQL?
In MySQL, you can use the CREATE TABLE statement to create a table and set the character set.
Below is an example demonstrating how to create a table using the UTF-8 character set.
CREATE TABLE 表名 (
列名1 数据类型,
列名2 数据类型,
...
) CHARACTER SET = utf8;
In the example above, the CHARACTER SET clause is used along with the character set name to set the character set of a table. Here, we used the utf8 character set, but you can also choose other character sets like utf8mb4 or latin1 as needed.
To set the character set for each column, you can use the CHARACTER SET clause in the column definition. For example:
CREATE TABLE 表名 (
列名1 数据类型 CHARACTER SET utf8,
列名2 数据类型 CHARACTER SET utf8,
...
);
In the above example, each column is set using the CHARACTER SET clause to the utf8 character set.
Please note:
- Setting the character set when creating a table will impact the default character set of the table.
- If you want to set the character set for a column, it will override the default character set for the table.
- If the character set for a table or column is not explicitly defined, the default character set will be used.