MySQL筑基篇之增刪改查操作詳解
一、增加表中數(shù)據(jù)
1、無自增列時(shí)
1.指定字段添加數(shù)據(jù)
給表中的部分列添加數(shù)據(jù):值的順序必須跟指定列的順序保持一致
語法:insert into 表名(列1,列2,...) values(值1,值2,...)
2.默認(rèn)添加數(shù)據(jù)
向表中的所有列添加數(shù)據(jù):值的順序必須跟字段順序保持一致
語法:insert into 表名 values(值1,值2,...)
2、有自增列時(shí)
1.對(duì)于指定字段的添加
不用指定自增列,語法同無自增列一致
2.對(duì)于默認(rèn)情況的添加
必須手動(dòng)為自增列賦值或者填上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 表名
通過表截?cái)?truncate)的方式刪除數(shù)據(jù)要優(yōu)于使用delete,原因:
delete是一條一條刪除,效率低,而truncate是直接在物理空間中將存放該表數(shù)據(jù)的空間截?cái)嗌釛?,效率更?/p>
delete主鍵會(huì)繼續(xù)刪除之前的自增,而truncate會(huì)重新開始自增
三、修改表中數(shù)據(jù)
語法:update 表名 set 列名1=新值,列名2=新值,... [where 條件]
操作整張表,例如:update t_person set age=18
操作部分?jǐn)?shù)據(jù),例如:update t_person set age=28 where p_id=1
第一個(gè)例子的含義是把t_person表中所有的age屬性改為18,第二個(gè)含義是只把p_id為1對(duì)應(yīng)的age改為28
四、*查詢操作
查詢是數(shù)據(jù)庫基礎(chǔ)的重點(diǎn),拿小本本記上
1、簡單查詢
1.查詢所有列
select * from 表名
2.查詢部分列
select 列名1,列名2,... from 表名
可以通過列出所有字段名的方式查詢所有列
弊端:書寫繁瑣
優(yōu)勢:可維護(hù)性更高、更靈活、執(zhí)行效率更快
3.別名
select 列名1 as 別名1,列名2 as 別名2,... from 表名
as關(guān)鍵字可省
select 列名1 別名1,列名2 別名2,... from 表名
表名也可以起別名
別名使用示例:
SELECT employee_id as 員工編號(hào),salary as 工資 from employees SELECT employee_id 員工編號(hào),salary 工資,first_name,last_name from employees e
4.數(shù)學(xué)運(yùn)算
select 列名+數(shù)字,列名-數(shù)字,列名*數(shù)字,列名/數(shù)字,列名%數(shù)字 from 表名
5.去重
select distinct 列名 from 表名
去重規(guī)則可以為多個(gè)列,只有當(dāng)規(guī)則中的所有列的信息完全一致時(shí)才會(huì)去重 :
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ù)只會(huì)執(zhí)行一個(gè)when
- 類似java中的多重if分支:case開啟分支,end結(jié)束分支
使用示例:
查詢員工id及其工資,并對(duì)工資進(jìn)行評(píng)級(jí):工資>10000 高薪,工資>8000 中級(jí),工資>5000 低級(jí),其他 底層
select employee_id,salary, case when salary>10000 then '高薪' when salary>8000 then '中級(jí)' when salary>5000 then '低級(jí)' else '底層' end as '薪資等級(jí)' from employees
7.查詢表詳情
describe 表名
describe可以簡寫成desc:
desc 表名
2、條件查詢
語法:
select 列名 from 表名 where 條件
1.單條件查詢
查詢工資>10000的員工信息:
SELECT * from employees where salary>10000
比較的類型為字符串時(shí),對(duì)比數(shù)據(jù)需要加上單引號(hào)
mysql默認(rèn)不區(qū)分大小寫,如有需求,則在對(duì)應(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.多條件查詢
多個(gè)條件之間須通過and或者or進(jìn)行拼接:
and:代表并且,多個(gè)條件同時(shí)滿足,相當(dāng)于java中的&&
or:代表或者,滿足任意一個(gè)即可,相當(dāng)于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
等價(jià)于:
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.空值查詢
為空時(shí)
列名 is null
不為空時(shí)
列名 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個(gè)字符
_:代表一個(gè)長度
示例:
查詢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中第二個(gè)字母為s的員工信息
select * from employees where first_name like '_s%'
3、排序
對(duì)查詢結(jié)果進(jìn)行指定規(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: 默認(rèn)為升序排列
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)文章
Mysql數(shù)據(jù)庫定時(shí)備份腳本分享
這篇文章主要分享了Mysql數(shù)據(jù)庫的定時(shí)備份腳本,幫助大家更好的理解和使用MySQL數(shù)據(jù)庫,感興趣的朋友可以了解下2020-09-09淺談?dòng)唵沃貥?gòu)之 MySQL 分庫分表實(shí)戰(zhàn)篇
這篇文章主要介紹了 MySQL 分庫分表方法的相關(guān)資料,需要的朋友可以參考下面文章內(nèi)容,希望能幫助到你2021-09-09MySQL實(shí)現(xiàn)顯示百分比顯示和前百分之幾的方法
這篇文章主要介紹了MySQL中如何顯示百分比和顯示前百分之幾的,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)MySQL有一定的幫助,感興趣的小伙伴可以了解一下2021-12-12mysql中關(guān)于Myisam_recover自動(dòng)修復(fù)的使用方法
這篇文章主要介紹了mysql中關(guān)于Myisam_recover自動(dòng)修復(fù)的使用方法,需要的朋友可以參考下2016-05-05

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