? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Python學(xué)習(xí)day01(mysql)
Mysql 一般用來做網(wǎng)站,
Redis 一般用來做緩存
Mongodb 做非關(guān)系數(shù)據(jù)庫
SQL : 結(jié)構(gòu)化查詢語言,
SQL:語言分為:
DQL: 數(shù)據(jù)查詢語言,用于對數(shù)據(jù)進(jìn)行查詢,如select
DML: 數(shù)據(jù)操作語言,對數(shù)據(jù)進(jìn)行增加,刪除,修改,如insert,update,delete.
TPL:事務(wù)處理語言,對事務(wù)進(jìn)行處理,包括begin,transaction,commite,rollback.
DCL:數(shù)據(jù)控制語言,進(jìn)行授權(quán)與權(quán)限回收,如grant,revoke,
DDL:數(shù)據(jù)定義語言,進(jìn)行數(shù)據(jù)庫,表的管理等,如create,drop
CCL: 指針控制語言,通過控制指針完成表的操作,如declare cursor
使用數(shù)據(jù)的原則是:夠用就行,盡量使用取值范圍小的,而不用大的,這樣可以更多的節(jié)省存儲空間
常用數(shù)據(jù)類型如下:
堕汞。整數(shù):int,bit
蕊程。小數(shù):decimal???? decimal(5喂走,2)表示有5位其中2位是小數(shù)
。字符串:varchar(可變) char
犁柜。日期時間:data剖毯,time圾笨,datetime
。枚舉類型(enum)
特別說明如下:
逊谋。Decimal表示浮點數(shù)擂达,如decimal(5,2)表示共存5位數(shù),小數(shù)占2位
涣狗。Char表示固定長度的字符串谍婉,如char(3)若位數(shù)達(dá)不到會在末尾補空字符
。字符串text表示儲存大文本镀钓,當(dāng)字符大于4000時推薦使用
穗熬。對于圖片,音頻丁溅,視頻等文件唤蔗,不存儲在數(shù)據(jù)庫中,而是上傳到某個服務(wù)器窟赏,然后在表中存儲這個文件的保存路徑
約束:
妓柜。主鍵primary key
:物理上存儲的順序
。非空not null:此字段不允許填寫空值
涯穷。外鍵foreign key(存儲別的表的主鍵到自己表中就是外鍵):對關(guān)系字段進(jìn)行約束棍掐,
MySQL數(shù)據(jù)庫操作的基本語句;
Show databases;? //展示數(shù)據(jù)庫
show create database wuliu? //展示創(chuàng)建數(shù)據(jù)庫wuliu的語句
查看當(dāng)前使用的數(shù)據(jù)庫:? select database();
查看表結(jié)構(gòu): desc +具體表名拷况;
修改表--添加字段: alter table 表名 add 列名類型作煌;
修改表---修改字段:不重命名版
alter table 表名modify 列名類型及約束;
alter table students modify birthday date;
修改表---修改字段:重命名版
alter table 表名change 源表名新名類型及約束赚瘦;
alter table students change birthday birthdate default “1990-01-01”;
刪除表字段:alter
table 表名 drop 列名粟誓;
alter table students drop high;
刪除表:drop table表名;
查看表創(chuàng)建語句:show create
table students; //就會顯示創(chuàng)建表的語句
---修改表內(nèi)容:
update 表名 set 列1=值1起意,列2=值2..... where 條件鹰服;
---刪除
----物理刪除
----- delete
from students; ---整個數(shù)據(jù)表中得所有數(shù)據(jù)全部刪除
---邏輯刪除
--- 用一個字段來表示這條信息是否已經(jīng)不能再使用了
---- 給students表添加一個 is_delete 字段 bit 類型
alter tablestudents add is_delete bit default 0;
數(shù)據(jù)庫語句的查詢
---查詢
-----查詢所有字段
----- select *from 表名;
------ 查詢指定字段
----- select 列1揽咕,列2悲酷,.... from 表名
-----使用 as 給字段起別名
----- select 字段 as 名字,.... from 表名心褐;
-----可以通過as 給表起別名
select s.name ,s.age from students as s;
------消除重復(fù)行
------ distinct 字段 select distinct gender from? student //把表中相同的數(shù)據(jù)篩選掉舔涎。
----條件查詢
--比較運算符
---- select .......from表名where ......
---? >
, <, >=,= !=或者< >
--- or 表示或者
--- not
---不在 18歲以上的女性這個范圍內(nèi)的信息
select *from students where not age >18 and gender =2;
select *from students where not (age >18 and gender =2);
not 在誰前面就否定誰,加括號使后面的先運行逗爹。
----模糊查詢
----like
---- % 替換1個或者多個
---- _ 替換1個
---- 查詢姓名中以“小“開始的名字”
---- rlike 正則
--- 查詢以周開始的姓名? ?select name from students where name relike? “^周.*”;
----范圍查詢
--- in(1,3,8)表示在一個非連續(xù)的范圍內(nèi)
-- 查詢年齡為18亡嫌, 34 的姓名
select name,agefrom students where age =18 or age = 34
或者select name age from students where age in (19,34)
---not in 不在什么范圍之類
----between and 表示在一個連續(xù)的范圍內(nèi)
-----not between and 表示不在一個連續(xù)的范圍內(nèi)
----空判斷
--- 判斷is null
-- 查詢身高為空的信息
----判斷非空is not null’
---排序
----order by 字段
----asc 從小到大排序,即升序
--- desc 從大到小的排序掘而,即降序
----聚合函數(shù)
---總數(shù)
---count
---查詢男性有多少人挟冠,女性有多少人
select count(*)from students where gender=1;
select count(*)
as 男姓人數(shù)fromstudents where gender =1;
---最大值
--max
---查詢最大的年齡
select max(age)from students;
---最小值
---min
---求和
---sum
select sum(age)from students;
--平均值
---avg
---計算平均值
select avg(age)
from students; 或者selectsum(age)/count(*) from students;
----四舍五入 round(123.23 ,1) 保留1位小數(shù)
---- 計算所有人的平均年齡,保留兩位小數(shù)
select round(sum(age)/count(*),2) from students ;
----分組
----group by
--- 按照性別分組袍睡,查詢所有的性別
---group_concat(字段名稱)知染,顯示分組詳情
----having
----查詢平均年齡超過30歲的性別,以及姓名having avg(age)>30;
----分頁
--- limit start,
count (start:開始位置斑胜,count:個數(shù))
----限制查詢出來的數(shù)據(jù)個數(shù)
select *fromstudents where gender=1 limit 2;
---查詢前5個數(shù)據(jù)
select *fromstudents limit 0,5;
----連接查詢
---inner join ... on
----select ...from 表 A inner join表B;
select * from students inner join classes;
--- 查詢有能夠?qū)?yīng)班級的學(xué)生以及班級信息
select *fromstudents inner join classes on students.cls_id = classes.id
---- 按照要求顯示姓名控淡,班級
selectstudents.name, classes.name from students inner join classes on students.cls_id= classes.id;
--- 給數(shù)據(jù)表起名字
select s.name,c.name from students as s inner join classes as c on s.cls_id = c.id;
----left join (以左邊表里面的所有信息為基準(zhǔn)到右邊表里面去匹配)沒有就補NULL
[if !vml]
[endif]
而內(nèi)連接是需要兩個表都要才取出來
[if !vml]
[endif]
---- 查詢每位學(xué)生對應(yīng)的班級信息
selectc.name,s.*? from students as s left joinclasses as c on s.cls_id = c.id
注意:外鍵填另一個表里面的主鍵嫌吠。
------內(nèi)關(guān)聯(lián)
-----用一張表來解決關(guān)聯(lián)問題叫做內(nèi)關(guān)聯(lián)(后面的字段關(guān)聯(lián)前面的字段)即能不能將兩張表合為一張表
[if !vml]
[endif]
數(shù)據(jù)庫設(shè)計必須要滿足三范式