mysql的常用指令以及增删改查

目录

  • mongodb常用指令
  • 导出库
  • 导入库
  • 导入数据
  • 导出单张表
  • 导出所有表结构
  • 导出单张表结构
  • 查看所有库
  • 创建库
  • 删除指定库
  • 使用指定库
  • 查看当前使用的库
  • 查看当前库下所有表
  • 创建表
  • 删除表
  • 查看表结构
  • 查看建表语句
  • 查看引擎
  • 修改表名字
  • 修改表字段
  • 修改表字段数据类型
  • 插入新的字段
  • 删除字段
  • 在指定字段后面插入新的字段
  • 在开头查如新的字段
  • 数据库索引
  • 查看索引
  • 复制表
  • 修改自增的初始值
  • 多表查询
  • 隐式内连接
  • 显示内连接
  • 左连接查询
  • 右连接查询
  • 嵌套查询
  • union 联合查询
  • 同时修改两张表
  • 多字段查询
  • sum、count、max、min、avg
  • 关于用户
  • 查询管理员

mongodb常用指令

\c:结束当前mysql命令
\g:== go 执行 ==
cls:清空界面
cd:进去目录
dir:查看文件

导出库

C:\Users\fwhf-pc>mysqldump -uroot -p test > e:\a.sql

导入库

C:\Users\fwhf-pc>mysql -h localhost -uroot -p test < e:\a.sql

导入数据

source e:\a.sql

导出单张表

C:\Users\fwhf-pc>mysqldump -uroot -p test user > e:\d.sql

导出所有表结构

C:\Users\fwhf-pc>mysqldump -uroot -p -d --add-drop-table test > e:e.sql

导出单张表结构

C:\Users\fwhf-pc>mysqldump -uroot -p -d test user > e:\f.sql

查看所有库

show databases

创建库

create database 库名

删除指定库

drop database 库名

使用指定库

use 库名

查看当前使用的库

select database()

查看当前库下所有表

show tables

创建表

create table 表名(id int(8) , username varchar(45) , password char(32)); 

删除表

drop table 表名

查看表结构

desc 表名

查看建表语句

show create table 表名

查看引擎

show engines

修改表名字

alter table 原来的表名字 rename 新的表名字

修改表字段

alter table 表名 change 原来的字段 新的字段(数据类型)

修改表字段数据类型

alter table 表名 modify 原来的字段(新的数据类型)

插入新的字段

alter table 表名 add 新的字段(数据类型)

删除字段

alter table 表名 drop 你要删除的字段

在指定字段后面插入新的字段

alter table 表名 add 新字段(数据类型) after 字段名

在开头查如新的字段

alter table 表名 add 新字段(数据类型) first

数据库索引

alter table user add index(money)

普通索引

alter table user add unique(province)

唯一索引

alter table user add primary key(id)

主键索引

alter table user add fulltext(username)

全文索引

查看索引

show index from user \G

复制表

create table newuser select * from user

不仅仅复制表结构还有数据

create table newuser like user

复制表结构

修改自增的初始值

alter table user auto_increment=起始值

insert into user(id,name,money,province,age,sex)values(1,'名字',20,'中国',12,1);
insert into user values(2,'名字',12,'山东',56,0);
insert into user(name,money,province,age,sex)values('名字',23,'山西',45,1),('名字2',34,'河南',32,0),('名字3',23,'河北',12,0);

delete from user where id = 7;
delete from user where money=34 or age=12;

update user set name='new名字' where id = 2;
update user set name='new名字',money=99,province='哪里' where id = 2;
update user set money=money-9 where name = 'new名字';

select * from user;
select name , age from user;
select distinct name from user;
select * from user where age = 32;
select * from user where age between 20 and 40;
select * from user where age > 32 and age < 50;
select * from user where age=32 or age=12;
select * from user where age != 40;
select * from user where age <> 40;
select * from user where age in(12,32);
select * from user where province like '河%';
select * from user where province like '%河%';
select * from user where province like '河_';
select * from user where province like '_河_';
select * from user order by id asc;(默认的排序,升序)
select * from user order by age desc;(降序)
select * from user limit 0 , 2;
select * from user order by age asc limit 0 , 2;
select * from user where > 20 order by age asc limit 0 , 2;
select count(*) , name from user group by name;

分组

select count(*) as total , name from user group by name;

起别名

select count(*) as total , name from user group by name having total > 2;

对结果进行再次过滤 having

多表查询

mysql> select * from shop_user join shop_goods;

隐式内连接

mysql> select shop_user.username,shop_goods.gname from shop_user,shop_goods where shop_user.gid = shop_goods.gid;

显示内连接

mysql> select username as uname , gname as name from shop_user inner join shop_goods on shop_user.gid = shop_goods.gid;

左连接查询

mysql> select username , gname from shop_user left join shop_goods on shop_user.gid = shop_goods.gid;

右连接查询

mysql> select username , gname from shop_user right join shop_goods on shop_user

嵌套查询

mysql> select username from shop_user where gid in(select gid from shop_goods);

union 联合查询

mysql> select * from shop_user left join shop_goods on shop_goods.gid = shop_user.gid union select * from shop_user right join shop_goods on shop_user.gid = shop_goods.gid;

同时修改两张表

mysql> update shop_user as u , shop_goods as g set u.username='qwq' , g.gname='rtr' where u.gid = g.gid and u.gid=1;

多字段查询

mysql> select * from shop_goods order by price , gid desc;

sum、count、max、min、avg

sum 求和
count 求总条数
max 求最大值
min 求最小值
avg 求平均值

select count(shop_user.gid) as count , shop_goods.gname ,avg(shop_goods.price) from shop_user inner join shop_goods on shop_user.gid = shop_goods.gid group by shop_goods.gname order by count desc limit 0 , 1;
select count(shop_user.gid) as ugid , shop_goods.gname as name from shop_user right join shop_goods on shop_user.gid = shop_goods.gid group by shop_goods.gname order by ugid desc limit 0 , 1;
select count(shop_user.gid) as ugid , shop_goods.gname from shop_user left join shop_goods on shop_user.gid = shop_goods.gid group by shop_goods.gname order by ugid desc limit 0 , 1 ;

关于用户

revoke grant
mysql> create user 'whj'@'localhost' identified by 'love';

创建用户

mysql> drop user 'whj'@'localhost';

删除用户

mysql> grant select on *.* to 'whj'@'localhost';

赋予权限

mysql> revoke select on *.* from 'whj'@'localhost';

剥夺权限

查询管理员

mysql> select user from mysql.user;

你可能感兴趣的