數(shù)據(jù)庫操作:
* 創(chuàng)建數(shù)據(jù)庫
create database 數(shù)據(jù)庫名 charset=utf8;
* 修改數(shù)據(jù)庫編碼格式
alter database 數(shù)據(jù)庫名 character set utf8;
* 查看所有數(shù)據(jù)庫
show databases;
* 刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名;
* 切換數(shù)據(jù)庫
use 數(shù)據(jù)庫名;
* 查看當(dāng)前選擇的數(shù)據(jù)庫
select database();
表操作:
* 查看當(dāng)前數(shù)據(jù)庫中所有表 show tables;
* 創(chuàng)建表 auto_increment表示自動增長
* 修改表
alter table 表名 add|change|drop 列名 類型;
如: alter table students add birthday datetime;
alter table students change birthday day datetime;
* 刪除表
drop table 表名;
* 查看表結(jié)構(gòu)
desc 表名;
* 更改表名稱
rename table 原表名 to 新表名;
* 查看表的創(chuàng)建語句
show create table '表名';
數(shù)據(jù)操作:
* 查
select * from 表名
* 增加
全列插入:insert into 表名 values(...)
缺省插入:insert into 表名(列1,...) values(值1,...)
同時插入多條數(shù)據(jù):insert into 表名 values(...),(...)...;
或insert into 表名(列1,...) values(值1,...),(值1,...)...;
注* 主鍵列是自動增長驳棱,但是在全列插入時需要占位批什,通常使用0,插入成功后以實際數(shù)據(jù)為準(zhǔn)
* 修改
update 表名 set 列1=值1,... where 條件
* 刪除
delete from 表名 where 條件
- 邏輯刪除蹈胡,本質(zhì)就是修改操作update
alter table students add isdelete bit default 0;
如果需要刪除則
update students set isdelete=1 where ...;
邏輯刪除
* 對于重要數(shù)據(jù)渊季,并不希望物理刪除,一旦刪除罚渐,數(shù)據(jù)無法找回
* 一般對于重要數(shù)據(jù)却汉,會設(shè)置一個isDelete的列,類型為bit荷并,表示邏輯刪除
* 大于大量增長的非重要數(shù)據(jù)合砂,可以進行物理刪除
* 數(shù)據(jù)的重要性,要根據(jù)實際開發(fā)決定
消除重復(fù)行
* 在select后面列前使用distinct可以消除重復(fù)的行
select distinct 字段名 from 表名;
條件
* 使用where子句對表中的數(shù)據(jù)篩選源织,結(jié)果為true的行會出現(xiàn)在結(jié)果集中
* 語法如下:
select * from 表名 where 條件;
比較運算符
* 等于=
* 大于>
* 大于等于>=
* 小于<
* 小于等于<=
* 不等于!=或<>
* 查詢編號大于3的學(xué)生
select * from 表名 where 條件;
* 查詢編號不大于4的科目
select * from 表名 where id<=4;
* 查詢姓名不是“黃蓉”的學(xué)生
select * from students where sname!='黃蓉';
* 查詢沒被刪除的學(xué)生
select * from students where isdelete=0;
邏輯運算符
* and
* or
* not
* 查詢編號大于3的女同學(xué)
select * from students where id>3 and gender=0 ;
* 查詢編號小于4或沒被刪除的學(xué)生
select * from students where id<4 or isdelete=0;
模糊查詢
* like
* %表示多個任意字符
* _表示一個任意字符
查詢姓黃的學(xué)生
select * from students where name like '黃%';
查詢姓黃并且名字是一個字的學(xué)生
select * from 表名 where 字段名 like '黃_';
查詢姓黃或叫靖的學(xué)生
select * from students where sname like '黃%' or sname like '%靖%';
范圍查詢
* in表示在一個非連續(xù)的范圍內(nèi)
* 查詢編號是1或3或8的學(xué)生
select * from students where id in(1,3,8);
* between ... and ...表示在一個連續(xù)的范圍內(nèi)
* 查詢學(xué)生是3至8的學(xué)生
select * from students where id between 3 and 8;
* 查詢學(xué)生是3至8的男生
select * from students where id between 3 and 8 and gender=1;
空判斷
* 注意:null與‘’是不同的
* 判空is null
* 查詢沒有填寫地址的學(xué)生
select * from students where hometown is null;
* 判非空is not null
* 查詢填寫了地址的學(xué)生
select * from students where hometown is not null;
* 查詢填寫了地址的女生
select * from students where hometown is not null and gender=0;
優(yōu)先級
* 小括號翩伪,not,比較運算符谈息,邏輯運算符
* and比or先運算缘屹,如果同時出現(xiàn)并希望先算or,需要結(jié)合()使用
聚合函數(shù):
為了快速得到統(tǒng)計數(shù)據(jù)侠仇,提供了5個聚合函數(shù)
count(*)表示計算總行數(shù)轻姿,括號中寫星與列名,結(jié)果是相同的
max(列)表示求此列的最大值
min(列)表示求此列的最小值
sum(列)表示求此列的和
avg(列)表示求此列的平均值
排序:
* 默認(rèn)按照列值從小到大排列
* asc從小到大排列逻炊,即升序
* desc從大到小排序互亮,即降序
查詢未刪除男生學(xué)生信息,按學(xué)號降序
select * from students where gender=1 and isdelete=0 order by id desc;
分頁:
* 當(dāng)數(shù)據(jù)量過大時余素,在一頁中查看數(shù)據(jù)是一件非常麻煩的事情
* 語法 :select * from 表名limit start,count
* 從start開始豹休,獲取count條數(shù)據(jù)
* start索引從0開始
* 執(zhí)行順序為:
* from 表名
* where ....
* group by ...
* select distinct *
* having ...
* order by ...
* limit star,count
外鍵
如果一張表中有一個非主鍵的字段指向了別一張表中的主鍵,就將該字段叫做外鍵桨吊。
一張表中可以有多個外鍵威根。
外鍵的默認(rèn)作用有兩點:
1.對子表(外鍵所在的表)的作用:子表在進行寫操作的時候,如果外鍵字段在父表中找不到對應(yīng)的匹配视乐,操作就會失敗医窿。
2.對父表的作用:對父表的主鍵字段進行刪和改時,如果對應(yīng)的主鍵在子表中被引用炊林,操作就會失敗姥卢。
使用外鍵的前提:
1. 表儲存引擎必須是innodb,否則創(chuàng)建的外鍵無約束效果渣聚。
2. 外鍵的列類型必須與父表的主鍵類型完全一致独榴。
3. 外鍵的名字不能重復(fù)。
4. 已經(jīng)存在數(shù)據(jù)的字段被設(shè)為外鍵時奕枝,必須保證字段中的數(shù)據(jù)與父表的主鍵數(shù)據(jù)對應(yīng)起來棺榔。
連接查詢
* 連接查詢分類如下:
* 表A inner join 表B:表A與表B匹配的行會出現(xiàn)在結(jié)果中
* select * from 表1 inner join 表2 on 外鍵=主鍵;
* 表A left join 表B:表A與表B匹配的行會出現(xiàn)在結(jié)果中,外加表A中獨有的數(shù)據(jù)隘道,未對應(yīng)的數(shù)據(jù)使用null填充
* select * from 表1 left join 表2 on 外鍵=主鍵;
* 表A right join 表B:表A與表B匹配的行會出現(xiàn)在結(jié)果中症歇,外加表B中獨有的數(shù)據(jù)郎笆,未對應(yīng)的數(shù)據(jù)使用null填充
* 在查詢或條件中推薦使用“表名.列名”的語法
* 如果多個表中列名不重復(fù)可以省略“表名.”部分
* 如果表的名稱太長,可以在表名后面使用' as 簡寫名'或' 簡寫名'忘晤,為表起個臨時的簡寫名稱
子查詢(嵌套查詢)
select * from 表 where 字段 in (select 字段 from 表 where 條件)
- 查詢支持嵌套使用
日期時間函數(shù)
* 獲取子值宛蚓,語法如下
* year(date)返回date的年份(范圍在1000到9999)
* month(date)返回date中的月份數(shù)值
* day(date)返回date中的日期數(shù)值
* hour(time)返回time的小時數(shù)(范圍是0到23)
* minute(time)返回time的分鐘數(shù)(范圍是0到59)
* second(time)返回time的秒數(shù)(范圍是0到59)
select date_format('2016-12-21','%Y %m %d');
* 當(dāng)前日期current_date()
select current_date();
* 當(dāng)前時間current_time()
select current_time();
* 當(dāng)前日期時間now()
select now();
事務(wù)
* 當(dāng)一個業(yè)務(wù)邏輯需要多個sql完成時,如果其中某條sql語句出錯设塔,則希望整個操作都退回
* 使用事務(wù)可以完成退回的功能凄吏,保證業(yè)務(wù)邏輯的正確性
* 事務(wù)四大特性(簡稱ACID)
* 原子性(Atomicity):事務(wù)中的全部操作在數(shù)據(jù)庫中是不可分割的,要么全部完成闰蛔,要么均不執(zhí)行
* 一致性(Consistency):幾個并行執(zhí)行的事務(wù)痕钢,其執(zhí)行結(jié)果必須與按某一順序串行執(zhí)行的結(jié)果相一致
* 隔離性(Isolation):事務(wù)的執(zhí)行不受其他事務(wù)的干擾,事務(wù)執(zhí)行的中間結(jié)果對其他事務(wù)必須是透明的
* 持久性(Durability):對于任意已提交事務(wù)序六,系統(tǒng)必須保證該事務(wù)對數(shù)據(jù)庫的改變不被丟失任连,即使數(shù)據(jù)庫出現(xiàn)故障
* 要求:表的類型必須是innodb或bdb類型,才可以對此表使用事務(wù)
* 事務(wù)語句
開啟begin;
提交commit;
回滾rollback;