Mysql--DDL、DML、DQL操作(数据定义语言、数据操纵语言、数据查询语言)数据库语言基础

目录

一、DDL操作

1、数据库操作

 2、字段操作

3、表格操作

二、DML操作

1、添加数据

2、修改数据

3、删除数据

三、DQL操作

1、基本查询

1、查询字段列表

 2、查询某字段的种类

2、条件查询

1、一个条件的查找实例

 2、多个条件的查找

 3、in、like的用法

4、聚合函数

3、分组查询

1、重点

4、排序查询

5、分页查询

6、案例


一、DDL操作(数据定义语言)

1、数据库操作

创建数据库:

create database if not exists db1;

销毁数据库:

drop databasae if exists db1;
 2、字段操作
alter table tname add 字段名称 类型(长度);//追加字段
3、表格操作

展示所有表格

show tables ;

二、DML操作(数据操纵语言)

1、添加数据

关键字 insert into

如果插入的记录的数据并不完整,可以用如下方式插入:

insert into table1 (fie1,fie2,fie3) 
values (v1,v2,v3);

如果插入的记录的数据完整,可以用如下的方式插入:

insert into table1
values (v1,v2,v3,v4~~);

 table表示表格的名字。数据的顺序要与表格里字段顺序相同;

如果需要插入多条数据,可以用如下方式:

insert into table1 (f1, f2, f3,f4) 
values (v1,v2,v3,v4),
       (v1,v2,v3,v4),
       (v1,v2,v3,v4),
       (v1,v2,v3,v4);

2、修改数据

关键字:update table set ...where continue;

table:表的名字;

set后面是要修改的内容;

where 后面必须是主键信息,(修改条件)

update table1 set f1 = v1 f2 = v2 where id = 1;

3、删除数据

关键字:delete

delete  from table1 where id = 1;

 where 后面为主键信息。

三、DQL操作(数据查询语言)

1、基本查询

关键字:select  f1,f2,f3 from table

1、查询字段列表
select f1,f2,f3,f4.. from table1;

 可以在字段f1后面加as' ',在引号里面加入别名。

select f1 as '性别',f2 as '年龄' ,f3 as '身高',f4 as'体重'.. from table1;
 2、查询某字段的种类

如职业的种类:

select distinct job from table1;

 查询结果为:

 

2、条件查询

关键字:where +条件

条件关键字:> , >= , < , <= , != , between...and... , in , like , is null ,and , or , not

1、一个条件的查找实例
select * from table1 where name = '小明';
select * from table1 where id <= 5;
select * from table1 where job is null;
select * from table1 where job is not null ;
select * from table1 where password != '123456';

 分别为查询指定姓名、id小于等于5、job不为空、job为空、password 不等于 ’123456‘。

 2、多个条件的查找
select * from table1 where entrydate >='2000-01-01' and entrydate <= '2010-01-01';
select * from table1 where entrydate between '2000-01-01' and  '2010-01-01';

select * from table1 where entrydate between '2000-01-01' and  '2010-01-01' and gender = 2;
 3、in、like的用法

in:  select * from table where job in(2,3,4); //查询job为2,3,4的数据;

like:用于查找姓名,“_”表示任意一个字,“%”表示任意 如下:

select * from table1 where name like '__';//查找名字为两个字的人

select * from table1 where name like '张%';//查找姓张的人
4、聚合函数
-- 求总数
select count(job) from table1;
select count(*) from table1;
--求最大值、最小值
select max(entrydate) from table1;
select min(entrydate) from table1;
-- 求均值、求和
select avg(id) from table1;
select sum(id) from table1;

3、分组查询

关键字:group byhaving

示例:

select job,count(*) from table1 where entrydate <= '2015-01-01' group by job having count(*) >= 2;

结果: 

 

1、重点

where 和 having 区别
where:分组之前进行过滤,不参与分组;不能对聚合函数进行判断;
having:分组之后对结果进行过滤;可以对聚合函数进行判断;

4、排序查询

 升序ASC,降序DESC

示例:

select *from table1 order by  entrydate asc ;

select *from table1 order by  entrydate desc ;

select *from table1 order by  entrydate ,update_time desc ;

5、分页查询

关键字:limit

limit 起始索引,每页显示记录数

起始索引 = (查询页码 - 1)* 每页显示记录数;

示例:

select *
from table1 limit  0,5;

select *
from table1 limit  5,5;

select *
from table1 limit  10,5;

6、案例

select *
from table1
where name like '%张%'
  and gender = 1
  and entrydate between '2000-01-01' and '2015-01-01'
order by update_time desc
limit 10,10;

 

if语句和case语句:

-- if(表达式,ture,fault)
select if(gender = 1, '男性员工', '女性员工') 性别, count(*)
from table1
group by gender;

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幼儿园大哥7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值