數(shù)據(jù)庫學習入門

1.數(shù)據(jù)庫作用

數(shù)據(jù)是數(shù)據(jù)庫中存儲的基本對象,包括:文字 圖形 圖像 聲音 有組織的 可共享的數(shù)據(jù)集合

2.常用的DBMS

MYSQL oracle SQL-Server ?DB2

3.MYSQL登錄

開始-運行-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ù)庫中存儲數(shù)據(jù)

首先進入數(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();

查看當前數(shù)據(jù)庫時間--select current_date();

查看當前連接數(shù)據(jù)庫的用戶--select user()

or(滿足一個條件)和and(都需要滿足)

in(x,x)返回條件中的記錄與or作用相似

between and 返回兩者之間的記錄

like與%一起使用 模糊查詢 ?like‘張%’ like‘3.com’ ?like'%a%';

order by 實現(xiàn)排序-asc desc ?select * ?from stu order by age desc;

as為查詢的列起別名 select name as '姓名',age as '年齡',sex as '性別' from stu;

group by對于查詢出的數(shù)據(jù)結(jié)果進行分類(分組)--select ?* ?from stu group by sex;

having子查詢:對于where查詢出的結(jié)果再次查詢

查找出年齡大于20歲學生,并且在其中找出姓名等于xxx的學生

select * ?from ?stu where age>20 having name='XX' 或者age>20 and name='XX'

destinct過濾查詢的重復型記錄,只顯示唯一的記錄

將學生的性別過濾---select distinct(sex) from stu;

count 查看表中有幾條數(shù)據(jù) ?select count(*) from stu;

limit限制查詢結(jié)果的輸出數(shù)量 同時也可以實現(xiàn)數(shù)據(jù)的分頁

查詢EQ前三名的學生---select * ?from stu order by EQ desc limit 3;

實現(xiàn)查詢記錄的分頁 ?select * ?from stu limit 0,3;

約束----定義表級的強制規(guī)則 數(shù)據(jù)的完整性?

非空約束(not null) 唯一約束(UNIQUE) 默認約束(default) 主鍵約束(primary key)

id逐漸自增,減 ?auto_increment的特點----自增值 當刪除某一值時,他不會自動填充,而是繼續(xù)自增1 ?外鍵約束(foreign key)

8.如何控制冗余數(shù)據(jù)

一般來說通過數(shù)據(jù)庫的范式理論 設計數(shù)據(jù)庫的范式來控制冗余

共有5個范式 一般達到第三范式 第一范式:對于表中的每一行,必須且僅僅有唯一的行值,在一行中的每一列僅有唯一的值并且具有原子性 第二范式 要求非主鍵列是主鍵的子集,非主鍵列活動必須完全依賴整個主鍵。主鍵必須有唯一性的元素,一個主鍵可以由一個或更多的組成唯一值的列組成。一旦創(chuàng)建,主鍵無法改變,外鍵關(guān)聯(lián)一個表的主鍵。主外鍵關(guān)聯(lián)意味著一對多的關(guān)系。第三范式:要求非主鍵列互不依賴 ?

MYSQL的聚合函數(shù)

1.最大值 找出EQ最高的學生 ?select ?name,eq ?from student where eq=(select ?max(EQ) from student);

select max(article) as article from shop;

2.最小值 找出EQ最低的學生 select name,min(EQ) from student; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

3.count()統(tǒng)計查詢出的記錄總數(shù) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?查詢出學生的總條數(shù)? select count(name)? from student; ?

4.avg()求平均值

查詢學生EQ的平均值 select avg(EQ) ?from ?student;

5.sun() 求和 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

查詢學生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)鏈接:等值連接,因為他將兩個表中的公共字段有相等的值連接起來。

左連接:顯示sql語句中l(wèi)eft join左邊表中的所有記錄,即使在left join右邊的表中沒有滿足連接條件的數(shù)據(jù)也被顯示。當滿足連接條件時,left join右邊的表中的相應的記錄與left join左邊表中的相應記錄結(jié)合顯示。

