欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql/mariadb学习过程中出现的问题与解决

程序员文章站 2022-04-18 11:22:18
mysql> insert into student values(162111,'周帅','男',21,'CS'); ERROR 1062 (23000): Duplicate entry '16211160127' for key 'PRIMARY'(duplecate为重复的意思) prima... ......
mysql> insert into student values(162111,'周帅','男',21,'CS');
ERROR 1062 (23000): Duplicate entry '16211160127' for key 'PRIMARY'(duplecate为重复的意思)
primary key 主键不能插入与其相同的字段 同 unique

ERROR 1025 (HY000): Error on rename of './test/#sql-27c_2308' to './test/student' (errno: 150)
更改类型编码类型时 出现此错误一般为有外键约束 解决方法 暂时停止外键检查 set foreign_key_checks=0;

数据库刚安装时数据库的默认编码可能并不一定为utf8的格式,那么存入中文数据时可能会出错以下为解决方法:
1.首先将mariadb的客户端以及数据库的默认编码改成utf8(基于cento7.2 mariadb5.7):
查看mariadb/mysql数据库的相关编码:
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.38 sec)
这其中有database和server不是utf8的格式,则进入/etc/my.cnf.d目录中更改配置文件:
在client.cnf中的[client]下添加:
[client]
default-character-set=utf8
在server.cnf中的[mysqld]下添加:
[mysqld]
character-set-server=utf8
重启数据库,查看mariadb/mysql数据库的相关编码:
mysql> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.38 sec)
2.因为已建立的数据库默认的编码格式不为utf8则虽然更改默认的编码格式后,已建立的数据库并不会更改,在之后新创建的表格中也会默认继承数据库原来的编码格式,因此这里来更改单个数据库的默认编码:
首先选择要更改的数据库
mysql> use test
Database changed
查看test数据库的编码格式:
mysql> show create database test;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.38 sec)
更改数据库的编码格式:
mysql> alter database test default character set utf8;
Query OK, 1 row affected (0.38 sec)
再次查看是否更改成功:
mysql> show create database test;
+----------+---------------------------------------------------------------+
| Database | Create Database                                               |
+----------+---------------------------------------------------------------+
| test     | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.38 sec)
新建一个表格来确认:
mysql> create table emp(empno numeric(4) primary key,ename varchar(10) not null,job varchar(9),mgr numeric(4),hiredate date, sal numeric(7,2),comm numeric(7,2),deptno numeric(2));
Query OK, 0 rows affected (0.41 sec)
查看emp表格中的相关信息:
mysql> show full columns from emp;

 mysql/mariadb学习过程中出现的问题与解决

查看student表中的编码:
mysql> show full columns from student;
mysql/mariadb学习过程中出现的问题与解决
修改字段中的编码:
mysql> alter table student convert to character set utf8 collate utf8_general_ci;
ERROR 1025 (HY000): Error on rename of './test/#sql-27c_2308' to './test/student' (errno: 150)
更改类型编码类型时 出现此错误一般为有外键约束 解决方法 暂时停止外键检查 set foreign_key_checks=0;
mysql> show full columns from student;
mysql/mariadb学习过程中出现的问题与解决
再来查看一下,可见所有的编码都已经改成utf8的格式;
2018-04-29