1、打开客户端连接Mysql:window+r cmd
- 在终端执行:
mysql -uroot -p
回车 - 查看Mysql的安装路径:
show variables like '%dir%';
2、数据库相关操作:
- 查询所有数据库:
show databases;
- 创建数据库:
create database 数据库名称;
- 查看数据库详情:
show create database 数据库名称
- 创建数据库(指定字符集):
create database 数据库名称 character set gbk/utf8;
- 删除数据库:
drop database 数据库名称;
- 使用数据库:
use 数据库名称;
3、数据库的储存引擎相关:https://www.cnblogs.com/y-rong/p/8110596.html
- 查看数据库所支持的储存引擎:
show engines;
或show engines \G;
- 查看当前数据库使用的储存引擎:
show variables like 'storage_engine';
或show variables like '%storage_engine%';
- 也可以通过
show create table 表名
,来查看某个表使用的引擎 - 如果创建时没自己改变则该表默认使用的引擎应该与数据库的一致;
- 数据库的储存引擎和表的储存引擎之间没有存在必然的联系,只是优先级不同,如果表没有设置字符集,则会使用数据库的默认字符集,仅此而已。
4、数据库表相关操作:
- 创建表:
create table 表名(字段名 类型);
- 创建表时修改表的引擎和字符集:
create table 表名(字段名 类型) engine = myisam/innodb(默认) charset = gbk;
- 查看表:
show tables;
- 查看某个表的详情:
show create table 表名;
- 查看某个表的字段:
desc 表名;
- 删除表:
drop table 表名;
5、数据库表字段相关操作:
- 改表名:
rename table 表名 to 新表名;
- 修改引擎和字符集:
alter table 表名 engine=myisam/innodb charset=gbk;
- 添加字段:
alter table 表名 add 字段名 类型 first/after XXX;
- 修改字段的名称:
alter table 表名 change 字段名 新字段名 类型;
- 修改字段的位置:
alter table 表名 modify 字段名 类型 first/after XXX;
- 删除字段:
alter table 表名 drop 字段名;
- 删除某表中的主键:
alter table 表名 drop primary key;
- 为插入好的字段添加主键:
alter table 表名 add primary key (主键);
6、数据相关:
- 插入数据:
- 先创建一个emp表:
create table emp(id int ,name varchar(10),age int,sal int);
- 全表插入数据(顺序不可以错,也不可以少添加属性):
insert into emp values(1,'tom',18,3000);
- 先创建一个emp表:
- 指定字段插入数据:
insert into emp(name,age)values('jerry',19);
insert into emp(name)values('李白');
- 批量插入数据:
insert into emp values(3,'刘备',28,60000),(4,'张飞',30,'90000');
insert into emp(name,age)values('悟空',500),('八戒',400);
-
查询数据:
- 查询全部数据的全部字段信息:
select * from 表名;
- 例:查询所有员工的姓名和年龄:
select name,age from emp;
- 查询全部数据的全部字段信息:
-
修改数据:
- 语法:
update 表名 set 字段名=值;
- 例:修改TOM的工资为3333:
update emp set sal = 3333 where name ='TOM';
- 语法:
-
删除数据:
- 语法:
delete from 表名 where 字段名=值;
- 例:删除emp表中年龄在25岁以下人:
delete from emp where age < 25;
- 语法:
7、中文字符集问题:set names gkb;