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';