SQL的命令

目录

创建用户

​编辑

DDL数据库操作

查询

创建

使用

删除

创建数据库表

在表中修改字段

查询表

DML

添加数据

修改

删除

DQL

查询


创建用户

DDL数据库操作

查询

show databases;

创建

权限问题导致无法创建,连接root修改用户权限

CREATE DATABASE db01;
CREATE DATABASE if not EXISTS db02;

使用

use db01;
SELECT DATABASE();

删除

DROP DATABASE db01;
DROP DATABASE if EXISTS db01;

创建数据库表

USE db00;
CREATE TABLE tb_user(
	id INT PRIMARY KEY UNIQUE COMMENT "ID",
	username VARCHAR(20) not NULL UNIQUE COMMENT "用户名",
	name VARCHAR(10) NOT NULL COMMENT "姓名",
	age int COMMENT "年龄",
	gender CHAR(1) DEFAULT "男" COMMENT "性别"
) COMMENT "用户表";
primary key (auto_increment)主键(数值自增)
unique        唯一
not null        非空
default默认
varchar()字符串,不能用string
comment注释

字符串类型要使用varchart,不能使用string。

auto_increment 在 primary key 前面。

id int auto_increment  primary key

在表中修改字段

-- 修改表的结构
-- 添加字段(列)
alter table 表名 add 段名 类型 [注释];

-- 修改字段类型
alter table 表名 modify 段名 类型;

-- 修改字段名
alter table 表名 change 原段名 新段名 类型;

-- 删除字段
alter table 表名 drop column 段名;

-- 修改表名
rename TABLE 原表名 to 新表名;
-- 修改表的结构
-- 添加字段(列)
alter table tb_user add qq varchar(11) comment 'QQ';

-- 修改字段类型
alter table tb_user modify qq varchar(13);

-- 修改字段名
alter table tb_user change qq qq_11 varchar(13);

-- 删除字段
alter table tb_user drop column qq_11;

-- 修改表名
rename TABLE tb_user to users;

查询表

-- 选择数据库
use 库名;
-- 查询当前数据库所有表,table是复数,记得加s!!!
show tables;

-- 查询表的结构
desc 表名;

-- 查询建表语句
show create table 表名;
-- 选择数据库
use db00;
-- 查询当前数据库所有表
show tables;

-- 查询表的结构
desc users;

-- 查询建表语句
show create table users;

DML

-- 创建表
use db02;
create table uu(
	id int auto_increment primary key ,
	name varchar(10) not null,
	age int  ,
	create_time varchar(20) not null 
);

添加数据

-- 指定字段添加数据
insert into 表名 (字段名1,...) values (值1,...);

-- 全部字段添加数据
insert into 表名 values (值1,...);

-- 指定字段批量添加数据
insert into 表名(字段名1,...) 
	values(值1,...),(值1,...);
	
-- 全部字段批量增加数据
insert into 表名 
	values(值1,...),(值1,...); 
-- 指定字段添加数据
insert into uu (name,create_time) values ('Tom',now());

-- 全部字段添加数据
insert into uu values (null,'Linda',22,now());

-- 指定字段批量添加数据
insert into uu(name,age,create_time) 
	values('Tedy',21,now()),('Gabe',15,now());
	
-- 全部字段批量增加数据
insert into uu 
	values(null,'Amy',35,now()),(null,'Bob',36,now()); 

在添加全部字段的时候,若id是自增的话,可以添加值的位置附上null,否则会报错。

now()显示当前时间。

字段和值要一一对应。

修改

-- 修改指定数据
update 表名 set 字段名1=值1,字段名2=值2... where 条件;

-- 修改指定字段
update 表名 set 字段名1=值1,字段名2=值2...;
-- 修改指定数据
update uu set name='PJ' where id=1;

-- 修改指定字段
update uu set create_time='2022-01-03';

删除

-- 删除指定数据
delete from 表名 where 条件;

