########################課堂代碼#####################################
mysql:典型關(guān)系型數(shù)據(jù)
1、安裝mysql:
window:
.msi
.zip
修改my.ini
bin添加到path(此電腦右鍵屬性-高級管理設(shè)置-環(huán)境變量-path新建)
以管理員身份打開cmd命令提示符,將路徑切換到bin下去
注冊mysql:mysqld install 移除mysql:mysqld remove
啟動服務(wù):net start mysql
Linux:
使用應(yīng)用倉庫安裝:
CenterOS/redhat:
sudo yum install mysql-sever mysql
ubuntu:
sudo apt|apt-get install mysql-server mysql-client
sudo service mysql restart/start/stop/status
從官網(wǎng)下載的源代碼:
2硫惕、mysql的使用
進入mysql:mysql -u用戶名 -p密碼 -h主機ip或者域名 -P端口
退出mysql:exit/quit/\q/Ctrl+c
3、mysql的遠程登錄:
1故响、以管理員身份進入mysql
update mysql.user set host='%' where user='root';
2签则、修改/etc/mysql/mysql.conf.d/mysql.cnf
找到43行把bind-address注釋掉
3、重啟服務(wù)
service mysql start
4碍遍、mysql的一些常規(guī)操作:
show databases like pattern; #顯示當前所有的數(shù)據(jù)庫
use dbName; #進入到對應(yīng)的數(shù)據(jù)庫
show tables like pattern; #顯示當前數(shù)據(jù)庫的所有的表
desc/describe tableName; #描述表的結(jié)構(gòu)
show columns from t_user; #描述表的結(jié)構(gòu)
select database(); #顯示當前所在的數(shù)據(jù)庫
select now(); #返回當前的時間
select version(); #返回當前數(shù)據(jù)庫的版本信息
5、Sql標準的講解:
DDL(data definition language)數(shù)據(jù)庫定義語言
create drop alter show
DML(data manipulation language)數(shù)據(jù)庫操作語言
CRUD
DQL數(shù)據(jù)庫查詢語言
DCL()數(shù)據(jù)庫控制語言
grant invoke
6阳液、數(shù)據(jù)庫:
創(chuàng)建數(shù)據(jù)庫:create database dbName [[default] charset'utf8']
刪除數(shù)據(jù)庫:drop database dbName;
修改數(shù)據(jù)庫:alter database dbName default charset gbk;
查看數(shù)據(jù)庫定義:show creat database dbName;
7怕敬、mysql的數(shù)據(jù)類型:
整型:
tinyint 一個字節(jié)
smallint 兩個字節(jié)
mediumint 三個字節(jié)
int 四個字節(jié)
bigint 八個字節(jié)
浮點型:
float 四個字節(jié)
double 八個字節(jié)
decimal(2,2) 精確運算時使用
文本型:
char(lenght) 定
varchar(lenght) 不定
二進制:
bit,binary,Blob
日期:
year,month,day,time,data,datatime,timestamp(游標戳)
枚舉:
enum()
8、表的常見操作:
表的創(chuàng)建:
create table tName(
field int primary key auto-increment,
gender varchar(30) not null
)default charset utf8 engint=innodb/mysiam
查詢表創(chuàng)建的信息:
show create table tName[\G];#\G翻轉(zhuǎn)90度
對表結(jié)構(gòu)的修改:
修改表名:
rename table oldTableName to newTableName;
alter table tableName rename to new TableName;
修改表的屬性(字符集|引擎):
alter table tableNme charset=gbk
alter table t_test charset='gbk';
修改表的字段:
alter table tName change/modify/drop/add field
增加一個新的字段:
alter table t_test add newColumn codition位置
eg:alter table t_test add nickname varchar(50) not null unique
修改一個字段的名稱:
alter table tName change nickname test varchar(30) default 'liu' first;
修改字段的類型:
alter table tName modify test varchar(80) default 'hehe' after age;
刪除一個字段:
alter table t_test drop test;
9帘皿、CRUD(create增加东跪,retrieve查詢,update修改,delect刪除)
insert into tableName(field1,field2,....fieldn) values(val1,val2,......valn);
update tableName set field1=new where condition;
delect from tableName where condition;
truncate [table] tableName; 慎用
select * [field1,field2,...fieldn] from table [where condition];
10虽填、單表的條件查詢:
關(guān)系運算符:>? >=? <? <=? =? !=? <>
邏輯運算符:and or not
空和非空的判斷:
is null is not null
between and in not in
排序: order by field1 [asc|desc],field2 [asc|desc]
分組: group by field [having condition]
分頁: limit offset丁恭,pageSize;
11斋日、編碼問題:
utf8
gbk
校對集:
_bin:
_cs:區(qū)分大小寫
_ci:忽略大小寫
12牲览、數(shù)據(jù)路設(shè)計三范式(3NF)
1、
2恶守、每條記錄都有唯一性(主鍵)
3第献、外鍵
13、分表的重要性
垂直分表
水平分表
14兔港、多表的關(guān)聯(lián)查詢:
1庸毫、交叉連接(數(shù)據(jù)庫底層的實現(xiàn)原理):
select * from emp,dept;
select * from emp cross join dept;
會產(chǎn)生笛卡爾積現(xiàn)象
2、內(nèi)連接(inner join)
select dept.name from emp,dept
where emp.deptid=dept.id and emp.name='zs';
select * from emp [inner] join dept on(emp.deptid=dept.id);
select name from dept where id in(select distinct deptid from emp);
3衫樊、外連接(outer join)
左連接:
select * from emp left [outer] join dept on(emp.deptid=dept.id);
右連接:
select * from emp right [outer] join dept on(emp.deptid=dept.id);
4飒赃、自然連接(nature join)
5、自連接
15科侈、表與表的關(guān)聯(lián)關(guān)系:
OneToOne(1:1)? 外鍵在那方都可以
ManyToOne(n:1) 外鍵在多的一方
ManyToMany(n:n)需要建立第三方連接
16载佳、外鍵約束
創(chuàng)建表的同時創(chuàng)建外鍵:
create table tName(
id ....
deptid int,
[constraint fk_xx_xx] foreign key(deptid)references dept(id) [on delete cascade|restrict|set null on update restrict]
)
為已經(jīng)存在的表創(chuàng)建外鍵:
alter table add tName [constraint fk_xx_xx] foreign key(deptid) references dept(id) [on delete cascade|restrict|set null on update restrict]
刪除外鍵
alter table drop 外鍵名稱;
###############################課堂代碼###############################################
1兑徘、函數(shù)
select now(); 顯示當前時間
select databases(); 顯示當前所在的數(shù)據(jù)庫
select version(); 顯示當前數(shù)據(jù)庫的版本信息
select length(name),name from emp;
select length('劉建宏');
select char_length('劉建宏')刚盈;
select trim('? asdfasdfasd? '); 去空格
MySQL中的substring和JavaScript中的substr類似
select substring('today is rainy very!',2,8);
select substr(today is rainy very!',2,8);
2表示開始索引的位置 8表示間隔長度
select ASCLL('0'); 顯示數(shù)字48(A對應(yīng)65,a對應(yīng)97挂脑,0對應(yīng)48)
select upper('LiujiaN'),LOWER('LIujian');轉(zhuǎn)大小寫
select year(now()); 顯示年
select curdate(); 年月日
select curtime(); 時分秒
select now(); 年月日時分秒
把字符串轉(zhuǎn)為日期格式
SELECT DATE_FORMAT('2011-09-20 08:30:45',? '%Y-%m-%d %H:%i:%S');
把日期轉(zhuǎn)為字符串格式
select date_format(now(),'%Y年%m月%d日 %H:%i:%s);
select uuid();
字符串函數(shù):length,char|_length,trim,substring,ascll,concat,upper,lower,replace
數(shù)學函數(shù):ceil,floor,round,mod,sin,cos,sqrt
日期函數(shù):Year,month
2藕漱、視圖:視圖也是一個數(shù)據(jù)庫對象,視圖是一張?zhí)摂M表
如何創(chuàng)建視圖: create view vName as 查詢語言崭闲;
eg: create view v_all_emp as select * from emp;
select * from v_all_emp;
視圖的意義:
簡化查詢(不能加快查詢速度)
安全問題
視圖的優(yōu)點:
視點集中
簡化操作
定制數(shù)據(jù)
合并分割數(shù)據(jù)
安全性
查詢視圖和表一樣肋联,通過select語句來查詢
select * from v_give_you;
不建議對視圖進行增刪改操作,因為約束條件未知
刪除視圖:drop view vName;
3刁俭、index索引:用來加快查詢速度(優(yōu)化)
在數(shù)據(jù)庫系統(tǒng)中建立索引主要有以下作用:
1橄仍、快速取數(shù)據(jù);
2牍戚、保證數(shù)據(jù)記錄的唯一性侮繁;
3、實現(xiàn)表與表之間的參照完整性如孝;
4宪哩、在使用ORDER by、group by子句進行數(shù)據(jù)檢索時第晰,利用索引可以減少排序和分組的時間锁孟。
創(chuàng)建索引:
1彬祖、在創(chuàng)建表的時候創(chuàng)建索引
create table tName(
id int primary key auto-increment,
name varchar(255)not null,
index my_index(name(255))
);
2、alter 修改表品抽,添加索引
alter table tName add index indexName name(255);
3储笑、create關(guān)鍵字創(chuàng)建索引(用的最多)
create index indexName on tName (fieldn(length));
顯示當前表的索引:show index from emp;
為emp表添加索引:create index i_name on emp(name(50));
刪除索引:drop index i_name on emp;
4、面試題:
索引的分類:
1圆恤、普通索引
2突倍、主鍵索引
3、唯一索引
4哑了、組合索引
5赘方、全文檢索索引(Full Text)
索引的原理:
BTREE:B+tree B-tree
hash
5、MySQL python中使用它弱左,來存儲數(shù)據(jù)
python3.2之前mysqldb模塊來完成調(diào)用python調(diào)用MySQL
python3.3之后pymysql模塊來完成調(diào)用python調(diào)用MySQL
6、安裝第三方模塊
pip install moduleName
pip install pymysql
ubuntu下:
pip==pip2 apt install python-pip
pip3? ? ? apt install python3-pip
#################################################
下載安裝pymysql
導(dǎo)入pymysql模塊
創(chuàng)建連接
創(chuàng)建游標
執(zhí)行execute
如果是查詢炕淮,需要使用fetchOne或者fetchMany獲取數(shù)據(jù)
導(dǎo)入pymysql模塊:import pymysql
##############################################################
創(chuàng)建員工表格:
create table emp(
id int primary key auto_increment,
name varchar(50) not null,
gender char(10) default 'nan',
tel varchar(20) default '110',
address varchar(30),
qq varchar(30),
age int default 18,
deptid int
);
#在員工表中插入數(shù)據(jù):
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"zhangsan","nan","110","zhengzhou","110",18,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"lisi","nan","110","zhengzhou","120",18,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"wangwu","nan","130","hangzhou","130",20,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"zhaoliu","nan","140","guangzhou","140",23,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"qianqi","nan","15000","lanzhou","150",33,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"wangba","nan","16","xuzhou","180",38,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"laomou","nan","110120","lanzhou","110",33,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"laoli","nan","200000","zhengzhou","205845",38,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"laozeng","nan","2356","changsha","256789",29,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"zhaofeihong","nan","123456","zhengzhou","123456",21,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"liuwei","nan","456123","zhengzhou","78952",23,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"zenqingzhi","nan","12548","zhengzhou","458123",21,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"wuzuodong","nan","1254","zhengzhou","25561",25,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"liujie","nan","125","zhengzhou","1459",25,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"tianxiaoyong","nan","45612","zhengzhou","75251",29,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"yangjingkun","nan","110","beijing","154",27,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"liulijuan","nan","152","tieling","1554",21,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"yiming","nan","110","zhengzhou","255",18,null);
insert into emp(id,name,gender,tel,address,qq,age,deptid) values(null,"佚名","nan","110","zhengzhou","255",18,null);
#刪除數(shù)據(jù)
delete from emp where id=23;
#查詢表格的全部數(shù)據(jù)
select * from emp;
#修改表的屬性
alter table emp charset=utf8;
#創(chuàng)建部門表格:
create table dept(
id int primary key auto_increment,
name varchar(50) not null,
describle text
);
#在部門表中插入數(shù)據(jù):
insert into dept(id,name,describle) values(null,'zongjingliban','guanrende');
insert into dept(id,name,describle) values(null,'yanfabu','kaifaruanjiande');
insert into dept(id,name,describle) values(null,'xiaoshoubu','maidongxide');
insert into dept(id,name,describle) values(null,'shichangbu','dianyande');
insert into dept(id,name,describle) values(null,'renshibu','renliziyuanguanli');
insert into dept(id,name,describle) values(null,'houqinbu','saodide');
insert into dept(id,name,describle) values(null,'caiwubu','faqiande');
insert into dept(id,name,describle) values(null,'baoanbu','darende');
insert into dept(id,name,describle) values(null,'gongguanbu','nidongde');
select * from dept;
python -m pip list在cmd中查找下載的軟件
數(shù)據(jù)庫的事務(wù)
面試題:
1拆火、四大特性(ACID)(重要)
? ? ? ? ? ? ? ? ?原子性:不可分割
? ? ? ? ? ? ? ? 一致性:
? ? ? ? ? ? ? ? 隔離性:
? ? ? ? ? ? ? ?持久性:
2、不考慮隔離會產(chǎn)生的問題
1涂圆、臟讀:臟數(shù)據(jù)即錯誤的數(shù)據(jù)们镜,讀取未提交的數(shù)據(jù)
2、不可重復(fù)讀:讀已提交的數(shù)據(jù)润歉,查詢的是同一個數(shù)據(jù)
3模狭、虛讀:讀已提交的數(shù)據(jù),針對的是一批數(shù)據(jù)
3踩衩、隔離嚼鹉,事務(wù)有四大隔離級別:(選學)
串行化:可避免臟讀,不可重復(fù)讀驱富,虛讀的發(fā)生
可重復(fù)讀:可避免臟讀锚赤,不可重復(fù)讀的發(fā)生
讀已提交:可避免臟讀的發(fā)生
讀未提交:級別最低,任何情況都無法保證
釋放資源 連接對象
if cursor !=None:
cursor.close()
if conn !=None:
conn.close()
sqlHerper,一個python調(diào)用MySQL的封裝模塊
用于封裝大量重復(fù)的代碼褐鸥,減少代碼的書寫
作業(yè):簡略描述經(jīng)典類和新式的類區(qū)別线脚?