????此前在校學(xué)習(xí)期間,只是知道數(shù)據(jù)庫(kù)很重要,但是并未主動(dòng)去學(xué)習(xí)了解?,F(xiàn)在的學(xué)習(xí)過(guò)程中學(xué)了一遍mysql,就簡(jiǎn)單的做一個(gè)總結(jié)吧。
? ? 首先記住三個(gè)概念:
? ? 1.數(shù)據(jù)庫(kù)(Database)是按照數(shù)據(jù)結(jié)構(gòu)來(lái)組織、存儲(chǔ)和管理數(shù)據(jù)的建立在計(jì)算機(jī)存儲(chǔ)設(shè)備上的倉(cāng)庫(kù)。 ??
? ? 2.SQL :結(jié)構(gòu)化查詢語(yǔ)言(Structured Query Language)
? ? 3.MySQL:關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)
? ? database中存儲(chǔ)著各種數(shù)據(jù),sql語(yǔ)句用于從database中找出我們需要的數(shù)據(jù),mysql是一種應(yīng)用軟件,通過(guò)語(yǔ)句對(duì)database進(jìn)行操作。
????MySQL我使用的是5.6版本,通過(guò)管理員身份打開(kāi)cmd后,啟用mysql服務(wù)為:net start mysql56,關(guān)閉服務(wù)為:net stop mysql56。登錄:mysql -h localhost -u root -p ? ? 回車后輸入密碼:123456(用戶名和密碼在安裝時(shí)進(jìn)行設(shè)置)
? ? 下面將會(huì)從四個(gè)方面進(jìn)行總結(jié):
? ? 1.數(shù)據(jù)定義語(yǔ)言(DDL)
? ? 2.數(shù)據(jù)操作語(yǔ)言(DML)
? ? 3.數(shù)據(jù)查詢語(yǔ)言(DQL)
? ? 4.函數(shù)
? ? 本篇小結(jié)大部分是語(yǔ)句格式,如果需要代碼實(shí)現(xiàn)的截圖,以及部分額外知識(shí)點(diǎn)標(biāo)注,可以下載安裝xmind軟件后,下載云盤(pán)里的思維導(dǎo)圖進(jìn)行查看。
鏈接:https://pan.baidu.com/s/18a3gY9Rzu7TOox-Tkgq-_w 密碼:qrie
ximd內(nèi)容如下:

