考慮這個見下表:MySQL的重置列字符集爲默認
CREATE TABLE `user` (
`modified_at` datetime DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(40) CHARACTER SET utf8mb4 DEFAULT NULL,
`middle_name` varchar(40) CHARACTER SET utf8mb4 DEFAULT NULL,
`last_name` varchar(40) CHARACTER SET utf8mb4 DEFAULT NULL,
`username` varchar(100) CHARACTER SET utf8mb4 DEFAULT NULL,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
是否可以刪除該列特定字符集,即first_name
,last_name
沒有列字符集覆蓋,以便它採取表級字符集設置?
換句話說,什麼是ALTER TABLE語句來獲取表格定義成爲如下所示?
CREATE TABLE `user` (
`modified_at` datetime DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(40) DEFAULT NULL,
`middle_name` varchar(40) DEFAULT NULL,
`last_name` varchar(40) DEFAULT NULL,
`username` varchar(100) DEFAULT NULL,
`active` tinyint(1) NOT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
我已經試過
ALTER TABLE user MODIFY first_name varchar(40) DEFAULT NULL
和
ALTER TABLE user MODIFY first_name varchar(40) CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL;
無論那重置/刪除字符集設置在列覆蓋,而是增加了整理,所以該列看起來像這樣first_name varchar(40) COLLATE utf8mb4_unicode_ci DEFAULT NULL
UPDATE
順便說一句,由於該表中定義的默認字符集和默認排序規則,我倒覺得varchar
列本應繼承表默認排序規則,但他們不!一旦在列級別覆蓋字符集,MySQL會錯誤地選擇覆蓋的字符集的默認排序規則,在我的情況下,utf8mb4_general_ci
是utf8mb4
字符集的默認排序規則!請參閱下面的列說明
mysql> SHOW FULL COLUMNS FROM user;
+-----------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-----------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
| modified_at | datetime | NULL | YES | | NULL | | select,insert,update,references | |
| created_at | datetime | NULL | YES | | NULL | | select,insert,update,references | |
| id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| first_name | varchar(40) | utf8mb4_unicode_ci | YES | | NULL | | select,insert,update,references | |
| middle_name | varchar(40) | utf8mb4_general_ci | YES | | NULL | | select,insert,update,references | |
| last_name | varchar(40) | utf8mb4_general_ci | YES | | NULL | | select,insert,update,references | |
| username | varchar(100) | utf8mb4_general_ci | YES | UNI | NULL | | select,insert,update,references | |
| active | tinyint(1) | NULL | NO | | NULL | | select,insert,update,references | |
+-----------------+--------------+--------------------+------+-----+---------+----------------+---------------------------------+---------+
8 rows in set (0.01 sec)
謝謝。
此[鏈接](https://codex.wordpress.org/Converting_Database_Character_Sets#The_basic_ALTER_TABLE_command_with_a_specified_charset)建議從一個字符集到另一個2通轉換 - 也許這將工作 – PaulF
@PaulF我認爲這工作!稍後再試。 – Devy
那,我說得太快了。它沒有工作:(在調查這個問題時,它確實給了我一個線索,我自己解決了我的原始問題! – Devy