1.create dateabase + 数据库名。
2.create table +表名(字段。。。。);
3.查看表结构:desc +表名
4.修改表字段类型:alter table 表名modify column 字段名 修改类型 约束(not null);
5.查看全部数据。select *from 表名
6.修改表记录:update class set classTeacher = ‘唐欣’ where class_id = 01; //将classTeacher修改为唐欣在class_id = 01的地方。
7.删除字段:alter table 表名 drop 字段名。
8.查看数据库下面的表:show tables;
9.添加新字段:alter table 表名add 字段名 tinyint not null;
10。外键添加失败的原因Mysql error 1452 - Cannot add or update a child row: a foreign key constraint fails
报错的原因大概分为4种:
原因四:
先前的表中已经有了数据,重新设置外键会不兼容失败。
解决办法:清空之前表中的记录。delete from 表名。
原因一:
添加的外键列与另一个表的唯一索引列(一般是主键)的数据类型不同
原因二:
要添加外键的表类型与另一个表的存储引擎是不是都为innodb引擎
#查看表引擎
法一: show create table 表名;
法二:show table status from 数据库 where name=‘表名’;
法三:use information_schema;
select table_catalog,table_schema,table_name,engine from tables
where table_schema=‘数据库名’ and table_name=‘表名’;
原因三:
设置的外键与另一个表中的唯一索引列(一般是主键)中的值不匹配
#解决办法:删除要成为外键的列,再次创建并默认为NULL
小技巧:使用workbench添加外键时经常出现外键的参数勾选不上的情况,这时候建议到cmd中去添加