MySQL從刪庫到跑路
MySQL資料庫指令集
增(insert)
- 不指定欄位
insert into <表名> values(值1,值2,值3...);
- 指定欄位(沒給到值的欄位為預設值或null)
insert into <表名>[(欄位1,欄位2,欄位3,...)] values(值1,值2,值3...);
- insert與子查詢(插入多條資料)
insert into <表名> <子查詢>;
- replace插入
用法與insert基本相同,如:replace into <表名> values(值1,值2,值3...);
,不同的是如果發現表中已經有此行資料(根據主鍵或者唯一索引判斷)則先刪除此行資料,然後插入新的資料。否則,直接插入新資料。
注意:因為要根據主鍵或者是唯一索引判斷是否有重複資料,所以操作的表必須要有主鍵或者是唯一索引。否則的話,replace into 會直接插入資料。
刪(delete)
- 常用刪除語句
delete from <表名> [where condition];
- delete與子查詢(刪除多條資料)
delete from <表名> where 欄位=<子查詢>;
- truncate清空表記錄
truncate <表名>;
改(update)
- 常用更新語句
update <表名> set 欄位1=值1,欄位2=值2... [where condition];
- update與子查詢(修改多條資料)
update <表名> set 欄位1=值1,欄位2=值2... where 欄位=<子查詢>;
查(select)
- 常用查詢語句
select [distinct] <欄位名或表示式>[,<欄位名或表示式>] from <表名或檢視名>[,<表名或檢視名>] [where <條件表示式>] [group by <欄位名>[having <條件表示式>]] [order by<欄位名>[asc|desc]] [limit [start,]count]
- distinct關鍵字
這個關鍵字來過濾掉多餘的重複記錄只保留一條,但往往只用它來返回不重複記錄的條數,而不是用它來返回不重記錄的所有值。其原因是distinct只能返回它的目標欄位,而無法返回其它欄位
- [where <條件表示式>]
1.關係表示式查詢
關係運算符:=(等於)、>(大於)、<(小於)、>=(大於等於)、<=(小於等於)、!=或<>(不等於)
eg:select name from user where id>10;
2.邏輯表示式查詢
邏輯運算子(優先順序從高到低):not、and、or
eg:select * from user where name='simu' and age=20;
3.設定取值範圍的查詢
謂詞:between ... and ... 或 not between ... and ...
eg:select * from user where id between 10 and 20;
4.空值查詢
謂詞:is null 或 is not null
eg:select * from user where id is null;
5.模糊查詢
謂詞:like 或 not like
eg:select * from user where name like 'simu';