一、數(shù)據庫的作用
????????????用來存儲數(shù)據,方便查詢。? ?
二、關系型數(shù)據庫(RDBMS)?
????????????核心是用二維表存儲數(shù)據
? ? ????????行,記錄,代表的是一條事物的信息
????????????列,字段,代表的是一條事物的某一個屬性
????????????表可以存很多行
????????????數(shù)據庫可以存很多表
三、sql
????????????結構化查詢語言,操作關系型數(shù)據庫
????????????不區(qū)分大小寫
四、Mysql(關系型數(shù)據庫管理系統(tǒng),一套軟件,包括客戶端和服務端)
????????????瑞典公司開發(fā),被賣給Sun,Sun又被賣給Oracle
????????????開源,免費,支持多平臺
? ? ? ? ? ? 客戶端提出命令,顯示結果
? ? ? ? ? ? 服務端是提供數(shù)據存儲,查詢
五、MySQL 安裝
? ? ? ? ? ? 服務端:不能帶中文路徑
????????????????啟動、停止、重啟
? ? ? ? ? ? 客戶端:
????????????????命令行客戶端
????????????????navicat
六、Navicat數(shù)據庫相關操作:
????????????數(shù)據庫操作:
????????????????創(chuàng)建 —— 字符集用utf8
????????????????編輯 —— 不能改名字
? ? ????????????刪除
????????????數(shù)據表
? ? ????????????創(chuàng)建
? ? ????????????設計表 —— 添加字段、修改字段、刪除字段
? ? ????????????重命名
? ? ????????????刪除
????????????數(shù)據操作
? ? ????????????添加、刪除、修改、查詢
七、數(shù)據類型與約束
????????數(shù)據類型
? ? ????????int :有符號范圍(-2147483648 ~ 2147483647),無符號范圍(0 ~ 4294967295),長度沒有意義
? ????????? varchar:varchar(3)表示最多存3個字符,一個中文或一個字母都占一個字符
? ? ????????decimal:decimal(5,2) 表示共存5位數(shù),小數(shù)占2位,整數(shù)占3位
? ? ????????datetime
? ? ? ? 約束:
????????????主鍵(primary key):字段值必須唯一,且不能為null,物理上存儲的順序,唯一標識某一條數(shù)據,一般字段名叫id,int類型,無符號、自動遞增
????????????非空(not null):此字段不允許寫空值
????????????唯一(unique):此字段的值不允許重復
????????????默認值(default):當不填寫此值時,會使用默認值,如果填寫時以填寫為準
????????????外鍵(foreign key):維護兩個表之間的關聯(lián)關系
八、數(shù)據庫的備份和恢復:
????????界面操作:
????????????備份的話,點數(shù)據庫——》轉儲SQL文件——》結構和數(shù)據,就會存成sql文件,里面都是sql語句
????????????恢復,新建數(shù)據庫(注意字符集要與sql文件一致),點數(shù)據庫——》運行SQL文件 即可
????????但一般,這由備份與恢復代碼文件執(zhí)行
九、SQL語句
? ??????點數(shù)據庫——》點“查詢”——》在查詢頁面寫sql語句
? ??????ctrl+/:注釋;ctrl+shift+/:去掉注釋
? ? ? ? 1、?創(chuàng)建表:create table 表名(字段名 類型 約束)
????????????create table student(
????????????id int UNSIGNED PRIMARY KEY auto_increment,
????????????name VARCHAR(10),? # 表示長度是10
????????????age int UNSIGNED,? ? ?#表示無符號
????????????height decimal(5,2)
????????????)
? ? ? ? 2、 刪除表:drop table if exists 表名
? ??????????每條sql語句后要有;隔開,最后一條sql后面不用加;
????????????drop table if exists student;?
????????????create table student(
????????????id int UNSIGNED PRIMARY KEY auto_increment,
????????????name VARCHAR(10),
????????????age int UNSIGNED,
????????????height decimal(5,2)
????????????)
????????3、數(shù)據操作:增、刪、改、查:我們這些寫的sql語句,可以在navicat中的數(shù)據庫中的“查詢”欄里新建查詢頁面編寫,寫完可以直接按“保存”,就會保存在“查詢”欄下面
? ? ? ? ? ? ? ?1)增加數(shù)據:insert into 表名 values(...)
? ? ? ? ? ?????????????????給所有字段插入數(shù)據:?insert into 表名 values(0,'亞瑟',16,160.32) —— 第一位表示id的時候,填0,default,或者null表示自動遞增,但不能不寫
? ? ? ? ? ? ???????????????只給某一字段添加數(shù)據:insert into 表名(name,age) values('亞瑟',30) —— 注意數(shù)據要和字段一一對應,這里“name”,“age”是字段名
? ?????????????????????????一條sql語句插入多條數(shù)據:insert into 表名(name) values('亞瑟'),('魯班'),('大喬') —— 推薦,效率高
? ?????????????????????????多條sql語句插入多條數(shù)據,之間用;隔開
? ? ? ? ? ? 2)修改數(shù)據:update 表名 set 列1=值1,列2=值2 where 條件
????????????????????????????例如:update student set name='mike' where age=20
????????????3)刪除數(shù)據:delete from 表名 where 條件
????????????????????????????例如:delete from student where id=20
? ??????????????????????邏輯刪除:對于重要數(shù)據,不能輕易刪除。加多一個字段例如“isdelete”來表示這條數(shù)據是否被刪除,類型設成int,初始isdelete字段都設置成0,0代表沒刪除,1代表刪除
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????update student set isdelete=0
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?然后刪除的時候就執(zhí)行:
????????????????????????????????update student set isdelete=1 where id=5 (表示把id=5的人刪除)
? ? ? ? ? ? ? ? ? ? ? ? ? ??以后查詢,只要看isdelete=0的人就可以了,select * from student where isdelete=0
????????????4)查詢數(shù)據:select 列名,列名 from 表名 where 條件
? ??????????????????????????查詢所有數(shù)據:select * from students
? ??????????????????????????查詢某幾列數(shù)據:select name,hometown from students
? ??????????????????????????查詢并起別名:select name as 姓名,hometown as 家鄉(xiāng) from students
? ??????????????????????????多表的時候給表起別名:select stu.name,stu.hometown from students as stu
? ??????????????????????????刪除重復值:select distinct sex,class from students
? ??????????????????????????條件查詢比較運算:select * from students where hometown!='北京'
? ??????????????????????????條件查詢邏輯運算:select * from students where age<20 and sex='女'
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? select * from students where age='女' or class='1班'
????????????????????????????????????????????????????????????select * from students where not hometown='天津'
? ??????????????????????????條件查詢模糊查詢:select * from students where name like '%孫%' —— 孫前后面可以有任意個任意字符
????????????????????????????????????????????????????????????select * from students where name like '孫_' —— 孫后面只能有一個任意字符
? ??????????????????????????條件查詢范圍查詢:select * from students where hometown in ('北京','上海','廣東')
????????????????????????????????????????????????????????????select * from students where hometown not in ('北京','上海','廣東')
????????????????????????????????????????????????????????????select * from students where age between 18 and 20 —— 包含18和20,只能用于數(shù)字,而且必須小的18放前,大的20放后
? ??????????????????????????條件查詢空判斷:select * from students where card is null (注意一定要用is)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?select * from students where card is not null (找非空)
? ????????????????????????????!注意,null(指沒有填寫值)與''(指空字符串)是不一樣的??!如何插入null呢?
????????????????????????????????????insert into students values('013','mike','男','北京','1班',null)
????????????????????????????????????或者?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? insert into students(studentNo,name) values('013','mike') —— 其余沒有定義的字段就是null
? ??????????????????????????如何插入''呢?insert into students values('013','mike','男','北京','1班','')
????????????5)排序:select * from 表名 order by 列1 asc|desc, 列2 asc|desc,...(不寫asc或者desc,默認是asc,正序;先按列1排,排完再按列2)
????????????????????????select * from students order by convert(name using bgk) —— bgk是中國語言的編碼,可用于對中文字段按拼音排序
????????????6)聚合函數(shù)
????????????????????count統(tǒng)計一個表有多少行:select count(*) from students where sex='男'—— 一行記錄只要有一個字段有值,就會統(tǒng)計在內
????????????????????count統(tǒng)計某一個字段有多少非空行:select count(name) from students —— null的值不計入
????????????????????max,min統(tǒng)計最大最小值:select max(age) from students where sex='女' ——先執(zhí)行from,再是where,最后才是select
????????????????????sum求和:select sum(age) from students where hometown='北京'
????????????????????avg求平均值:select avg(age) from students where sex='女'
????????????7)分組:select 列1,列2,聚合... from students group by 列1,列2,... (null也算是一組)
? ? ? ? ? ? ? ? ? ? ? ? select sex,count(*) from students group by sex —— 查詢各種性別的人數(shù)
????????????????????????select class,max(age),min(age),avg(age) from students group by class —— 查詢各個班級學生的平均年齡,最小年齡,最大年齡
????????????8)分組后過濾:select 列1,列2,聚合... from students group by 列1,列2,... having 條件
????????????????????????select sex,count(*) from students group by sex having sex='男' —— 分組后,即group by后面不能跟where,只能用having過濾
????????????????????????相當于 select count(*) from students where sex='男',執(zhí)行順序是先from,where,再group by 最后再select
????????????9)按照多個字段分組:這里class 與 sex 的值一模一樣時,認為是一個組
? ? ? ? ? ? ? ? ? ? ? ? select class,sex,count(*) from students group by class,sex
????????????10) 分頁,用到獲取部分行的語法:select * from 表名 limit start,count
????????????????????????select * from students limit 0,3 —— 從0開始表示第一條數(shù)據,顯示3條數(shù)據,如果limit后面只寫一個數(shù)字,就是省略了start
? ??????????????????????分頁:已知每頁顯示m條數(shù)據,顯示第n頁的數(shù)據的時候,就需要這個sql語句:
? ??????????????????????????????????select * from students limit (n-1)*m,m
? ? ? ? ? ? ? ? ? ? ? (count一下求總條數(shù)p1,p1除以m得頁數(shù)p2,如果整除頁數(shù)就是p2,如果不是整數(shù)則頁數(shù)是p2+1)
????????4、多表查詢或者叫多表連接查詢
????????????等值連接:
????????????1)、where:select * from 表1,表2 where 表1.列=表2.列
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?生成臨時表,然后在臨時表中使用關聯(lián)字段進行過濾 —— 臨時表存大小是兩表的笛卡爾積,存在內存中,性能比較差
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?????select * from students as stu,scores as sc where stu.studentNo=sc.studentNo
????????????2)內連接(推薦):select * from 表1 inner join 表2 on 表1.列=表2.列
????????????????????????????不生成臨時表,連接時先判斷條件,只有符合條件才會連接再放到結果 —— 性能比較好
????????????????????????????????????select * from students as stu
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ????? inner join?scores as sc on?stu.studentNo=sc.studentNo
????????????????????????????????三個表連接示例(至少要兩個條件):
????????????????????????????????????select * from?students as stu,scores as sc,courses as cs
????????????????????????????????????where?stu.studentNo=sc.studentNo and?sc.courseNo=cs.courseNo
????????????????????????????????或者:
????????????????????????????????????select * from?students as stu
????????????????????????????????????inner join?scores as sc on?stu.studentNo=sc.studentNo
????????????????????????????????????inner join courses as cs on?sc.courseNo=cs.courseNo
?????????????3)左連接:select * from 表1 left join 表2 on?表1.列=表2.列 (把左邊的表的信息全部顯示出來,找不到交集的填null,這里join前面的是左邊的表,join后面的是右邊的表)
????????????????????????select * from students as stu
????????????????????????left join?scores as sc on?stu.studentNo=sc.studentNo
? ? ? ? ? ? ? ? ? ? ? ? left join courses as cs on?sc.courseNo=cs.courseNo
????????????4)右連接:select * from 表1 right join 表2 on?表1.列=表2.列 (把右邊的表的信息全部顯示出來,找不到交集的填null,這里join前面的是左邊的表,join后面的是右邊的表)注意??!表的書寫順序!!
? ? ? ? ? ? ? ? ? ? ? ? select * from scores as sc ? ? ? ? ? ? ? ? ? ?
????????????????????????right join? courses as cs on?sc.courseNo=cs.courseNo ? ? ? ? ? ? ? ? ? ? ? ??
????????????????????????left join?students as stu?on?stu.studentNo=sc.studentNo —— 這里注意,到底是想把哪個表的數(shù)據全部顯示出來,這里想course表的信息全部顯示出來,所以就要用left join
? ??????5、自關聯(lián):針對表中的數(shù)據有上下級關系,帶層次關系的
? ??????????????????例如:對于一張表areas,里面既有省的數(shù)據,也有市的數(shù)據,找出省對應的市
? ??????????????????select * from areas as sheng, areas as shi
? ??????????????????where sheng.aid=shi.pid
????????????????????例2:找出省對應的市,市對應的區(qū)
? ??????????????????select?*?from?areas?as?sheng,?areas?as?shi,areas?as?qu
? ??????????????????where?sheng.aid=shi.pid and shi.aid=qu.pid
????????????????????或者:
? ??????????????????select?*?from?areas?as?sheng
? ??????????????????inner join?areas?as?shi on?sheng.aid=shi.pid
? ??????????????????inner join?areas?as?qu on?shi.aid=qu.pid
當我們想把沒有區(qū)的市都顯示出來的時候:
? ??????????????????select*?from?areas?as?sheng
? ??????????????????inner join?areas?as?shi?on?sheng.aid=shi.pid
? ??????????????????left join?areas?as?qu?on?shi.aid=qu.pid
????????6、子查詢:
????????????1)標量子查詢:寫在where后,當成條件使用,子查詢的結果是一個數(shù)據(一行一列)
? ? ? ? ? ? ? ? ? ? ? ? select * from students where age>(select avg(age) from students) 括號內的是子查詢,括號外的是主查詢
????????????????????????例如:
? ??????????????????????select score from scores?
? ??????????????????????where studentNo=(select studentNo from students where name='王昭君')?
? ??????????????????????and courseNo=(select courseNo from courses where name='數(shù)據庫')
????????????2)列級子查詢:寫在where后,當成條件使用,子查詢返回的結果是一列(一列多行)
????????????????????????select * from scores where studentNo in (select studentNo from students where age=18)
? ??????????????????????除了in以外,還可以使用any,some,all,但前面一定要有=,!=,>,<
? ??????????????????????select?*?from?students?where age?=any?(select?age?from?students?where?age between 18 and 20)? (相當于some,等于任一個,或者<>any,小于或者大于任一個,即大于里面的最小值,或者 小于里面的最大值)
? ??????????????????????select?*?from?students?where?age?=some?(select?age?from?students?where?age?between?18?and?20) (等于任一個,或者<>some,小于或者大于任一個)
? ??????????????????????select?*?from?students?where?age<all?(select?age?from?students?where?age?between?18?and?20) 表示所有,<所有即小于括號里面的所有結果,即小于18
????????????3)行子查詢:寫在where后,當成條件使用,子查詢返回的結果是一行(一行多列)
????????????????????????select * from students
????????????????????????where (sex,age) = (select sex, age from students where sex='男' order by age desc limit 1)
????????????4)表級子查詢:寫在from后,當成數(shù)據源使用,子查詢的結果是多行多列
????????????????????????把臨時生成的表當成是數(shù)據源,一定要給這個臨時的表起別名,這里是“as c”?
? ??????????????????????select * from scores
? ??????????????????????inner join?
????????????????????????(select * from courses where name in ('數(shù)據庫','系統(tǒng)測試')) as c
? ??????????????????????on scores.courseNo = c.courseNo
? ??????????????????????*round 函數(shù)
? ??????????????????????select round(avg(price),2) from goods —— 四舍五入保留兩位小數(shù)的函數(shù)
? ??????7、拆分表:
? ??????????????????對于一個表中的數(shù)據如果有冗余,例如一個商品表,它有類型這一個字段,有很多商品都是標上“筆記本”的類型,存在我們的電腦硬盤中很占地方,所以會新建一個類型表,一個類型對應一個編號,在原表中,每個商品的類型寫的是一個編號,這樣占的空間小點,也利于以后類型名稱的修改。
? ??????????????????— 1、創(chuàng)建create table goods_cates (...)之后,給商品類型表插入數(shù)據
? ??????????????????????????insert into goods_cates(cate_name) select distinct cate from goods(如果插入的是兩個字段,則查詢也要查兩個,一一對應)
? ??????????????????或者創(chuàng)建并同時插入:
? ??????????????????????create table goods_brands(
????????????????????????brand_id int unsigned primary key auto_increment,
????????????????????????brand_name varchar(10)
????????????????????????) select distinct brand_name? as brand?from goods (這里注意??!查詢出來的字段名要在新建表中找到對應,如果找不到,例如這里的brand,在新建表中找不到對應的字段名叫“brand”,那它就會自動在創(chuàng)建表的最后添加一列叫“brand”,最終表就有“brand_id”,“brand_name”,“brand” 3列)
? ??????????????????* 延伸:快速備份一張表:
? ??????????????????create table brand_bak select * from goods_brands
? ??????????????????— 2、接著更新原goods中的數(shù)據,把類型名稱改成類型表中的對應編號
? ??????????????????????update goods
? ??????????????????????inner join goods_cates on goods.cate=goods_cates.cate_name
? ??????????????????????set goods.cate=goods_cates.cate_id
? ??????????????????— 3、展示的時候,就把goods,goods_cates,goods_brands 三個表連接起來:
????????????????????????select goods_name,goods_cates.cate_name,goods_brands.brand_name from goods
????????????????????????inner join goods_cates on goods.cate_id = goods_cates.cate_id
????????????????????????inner join goods_brands on goods.brand_id=goods_brands.brand_id
????????8、數(shù)據庫設計——ER模型
????????????????????E表示entry,一個實體,一個數(shù)據對象
????????????????????R表示relationship,聯(lián)系,包括一對一,一對多,多對一,多對多
????????????????????屬性:實體的某一特性
????????????????????實體A對實體B為1對1,則在字段少的那個表,創(chuàng)建一個字段,儲存另一個表的主鍵值
????????????????????實體A對實體B為1對多,則在表B中創(chuàng)建一個字段,儲存表A的主鍵值
????????????????????實體A對實體B為多對多的,新建一個中間表C,這個表C只有兩個字段,一個用于儲存表A的主鍵值,一個用于儲存表B的主鍵值
? ??????9、命令行客戶端
????????????????????在黑窗口,cd到mysql的安裝路徑下,mysql -uroot -p123456 (這里123456是你的密碼)就能進入命令行客戶端,連接上了mysql服務端。連接時,更為安全的做法是:mysql -uroot -p 直接回車,再輸入密碼
????????????????????在命令行客戶端,一般所有語句都有;結尾,不然有可能運行不了。接著,要查看有哪里數(shù)據庫,show database; 下一步,再選擇你要操作的數(shù)據庫,例如 use ceshi; 如果要查詢當前自己所在的倉庫,即 select database();要顯示中文數(shù)據,要先做轉換, set charset gbk;
????????????????????創(chuàng)建數(shù)據庫:create database seshi_bak charset='utf8'
????????????????????刪除數(shù)據庫:drop database ceshi_bak;
????????????????????進入某個數(shù)據庫中操作:use ceshi;
????????????????????顯示當前數(shù)據庫所有的表:show tables; | 而 show create table?students; 返回一個創(chuàng)建students表時的sql語句
????????????????????查看某個表中字段內容:desc students;
????????????????????對數(shù)據庫進行備份,cmd一定要用管理員身份打開,再去到mysql的安裝目錄下,然后mysqldump -uroot -p123456 cheshi > ceshi.sql (利用重定向把ceshi這個數(shù)據庫存到.sql文件中);備份以后,要恢復的話,就運行mysql -uroot -p123456 ceshi_bak<ceshi.sql (把剛才備份的ceshi.sql文件恢復到ceshi_bak數(shù)據庫中)
????????10、內置函數(shù),跟在select后面,select后面都可以寫函數(shù)
????????????????1)、拼接函數(shù):select concat(str1,str2,......)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?select name,hometown,concat(name,'的家鄉(xiāng)是',hometown) from students
????????????????2)求字符串長度:select length(str) (這里是返回多少個字節(jié),英文是a的長度是1,中文“我”的長度是3)
? ??????????????????????????????select * from students where length(name)=6
????????????????3)截取字符串:select left(str,len) ;select right(str,len) ;select subring(str,pos,len)?
????????????????????????????????select name,left(name,1) from students
???????????????4)去除字符串空格:select ltrim(str) 刪除左空格 ;select rtrim(str) 刪除右空格
????????????????5)大小寫轉換:select lower(str)? ;select upper(str)
????????????????6)四舍五入 select round (n,d) ,n表示原數(shù),d表示小數(shù)位置,默認為0
????????????????7)求次冪:select pow(x,y) , x的y次冪
????????????????8)獲取圓周率: select PI();
????????????????9) 獲取隨機數(shù):select rand(), 值為0-1.0的浮點數(shù);select round(rand()*10), 返回一個1-10的隨機數(shù)
????????????????????????????隨機選取表中的一條數(shù)據:select *?from students order by rand() limit 1
????????????????10) 顯示當前日期:select current_date(); 當前時間:select current_time(); 當前日期和時間,select now();規(guī)定時間的格式:select date_format(now(),'%Y/%m/%d') 這里用/連接年月日,可以自定義用什么符號連接
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?%Y —— 完整年份,%y —— 簡寫年份,%m —— 月, %d —— 天,%H —— 小時,24小時制, %h —— 小時,12小時制,%i —— 分,%s —— 秒
????????11、流程控制:case 值 when 比較值1 then 結果1 when 比較值2 then 結果2 ... else 結果 end
????????????????????????當值等于某個比較值時,對應的結果會返回;如果所有的比較值都不相等則返回else的結果,如果沒有else并且所有比較值都不相等則返回null
????????????????????????這里的case整個相當于一個字段
? ??????????????????????select name,sex,
? ??????????????????????case sex?
? ??????????????????????when '男' then concat(left(name,1),'帥哥')
? ??????????????????????when '女' then concat(left(name,1),'美女')
? ??????????????????????else 'xx' end as 結果 —— 不男不女的話返回xx
? ??????????????????????from students
????????12、自定義函數(shù),調用用select, 一次編譯,緩存起來在服務端,下次使用直接命中緩存中已經編譯好的sql,不需要重復編譯,減少網絡交互,減少網絡訪問流量,提高性能;另外還有隱藏核心函數(shù)功能的作用。?
????????????????????????在命令行客戶端寫:需要delimiter,用于設置分割符,默認是;因為sql語句中可能含有;
????????????????????????delimiter $$
????????????????????????create function 函數(shù)名稱(參數(shù)列表) returns 返回類型
????????????????????????begin
????????????????????????sql 語句
????????????????????????end
????????????????????????$$
????????????????????????delimiter ;
? ??????????????????????在navicat中寫
????????????????????????create function my_trim(str varchar(100)) returns varchar(100)
????????????????????????begin?
????????????????????????return ltrim(rtrim(str));
????????????????????????end
?????????13、儲存過程:調用用call,一次編譯,緩存起來在服務端,下次使用直接命中緩存中已經編譯好的sql,不需要重復編譯,減少網絡交互,減少網絡訪問流量,提高性能;另外還有隱藏核心函數(shù)功能的作用。?
????????????????????delimiter //
? ? ? ? ? ? ? ? ? ? create procedure 存儲過程名稱(參數(shù)列表)
????????????????????begin
????????????????????sql語句
????????????????????end
????????????????????//
????????????????????delimiter ;
????????????????????例如:
? ??????????????????create procedure?proc_stu()
? ??????????????????begin
????????????????????select * from students;
? ??????????????????end
? ??????????????????call proc_stu
????????14、視圖:就是一個查詢存在服務端,當寫的sql語句很多,可以存成視圖放在服務端, 下次使用就能直接調用視圖;也有隱藏關鍵字段數(shù)據的作用;減少網絡傳輸?shù)膕ql數(shù)量。
????????????????????create view v_students as
????????????????????select * from students
????????????????????調用:
????????????????????select * from v_students
????????????????????在命令行客戶端,要顯示視圖:show tables; 會把表和視圖都顯示出來。
????????????????????刪除視圖:drop view 視圖名稱;如果update 視圖就是update它的原表。
????????15、事物:一個事物有多個操作,需要成功就要全部操作成功,一個失敗就整個事物失敗
????????????????????begin; —— 一begin,開啟事物,當所有操作都執(zhí)行完了,你也確認過結果了,在commit; 最終表的內容才會改變
????????????????????update students set age=age-10 where name='大喬';
????????????????????update students set age=age+10 where name='小喬';
????????????????????select * from students where name in ('大喬','小喬'); —— 確認上兩步操作成功了
? ??????????????????rollback; —— 如果某一步操作出錯了,rollback后所有的操作全部失敗,數(shù)據回到原始狀態(tài)
????????????????????commit;
????????16、索引:為了查詢更快
????????????????????????查看表的索引: show index from 表名
????????????????????????創(chuàng)建索引的方式,primary key,unique,key(字段):這里name,id,age都是索引
????????????????????????create tables 表名(
????????????????????????in int primary key,
????????????????????????name varchar(10) unique,
????????????????????????age int,
????????????????????????key(age)
????????????????????????);
????????????????????????添加索引:create index 索引名稱 on 表名(字段名稱(長度)) 例如:
????????????????????????create index age_index on create_index(age);
????????????????????????create index name_index on create_index(name(10)); 如果創(chuàng)建索引的字段類型是varchar要指定長度,而且要與字段創(chuàng)建表時設置的長度一樣
? ??????????????????????刪除索引:drop 索引名稱 on 表名;
? ??????????????????????* 開啟運行時間檢測(在命令行客戶端):set profiling=1;執(zhí)行完一個sql語句后,再show profiles;就能查詢剛才的sql語句的執(zhí)行時間;要想在navicat看sql語句的執(zhí)行快慢,可以explain sql語句,返回結果中有個rows表示掃描了多少行,間接表示執(zhí)行速度
? ??????????????????????select * from 表名 where title='test10000' —— 當title是一個索引,查詢速度會很快
????????????????????????在創(chuàng)建索引的時候,數(shù)據會以樹的形式存放(BTREE,每個節(jié)點,左節(jié)點比上級節(jié)點小,右節(jié)點比上級節(jié)點大,一路建立一個樹)這樣之后查詢的速度會更快;但是會降低表的更新速度,每次對表的增刪改,都要重新創(chuàng)建索引的樹,MySQL不僅要保存數(shù)據,還要保存索引文件,耗費性能,所以只有當表的查詢量大,修改很少要添加索引,要經常修改的,就不要加索引了。
????????17、外鍵:foreign key,從表被約束一列的值,必須要在主表那一列的值范圍內,約束的一種,少用,降低更新數(shù)據的性能
????????????????????create table class(
????????????????????id int unsigned primary key auto_increment,
????????????????????name varchar(10)
????????????????????);
????????????????????create table stu(
????????????????????name varchar(10),
????????????????????class_id int unsigned,
????????????????????foreign key(class_id) references class(id) —— 這里的class_id字段被class表中的id約束,只能填class表中id出現(xiàn)的數(shù)據
????????????????????);
????????????????????對于已經創(chuàng)建的表,要添加外鍵:
????????????????????alter table 從表名 add foreign key (從表字段) references 主表名(主表字段);
????????????????????例如:alter table stu add foreign key (class_id) references class(id);
? ? ? ? ? ? ? ? ? ? 刪除外鍵,首先獲取外鍵名稱:show create table stu; 再刪除:alter table stu drop foreign key?這里寫的是外鍵名;
? ??????18、修改密碼
????????????????????????連接后,會有一個自帶的mysql數(shù)據庫,里面存我們用戶的數(shù)據。然后在mysql的數(shù)據庫中,查詢頁面下寫以下sql語句:
????????????????????????use mysql;
? ??????????????????????update user set password=password('新密碼') where user='用戶名';
? ??????????????????????例如:?update user set password=password('123') where user='root';
? ??????????????????????flush privileges;
????????19、root忘記密碼
????????????????????首先,修改配置文件,配置mysql登錄時不需要密碼:
????????????????????CentOS中:配置文件位置為/data/server/mysql/my.cnf
????????????????????Windows中:配置文件位置為C:\Program Files\MySQL\MySQL Server 5.1\my.ini
????????????????????在配置文件中找到mysqld,在它的下一行,添加skip-grant-tables
????????????????????然后,要重啟mysql服務(對于任何軟件,修改了配置文件,都要重啟服務),免密碼登錄,再按上述方法修改密碼;之后再把配置文件中的skip-grant-tables 刪除,重啟服務,登錄mysql使用。要退出mysql則在命令行窗口輸入exit
十、網站運行需要的環(huán)境:LNMP環(huán)境搭建
????????????訪問一個網址,事實上是訪問一個電腦(服務器,服務器上用的是Linux系統(tǒng))的IP 。在服務器Linux上還需要安裝一個web服務軟件,才能提供一個網址給別人訪問;另外網站的代碼多數(shù)是用php代碼寫的,還需要在服務器上安裝一個能運行php代碼的軟件,來寫一個網站出來;最后一個網頁訪問的數(shù)據,都要存在數(shù)據庫中,可能是mysql數(shù)據庫中;那我們就要學會如何去搭建一個平臺,一個環(huán)境,包含上面幾個部分,去提供一個網址,給別人訪問。

