MySQL自帶資料庫test編碼問題

2021-07-10 03:38:40 字數 1625 閱讀 6112

今天突然遇到插入中文失敗的情況,如下:

error 1366 (hy000): incorrect string value: '\xe5\xb0\x8f\xe6\x9d\x8e' for column 'name' at row 1

經分析,資料庫編碼問題:

1、my.ini配置檔案編碼均已調整為utf8。

2、安裝完mysql後有個資料庫的名字叫test,就用了一下,也沒在意編碼(因為配置檔案中已經設定好了),建了個測試表,如下:

create table inttest(

id int unsigned not null primary key comment '編號',

name varchar(30) not null comment '姓名',

age int not null comment '年齡'

);插入資料:

insert into inttest values(1,'津津',11);

報錯,如:error 1366 (hy000): incorrect string value: '\xe5\xb0\x8f\xe6\x9d\x8e' for column 'name' at row 1

然後去網上查資料時編碼問題,一查test資料庫的編碼,查詢語句如下:

a.進入test資料庫:use test

b.查詢編碼:show variables like 'character_set_database';

結果:資料庫編碼格式為latin1

3、需要調整的地方

a.修改資料庫編碼:

語法:alter database 資料庫名 character set 編碼格式 collate 排序規則;     

示例:alter databasetestcharacter set utf8 collate utf8_general_ci;

b.修改資料表編碼:

語法:alter table 表名 character set 編碼;

示例:alter table inttest character set utf8;

c.修改字段(涉及到中文的字段)編碼:

使用修改字段型別語法即可:alter table 表名 modify 欄位名  新字段型別 character set 編碼格式 comment '字段備註';      

示例:alter table inttest modify name varchar(30) character set utf8 comment '姓名';

結論:1、使用資料庫前要先檢視編碼是否和配置檔案中已設定好一致;

2、若插入資訊失敗或存在亂碼,需檢視資料庫編碼、資料表編碼、字段編碼

a.檢視資料庫編碼:

首先進入資料庫:use 資料庫名

然後查詢編碼   :show variables like 'character_set_database';

b.檢視資料表編碼:show create table 表名;

c.檢視字段編碼:b中也可以看到,還有一種方式:show full fields from 表名;

切莫大意,仔細點就可以避免類似錯誤了!

MYSQL 自帶4個預設資料庫

預設資料庫分類 information schema performance schema mysql test informance schema 儲存了mysql服務所有資料庫的資訊。具體mysql服務有多少個資料庫,各個資料庫有哪些表,各個表中的字段是什麼資料型別,各個表中有哪些索引,各個資料...

資料庫自帶角色

blkadmin 執行bulk insert 語句 dcreator 建修改刪除和還原資料庫 diskadmin 盤檔案 processadmin 管理在 sql中執行的程序 securityadmin 理伺服器登入賬戶 serveradmin 置伺服器 範圍設定 setupadmin sysadm...

SQL server自帶的資料庫

select name from sysdatabases 在master資料庫下查詢 前六個是系統自帶的資料庫 master 記錄了系統級別的資訊,包含所有的登陸資訊,系統設定資訊,初始化資訊等 不可以刪除 tempdb 臨時資料庫,儲存臨時表和臨時儲存過程以及儲存空間的要求 model 為使用者...