数据库单表 DML(增删改)和基本的DQL(查)操作语

DML语句:数据删除,数据增加,数据修改

数据增加:

             注意:当主键字段自增时,我们添加数据时,可以不写或写null

                        字符类型,必须加引号,其次非空字段必须添加数据,或者我们在建立表时设立默认值

-- insert into 表名(字段1,字段2,.....,字段n)    VALUES/VALUE(值,值....,值)

-- DML
-- 新增
-- insert into 表名(字段1,字段2,.....,字段n)
-- VALUES/VALUE(值,值....,值)

--  日期 使用字符串的形式进行书写日期格式(yyyy-MM-dd HH:mm:ss)
-- 全字段的插入

-- 方式一:
insert into student(sid,sname,birthday,ssex,classid)
VALUES(9,'张三','2007-1-1','男',1)

-- 方式二:
insert into student VALUES(null,'齐齐','1789-1-1','女',2)
insert into student VALUES(DEFAULT,'齐齐','1789-1-1','女',2)

-- 部分字段插入
insert into student (sname,ssex) VALUES('齐同学','女')

alter table student MODIFY ssex VARCHAR(10) not null DEFAULT '保密';

insert into student(sname) VALUES('陈伯源');
添加多条数据:insert into 表名(字段名...) values(值...),VALUES(值...),...
-- 一次性添加多条数据
-- 方式一:
-- insert into 表名(字段名...) values(值...),VALUES(值...),...
insert into student(sname,ssex) 
VALUES('丫丫','男'),
('阿特','男'),
('吉吉','男');

-- 方式二  不常用
-- insert into SELECT
-- 插入和被插入的表都必须存在
create table newstu(
	xingming VARCHAR(10),
	xingbie VARCHAR(10),
	calssid int
);
insert into newstu(xingming,xingbie,calssid)
select sname,ssex,classid from student;

-- 方式三
-- create table select
-- 要求被插入表不能存在  --  被插入表没有任何约束
create table stu1 
select sid,sname,birthday from student; 
修改数据:

             -- update 表名  set  字段名=值,字段名=值,....,字段名=值
                -- [where 子句条件]
                -- where 子句 中的条件是对表中每一条数据进行判断
                -- 判断成立该数据的父句执行,
                -- 判断不成立该数据的父句不执行

-- 修改
-- [where 子句条件]
-- where 子句 中的条件是对表中每一条数据进行判断
-- 判断成立该数据的父句执行,
-- 判断不成立该数据的父句不执行
update stu1 set birthday='1678-1-1 12:31:15' WHERE sname='齐同学';

UPDATE newstu set calssid=2 where xingbie!='男';
UPDATE newstu set calssid=3 where xingbie<>'女';

UPDATE newstu set xingbie='保密' where calssid<260;

update newstu set xingbie='外星人'
where calssid>=200  and calssid <=300  ;

update newstu set xingbie='水星人'
where calssid BETWEEN 30 and 90;

-- 30 50 70  它们的性别变为地球人
update newstu set xingbie='地球人'
where calssid=30 or calssid=50 or calssid=70

update newstu set xingbie='地球人'
where calssid in (30,50,70)

删除数据:

                -- 删除  :仅仅删除数据
                -- delete from 表名 [where 子句]

                注意:truncate和drop,以及delete的区别:

                -- delete 只删除数据
                -- truncate 不仅删除数据,还删除了索引
                -- drop :不仅删除数据,还删除索引,表结构也删了


DELETe from newstu;

delete from stu1 where birthday is null;

-- 清空表,截断
-- truncate 表名
TRUNCATE stu1;

DQL(数据查询语言)(单表查询):-- 所有的查询都会得到一张虚拟表,在内存中

简单查询:

        注意:利用* 全字段查询:先查字段名,再查,不利于SQL优化,效率低

-- 从表中获取数据
-- select 字段名,字段名 from student
-- 全字段查询
SELECT sid,sname,birthday,ssex,classid from student;
select * from student; -- 先查字段名,再查,不利于SQL优化,效率低

-- 部分字段查询
select ssex,sname from student;

