数据库mysql常见报错指南

1.Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法

原因:设置的外键与另一个表中的唯一索引列(一般是主键)中的值不匹配
办法:删除要成为外键的列,再次创建并默认为NULL

坑点:想了半天,明明外键Spno都是NULL,怎么就一直还报这个错,后来才发现,我往NULL加了引号!!!逐渐失去耐心。。。

正解参考:

insert
into course
values('1', '数据库', NULL, 4);

 

 

2.  Mysql 插入中文错误:Incorrect string value: '\xE7\xA8\x8B\xE5\xBA\x8F...' for column 'course' at row 1

原因:数据库的编码格式为latin1 而我要将utf8的中文插入到数据库中。

办法:

查看数据表的编码:

 show create table Student; 


修改表的编码方式:

alter table Student default character set utf8;

(该命令用于将表Student的编码方式改为utf8)



修改字段的编码方式:

 alter table Student change Sname Sname varchar(20) character set utf8;

该命令用于将表Student中Sname字段的编码方式改为utf8

坑点:修改字段时注意Sname Sname(与前面字段名称一致)  verchar(20)(必须是在表中定义该字段时的格式

正解参考:

alter table student default character set utf8;
alter table sc default character set utf8;
alter table course default character set utf8;

/*修改字段*/
 alter table student change Sname Sname char(20) character set utf8;
 alter table student change Ssex Ssex char(1) character set utf8;

 

 

3. Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE tha

原因:mysql在执行删除更新语句时报这种错误,是因为在mysql在safe-updates模式中,如果你where后跟的条件不是主键id,那么就会出现这种错误

解决方法: 

1、SET SQL_SAFE_UPDATES = 0;执行该命令更改mysql数据库模式。
2、在where判断条件中跟上主键id    例如:delete from firstmysqldatabase.user where UserName='zhangsan' and ID>=0;

posted @ 2020-03-11 20:58  zyddd915  阅读(418)  评论(0)    收藏  举报