mysql数据库的常用指令

mysql -uroot -p 进入数据库指令
请输入密码:--密文展示,输入完成按回车就好

 

mysql:
show databases;---查看数据库
create database nj05; -- nj05(库名,可以自定义) --创建库
use+库名; --切换库
select database();--查看当前所在库
show tables;--查看库中的表
desc + 表名;--查看表字段(表头)
drop table +表名;--删除表
insert into stu(name,age,sex,hobby)
values('zhangsan',18,'GG','python');
--插入数据

insert into stu values('lisi',28,'MM','paly');
--已知表字段,直接插入表数据

insert into stu (name)values('wangwu'),('tom');
---指定插入表中某个字段的值

select * from +表名;--查看表数据

查询lisi的年龄;
select age from stu where name = 'lisi';


update stu set age=20 where name='lisi';
update+表名+插入的数据+条件 ----修改表中指定的数据
updata...set...

delete from 表 ---清空表数据
delete from 表 where+条件 --删除指定的表数据
如:delete from stu where name='lisi';


DDL、DML
DML 数据操纵语言--对表数据新增、修改、删除、查询的语句
DDL 数据定义语言--对库、表的新增、修改、删除


create table pro(id bigint,p_name varchar(255),price float,
create_time datetime,create_name varchar(255))charset utf8
comment'商品表';---创建商品表,并设置中文编码--(给备注)

数据库中的运算:
整数运算:
1.查询id=1的数据
select * from stu where id=1;
2.查询id不等于1的数据
select * from stu where id!=1;
select * from stu where id<>1;
3.查询id>1的数据
select * from stu where id>1;
4.查询id小于2的数据
select * from stu where id<2;
5.查询id大于等于1的数据
select * from stu where id>=1;
6.查询id小于等于2的数据
select * from stu where id<=2;


与运算:
and
select * from stu where age=20 and name='shangsan';
--查询年龄20并且姓名为张三的学生信息


between
select * from stu where id between '1'and'3' ;
--查询id在1-3之间学生信息

not between
select * from stu where id not between '1'and'3';
--查询id不在1-3之间的学生信息

或运算:
or
select * from stu where name='zhangsan'or name='lisi';
查询学生表中姓名是张三或者是李四的信息

集合:
1.在集合内 in
select * from stu where id in(3,4,5);
查询id在3,4,5中的学生信息
2.不在集合内 not in
select * from stu where id not in(3,4,5);
查询id不在3,4,5中的学生信息


排序:
从大到小 order by....desc
select * from stu order by id desc;

从小到大 order by....asc
select * from stu order by id asc;

先升序后降序
select * from pro order by id asc,price desc;
---按照id先升序,后按照价格降序


limit 0,1 从0开始数取第一个值
[1,2,3,4,5,6]
0,1,2,3,4,5
select * from pro order by id desc limit 0,1;
第一位数代表第几个开始取,第二位代表取多少个

模糊匹配(查询) --like
%匹配0个或者多个任意字符
_匹配一个字符
select * from stu where name like 'zhang%';
查询学生表中姓名像zhang开头的学生信息

不匹配
select * from stu where name not like 'zhang%';

聚合函数:
最大值:
select max(id) from stu;
最小值:
select min(id) from stu;
平均值:
select avg(id) from stu;
求和:
select sum(id) from stu;
统计:
select count(id) from stu;
去重:
select distinct(id) from stu;

最大值减去最小值
select max(price)-min(price) from pro;

分组:
group by
having 过滤 只能用在group by 后面使用
select name from stu group by name;
查询学生姓名且分组

select name from stu group by name having count(name)>=2;
--统计出现了两次及以上的学生姓名


空对象查询:
为空:
select * from stu where sex is NULL or sex='';
查询性别为空的学生信息
不为空:
select * from stu where sex is not NULL or sex!='';
查询性别不为空的学生信息

数据库备份:
备份表结构:
create table new_stu_bak202207041127 like stu;
new_stu_bak202207041127---新表名(备份表的名称)
创建一个备份表,结构像原表stu


备份表数据:
insert into new_stu_bak202207041127 select * from stu;
插入 备份表数据(数据来源于你的原表数据--select * from stu;)

备份表结构以及表数据:
create table aaa_bak202207041136 as select * from stu;

补充:
备份库:
mysqldump -uroot -p 库名 > 备份文件名称.sql

还原库:
mysql -uroot -p 需要还原的库名 < 备份文件名称.sql

 

多表连接:
select * from stu,cjb where stu.id = cjb.id ;--基本连接

select * from stu inner join cjb on stu.id=cjb.id; --内连接

select * from stu LEFT join cjb on stu.id=cjb.id;--左连接
ps:左连接 以left join左边的表为基准,右边没有的就补NULL-- 一般基
准表都是数据多的那张表

select * from cjb RIGHT join stu onstu.id=cjb.id;--右连接
ps:右连接 以right join右边的表为基准,左边边没有的就补NULL-- 一般
基准表都是数据多的那张表


子查询:
1.标量子查询:
select name from stu where id=
(select id from stu where name='王五');


2.表子查询
select * from (select name from stu)a;

3.列子查询
select name from stu where id in
(select * from (select id from stu )a);--列子查询


union:
连接两个表中,若有相同的记录则只显示一条
加上all
不会去重,直接把所有内容展示出来

alter table pro rename ppro;--修改表名称(pro原表 ppro重新命名的新表)

alter table ppro change id sid int comment'商品编号';
--修改表字段名称并且做备注(id 原字段名称 sid 新字段名称 int字符类型)

alter table ppro add chandi varchar(10) comment'产地';
--新增表字段并且给备注

alter table ppro add id int first;
--新增表字段并且放在第一位

alter table ppro add ssid int after sid;
--新增表字段并且放在指定字段的后面

alter table ppro add (d int,iid int,addr varchar(10));
--新增多个字段,字段之间用逗号隔开

alter table ppro drop iid;---删除表中某字段
alter table ppro drop d,drop addr;--删除多个表字段,字段间用逗号隔开
posted @   与世无争小陌逢  阅读(136)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示