-- 删除所有数据
delete from 表名;
-- 删除指定数据
delete from uu where id=2;

-- 删除所有数据
delete from uu;

DQL

建表

	create table tb_emp (
    id int unsigned primary key auto_increment comment 'ID',
    username varchar(20) not null unique comment '用户名',
    password varchar(32) default '123456' comment '密码',
    name varchar(10) not null comment '姓名',
    gender tinyint unsigned not null comment '性别, 说明: 1 男, 2 女',
    image varchar(300) comment '图像',
    job tinyint unsigned comment '职位, 说明: 1 班主任,2 讲师, 3 学工主管, 4 教研主管',
    entrydate date comment '入职时间',
    create_time datetime not null comment '创建时间',
    update_time datetime not null comment '修改时间'
) comment '员工表';
	
	
-- 准备测试数据
INSERT INTO tb_emp (id, username, password, name, gender, image, job, entrydate, create_time, update_time) VALUES
    (1, 'jinyong', '123456', '金庸', 1, '1.jpg', 4, '2000-01-01', '2022-10-27 16:35:33', '2022-10-27 16:35:35'),
    (2, 'zhangwuji', '123456', '张无忌', 1, '2.jpg', 2, '2015-01-01', '2022-10-27 16:35:33', '2022-10-27 16:35:37'),
    (3, 'yangxiao', '123456', '杨逍', 1, '3.jpg', 2, '2008-05-01', '2022-10-27 16:35:33', '2022-10-27 16:35:39'),
    (4, 'weiyixiao', '123456', '韦一笑', 1, '4.jpg', 2, '2007-01-01', '2022-10-27 16:35:33', '2022-10-27 16:35:41'),
    (5, 'changyuchun', '123456', '常遇春', 1, '5.jpg', 2, '2012-12-05', '2022-10-27 16:35:33', '2022-10-27 16:35:43'),
    (6, 'xiaozhao', '123456', '小昭', 2, '6.jpg', 3, '2013-09-05', '2022-10-27 16:35:33', '2022-10-27 16:35:45'),
    (7, 'jixiaofu', '123456', '纪晓芙', 2, '7.jpg', 1, '2005-08-01', '2022-10-27 16:35:33', '2022-10-27 16:35:47'),
    (8, 'zhouzhiruo', '123456', '周芷若', 2, '8.jpg', 1, '2014-11-09', '2022-10-27 16:35:33', '2022-10-27 16:35:49'),
    (9, 'dingminjun', '123456', '丁敏君', 2, '9.jpg', 1, '2011-03-11', '2022-10-27 16:35:33', '2022-10-27 16:35:51'),
    (10, 'zhaomin', '123456', '赵敏', 2, '10.jpg', 1, '2013-09-05', '2022-10-27 16:35:33', '2022-10-27 16:35:53'),
    (11, 'luzhangke', '123456', '鹿杖客', 1, '11.jpg', 2, '2007-02-01', '2022-10-27 16:35:33', '2022-10-27 16:35:55'),
    (12, 'hebiweng', '123456', '鹤笔翁', 1, '12.jpg', 2, '2008-08-18', '2022-10-27 16:35:33', '2022-10-27 16:35:57'),
    (13, 'fangdongbai', '123456', '方东白', 1, '13.jpg', 1, '2012-11-01', '2022-10-27 16:35:33', '2022-10-27 16:35:59'),
    (14, 'zhangsanfeng', '123456', '张三丰', 1, '14.jpg', 2, '2002-08-01', '2022-10-27 16:35:33', '2022-10-27 16:36:01'),
    (15, 'yulianzhou', '123456', '俞莲舟', 1, '15.jpg', 2, '2011-05-01', '2022-10-27 16:35:33', '2022-10-27 16:36:03'),
    (16, 'songyuanqiao', '123456', '宋远桥', 1, '16.jpg', 2, '2010-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:05'),
    (17, 'chenyouliang', '12345678', '陈友谅', 1, '17.jpg', null, '2015-03-21', '2022-10-27 16:35:33', '2022-10-27 16:36:07'),
    (18, 'zhang1', '123456', '张一', 1, '2.jpg', 2, '2015-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:09'),
    (19, 'zhang2', '123456', '张二', 1, '2.jpg', 2, '2012-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:11'),
    (20, 'zhang3', '123456', '张三', 1, '2.jpg', 2, '2018-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:13'),
    (21, 'zhang4', '123456', '张四', 1, '2.jpg', 2, '2015-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:15'),
    (22, 'zhang5', '123456', '张五', 1, '2.jpg', 2, '2016-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:17'),
    (23, 'zhang6', '123456', '张六', 1, '2.jpg', 2, '2012-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:19'),
    (24, 'zhang7', '123456', '张七', 1, '2.jpg', 2, '2006-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:21'),
    (25, 'zhang8', '123456', '张八', 1, '2.jpg', 2, '2002-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:23'),
    (26, 'zhang9', '123456', '张九', 1, '2.jpg', 2, '2011-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:25'),
    (27, 'zhang10', '123456', '张十', 1, '2.jpg', 2, '2004-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:27'),
    (28, 'zhang11', '123456', '张十一', 1, '2.jpg', 2, '2007-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:29'),
    (29, 'zhang12', '123456', '张十二', 1, '2.jpg', 2, '2020-01-01', '2022-10-27 16:35:33', '2022-10-27 16:36:31');

