MySQL筑基篇之增刪改查操作詳解
一、增加表中數(shù)據(jù)
1、無自增列時
1.指定字段添加數(shù)據(jù)
給表中的部分列添加數(shù)據(jù):值的順序必須跟指定列的順序保持一致
語法:insert into 表名(列1,列2,...) values(值1,值2,...)
2.默認添加數(shù)據(jù)
向表中的所有列添加數(shù)據(jù):值的順序必須跟字段順序保持一致
語法:insert into 表名 values(值1,值2,...)
2、有自增列時
1.對于指定字段的添加
不用指定自增列,語法同無自增列一致
2.對于默認情況的添加
必須手動為自增列賦值或者填上null
例如:insert into t_person VALUES(null,'wangwu',32,'男',150.9')
自增列設(shè)置方法:
create table if not exists t_person( p_id int primary key auto_increment,-- 主鍵 自增 ... )
關(guān)鍵字:auto_increment
二、刪除表中數(shù)據(jù)
1、使用delete
語法:delete from 表名 [where條件]
刪除所有數(shù)據(jù),例如:delete from t_person
刪除指定數(shù)據(jù),例如:delete from t_person where p_id=8
2、使用truncate
語法:truncate table 表名
通過表截斷(truncate)的方式刪除數(shù)據(jù)要優(yōu)于使用delete,原因:
delete是一條一條刪除,效率低,而truncate是直接在物理空間中將存放該表數(shù)據(jù)的空間截斷舍棄,效率更快
delete主鍵會繼續(xù)刪除之前的自增,而truncate會重新開始自增
三、修改表中數(shù)據(jù)
語法:update 表名 set 列名1=新值,列名2=新值,... [where 條件]
操作整張表,例如:update t_person set age=18
操作部分數(shù)據(jù),例如:update t_person set age=28 where p_id=1
第一個例子的含義是把t_person表中所有的age屬性改為18,第二個含義是只把p_id為1對應(yīng)的age改為28
四、*查詢操作
查詢是數(shù)據(jù)庫基礎(chǔ)的重點,拿小本本記上
1、簡單查詢
1.查詢所有列
select * from 表名
2.查詢部分列
select 列名1,列名2,... from 表名
可以通過列出所有字段名的方式查詢所有列
弊端:書寫繁瑣
優(yōu)勢:可維護性更高、更靈活、執(zhí)行效率更快
3.別名
select 列名1 as 別名1,列名2 as 別名2,... from 表名
as關(guān)鍵字可省
select 列名1 別名1,列名2 別名2,... from 表名
表名也可以起別名
別名使用示例:
SELECT employee_id as 員工編號,salary as 工資 from employees SELECT employee_id 員工編號,salary 工資,first_name,last_name from employees e
4.數(shù)學(xué)運算
select 列名+數(shù)字,列名-數(shù)字,列名*數(shù)字,列名/數(shù)字,列名%數(shù)字 from 表名
5.去重
select distinct 列名 from 表名
去重規(guī)則可以為多個列,只有當規(guī)則中的所有列的信息完全一致時才會去重 :
select distinct 列名1,列名2,... from 表名
6.case when
select 列名1,列名2, case when 條件1 then 結(jié)果2 when 條件2 then 結(jié)果2 ... else 其他結(jié)果 end from 表名
- when從上至下判斷
- 每行數(shù)據(jù)只會執(zhí)行一個when
- 類似java中的多重if分支:case開啟分支,end結(jié)束分支
使用示例:
查詢員工id及其工資,并對工資進行評級:工資>10000 高薪,工資>8000 中級,工資>5000 低級,其他 底層
select employee_id,salary, case when salary>10000 then '高薪' when salary>8000 then '中級' when salary>5000 then '低級' else '底層' end as '薪資等級' from employees
7.查詢表詳情
describe 表名
describe可以簡寫成desc:
desc 表名
2、條件查詢
語法:
select 列名 from 表名 where 條件
1.單條件查詢
查詢工資>10000的員工信息:
SELECT * from employees where salary>10000
比較的類型為字符串時,對比數(shù)據(jù)需要加上單引號
mysql默認不區(qū)分大小寫,如有需求,則在對應(yīng)位置添加binary關(guān)鍵字
查詢first_name為Steven的所有員工信息:
select * from employees where first_name='STEVEN'
區(qū)分大小寫:
select * from employees where binary first_name='STEVEN'
2.多條件查詢
多個條件之間須通過and或者or進行拼接:
and:代表并且,多個條件同時滿足,相當于java中的&&
or:代表或者,滿足任意一個即可,相當于java中的||
3.區(qū)間查詢
在區(qū)間內(nèi)
between 最小值 and 最大值
不在范圍內(nèi)
not between 最小值 and 最大值
4.枚舉查詢
在列舉范圍內(nèi)
列名 in(值1,值2,值3,...)
查詢員工id為100、105、110的員工信息
select * from employees where employee_id=100 or employee_id=105 or employee_id=110
等價于:
select * from employees where employee_id in(100,105,110)
不在列舉范圍內(nèi)
列名 not in(值1,值2,值3,...)
查詢員工id不是100、105、110的員工信息
select * from employees where employee_id not in(100,105,110)
5.空值查詢
為空時
列名 is null
不為空時
列名 is not null
查詢commission_pct為null的員工信息
select * from employees where commission_pct is null
查詢commission_pct不為null的員工信息
select * from employees where commission_pct is not null
6.模糊查詢
語法:
where 列名 like '值'
%:代表不固定長度,可以為0-n個字符
_:代表一個長度
示例:
查詢first_name中包含s的員工信息
select * from employees where first_name like '%s%'
查詢firstname中以s開頭的員工信息
select * from employees where first_name like 's%'
查詢firstname中以s結(jié)尾的員工信息
select * from employees where first_name like '%s'
查詢firstname中第二個字母為s的員工信息
select * from employees where first_name like '_s%'
3、排序
對查詢結(jié)果進行指定規(guī)則的排序顯示
1、單列排序
select 列名 from 表名 order by 列名 asc(升序)|desc(降序)
示例:
根據(jù)工資從高到低顯示員工信息
select * from employees order by salary desc
根據(jù)工資從低到高
select * from employees order by salary asc select * from employees order by salary
tips: 默認為升序排列
2、多列排序
order by 列名1 asc|desc , 列名2 asc|desc,...
示例:
根據(jù)工資從低到高顯示員工信息,如果工資相同,根據(jù)員工id從高到低顯示
select * from employees order by salary asc,employee_id desc
3、where+order by
select 列名 from 表名
where 篩選條件
order by 排序條件
示例:
查詢工資>10000的員工信息,從高到低顯示
select * from employees where salary>10000 order by salary desc
到此這篇關(guān)于MySQL筑基篇之增刪改查操作詳解的文章就介紹到這了,更多相關(guān)MySQL增刪改查內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談訂單重構(gòu)之 MySQL 分庫分表實戰(zhàn)篇
這篇文章主要介紹了 MySQL 分庫分表方法的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容,希望能幫助到你2021-09-09mysql中關(guān)于Myisam_recover自動修復(fù)的使用方法
這篇文章主要介紹了mysql中關(guān)于Myisam_recover自動修復(fù)的使用方法,需要的朋友可以參考下2016-05-05

xtrabackup備份還原MySQL數(shù)據(jù)庫