大家好,我是anyux。上一節簡單介紹了SQL語句的DDL(數據定義語言)、DCL(數據控制語言)。本節是對於SQL語句DML(數據操作語言)、DQL(數據查詢語言)進入演示操作。
DML的應用
數據操作語言DML主要有三種形式:
1.插入:INSERT
2.更新:UPDATE
3.刪除:DELETE
以下操作基於已經創建的zabbix數據庫及庫下的stu表,請先準備。也可聯繫作者獲取相應sql文件
<code> #-----偷懶插入數據方法 insert `zabbix`.`stu` values(1,'zs',18,'m',now()); #-----最規範插入數據方法 insert into `zabbix`.`stu`(id,name,age,sex,intime) values(2,'ls',19,'m',now()); #-----針對性錄入數據 insert into `zabbix`.`stu`(name,age,sex) values('ww',11,'m'); #-----一次性錄入多行數據 insert into `zabbix`.`stu`(name,age,sex) values('aa',11,'m'),('bb',12,'f'),('cc',13,'m'); update #-----更新操作 update `zabbix`.`stu` set name='zhangfeifei' where id=10; delete #-----刪除操作 insert into `zabbix`.`stu` (name,age,sex) values('dd',20,'f'); select * from `zabbix`.`stu`; delete from `zabbix`.`stu` where name='dd' and age =20 and sex='f';/<code>
業務數據應用
delete 和truncate刪除區別
- delete:DML操作,是邏輯性質刪除,逐行刪除,速度慢
- truncate:DDL操作,將表中的數據頁進行清空,速度快
<code> #----- 邏輯刪除 delete from `zabbix`.`stu`; #----- 數據清空 truncate table `zabbix`.`stu`;/<code>
數據偽刪除
一般地,使用update替代delete,最終保證業務查找不到
<code> #-----增加數據列 alter table `zabbix`.`stu` add state tinyint not null default 1; #-----查看數據表狀態 select * from `zabbix`.`stu` where state=1; #-----使用update替換delete update `zabbix`.`stu` set state=0 where id=6; #-----查看數據表狀態 select * from `zabbix`.`stu` where state=1;/<code>
DQL的應用
select語法簡單應用
<code>查看當前數據庫端口select @@port;+--------+| @@port |+--------+| 3306 |+--------+查看當前數據庫應用程序目錄select @@basedir;+---------------------+| @@basedir |+---------------------+| /application/mysql/ |查看當前數據庫應用程序服務idselect @@server_id;+-------------+| @@server_id |+-------------+| 6 |+-------------+控制MySQL 磁盤寫入策略select @@innodb_flush_log_at_trx_commit;+----------------------------------+| @@innodb_flush_log_at_trx_commit |+----------------------------------+| 1 |+----------------------------------+查看MySQL應該支持的sql語法,對數據的校驗等等select @@sql_mode;+-------------------------------------------------------------------------------------------------------------------------------------------+| @@sql_mode |+-------------------------------------------------------------------------------------------------------------------------------------------+| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |+-------------------------------------------------------------------------------------------------------------------------------------------+/<code>
其實數據庫中自帶了很多的內置函數,比如查看當前所在數據庫,相當於Linux的pwd命令
<code> -----查看當前數據庫select database(); -----查看當前時間 select now();/<code>
以上select查詢都簡單的使用,可以被其他工具替代。下面是select語句主要的使用場景
select通用語法(單表)
所謂select通用語法是指,select語法通過配合其他查詢子句。如select .... from
語法構成
<code> select 列 from 表 where 條件 group by 條件 order by 條件 limit x,y/<code>
環境說明
此處需要一個world.sql文件,導入到數據庫中,需要文件的同學,可以聯繫作者
select配合from子句應用
查詢表中所有的信息(生產中幾乎是沒有這種需求的)
<code> use world; select id,name,countrycode,district,population from city;/<code>
<code> #-----簡便寫法 select * from city; #-----查詢表中 name 和 population的值 select name,population from city;/<code>
select配合where子句應用
<code> #-----select 列,列,列 from 表 where 過濾條件 select name,population from city where countrycode='CHN';/<code>
世界上小於100人的城市
<code> #-----where配合比較判斷查詢(> < >= <=) select name,population from city where population<100;/<code>
查詢中國人口數量大於10萬的城市和人口
<code> #-----where配合邏輯連接符(and or not) select name,population from city where countrycode='CHN' and population>100000;/<code>
查詢中國或美國的城市名和人口數
<code> select name,population from city where countrycode='CHN' or countrycode="USA";/<code>
查詢人口數量在50萬到60萬之間的城市名和人品數
<code> #-----查詢人口數量在50萬到60萬之間的城市名和人品數 select name,population from city where population > 500000 and population /<code>
where配合like語句,實現模糊查詢
<code> #-----where配合like語句,實現模糊查詢 #-----查詢contrycode中有C開頭的城市信息 #-----注意,不要出現%C%這種情況,這樣做不走索引,功能上可以實現,但對性能影響巨大。如果業務中有大量模糊查詢的需求,可以使用ES來替代 select * from city where countrycode like 'C%';/<code>
查詢中國或美國的城市信息
<code> #-----where配合in語句 select name,population from city where countrycode='CHN' or countrycode='USA'; select name,population from city where countrycode in ('CHN',"USA")/<code>
本節主要是對於SQL語句的DML(數據操作語言)和DQL(數據查詢語言)進行演示。其中如有不足還各位同學指出。下一節將對DQL的select操作進行更多深入的演示。
閱讀更多 anyux1 的文章