基础查询

-- 查询指定字段
select 字段1,字段2... from 表名;

-- 查询所有字段
-- 推荐
select 字段1,字段2... from 表名;
-- 不推荐
select * from 表名;

-- 查询给字段设置别名
select 字段1 as 别名1, 字段2 as 别名2 from 表名;
select 字段1 别名1, 字段2 别名2 from 表名;
select 字段1 '别名1', 字段2 '别名2' from 表名;

-- 查询,去除重复记录
select distinct 字段 from 表名;
-- 1. 查询指定字段 name,entrydate 并返回
select name,entrydate from emp;

-- 2. 查询返回所有字段
select username,password,name,gender,image,job,entrydate,create_time,update_time from emp;
select * from emp;

-- 3. 查询所有员工的 name,entrydate, 并起别名(姓名、入职日期)
select name as 姓名, entrydate as 入职日期 from emp;
select name 姓名, entrydate 入职日期 from emp;
select name '姓 名', entrydate '入职/日期' from emp;


-- 4. 查询已有的员工关联了哪几种职位(不要重复)
select job from emp;
select distinct job from emp;

别名中如果有空格或特殊字符,可以使用引号把整个别名圈起来。

条件查询

select 段名... from 表名
    where 条件;
-- 1. 查询 姓名 为 杨逍 的员工
select * from emp 
	where name='杨逍';

-- 2. 查询 id小于等于5 的员工信息
select * from emp
	where id <= 5;

-- 3. 查询 没有分配职位 的员工信息
select * from emp
	where job is null;
-- 不能使用job = NULL

-- 4. 查询 有职位 的员工信息
select * from emp 
	where job is not null;

-- 5. 查询 密码不等于 '123456' 的员工信息
select * from emp
	where password != 123456;

select * from emp
	where password <> 123456;

-- 6. 查询 入职日期 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间的员工信息
select * from emp
	where entrydate between '2000-01-01' and '2010-01-01';
	
select * from emp
	where entrydate >= '2000-01-01' and entrydate <= '2010-01-01'; 

-- 7. 查询 入职时间 在 '2000-01-01' (包含) 到 '2010-01-01'(包含) 之间 且 性别为女 的员工信息
select * from emp
	where entrydate between '2000-01-01' and '2010-01-01' and gender = 2 ;

-- 8. 查询 职位是 2 (讲师), 3 (学工主管), 4 (教研主管) 的员工信息
select * from emp
	where job = 2 or job = 3 or job =4;
	
