1.數(shù)據(jù)庫作用
數(shù)據(jù)是數(shù)據(jù)庫中存儲(chǔ)的基本對(duì)象,包括:文字 圖形 圖像 聲音 有組織的 可共享的數(shù)據(jù)集合
2.常用的DBMS
MYSQL oracle SQL-Server ?DB2
3.MYSQL登錄
開始-運(yùn)行-cmd ? 輸入:mysql -uroot -p,然后輸入密碼或者 mysql -uroot -p 密碼 退出 quit \q
4.基本SQL語句
創(chuàng)建數(shù)據(jù)庫--create database school;
查看有哪些數(shù)據(jù)庫---show databases;
刪除數(shù)據(jù)庫-drop database school;
5.向數(shù)據(jù)庫中存儲(chǔ)數(shù)據(jù)
首先進(jìn)入數(shù)據(jù)庫 use dbname;
查看數(shù)據(jù)庫中的表 show tables沟堡;
在數(shù)據(jù)庫中創(chuàng)建表 create table student(name varchar(20),age int sex char(2));
向表中插入數(shù)據(jù)-insert into dbname values('張三'坦辟,18,‘男’)位喂;
查看表中所有的數(shù)據(jù)--select * ?from student栖茉;
只查看姓名和年齡--select name,age ?from student;
6.常用的數(shù)據(jù)類型
char varchar int?
7.其他常用的數(shù)據(jù)類型
date datetime text/blob
8.mysql常用函數(shù)
查看數(shù)據(jù)庫版本-select versin();
查看當(dāng)前數(shù)據(jù)庫時(shí)間--select current_date();
查看當(dāng)前連接數(shù)據(jù)庫的用戶--select user()
or(滿足一個(gè)條件)和and(都需要滿足)
in(x,x)返回條件中的記錄與or作用相似
between and 返回兩者之間的記錄
like與%一起使用 模糊查詢 ?like‘張%’ like‘3.com’ ?like'%a%';
order by 實(shí)現(xiàn)排序-asc desc ?select * ?from stu order by age desc;
as為查詢的列起別名 select name as '姓名',age as '年齡'拴签,sex as '性別' from stu;
group by對(duì)于查詢出的數(shù)據(jù)結(jié)果進(jìn)行分類(分組)--select ?* ?from stu group by sex;
having子查詢:對(duì)于where查詢出的結(jié)果再次查詢
查找出年齡大于20歲學(xué)生,并且在其中找出姓名等于xxx的學(xué)生
select * ?from ?stu where age>20 having name='XX' 或者age>20 and name='XX'
destinct過濾查詢的重復(fù)型記錄旗们,只顯示唯一的記錄
將學(xué)生的性別過濾---select distinct(sex) from stu;
count 查看表中有幾條數(shù)據(jù) ?select count(*) from stu;
limit限制查詢結(jié)果的輸出數(shù)量 同時(shí)也可以實(shí)現(xiàn)數(shù)據(jù)的分頁
查詢EQ前三名的學(xué)生---select * ?from stu order by EQ desc limit 3;
實(shí)現(xiàn)查詢記錄的分頁 ?select * ?from stu limit 0,3;
約束----定義表級(jí)的強(qiáng)制規(guī)則 數(shù)據(jù)的完整性?
非空約束(not null) 唯一約束(UNIQUE) 默認(rèn)約束(default) 主鍵約束(primary key)
id逐漸自增蚓哩,減 ?auto_increment的特點(diǎn)----自增值 當(dāng)刪除某一值時(shí),他不會(huì)自動(dòng)填充上渴,而是繼續(xù)自增1 ?外鍵約束(foreign key)
8.如何控制冗余數(shù)據(jù)
一般來說通過數(shù)據(jù)庫的范式理論 設(shè)計(jì)數(shù)據(jù)庫的范式來控制冗余
共有5個(gè)范式 一般達(dá)到第三范式 第一范式:對(duì)于表中的每一行岸梨,必須且僅僅有唯一的行值,在一行中的每一列僅有唯一的值并且具有原子性 第二范式 要求非主鍵列是主鍵的子集稠氮,非主鍵列活動(dòng)必須完全依賴整個(gè)主鍵曹阔。主鍵必須有唯一性的元素,一個(gè)主鍵可以由一個(gè)或更多的組成唯一值的列組成隔披。一旦創(chuàng)建赃份,主鍵無法改變,外鍵關(guān)聯(lián)一個(gè)表的主鍵奢米。主外鍵關(guān)聯(lián)意味著一對(duì)多的關(guān)系抓韩。第三范式:要求非主鍵列互不依賴 ?
MYSQL的聚合函數(shù)
1.最大值 找出EQ最高的學(xué)生 ?select ?name,eq ?from student where eq=(select ?max(EQ) from student);
select max(article) as article from shop;
2.最小值 找出EQ最低的學(xué)生 select name鬓长,min(EQ) from student; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
3.count()統(tǒng)計(jì)查詢出的記錄總數(shù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?查詢出學(xué)生的總條數(shù)? select count(name)? from student谒拴; ?
4.avg()求平均值
查詢學(xué)生EQ的平均值 select avg(EQ) ?from ?student;
5.sun() 求和 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
查詢學(xué)生EQ的總和 select sum(EQ) ?from student;
6.修改字段類型 ?alter table student modify sex char(5);
增加列 alter table student add address varchar(50)涉波;
刪除列 alter table student drop ?address;
修改列的名稱 alter table student change name names varchar(20);
修改表的名字 ?alter table student ?to ?stu;
7.MYSQL之表連接
內(nèi)鏈接:等值連接英上,因?yàn)樗麑蓚€(gè)表中的公共字段有相等的值連接起來。
左連接:顯示sql語句中l(wèi)eft join左邊表中的所有記錄啤覆,即使在left join右邊的表中沒有滿足連接條件的數(shù)據(jù)也被顯示苍日。當(dāng)滿足連接條件時(shí),left join右邊的表中的相應(yīng)的記錄與left join左邊表中的相應(yīng)記錄結(jié)合顯示城侧。
右連接:與左連接相對(duì)應(yīng)易遣,他顯示sql中 right join右邊表中的記錄,即使在right join左邊沒有的記錄也被顯示嫌佑。當(dāng)滿足條件時(shí)豆茫,right join左邊表中相應(yīng)記錄將與right join右邊表中相應(yīng)記錄進(jìn)行結(jié)合顯示侨歉。通常左連接和右連接顯示內(nèi)容一致。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?left join 以左邊的表查詢?yōu)橹?? ?right join 以右邊的表查詢?yōu)橹?/p>
8.MYSQL之視圖 ?
他可以訪問一個(gè)或多個(gè)表中的數(shù)據(jù)揩魂,是從一個(gè)或多個(gè)表中派生出的數(shù)據(jù)對(duì)象
視圖的特點(diǎn):1將復(fù)雜的查詢簡單化 2,提供安全機(jī)制幽邓,它保證用戶只可以看得到的數(shù)據(jù),系統(tǒng)中真實(shí)的表不可被存取的
創(chuàng)建視圖 ?create view case_view as ?select cases.id as '訂單編號(hào)',user.name as '顧客姓名'火脉,goods.name as '物品名稱' from user inner join(case inner join goods on cases.goods_id=goods.id)on user.id=case.user_id;
create view test.v as ?select * from t;
修改視圖:alter view viewname as SQL;
刪除視圖:drop view viewname;
9.MYSQL的事務(wù)與引擎?
事務(wù)他是一個(gè)操作序列牵舵,這些操作要么執(zhí)行要么不執(zhí)行,他是一個(gè)不可分割的工作單位倦挂。
事務(wù)是數(shù)據(jù)庫維護(hù)數(shù)據(jù)一致性的單位畸颅,在每個(gè)事務(wù)結(jié)束時(shí),都能保持?jǐn)?shù)據(jù)一致性
10.如何在innodb中實(shí)現(xiàn)事務(wù):
begin 告知服務(wù)器要開始一個(gè)事務(wù)
rollback:如果事務(wù)發(fā)生異常方援,那么數(shù)據(jù)回滾
commit 事務(wù)執(zhí)行成功没炒,進(jìn)行數(shù)據(jù)提交
11.鎖 ?lock table tablename(read,write)
lock table 可以對(duì)表進(jìn)行加鎖控制,以保證用戶并發(fā)訪問時(shí)非事務(wù)表的數(shù)據(jù)一致性
unlock tables 釋放鎖
12.index 索引
索引是被用來快速找出在一個(gè)列上用一特定值的行犯戏。沒有索引送火,MySQL不得不首先以第一條記錄開始并然后讀完整個(gè)表直到他找出相關(guān)的行,表越大先匪,花費(fèi)時(shí)間越多种吸。如果表對(duì)于查詢的列有一個(gè)索引,MySQL能快速到達(dá)一個(gè)位置去搜尋到數(shù)據(jù)文件的中間呀非,沒有必要考慮所有數(shù)據(jù)坚俗。
MySQL有四種類型的索引:主鍵(primary key),唯一索引(uinque),全文索引(full ?index),普通索引(index)
12.mysql的備份方式:backup ?restore copy mysqldump select ?into
backup備份myisam表 ?use test; backup table chat to 'c:\\db_backup';(只生成.frm和.myd 可以在數(shù)據(jù)庫恢復(fù)后重建索引) ?drop table chat; ?restore table chat ?from 'c:\\db_backup';
copy備份 ?停止mysql服務(wù)器-避免在備份的時(shí)候有用戶進(jìn)行數(shù)據(jù)庫的訪問 ?復(fù)制數(shù)據(jù)庫的文件夾
mysqldump備份
1.備份 ? #mysqldump -uroot -p dbname >xxxname.sql ?2.恢復(fù) #mysql -uroot ?-p dbname<>
備份某個(gè)表
備份 ?#mysqldump -uroot -p dbname tablename ?>xxxname.sql
恢復(fù) #mysql -uroot -p dbname<>
用select into 備份
備份 mysql>select * ?from tablename into outfile ‘c:\\db_backup\table.dat’
刪除表 mysql>delete from tablename;
恢復(fù) mysql>load data infile 'c:\\db_backup\table.dat' into table tablename;
查看數(shù)據(jù)庫中有哪些數(shù)據(jù)庫 mysqlshow -uroot -p
查看數(shù)據(jù)庫中有哪些表 mysqlshow ?-uroot ?-p ?dbname?
查看數(shù)據(jù)庫中表的結(jié)構(gòu) ?mysqlshow -uroot -p dbname tablename
mysqlcheck -uroot -p ?dbname //檢測(cè)
mysqlcheck -urooot -p ?--auto-repair dbname //如碰到有問題的表自動(dòng)進(jìn)行修復(fù)
mysqlcheck -uroot -p ?--optimize //優(yōu)化表
13.MYSQL管理方式
連接方式--mysql -hlocalhost -uroot 通過本地主機(jī)姜钳,以root用戶訪問坦冠,無需密碼驗(yàn)證
mysql -hlocalhost 通過本地主機(jī),匿名用戶訪問哥桥,無需密碼驗(yàn)證
設(shè)置密碼 ---mysqladmin
外部:修改密碼-- mysqladmin hlocalhost -uroot -p password "newpassword"
密碼為空--mysqladmin hlocalhost -uroot -p ?password ""
設(shè)置root用戶遠(yuǎn)程訪問密碼--mysqladmin -hremote -uroot -p password ""
內(nèi)部設(shè)置密碼:
設(shè)置root用戶本地登錄密碼 ?set password for ‘root’@‘localhost’=password('000000');
設(shè)置root用戶遠(yuǎn)程登錄密碼 set password ?for 'root'@'remote'=password('newpassword');
privileges有哪些 :
alter 修改表和索引 create創(chuàng)建數(shù)據(jù)庫和表 delete刪除表中以有的記錄 drop 刪除數(shù)據(jù)庫和表 inde 創(chuàng)建和刪除索引 ?insert向表中插入數(shù)據(jù) ?select 檢索表中的數(shù)據(jù) update修改表中的記錄 file讀寫服務(wù)器上的數(shù)據(jù) process查看服務(wù)器中執(zhí)行的線程信息或殺死線程
reload重載授權(quán)表或清空日志辙浑,主機(jī)緩存或表緩存 shutdown 關(guān)閉服務(wù)器 all所有權(quán)限
revoke取消授權(quán)