【Linux】重生之从零开始学习运维之Mysql

Mysql基础指令

查看show

复制代码
mysql
复制代码
show databases;
复制代码
use db1;
show tables;

创建create

复制代码
show databases;
create database db2;
show databases;
复制代码
use db2;
show tables;
create table test1 like db1.stu1;
desc test1;

修改alter

增add

复制代码
alter table stu1 add phone varchar(11) after age;

删drop

复制代码
alter table stu1 drop column phone;

改名rename

复制代码
alter table stu1 rename stu;

修改字段名称和类型change

复制代码
alter table stu change phone mobile char(10);

增加主键-primary key

复制代码
alter table stu add primary key (id);

增加主键自增-auto_increment

复制代码
alter table stu modify column id int unsigned not null auto_increment;

自增起始值

复制代码
alter table stu auto_increment=1;

插入数据

复制代码
insert stu (name,age) values ('zhangsan',15);

移除drop

删除表

复制代码
drop table user3;

DML语句-insert、update、delete

插入数据

插入单行数据-insert

复制代码
insert stu (name,age) values('xiaoming',20);
复制代码
insert stu (name,age,is_del) values('xiaohong',18,false);

插入单行数据-insert into

复制代码
insert into stu values(12,'xiaoli',11111111112,19,null);

插入多行数据

insert into xxx values(,),(,)...;

复制代码
insert into stu (name,age) values('test1',20),('test2',21),('test3',22);

以更新的方式插入数据

insert into xxxx values() on duplicate key update xxx='xxx';

复制代码
insert into stu (id,name) values(12,'zhangsan') on duplicate key update name='zhangsan';

将查询结果当值插入

复制代码
insert into stu (name,age) select name,age from stu where id=11;

更新数据

根据条件更新数据-update

复制代码
update stu set age=26 where id>12;

多个条件or、and

复制代码
update stu set age=21 where (id=15 or name is null);
复制代码
update stu set age=22 where (id=16 and name='xiaohong');

删除数据

根据条件删除-delete

复制代码
delete from stu where id=16;

指定多条件删除

复制代码
delete from stu where (mobile is null and id=15);

清空表数据

清空表

复制代码
truncate table xxx
delete from xxx

DQL****语句

查询-select

普通查询

复制代码
select * from stu;

AS方式将标题改名

复制代码
select id as 学号,name as 姓名,age as 姓名 from stu;

多条件查询

复制代码
select id,name from stu where id>15;
复制代码
select id,name,age from stu where id in (10,14,15,17);

排序

指定排序order by

复制代码
select * from stu order by name;

去重distinct

复制代码
select distinct(age) from stu order by age desc;

Mysql视图基础

view视图

创建view视图

create view xxx as xxxxx;

复制代码
create view v_stu as select * from stu where age=18;

更新视图信息

复制代码
update v_stu set age=20 where id=16;

删除view视图

复制代码
drop view v_stu;
相关推荐
2301_32241428049 分钟前
基于web的校园失物招领管理系统59695-计算机毕设 原创(免费领源码+部署教程+开发环境)
mysql·nginx·php·apache·thinkphp·phpstudy·wampserver
SelectDB技术团队12 分钟前
Apache Doris 基于 Lance 的向量检索深度解析
数据库·数据仓库·数据分析·湖仓一体·apache doris·lakehouse·多模湖仓
A心有千千结13 分钟前
通过 SSH 反向代理让服务器借用本机网络访问 GitHub 的最佳实践
linux·服务器·网络·git·ssh·github
fengkai454521 分钟前
十三、Zabbix 监控
运维·服务器·zabbix·监控·网络设备
这个DBA有点耶28 分钟前
从INDEX到JOIN_ORDER:MySQL 8.0 Hint语法体系与五类实战场景
数据库·mysql·架构
古法老登瞎折腾31 分钟前
大数据智能运维实战(二):准备一个可复现的运维实验环境
java·大数据·运维·spring ai·mcp
数字新视界32 分钟前
机房动环监控助力智能管理
大数据·服务器·数据库·动环监控系统·动力与环境监控系统
书生执笔画浮沉34 分钟前
Linux 内核模块为什么不能自己解决兼容性问题?
linux·c·kernel·update·灵感
梅菲塔亚36 分钟前
博客_Redis入门到实战
数据库·redis·缓存
算力小木40 分钟前
#标题:5090 vs 4090D 到底差多少?我拿八卡实测了一周
服务器