數據庫的DML操作


DML:數據操縱語言 表中的數據進行操作的語言

增加數據 insert
刪除數據 delete
修改數據 update
查找數據 select 最高的使用頻率

一、DML數據操縱語言
數據操縱語言是對數據庫對象中數據的操作,比如對數據進行增加、刪除、修改和查詢等操作
關鍵字:insert / delete / update / select

1:插入數據
語法:insert into 表名[(字段名….)] values(值….)
#創建數據表
create table classroom(
cid int,
cname varchar(20),
des varchar(50)
)

#添加數據到classroom
#1.1、給所有字段添加值,和表中字段順序一致
insert into classroom values(1,'java','java班');

#1.2給cname字段添加值,和指定字段的順序必須一致
insert into classroom(cname) values('python');
insert into classroom(cid,cname) values(3,'test');

#1.3添加3條記錄(批量插入)
insert into classroom values(4,'oracle','oracle班'),
(5,'html','html班'),
(6,'Mysql','Mysql班');

#1.4將classroom的值整體復制到classroom1
create table classroom1(
cid int,
cname varchar(20),
des varchar(50)
)

insert into classroom1 select * from classroom;
select * from classroom1;
-- 前提:classroom1和classroom表的結構必須要一樣

#1.5查看表的所有記錄
select * from classroom;

2:刪除數據
語法:delete from 表名 [where 條件]

#2.1、刪除classroom中的一條數據
select * from classroom;
delete from classroom where cname='python';

#2.2、刪除classroom中的所有數據
delete from classroom;
select * from classroom;

#2.3、truncate清空表的數據
語法:truncate table 表名;
truncate table classroom;

注意:delete與truncate的區別是什么
1:delete是逐行刪除,truncate是文件級別的清空
2:delete刪除后,自增性會繼續執行,不會重置
truncate刪除后,自增性重置

3:更改數據
語法:update 表名 set 字段=新值… [where 條件]
#3.1修改classroom中的一條數據
update classroom set cname='test' where cid=1; #cid主鍵列
update classroom set cname='test1' where des='oracle班';

#3.2、修改classroom中的所有數據
update classroom set des='班級信息';

4:查詢數據
查詢的完整語法:
select 字段|表達式 from 表名|視圖|結果集
[where 條件]
[group by 分組]
[having 分組之后進行檢索]
[order by 排序]
[limit 限制結果]

#4.1查詢所有信息
select * from classroom

#4.1.1、查看部分信息
select cid,cname from classroom;

#4.1.2、查看所有員工的姓名和工資 員工表emp 部門表dept表
鏈接:https://pan.baidu.com/s/1UeJJBuzlgOrTKCAjjQ3rwA
提取碼:1cxi
select * from emp;
select * from dept;
select ename,sal from emp;

#4.1.3、員工工資提升5%之后的員工姓名和工資 --算術運算
select ename,sal+sal*0.05 from emp;

#4.2、范圍查詢
#4.2.1、查詢工資大於2000的員工信息 --比較運算
select * from emp where sal>2000;

#4.2.2、查詢工資在1000-2000之間的員工信息(范圍查詢) --比較+邏輯運算
select * from emp where sal between 1000 and 2000; #包括邊界
select * from emp where sal>=1000 and sal<=2000;

#4.3、集合查詢
#4.3.1、查詢員工編號為7521,7369,7788的員工信息(集合查詢)
select * from emp where empno=7521 or empno=7369 or empno=7788;
select * from emp where empno in(7521,7369,7788);
select * from emp where empno not in(7521,7369,7788);

#4.4、取別名 (小名,別號)
#4.4.1、字段,表達式,結果集,表都可以起別名
select ename,sal+sal*0.05 as 提升后的薪資 from emp;
select ename 姓名,sal+sal*0.05 提升后的薪資 from emp;

#4.5、去重查詢distinct
#4.5.1、查詢所有的職位信息 去重
select distinct job from emp;

