MySQL常用命令和常见问题

时间:2023-03-08 20:18:49
MySQL常用命令和常见问题

MySQL常用命令和常见问题

--创建数据库并设置字符集
create database wip default character set utf8 collate utf8_general_ci; -- 查看字符集变量
show variables like 'character%'; -- 备份数据库test到文件test.sql
mysqldump -uroot -pmax123 test > test.sql -- 恢复数据库
mysql -uroot -pmax123 test < test.sql --注册系统服务,服务名缺省为mysql
mysqld --install [mysql_service_name] --注销系统服务
mysqld --remove [mysql_service_name] --测试
mysql -uroot -p --查看版本, 注意是大V
mysql -V --环境变量
--可以新建一个MySQL的变量,PATH中加%MySQL%,如果有升级或者修改,这是最佳方式
--MySQL = C:\Program Files (x86)\mysql-5.6.24\bin --修改密码
mysqladmin -u root -p password newpassword

配置默认编码

my.ini中如下修改

MySQL常用命令和常见问题

windows下删除服务

sc delete mysql
[SC] DeleteService 成功,需要重启

或者直接编辑注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services,删除服务后重启电脑

启动服务

net start MySQL

外键

外键必须是parent表的主键/unique 字段,否则添加不成功

--ERROR 1215 (HY000): Cannot add foreign key constraint
--类似primary key的设置方式
foreign key (user_id) references user (id) on delete cascade
--建表后alter table
alter table team_member add constraint fk_team_member_user_id foreign key (user_id) references user (id) on delete cascade;

By the SQL standard, a foreign key must reference either the primary key or a unique key of the parent table. If the primary key has multiple columns, the foreign key must have the same number and order of columns