數(shù)據(jù)定義語(yǔ)言(DDL):
1.建表:
create table 表名( ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?create table test(
? ? ? ? ? 列名稱1 數(shù)據(jù)類型 , ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? id int,
? ? ? ? ? 列名稱2 數(shù)據(jù)類型, ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?name char(10),
? ? ? ? ? ......... ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?......
? ? ? ? ? 列名稱n 數(shù)據(jù)類型); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? birthday date);
常用的數(shù)據(jù)類型:整數(shù)(int,tinyint,smallint),小數(shù)(float,decimal),字符串(char,varchar),時(shí)間(date,time)
2.約束:
primary key(主鍵):約束唯一標(biāo)識(shí)數(shù)據(jù)庫(kù)表中的每條記錄
foreign key(外鍵):foreign key就是表與表之間的某種約定的關(guān)系,從一個(gè)表指向另一個(gè)表
unique:約束用于限制加入表的數(shù)據(jù)的類型
建表時(shí)可在數(shù)據(jù)類型后添加:
create table test(id int primary key);
create table test(id int,primary key(id);
或者建表時(shí)未添加,后面需要時(shí)再添加:
alter table test add primary key(id);
刪除主鍵:alter table test drop primary key;
unique的用法與primary key 相同。
外鍵的用法:
create table t1(id int primary key,name char(10));
create table t2(id int primary key,pri_id int,name char(10),
constraint i foreign key(pri_id) references t1(id));
意思為:把t2中的pri_id 作為外鍵,指向t1中的主鍵id
刪除外鍵:constraint i,把外鍵命名成i,方便了我們刪除外鍵
alter table t2 drop foreign key i;?
3.字段屬性
1.unsigned(無(wú)符號(hào)型),只能用在數(shù)值型字段
2.zerofill(自動(dòng)補(bǔ)零),只能用在數(shù)值型字段,前導(dǎo)零,同時(shí)該字段自動(dòng)式UNSIGNED
3.AUTO_INCREMENT(自動(dòng)增長(zhǎng)),寄生于主鍵
4.NOT NULL:強(qiáng)制約束列不守NULL值,即不添加數(shù)值就無(wú)法插入數(shù)據(jù)
5.缺省值(default):給數(shù)據(jù)一個(gè)默認(rèn)值(系統(tǒng)默認(rèn)值是NULL),不能與auto_increment同時(shí)用于一個(gè)字段上
寫(xiě)法均為:create table test(id int 字段屬性);
DML(數(shù)據(jù)操作語(yǔ)言):
1.索引:
創(chuàng)建:create index 索引名 on 表名(字段); ? ? ? ? ? ?create index i on test(id);
刪除:drop index 索引名; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?drop index i;
2.對(duì)數(shù)據(jù)操作:
insert(插入):
插入單獨(dú)數(shù)據(jù):insert into 表名 字段1、字段2... values(值1、值2...);
插入默認(rèn)數(shù)據(jù):insert inro 表名 values(值1、值2...);
insert into test(id,name) values(008,'周星星');
update(修改、更新):修改(更新)數(shù)據(jù):update 表名 set 字段=新值 where 列名稱=某值
update test set name='詹姆斯' where id=008;
修改多條數(shù)據(jù):
update test set name=case id
when 001 then 'qwe'
when 002 then 'asd'
end
where id in(001,002)
replace(批量更新數(shù)據(jù))(也可用于數(shù)據(jù)添加):replace into 表名(字段1、字段2...) values(值1、值2...)
replace into test(id,name) values(002,'777');
未添加的數(shù)據(jù)會(huì)默認(rèn)為null
批量更新(通過(guò)update):
insert into test(id,name) values(001,'i am 001'),(002,'i am 002')
on duplicate key update
id=values(id),name=values(name);
delete刪除數(shù)據(jù)(一條):delete from 表名 where 條件
delete from test where id=001;
3.對(duì)表操作
修改表名:alter table 舊表名 rename as 新表名;
alter table test rename as father;
修改字段的數(shù)據(jù)類型:alter table 表名 modify 字段名 數(shù)據(jù)類型
alter table test modify id char(10);
修改字段名:alter table 表名 change 字段名 新字段名 數(shù)據(jù)類型;
alter table test change name address char(50);
增加字段:alter table 表名 add 字段名1 數(shù)據(jù)類型
alter table test add address char(30);
刪除字段:alter table 表名 drop 字段名;
alter table test drop address;
DQL(數(shù)據(jù)查詢語(yǔ)句insert):
1.交叉查詢:select 表1.字段,表2.字段 from 表1,表2;
多表聯(lián)合查詢:select 表1.字段,表2.字段 from 表1,表2 where 表1.id=表2.id;
select * from t1,t2 where t1.id=t2.id;
2.查詢不重復(fù)的數(shù)據(jù):select distinct 字段 from 表名;
select distinct id from test;
3.取別名alias:select * from 表名 as 別名;
自連接:select b.* from shop as a,shop as b where a.name='包子' and a.price
把shop表取別名為a,b,通過(guò)a和b進(jìn)行自己數(shù)據(jù)的對(duì)比
4.limit(偏移量):
查詢前n條數(shù)據(jù):select *from 表名 limit n;
查詢前n條數(shù)據(jù)后的i條數(shù)據(jù):select *from 表名 limit n,i;
5.in(在where后規(guī)定多個(gè)值):select 字段 from 表名 where 字段 in(值1,值2);
select * from test where id in(1,2,3);
6.like(用于where子句中搜索列中的指定模式):select 字段 from 表名 where 字段 like 表達(dá)式;
例如搜索王姓青年:
select *from test where name like'王%';
%:表示0~多個(gè)字符
_:表示一個(gè)字符
7.order by(排序):select 字段 from 表名 order by 字段(排序方式) [desc](使用倒序排列)
select * from test order by id desc;
表示通過(guò)id從大到小進(jìn)行排序顯示
8.join連接:
inner jor(內(nèi)連接),left join(左連接),right join(右連接)
full join(全連接)(mysql不支持全連接)
格式都相同:select 字段 from 表1 inner/left/right join 表2 on 表1.字段=表2.字段;
9.union(聯(lián)合查詢):select *from 表1 union select *from 表2;
函數(shù):
1.合集函數(shù):操作面向一系列的值,并返回一個(gè)單一的值
寫(xiě)法都相同:
查詢某列的平均值:select avg(字段) from 表名;
返回某列的行數(shù):select count(字段) from 表名;
查詢某列最大值:select max(字段) from 表名;
查詢某列最小值:select min(字段) from 表名;
返回某列總和:select sum(字段) from 表名;
分組顯示總和:select sum(字段) from 表名 group by 字段;
2.標(biāo)量函數(shù):操作面向某個(gè)單一的值,并返回基于輸入值的一個(gè)單一的值
寫(xiě)法也都相同:
ucase(把字段的值轉(zhuǎn)換為大寫(xiě)):select ucase(字段) from 表名;
lcase(把字段的值轉(zhuǎn)換為小寫(xiě)):select lcase(字段) from 表名;
mid(提取字符):mid(字段,起始,結(jié)束):
select mid(name,2,3) from test;
表示從name列的第二個(gè)數(shù)據(jù)開(kāi)始,每個(gè)數(shù)據(jù)只顯示3位
len(返回文本長(zhǎng)度):select length(字段) from 表名;
round(把數(shù)值四舍五入,并保留相應(yīng)小數(shù)位):select round(字段,數(shù)字) from 表名;
now()(查詢當(dāng)前時(shí)間):select now() from 表名;(有幾個(gè)數(shù)據(jù)就出現(xiàn)幾個(gè))
? ? 此文章雖然是自己的學(xué)習(xí)小結(jié),而且還是身為初學(xué)者的我寫(xiě)的,但也希望更多的朋友能看到我的文章,如果有不足之處或疑問(wèn),歡迎到留言區(qū)留言。