#4.6、模糊查詢
#4.6.1、查詢名字以s開頭的員工信息
模糊查詢 like %:匹配字符,匹配0個或多個長度的任意字符
_:匹配一個長度的任意字符
select * from emp where ename like 's%';

#4.6.2、查詢名字中包含s的員工信息
select * from emp where ename like '%s%';

#4.6.3、查詢第二字符為L的所有員工信息
select * from emp where ename like '_L%';

#4.7、排序
#4.7.1、對所有員工的工資進行排序 升序asc和降序desc
select * from emp order by sal;#升序 asc 可以省略,默認是升序的方式排列
select * from emp order by sal desc;


#4.7.2、根據員工的工資降序排,如果工資一致,則按照員工編號降序排列
select * from emp order by sal desc,empno desc;


#4.8、限制結果集顯示
#4.8.1、查詢在10號部門工資最高的員工信息
-- 1:查詢在10號部門員工信息
select * from emp where deptno=10;

-- 2:工資最高 排序 desc
select * from emp where deptno=10 order by sal desc;

-- 3:只要第一個人的人信息
select * from emp where deptno=10 order by sal desc limit 0,1
-- limit m,n m:開始的位置,索引值從0開始 n:取值的長度(個數)

#4.9、為空/非空數據查詢與操作
#4.9.1、查詢所有有獎金的員工信息
-- #查詢所有有獎金的員工信息
select *from emp where comm>0;

-- #查詢所有獎金列為空的員工信息
select * from emp where comm is null;
select * from emp where comm is not null;


#4.9.2、將獎金小於500的員工獎金加100元
-- #將獎金小於500的員工獎金加100元
select * from emp where comm<500 or comm is null;
update emp set comm=comm+100 where comm<500 or comm is null;

update emp set comm=0 where comm is null;
update emp set comm=comm+100 where comm<500 or comm is null;
-- ifnull(comm,0) 判斷comm列是否是null,如果是null,則賦值為0
select * from emp;
update emp set comm=ifnull(comm,0)+100 where comm<500 or comm is null;


練習;

1. 查詢工資提升100元后超過2000的所有員工信息
select sal+100 from emp where sal>2000

2. 查詢工資在1000-2000之間的10號部門的最高工資的員工信息
select * from emp where deptno=10 and sal between 1000 and 2000 order by sal desc limit 0,1

3. 將所有工資低於2000的員工工資提升5%
update emp set sal=sal+sal*0.05 where sal<2000

4. 查詢名字包含s的並且在20號部門的員工信息
select * from emp where DEPTNO=20 and ename like '%s%'

5. 查詢工資大於1000的10號部門的前5條記錄
select * from emp where DEPTNO=10 and sal>1000 limit 0,5

6. 將獎金小於500的員工獎金加100元
update emp set comm=ifnull(comm,0)+100 where comm<500 or comm is null

二、SQL運算符
算數運算符、比較運算符、邏輯運算符
1:算數運算符
+、-、*、/、div、%、 mod
select 10/3; #3.3333 / 會保留小數
select 10 div 3; #3 div 整除(只會取整個結果的整數部分)
select 10 % 3; #1 % 取模 取余(取結果的余數)
select 10 mod 3; #1 mod 取模 取余

2:比較運算符
>、<、>=、<=、=、!=、is null / is not null / between..and / in / not in
返回結果永遠都是布爾類型的值(true/false)

3:邏輯運算符
and 、or、!

select 1<0 and 2>1; #0 0==>False
select 1<0 or 2>1; #1 1==>True
select !(1<0);#1 1==>True
select 1>0; #1 1==>True
select 1<0; #0 0==>False
select 1!=0;
select 10/3; #3.3333 / 會保留小數
select 10 div 3; #3 div 整除(只會取整個結果的整數部分)
select 10 % 3; #1 % 取模 取余(取結果的余數)
select 10 mod 3; #1 mod 取模 取余

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM