Home > Database > Mysql Tutorial > body text

How do you change the default character set of a MySQL table from latin1 to utf8?

Linda Hamilton
Release: 2024-10-28 01:40:29
Original
137 people have browsed it

How do you change the default character set of a MySQL table from latin1 to utf8?

Changing the Default Character Set of a MySQL Table

MySQL allows you to specify a default character set for a table when it's created. By default, MySQL uses latin1 as the character set. However, you may need to change this to accommodate data in a different character set, such as utf8.

Problem

Consider the following MySQL table with a default character set of latin1:

<code class="sql">CREATE TABLE `etape_prospection` (
    `etape_prosp_id` int(10) NOT NULL AUTO_INCREMENT,
    `type_prosp_id` int(10) NOT NULL DEFAULT '0',
    `prosp_id` int(10) NOT NULL DEFAULT '0',
    `etape_prosp_date` datetime DEFAULT NULL,
    `etape_prosp_comment` text,
    PRIMARY KEY (`etape_prosp_id`),
    KEY `concerne_fk` (`prosp_id`),
    KEY `de_type_fk` (`type_prosp_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;</code>
Copy after login

You want to change the default character set of this table to utf8.

Solution

To change the default character set of a table and its character columns to a new character set, use the following statement:

<code class="sql">ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;</code>
Copy after login

In this case, the query would be:

<code class="sql">ALTER TABLE etape_prospection CONVERT TO CHARACTER SET utf8;</code>
Copy after login

After executing this statement, the default character set of the etape_prospection table and all of its character columns will be changed to utf8.

The above is the detailed content of How do you change the default character set of a MySQL table from latin1 to utf8?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!