創(chuàng)建數(shù)據(jù)庫(kù)
create database 數(shù)據(jù)庫(kù)名 charset=utf8;
查看創(chuàng)建數(shù)據(jù)庫(kù)的語(yǔ)句
show crate database ...
使用數(shù)據(jù)庫(kù)
use 數(shù)據(jù)庫(kù)的名
刪除數(shù)據(jù)庫(kù)
drop database 數(shù)據(jù)庫(kù)名;
約束
主鍵 primary key:物理上存儲(chǔ)的順序
非空 not null:此字段不允許填寫空值
唯一 unique:此字段的值不允許重復(fù)
默認(rèn) default:當(dāng)粗填寫此值時(shí)會(huì)使用的默認(rèn)值
外鍵 foreign key:對(duì)關(guān)系字段進(jìn)行約束,當(dāng)為關(guān)系字段填寫值時(shí),會(huì)關(guān)聯(lián)的表中查詢此值是否存在,如果存在則填寫成功,如果不存在則填寫失敗并跑出異常
查看表結(jié)構(gòu)
desc tablename;
修改表-添加字段
alter table add 列名 類型;
修改字段
alter table modify 列名 類型及約束;
重命名字段
alter table change 原名 新名 類型及約束;
刪除字段
alter table drop 列名;
添加數(shù)據(jù)
insert into tablename(...) values(...);
更新數(shù)據(jù)
update tablename set 字段名=vialue where 條件;
刪除數(shù)據(jù)
delete from 表名 where 條件;
使用as給字段起別名
select 字段 as 名字 from tablename;
去重
select distinct 字段 from tablename;
條件查詢
比較運(yùn)算符
- >
select * from students where age >18;
- <
select * from students where age <18;
- >=
select * from students where age >=18;
- <=
select * from students where age <=18;
- =
select * from students where age = 18;
- != 或 <>
select * from students where age !=18;
邏輯運(yùn)算符
- and
select * from students where age >18 and age<28;
- or
select * from students where age >18 or age<28;
- not
select * from students where not (age >18 or age<28);
模糊查詢
like
% 替換1個(gè)或多個(gè)
_ 替換1個(gè)
- 以小開頭
select name from students where name like "小%";
- 包含小
select name from students where name like "%小%";
- rlike 正則
- 以周開頭
select name from students where name rlike "^周.*";
范圍查詢
in 表示在一個(gè)非連續(xù)的范圍內(nèi)
select name from students where age in (12,15);
not in 不非連續(xù)的范圍之內(nèi)
select name from students where age not in (12,15);
between ... and ... 表示在一個(gè)連續(xù)的范圍內(nèi)
select * from students where age between 18 and 35;
not between... and ... 表示不在一個(gè)連續(xù)的范圍內(nèi)
select * from students where age not between 18 and 35;
空判斷
判空 is null
select * from students where age is null;
判非空 is not null
select * from students where age is not null;
排序
order by 字段
asc 升序
desc 降序
select * from students order by age asc/desc;
聚合函數(shù)
count 統(tǒng)計(jì)
select count(*) from students;
最大值
max
最小值
min
求和
sum
平均值
avg
四舍五入
round(12.23,1) 保留1位小數(shù)
分組
group by
select gender from students group by gender;
按照性別分組并統(tǒng)計(jì)人數(shù)
select gender,count(*) from students group by
gender;
按照性別分組并顯示姓名
select gender,group_concat(name) from students
group by gender;
分頁(yè)
limit start,count
select * from students limit 2;
每頁(yè)顯示2個(gè),第2個(gè)頁(yè)面
select * from students limit 2,2;
連接查詢
內(nèi)連接
inner join ... on
select ... from 表A inner join 表B;
select * from students inner join classes on students.cls_id=classes.id;
select students.name ,classes.id inner join classes on students.cls_id=classes.id;
select s.*,c.name from students as s inner join classes as c on s.cls_id=c.id;
左連接,以左表為準(zhǔn),左表顯示所有數(shù)據(jù),右表沒有數(shù)據(jù)顯示null
left join
以c.name排序然后再按s.id排序
select s.*,c.name from students left join classes as c on s.cls_id=c.id order by c.name,s.id;
右連接
right join ...
子查詢
標(biāo)量子查詢
select * from students where age>(select avg(age) from students);
列級(jí)子查詢
select name from classes where id in (select cls_id from studets);
行級(jí)子查詢
select * from students where (height,age) = (select max(height),max(age) from students);
表關(guān)系 ER圖
1對(duì)1
兩張表任何一張表都可以建關(guān)聯(lián)字段
1對(duì)多
在多的表中建關(guān)聯(lián)字段
多對(duì)多
新建一張表建關(guān)聯(lián)字段
三范式
第一范式
原子性,表的字段不可再拆分成更小的字段
第二范式
在滿足第一范式的基礎(chǔ)上,非主鍵必須完全依賴主鍵,而不是依賴主鍵的一部分。
第三范式
滿足第二范式并且每個(gè)字段都不間接依賴于主鍵列
視圖
視圖是什么
視圖就是一條select語(yǔ)句執(zhí)行后返回的結(jié)果集
視圖是對(duì)若干張基本表的引用,一張?zhí)摫恚樵冋Z(yǔ)句
執(zhí)行的結(jié)果,不存儲(chǔ)具體的數(shù)據(jù)
定義視圖
create view 視圖名稱 as select語(yǔ)句;
查看視圖
查看表會(huì)將所有的視圖也列出來
show tables;
視圖的用途就是查詢
select * from v_stu_score;
刪除視圖
drop view 視圖名稱
drop view v_stu_score;
視圖的作用
1.提高了重用性,就像一個(gè)函數(shù)
2.對(duì)數(shù)據(jù)庫(kù)重構(gòu),卻不影響程序的運(yùn)行
3.提高了安全性能,可以對(duì)不同的用戶
4.讓數(shù)據(jù)更加清晰
事務(wù)
1 為什么要有事務(wù)
所謂事務(wù),它是一個(gè)操作序列,這些操作要么都執(zhí)行要么都不執(zhí)行,它是一個(gè)不可分割的工作單位
事務(wù)的四大特性(簡(jiǎn)稱 ACID)
原子性 Atomicity
一致性 Consistenvy
隔離性 Lsolation
持久性 Durablity
索引
索引是一種特殊的文件(InnoDB數(shù)據(jù)表上的索引是表空間的一個(gè)組成部分 ),它們包含著對(duì)數(shù)據(jù)表里所有記錄的引用指針
索引目的
索引的目的在于提高查詢效率,可以類比字典
索引的抵用
查看索引
show index from 表名;
創(chuàng)建索引
如果指定的字段是字符串,需要指定長(zhǎng)度,建議與定義字段時(shí)的長(zhǎng)度一致
字段類型如果不是字符串,可以不填寫長(zhǎng)度部分
create index 索引名稱 on 表名(字段名稱(長(zhǎng)度))
刪除索引
drop index 索引名稱 on 表名;