-- 字段名起别名,as可写可不写,单引号也可省略
select sname as '学生姓名',ssex '学生性别',birthday 生日 from student;

-- 添加一个字段
select sname,'猿究院' 学校 from student


-- 带条件查询
-- 【where 子句】
SELECT * from student where sid=5;
SELECT * from student where sid<>5;
select * from student where sid>5;
select * from student where sid BETWEEN 3 and 6;

-- 查找 1班的女同学
select * from student where classid=1 and ssex='女';
去重:所有字段的数据要一致才会去重
-- distinct 去重
-- 所有字段的数据要一致才会去重
SELECT distinct sname,ssex FROM student;
模糊查询:

        -- 模糊符号  %任意多的任意字符
        -- _ :一个任意字符

-- like  模糊查询
-- 模糊符号  %任意多的任意字符
-- _ :一个任意字符
select * from student where Sname like '%张%';
select * from student where Sname like '张%';
select * from student where Sname like '%张';

select * from student where sname like '张_';
分组:group by:与having连用

having和where的区别:
-- where :过滤的是表中的每一条数据
-- havaing:过滤的是聚合之后的数据


-- 分组 group by ******

-- 男女同学各有多少人
select ssex,count(*) from student GROUP BY ssex;

-- 统计出各班有多少人
select classid,count(*) from student group by classid;

-- 统计成绩表 统计每个同学的总分和平均分
select sid 学号,sum(score) 总分,avg(score) 平均分 from sc group by sid;

-- 查询出平均分不及格的sid 平均分
select sid 学号,sum(score) 总分,avg(score) 平均分 from sc  
group by sid HAVING avg(score)<60;
-- 面试题:having和where的区别:
-- where :过滤的是表中的每一条数据
-- havaing:过滤的是聚合之后的数据
聚合函数:

-- 聚合函数
-- 把多个值变成一个值
-- count():统计个数
-- max():统计最大数
-- min():统计最小值
-- sum():总和
-- avg():平均值

-- 聚合函数
-- 把多个值变成一个值
-- count():统计个数
-- max():统计最大数
-- min():统计最小值
-- sum():总和
-- avg():平均值

-- count(参数):不统计null
-- 参数:字段/常量/*
SELECT count(sid) from student;		-- 主键
SELECT count(classid) from student; -- 不统计null

SELECT count('a') from student; -- 不推荐
SELECT count(123) from student;	-- 推荐
SELECT count(*) from student;		-- 推荐

-- sum,avg,min,max  数值类型
select sum(score) from sc;
SELECT avg(score) from sc;
select max(score) from sc;
select min(score) from sc;

-- 统计出成绩表中一共有多少次考试,总成绩,平均分,最高分,最低分
select  count(DISTINCT Cid) 考试次数,sum(score) 总成绩,
avg(score) 平均分,max(score) 最高分,min(score)最低分   from sc
排序:

-- 先写先排
-- DESC:降序  必须写
-- asc :升序 或者不写(默认的)

-- order by  排序
-- 先写先排
-- DESC:降序  必须写
-- asc :升序 或者不写(默认的)
select * from student ORDER BY classid desc;
分页:

-- limit 分页 0 开始 (页码-1)*步长,步长
-- SELECT * from student LIMIT 位置,步长;

注意:sql语句不能写表达式,要写具体数值,所以由应用层解决此问题

-- limit 分页 0 开始 (页码-1)*步长,步长
-- SELECT * from student LIMIT 位置,步长;
select * from student limit 3,3;
-- 应用层解决,limit后面只能带具体数值,不能带表达式
-- select * from student limit (3-1)*3,3;  -- 错误的

-- 找到成绩及格的总分数排名第二的sid和总成绩
select sid,sum(score) from sc where score>=60 GROUP BY Sid ORDER BY sum(score) DESC LIMIT 1,1;
特殊字符:
-- in 在某个特定的范围
-- 3,5,7,9
select * from student where sid in(3,5,7,9); -- 推荐使用  可以是使用到索引

-- or 不推荐 会让索引失效

-- null的查询
select * from student where birthday is null;
select * from student where birthday is not null;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

cph_507

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

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

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

打赏作者

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

抵扣说明:

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

余额充值