MySQL表&事務

MySQL表&事務


本文偏重於基本DQL和DML,DDL請點擊MySQL對錶和數據庫操作

查詢表數據

select * from 表名;#查詢表中所有數據
select 字段1,字段2 from 表名;#查詢字段1,字段2數據
select * from 表名 where 字段1[ = | > | < ]'string';#查詢字段1爲string數據
select * from 表名 where 字段1 like '%string%';#查詢字段1中帶有string數據
#%a' //以a結尾的數據
#'a%' //以a開頭的數據
#'%a%' //含有a的數據
#'_a_' //三位且中間字母是a的
#'_a' //兩位且結尾字母是a的
#'a_' //兩位且開頭字母是a的

select * from 表名 where 字段1 in (1,3,5);#查詢字段1中1,3,5數據
select * from 表名 where 字段1=1 or 字段1=3;#查詢字段1中1,3數據
select distinct(字段名) from 表名#去掉重複值(字段名);
select 字段1,字段2[ + | - | * | / ] [as '別名'] from 表名;
select * from 表名 order by 字段名 acs/desc;#查詢表中所有數據並按升/降序排列
select sum(字段名) from 表名;#獲得所有字段名總和
	#與sum類似:avg/求平均值 count/行的數量 max/最大值 mini/最小值
	#MySQL聚合函數的總結很 👍 https://www.cnblogs.com/geaozhang/p/6745147.html
select * from 表名 group by 字段;#根據字段分組
#select 查詢的字段
#from 要查詢的表
#where 條件
#group by 分組
#having 分組後使用條件使用相當於where
#order by 必須放到最後面

插入表數據

insert into 表名(列名1,列名2,列名3……) values(1,2,3……)
insert into 表名 values(1,2,3……)#值必須爲全部

更新表數據

update 表名 set 字段名=, 字段名=...;
update 表名 set字段名=, 字段名=... where 條件;#where條件篩選

刪除表數據

delete from 表名;#清空表數據
delete from 表名 where 條件;#where條件篩選
truncate table 表名;#清空表數據
truncate table 表名 where 條件;#where條件篩選
#delete和truncate區別:
	#前者刪除自增列不會重置 後者相當於直接刪除並創一個相同的表 事務無法阻止truncate!

MySQL事務(默認開啓事務)

begin / start transaction#二選一都可作爲啓動事務
rollback#回滾事務開始之前
commit#提交事務(事務結束)
#修改MySQL默認開啓自動提交事務
①臨時生效 (只對當前客戶端有效) set @@autocommit=0 (0爲關閉狀態,1爲開啓狀態)
②永久有效 修改my.ini [mysqld] (服務器選項) 添加 : set autocommit=0
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章