初识MySQL

查询、创建、删除

查询所有数据库

mysql 复制代码
show databases;

查询当前数据库

mysql 复制代码
select database();

创建一个数据库

mysql 复制代码
create database test;

如果该数据库不存在就创建这个数据库

mysql 复制代码
create database test if not exists test;

创建数据库并设置字符集格式

mysql 复制代码
create database test1 default charset utf8mb4;

删除一个数据库

mysql 复制代码
drop database[if exists]数据库名;
drop database if exists test;

使用

mysql 复制代码
use 数据库名;

查询当前数据库所有表

mysql 复制代码
show tables;

查询表结构

mysql 复制代码
desc 表名;

查询指定表的建表语句

mysql 复制代码
show create table 表名;

创建表

mysql 复制代码
create table 表名(
	字段1 字段1类型[comment 字段1注释],
	字段2 字段2类型[comment 字段2注释],
	字段3 字段3类型[comment 字段3注释],
	......
	字段n 字段n类型[comment 字段n注释],
)[comment 表注释];

[...]为可选参数,最后一个字段后面没有逗号
mysql 复制代码
create table tb_user(
	id int comment'编号',
	name varchar(20) comment'姓名',
	age int comment'年龄',
	gender varchar(1) comment'性别'
);

表的修改

添加字段

mysql 复制代码
alter table 表名 add 字段名 类型(长度) [comment注释][约束];
alter table tb_user add nickname varchar(20) comment'昵称';

修改数据类型

mysql 复制代码
alter table 表名 modify 字段名 新数据类型(长度);

修改字段名和字段类型

mysql 复制代码
alter table 表名 change 旧字段名 新字段名 类型(长度) [comment 注释][约束];
alter table tb_user change nickname username varchar(30);

删除字段

mysql 复制代码
alter table 表名 drop 字段名;
alter table tb_user drop username;

修改表名

mysql 复制代码
alter table 表名 rename to 新表名;
alter table tb_user rename to user;

删除表

mysql 复制代码
drop table[if exists]表名;

删除指定表,并且重新创建该表

mysql 复制代码
truncate table 表名;

添加数据

mysql 复制代码
insert into 表名(字段名1,字段名2,...) values(值1,值2,.。。);

给全部字段添加数据

复制代码
insert into 表名 values(值1,值2,...);

批量添加数据

mysql 复制代码
insert into 表名(字段名1,字段名2,...) values(值1,值2,...),values(值1,值2,...),values(值1,值2,...);

INSERT INTO USER(id,name,age,gender) VALUES(12,'张三',18,'男');
mysql 复制代码
insert into 表名 values(值1,值2,...),(值1,值2,...),(值1,值2,...);

修改数据

mysql 复制代码
updata 表名 set 字段名1=值1,字段名2=值2,...[where 条件];

UPDATE user set name= 'hayaizo' WHERE id =1;

删除数据

mysql 复制代码
delete from 表名[where 条件]

delete from user where gender = '女';

delete语句的条件可以有,也可以没有,如果没有条件,则会删除整张表的所有数据。

delete语句不能删除某一个字段的值(可以使用update)。

查询语句

查询多个字段

mysql 复制代码
select 字段1,字段2,字段3... from 表名;

select name,workno,age from user;
mysql 复制代码
select * from 表名;

select * from user;

设置别名

mysql 复制代码
select 字段1 [as 别名1]... from 表名;

select workaddress as '工作地址' from user;

去除重复记录

mysql 复制代码
select distinct 字段列表 from 表名;

select distinct workaddress '工作地址' from user;

条件查询

mysql 复制代码
select 字段列表 from 表名 where 条件列表;

查询没有身份证号的员工信息。

mysql 复制代码
select * from user where idcard is null;

查询有身份证号的员工信息

mysql 复制代码
select * from user where idcard is not null;

查询年龄等于18或者20或者40的员工信息

mysql 复制代码
select * from user where age in(18,20,40);

查询名字为两个字的员工

mysql 复制代码
select * from user where name like '__';

查询身份证号最后一位是X的员工信息

mysql 复制代码
select * from user where idcard like '%X';

聚合函数

语法:

mysql 复制代码
select 聚合函数(字段列表) from 表名;

统计员工数量

mysql 复制代码
select count(*) from user;

select count(id) from user;

null不参与聚合计算。

统计企业员工的平均年龄

mysql 复制代码
select avg(age) from user;

统计企业员工中年龄最大的员工

mysql 复制代码
select max(age) from user;

统计企业员工中年龄最小的员工

mysql 复制代码
select min(age) from user;

统计西安地区员工的年龄之和

mysql 复制代码
select sum(age) from emp where workaddress='西安'

分组查询

mysql 复制代码
select 字段列表 from 表名[where 条件] group by 分组字段名[having 分组后过滤条件];

根据性别分组,统计男性员工和女性员工的数量。

mysql 复制代码
select gender,count(*) from user group by gender;

根据性别分组,统计男性员工和女性员工的平均年龄。

mysql 复制代码
select gender,avg(age) from user group by gender;

查询年龄小于45的员工,并且根据工作地址分组,获取员工数量大于等于3的工作地址。

mysql 复制代码
select workaddress,count(*) as address_count from user where age<=45 group by workaddress having address_count>=3; 

排序查询

mysql 复制代码
select 字段列表 from 表名 order by 字段1 排序方式1,字段2 排序方式2;

注:如果是多字段排序,当第一个字段值相同时,才会根据第二个字段进行排序。
根据年龄对公司的员工进行升序排序

mysql 复制代码
select * from user order by age asc;

根据年龄对公司的员工进行升序排序,年龄相同,再按照入职时间进行降序排序。

mysql 复制代码
select * from user order by age asc,entrydata desc;

分页查询

mysql 复制代码
select 字段列表 from 表名 limit 起始索引,查询记录数;

查询第一页员工数据,每一页展示10条记录

mysql 复制代码
select * from user limit 0,10;

DQL执行顺序

相关推荐
楚枫默寒12 分钟前
mongodb备份脚本(单机+副本集)
数据库
小蒜学长37 分钟前
springboot酒店客房管理系统设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端
准时准点睡觉1 小时前
window安装MYSQL5.5出错:a windows service with the name MYSQL alreadyexists....
数据库·windows·mysql
无敌最俊朗@2 小时前
SQlite:外键约束
数据库·oracle
金仓拾光集3 小时前
金仓替代MongoDB:安全与性能协同提升——社交用户画像系统的国产化实践
数据库·安全·mongodb·kingbase·kingbasees·数据库平替用金仓·金仓数据库
FinTech老王3 小时前
国产数据库替换MongoDB实战:浙江人民医院电子病历系统国产化升级案例
数据库·mongodb
l1t3 小时前
在Lua用luasql-sqlite3库访问SQLite数据库
数据库·git·sqlite·lua
2501_938780284 小时前
《轨道交通检测系统中 Qt 与数据库交互的优化方案》
数据库·qt·交互
qqxhb4 小时前
系统架构设计师备考第61天——嵌入式系统架构模式&操作系统&数据库&中间件
数据库·中间件·系统架构·sqlite·dds·层次化(封闭/开放)·递归模式
SelectDB4 小时前
Apache Doris 数据导入原理与性能优化 | Deep Dive
运维·数据库·数据分析