select * from emp
	where job in(2,3,4);

-- 9. 查询 姓名 为两个字的员工信息
select * from emp
	where name like '__';

-- 10. 查询 姓 '张' 的员工信息
select * from emp
	where name like '张%';

运算符       功能
>大于
>=大于等于
<小于
<=小于等于
=等于
<> 或 !=不等于
between .... and ....在某个范围之内
in(.....)值在(...)中,多选一
like 占位符       模糊匹配('_'匹配耽搁字符,'%'匹配任意个字符)
is null       
and 或 &&并且
or 或 ||或者
not 或 !非,不是

聚合函数

-- 1. 统计该企业员工数量
select count(*) from emp;

-- 2. 统计该企业员工 ID 的平均值
select avg(id) from emp;

-- 3. 统计该企业最早入职的员工
select min(entrydate) from emp;

-- 4. 统计该企业最迟入职的员工
select max(entrydate) from emp;

-- 5. 统计该企业员工的 ID 之和
select sum(id) from emp;	

函数功能
count统计数量
max最大值
min最小值
avg平均值
sum求和

null值不参与所有聚合函数。

分组查询

select ... from 表名 [where 条件] group by ... [having 分组后过滤条件];
-- 分组
-- 1. 根据性别分组 , 统计男性和女性员工的数量
select gender,count(*) from emp group by gender;

-- 2. 先查询入职时间在 '2015-01-01' (包含) 以前的员工 , 并对结果根据职位分组 , 获取员工数量大于等于2的职位
select job,count(*) from emp where entrydate <= '2015-01-01' group by job having count(*) >= 2;

聚合函数不能放在where里面,可以放在select和having之后。

where和having的区别

1、执行时机不同:where是分组之前进行过滤,不满足where条件,不参与分组;而having是分组之后对结果进行过滤。

2、判断条件不同:where不能对聚合函数进行判断,而having可以。

排序查询

select 字段... from 表名 [where ...] [group by ...] order by 字段1 排序方式,字段2 排序方式,....
-- 1. 根据入职时间, 对员工进行升序排序
select * from emp order by entrydate asc;
select * from emp order by entrydate ;

-- 2. 根据入职时间, 对员工进行降序排序
select * from emp order by entrydate desc;

-- 3. 根据 入职时间 对公司的员工进行 升序排序 , 入职时间相同 , 再按照 更新时间 进行降序排序
select * from emp order by entrydate,update_time desc;

asc升序(默认)
desc降序

多字段排序时,只有前面字段一样,后面的才会生效。

分页查询

select 字段名... from 表名 limit 起始索引,查询记录数;
-- 起始索引=(查询页数-1)*查询记录数 
--  =================== 分页查询 ======================
-- 1. 从起始索引0开始查询员工数据, 每页展示5条记录
select * from emp limit 0,5;

-- 2. 查询 第1页 员工数据, 每页展示5条记录
select * from emp limit 0,5;
select * from emp limit 5;


-- 3. 查询 第2页 员工数据, 每页展示5条记录
select * from emp limit 5,5;

-- 4. 查询 第3页 员工数据, 每页展示5条记录
select * from emp limit 10,5;

1、起始索引从0开始。

2、不同数据库有不同实现分页查询。

3、如果查询的是第一页数据,起始索引可以省略,直接简写为limit 查询记录数。

流程查询

if(表达式,true-value,false-value)
(case 字段名 when value1 then result1 [when value2 then result2 ....][else result] end)
select if(gender=1,'男','女') as 性别,count(*) as 数量 from emp group by gender;

select 
	(case job when 1 then '班主任' when 2 then '讲师' when 3 then '学工主管' when 4 then '教师主管' else '未分配职位' end) 职位,
	count(*) 数量
from emp group by job;	

如果你觉得本文对你有用的话,请随意打赏~

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值