以前,常用的經典Web服務環(huán)境組合就是LAMP(Linux、Apache、MySQL、PHP),現(xiàn)在Nginx Web服務逐漸流行,就出現(xiàn)了新的Wen服務環(huán)境組合——LNMP 或 LEMP (N或E代表的是Nginx)????
????????1、Nginx
????????????????????服務端的軟件,是輕量級Web服務器/反向代理服務器級電子郵件(IMAP/POP3)代理服務器,特點是占有內存少,并發(fā)能力強。(除了能提供web服務,還能提供郵箱服務,很多公司有自己的郵箱供自己公司內網服務。)
????????????????????以下所有操作用root用戶進行所有操作。
????????????????1)基本環(huán)境配置:
????????????????????????????????以root用戶操作,su
????????????????????????????????基本軟件目錄(mkdir b/a/b -p 在b中創(chuàng)建a,在a中創(chuàng)建b):mkdir /data/{server,soft} -p —— 以后裝軟件,都裝在server下
????????????????????????????????把資料里面的所有文件拖到CentOS的桌面,然后移動到剛創(chuàng)建的soft目錄:mv /home/admin/桌面/* /data/soft? —— 資料文件都是代碼,當做軟件放在soft文件夾下
????????????????2)Nginx 安裝:
????????????????????????注意:編譯安裝nginx的時候,應該有一個專用的啟動用戶,我們把這個用戶設置為www,linux的好處,軟件可以有專有用戶打開
????????????????????????創(chuàng)建專用用戶www:useradd www -s /sbin/nologin -M
????????????????????????編譯安裝nginx:
????????????????????????cd /data/soft
????????????????????????tar xzf nginx-1.10.2.tar.gz
????????????????????????cd nginx-1.10.2
????????????????????????./configure --prefix=/data/server/nginx (先執(zhí)行配置文件configure,配置軟件默認安裝的位置在/data/server/nginx)
????????????????????????make(以源碼方式安裝,都要先編譯代碼,所以這里make是編譯出一套程序出來,在windows中就是編譯出一個exe文件出來)
????????????????????????make install (安裝)
????????????????????????修改配置文件:
????????????????????????gedit /data/server/nginx/conf/nginx.conf
????????????????????????#user nobody;找到這一句 ——> user www; 改成這樣
? ??????????????????????檢查效果:啟動nginx:/data/server/nginx/sbin/nginx (對于可執(zhí)行文件,綠色的,像之前的configure,可以在其所在的路徑./configure或者在其他目錄下,直接輸入這個文件的絕對路徑)然后 檢查端口是否有80:netstat -tnulp | grep nginx 或者打開瀏覽器,輸入localhost,按回車按鈕,看瀏覽器顯示效果?* Linux 下查找本機的ip,ifconfig
? ??????????????????????nginx常用操作:
????????????????????????檢查nginx配置文件是否配置成功:/data/server/nginx/sbin/nginx -t
????????????????????????啟動nginx:/data/server/nginx/sbin/nginx
????????????????????????關閉nginx:/data/server/nginx/sbin/nginx -s stop
????????????????????????重啟nginx:/data/server/nginx/sbin/nginx -s reload
????????????????????????啟動后檢查:netstat -tnulp | grep nginx (netstat -tnulp打開查看所有端口)
軟件安裝總結:1、解壓tar文件:解壓文件,獲取真正的配置文件;2、配置configure文件:根據默認的配置項或者更改配置項,生成編譯配置文件(Makefile);3、編譯make:根據Makefile內容,編譯生成指定的軟件所需要的所有文件;4、安裝make install:將編譯生成的所有文件,轉移到軟件指定安裝的目錄下面
?????????2、安裝MySQL (安裝PHP前一定要現(xiàn)有MySQL)
? ??????????????????????????????????創(chuàng)建專用用戶mysql:useradd -s /sbin/nologin -M mysql
? ??????????????????????????????????解壓軟件
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? cd /data/soft
????????????????????????????????????tar xzf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz -C?/data/server/
????????????????????????????????????cd?/data/server/
????????????????????????????????????ln -s?mysql-5.6.35-linux-glibc2.5-x86_64 mysql (因為名字較長,創(chuàng)建一個軟鏈接,不用硬鏈接,因為硬鏈接不能針對目錄創(chuàng)建)
? ??????????????????????????????????初始化mysql數(shù)據庫:/data/server/mysql/scripts/mysql_install_db --basedir=/data/server/mysql --datadir=/data/server/mysql/data/ --user=mysql
? ??????????????????????????????????數(shù)據庫配置文件管理:mv /etc/my.cnf /etc/my.cnf-bak ;cp /data/server/mysql/support-files/my-default.cnf /etc/my.cnf
? ??????????????????????????????????數(shù)據庫啟動命令配置:cp /data/server/mysql/support-files/mysql.server /etc/init.d/mysqld(以后看到文件名以d結尾,代表是服務文件,相當于windows中的一個服務)
? ??????????????????????????????????修改啟動文件:sed -i 's#/usr/local/mysql#/data/server/mysql#g' /data/server/mysql/bin/mysqld_safe /etc/init.d/mysqld (sed -i 's#abc#efg#g'把abc都換成efg)
? ??????????????????????????????????數(shù)據庫文件權限設置:chown -R mysql.mysql /data/server/mysql/
? ??????????????????????????????????將mysql服務設置為開機自動啟動服務:chkconfig --add mysqld;然后chkconfig mysqld on
? ??????????????????????????????????啟動mysql:service mysql start
? ??????????????????????????????????檢查數(shù)據庫啟動狀態(tài):netstat -tnulp | grep mysql(看是不是3306)
? ??????????????????????????????????MySQL客戶端操作:
? ??????????????????????????????????配置環(huán)境標量:gedit /etc/profile
? ??????????????????????????????????末尾添加這條配置:PATH=/data/server/mysql/bin:$PATH ($PATH表示以前的路徑,改全局變量的目的是在任何地方都能執(zhí)行這個程序,不需要在進入到/data/server/mysql/bin/mysql 這樣去執(zhí)行mysql這個執(zhí)行文件)
? ??????????????????????????????????讓配置文件生效:source /etc/profile
? ??????????????????????????????????客戶端連接mysql服務器:沒有密碼(就不用加-p了) : mysql -uroot -p
? ??????????????????????????????????啟動數(shù)據庫:service mysql start
? ??????????????????????????????????停止數(shù)據庫:service mysql stop
? ??????????????????????????????????重啟數(shù)據庫:service mysql restart
?????????3、安裝php
? ????????????????????????????安裝依賴軟件:libiconv
????????????????????????????????cd /data/soft
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? tar xzf?libiconv-1.14.tar.gz
????????????????????????????????cd?libiconv-1.14
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?./configure --prefix=/usr/local/libiconv?
????????????????????????????????make
????????????????????????????????make install
? ??????????????????????????????解壓php:
????????????????????????????????cd /data/soft/
????????????????????????????????tar xzf?php-5.3.29.tar.gz
????????????????????????????????cd?php-5.3.29
????????????????????????????????配置:
????????????????????????????????ln -s /data/server/mysql/lib/libmysqlclient.so.18 /usr/lib64/
????????????????????????????????touch ext/phar/phar.phar
????????????????????????????????./configure \?
????????????????????????????????--prefix=/data/server/php-5.3.29 \
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? --with-mysql=/data/server/mysql \
????????????????????????????--with-pdo-mysql=mysqlnd \
????????????????????????????--with-iconv-dir=/usr/local/libiconv \
????????????????????????????--with-freetype-dir \
????????????????????????????--with-jpeg-dir \
????????????????????????????--with-png-dir \
????????????????????????????--with-zlib \
????????????????????????????--with-libxml-dir=/usr \
????????????????????????????--enable-xml \
????????????????????????????--disable-rpath \
????????????????????????????--enable-bcmath \
????????????????????????????--enable-shmop \
????????????????????????????--enable-sysvsem \
????????????????????????????--enable-inline-optimization \
????????????????????????????--with-curl \
????????????????????????????--enable-mbregex \
????????????????????????????--enable-fpm \
????????????????????????????--enable-mbstring \
????????????????????????????--with-mcrypt \
????????????????????????????--with-gd \
????????????????????????????--enable-gd-native-ttf \
????????????????????????????--with-openssl \
????????????????????????????--with-mhash \
????????????????????????????--enable-pcntl \
????????????????????????????--enable-sockets \
????????????????????????????--with-xmlrpc \
????????????????????????????--enable-zip \
????????????????????????????--enable-soap \
????????????????????????????--enable-short-tags \
????????????????????????????--enable-static \
????????????????????????????--with-xsl \
????????????????????????????--with-fpm-user=www \
????????????????????????????--with-fpm-group=www \
????????????????????????????--enable-ftp?...之后失敗
? ??????????????????????????啟動php
????????????????????????????/data/server/php/sbin/php-fpm
? ??????????????????????????關閉php
????????????????????????????pkill php-fpm
????????????????????????????netstat -tnulp|grep php (端口是9000)
? ? ? ? ? ? ? ? 5) nginx整合php
????????????????6)部署iwebshop軟件,安裝商城網站
????????????????7)部署禪道軟件:管理我們的項目,管理bug平臺,從測試到開發(fā)
部署網站,禪道軟件都是一樣的,先解壓,然后把裝著php代碼的文件移動到nginx的html文件夾下(mv /data/soft/ZenTaoPMS.8.2.5/zentaopms/ /data/server/nginx/html/chandao),然后修改權限(chown -R www.www /data/server/nginx/html/chandao),最后再到瀏覽器中訪問
一個網站運行需要的環(huán)境:一個服務器(linux系統(tǒng)),web服務軟件(nginx),php軟件,數(shù)據庫(mysql)
網站要正常訪問:nginx,php,mysql都啟動
????????4、在Linux中使用navicat
? ??????????????????linux中,把navicat的壓縮包點開,再把文件拖出桌面,再點擊使用,如果navicat過期后,刪除/home/admin下的.navicat64文件夾即可,以后每次點使用:rm -rf /home/admin/.navicat64
? ??????????????????linux中的navicat連接windows中的mysql服務端:
? ??????????????????輸入windows機的ip、端口、用戶名、密碼
? ? ? ? 5、windows中的navicat連接linux中的mysql服務器
????????????????????設置mysql支持遠程連接,支持別的電腦連接
? ? ? ? ? ? ? ? ? ? 在linux的navicat中的mysql數(shù)據庫的user,修改一個root用戶的host為%:update user set host='%'
? ? ? ? ? ? ? ? ? ? 在linux的navicat中的查詢頁面輸入:flush privileges
? ? ? ? ? ? ? ? ? ? 再在windows的navicat中,輸入linux機的ip、端口、用戶名、密碼
????????????????????* 數(shù)據庫中的表,函數(shù),視圖都是存在服務器中,但是查詢語句沒有存