Windows下使用記事本編輯MySQL db.sql報中文錯誤
阿新 • • 發佈:2018-11-04
即便已經設定了utf-8字符集
DROP DATABASE IF EXISTS `db_lab2`;
CREATE DATABASE `db_lab2` DEFAULT CHARACTER SET utf8;
USE `db_lab2`;
CREATE TABLE `tb_student`(
`pk_sno` INT(10) UNSIGNED AUTO_INCREMENT COMMENT '學號',
`sname` VARCHAR(20) NOT NULL COMMENT '姓名',
`ssex` VARCHAR(4) COMMENT '性別',
`sage` SMALLINT UNSIGNED COMMENT '年齡',
`sdept` VARCHAR(10) NOT NULL COMMENT '系別',
PRIMARY KEY (`pk_sno`),
CONSTRAINT chk_sex CHECK (`ssex` = '男' OR `ssex` = '女')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tb_course`(
`pk_cno` SMALLINT UNSIGNED AUTO_INCREMENT COMMENT '課程號',
`cname` VARCHAR(20) UNIQUE COMMENT '課程名',
`cpno` SMALLINT UNSIGNED COMMENT '先行課',
PRIMARY KEY (`pk_cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tb_sc`(
`pk_sno` INT(10) UNSIGNED AUTO_INCREMENT COMMENT '學號',
`cno` SMALLINT UNSIGNED COMMENT '課程號',
`grade` SMALLINT UNSIGNED COMMENT '成績',
PRIMARY KEY (`pk_sno`),
FOREIGN KEY (`pk_sno`) REFERENCES `tb_student`(`pk_sno`),
FOREIGN KEY (`cno`) REFERENCES `tb_course`(`pk_cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `tb_student` VALUES(1, "小米", "男", 20, "計算機系");
而且MySQL的預設編碼也是utf-8 (可以如下命令檢視字符集)
SHOW VARIABLES LIKE '%char%';
即便所有都設定好了,但是source lab2.sql (上面的sql程式碼),依舊顯示下面這句中文錯誤。
INSERT INTO `tb_student` VALUES(1, "小米", "男", 20, "計算機系");
但是複製這句到mysql>下執行是不會報錯的
這也說明了字符集沒有錯,是記事本的錯
把這些程式碼換個編輯器也不會報錯,比如Visual Code