右連接:與左連接相對應,他顯示sql中 right join右邊表中的記錄,即使在right join左邊沒有的記錄也被顯示。當滿足條件時,right join左邊表中相應記錄將與right join右邊表中相應記錄進行結(jié)合顯示。通常左連接和右連接顯示內(nèi)容一致。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?left join 以左邊的表查詢?yōu)橹?? ?right join 以右邊的表查詢?yōu)橹?/p>

8.MYSQL之視圖 ?

他可以訪問一個或多個表中的數(shù)據(jù),是從一個或多個表中派生出的數(shù)據(jù)對象

視圖的特點:1將復雜的查詢簡單化 2,提供安全機制,它保證用戶只可以看得到的數(shù)據(jù),系統(tǒng)中真實的表不可被存取的

創(chuàng)建視圖 ?create view case_view as ?select cases.id as '訂單編號',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的事務與引擎?

事務他是一個操作序列,這些操作要么執(zhí)行要么不執(zhí)行,他是一個不可分割的工作單位。

事務是數(shù)據(jù)庫維護數(shù)據(jù)一致性的單位,在每個事務結(jié)束時,都能保持數(shù)據(jù)一致性

10.如何在innodb中實現(xiàn)事務:

begin 告知服務器要開始一個事務

rollback:如果事務發(fā)生異常,那么數(shù)據(jù)回滾

commit 事務執(zhí)行成功,進行數(shù)據(jù)提交

11.鎖 ?lock table tablename(read,write)

lock table 可以對表進行加鎖控制,以保證用戶并發(fā)訪問時非事務表的數(shù)據(jù)一致性

unlock tables 釋放鎖

12.index 索引

索引是被用來快速找出在一個列上用一特定值的行。沒有索引,MySQL不得不首先以第一條記錄開始并然后讀完整個表直到他找出相關(guān)的行,表越大,花費時間越多。如果表對于查詢的列有一個索引,MySQL能快速到達一個位置去搜尋到數(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ù)庫恢復后重建索引) ?drop table chat; ?restore table chat ?from 'c:\\db_backup';

copy備份 ?停止mysql服務器-避免在備份的時候有用戶進行數(shù)據(jù)庫的訪問 ?復制數(shù)據(jù)庫的文件夾

mysqldump備份

1.備份 ? #mysqldump -uroot -p dbname >xxxname.sql ?2.恢復 #mysql -uroot ?-p dbname<>

備份某個表

備份 ?#mysqldump -uroot -p dbname tablename ?>xxxname.sql

恢復 #mysql -uroot -p dbname<>

用select into 備份

備份 mysql>select * ?from tablename into outfile ‘c:\\db_backup\table.dat’

刪除表 mysql>delete from tablename;

恢復 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 //檢測

mysqlcheck -urooot -p ?--auto-repair dbname //如碰到有問題的表自動進行修復

mysqlcheck -uroot -p ?--optimize //優(yōu)化表

13.MYSQL管理方式

連接方式--mysql -hlocalhost -uroot 通過本地主機,以root用戶訪問,無需密碼驗證

mysql -hlocalhost 通過本地主機,匿名用戶訪問,無需密碼驗證

設置密碼 ---mysqladmin

外部:修改密碼-- mysqladmin hlocalhost -uroot -p password "newpassword"

密碼為空--mysqladmin hlocalhost -uroot -p ?password ""

設置root用戶遠程訪問密碼--mysqladmin -hremote -uroot -p password ""

內(nèi)部設置密碼:

設置root用戶本地登錄密碼 ?set password for ‘root’@‘localhost’=password('000000');

設置root用戶遠程登錄密碼 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讀寫服務器上的數(shù)據(jù) process查看服務器中執(zhí)行的線程信息或殺死線程

reload重載授權(quán)表或清空日志,主機緩存或表緩存 shutdown 關(guān)閉服務器 all所有權(quán)限

revoke取消授權(quán)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容