绗竴閮ㄥ垎 鏁版嵁搴撲箣SQL璇彞

娉細1.mysql鏄竴绉嶅叧绯诲瀷鏁版嵁搴�

聽 聽 聽 聽 2.澶у皬鍐欎笉鏁忔劅

聽 聽 聽 聽 3.瀛楃涓茬敤鍗曞紩鍙凤紝鑻ュ瓧绗︿覆閲屾湁鍗曞紩鍙凤紝鍒欏彲浠ョ敤涓や釜鍗曞紩鍙疯〃绀轰竴涓崟寮曞彿 聽鈥榓ssad鈥欌�檃鈥樿〃绀� assad'a

涓�銆佹暟鎹簱姒傝堪

鏁版嵁锛氭暟鎹簱涓瓨鍌ㄧ殑鍩烘湰瀵硅薄

鏁版嵁搴擄紙DataBase锛孌B锛夛細鎸囬暱鏈熶繚瀛樺湪璁$畻鏈虹殑瀛樺偍璁惧涓婏紝鏈夌粍缁囧彲鍏变韩鐨勫ぇ閲忔暟鎹泦鍚�

鏁版嵁搴撶鐞嗙郴缁燂紙DataBase Management System锛孌BMS锛夛細鐢ㄤ簬鏁版嵁瀹氫箟銆佹搷绾点�佹暟鎹簱杩愯绠$悊锛屽缓绔嬬淮鎶�

鏁版嵁搴撶郴缁燂細鏁版嵁搴�+鏁版嵁搴撶鐞嗙郴缁�+搴旂敤绯荤粺+鏁版嵁绠$悊鍛�


浜屻�佹暟鎹簱鐨勯厤缃笌娴嬭瘯

瀹樼綉涓嬭浇 mysql 鍜孨avicat for mysql

1.鍚姩鍜岀粓姝ysql

锛�1锛夎绠楁満--銆嬬鐞�--銆嬫湇鍔�--銆嬫墦寮�鍏抽棴mysql

锛�2锛塪os绐楀彛 聽net 聽start mysql 聽 聽 聽 聽 聽net stop mysql

2.杩涘叆mysql锛堥粯璁よ处鍙穜oot锛�

鍦╞in鐩綍閲屽惎鍔╠os绐楀彛

mysql -uroot -p瀵嗙爜

3.杩涘叆鏁版嵁搴�

use 鏁版嵁搴撳悕


涓夈�丮ysql鏁版嵁绫诲瀷

1銆佹暣鍨�

MySQL鏁版嵁绫诲瀷鍚箟锛堟湁绗﹀彿锛�

tinyint(m)1涓瓧鑺偮� 鑼冨洿(-128~127)

smallint(m)2涓瓧鑺偮� 鑼冨洿(-32768~32767)

int(m)4涓瓧鑺偮� 鑼冨洿(-2147483648~2147483647)

bigint(m)8涓瓧鑺偮� 鑼冨洿(+-9.22*10鐨�18娆℃柟)

2銆佹诞鐐瑰瀷(float鍜宒ouble)

float(m,d)鍗曠簿搴︽诞鐐瑰瀷聽聽聽 8浣嶇簿搴�(4瀛楄妭)聽聽聽 聽m鎬讳釜鏁帮紝d灏忔暟浣�

double(m,d)鍙岀簿搴︽诞鐐瑰瀷聽聽聽 16浣嶇簿搴�(8瀛楄妭)聽聽 聽m鎬讳釜鏁帮紝d灏忔暟浣�

璁句竴涓瓧娈靛畾涔変负float(5,3)锛屽鏋滄彃鍏ヤ竴涓暟123.45678,瀹為檯鏁版嵁搴撻噷瀛樼殑鏄�123.457锛屼絾鎬讳釜鏁拌繕浠ュ疄闄呬负鍑嗭紝鍗�6浣嶃��

娴偣鏁版湁绮惧害鎹熷け锛屾瘮濡傛彃鍏ヤ簡123.14鍙兘鍙樻垚123.12

3銆佸畾鐐规暟

娴偣鍨嬪湪鏁版嵁搴撲腑瀛樻斁鐨勬槸杩戜技鍊硷紝鑰屽畾鐐圭被鍨嬪湪鏁版嵁搴撲腑瀛樻斁鐨勬槸绮剧‘鍊笺��

decimal(m,d)聽

4銆佸瓧绗︿覆

char(n)鍥哄畾闀垮害锛宯鏈�澶т负255.char(10)涓嶈冻鐢ㄧ┖瀛楃琛ラ綈锛堟晥鐜囬珮锛�

varchar(n)鍙彉闀垮害锛宯鏈�澶�255锛寁archar(10)鏈�澶т负10, 聽涓�瀹氳鎸囧畾闀垮害

text鍙彉闀垮害锛屾渶澶�65535涓瓧绗︼紝寰堝皯鐢紝

5銆佹棩鏈�

data 聽鍙〃绀烘棩鏈�

time 聽鍙〃绀烘椂闂�

娉細1112 mysql璇嗗埆涓�00锛�11锛�12鑰�11锛�12mysql璇嗗埆涓�11锛�12锛�00

YEAR 琛ㄧず骞�

datatime 琛ㄦ棩鏈�+鏃堕棿


鍥涖�丼QL璇彞

瑙i噴鎬ц瑷�锛屽啓涓�鍙ユ墽琛屼竴鍙ワ紝涓嶉渶瑕佹暣浣撶紪璇戞墽琛�

desc 琛ㄥ悕 瑙傚療琛ㄧ粨鏋�

琛╠ual锛岀敤鏉ヨ繘琛岃〃杈惧紡杩愮畻

璧峰埆鍚� 聽select <鍒楀悕>[As]鍒悕锛堝埆鍚嶈嫢鏈夌壒娈婂瓧绗﹀姞鍙屽紩鍙凤級


鍒嗙被

DDL(*)锛圖ata DefinitionLanguage锛夛細鏁版嵁瀹氫箟璇█锛宑reate銆丄LTER銆� DROP

DML(**)锛圖ata ManipulationLanguage锛夛細鏁版嵁鎿嶄綔璇█锛宨nsert delete update

DCL锛圖ata Control Language锛夛細鏁版嵁鎺у埗璇█锛実rant remove

DQL(*****)锛圖ata Query Language锛夛細鏁版嵁鏌ヨ璇█锛宻elect

*娉ㄦ剰锛歴ql璇彞浠�;缁撳熬


鎿嶄綔鏁版嵁搴�(浜嗚В)

1.鍒涘缓

Create database mydb1;

Create database mydb2 character set gbk;

2.鏌ヨ

鏌ョ湅褰撳墠鏁版嵁搴撴湇鍔″櫒涓殑鎵�鏈夋暟鎹簱

Show databases;

鏌ョ湅鍓嶉潰鍒涘缓鐨刴ydb2鏁版嵁搴撶殑瀹氫箟淇℃伅

Show create database mydb2;

3..淇敼

鏌ョ湅鏈嶅姟鍣ㄤ腑鐨勬暟鎹簱锛屽苟鎶妋ydb2鐨勫瓧绗﹂泦淇敼涓簎tf8;

alter database mydb2 character set utf8;

4.鍒犻櫎

Drop database mydb3;

5.鍏朵粬

鏌ョ湅褰撳墠浣跨敤鐨勬暟鎹簱

Select database();

6.鍒囨崲鏁版嵁搴�

Use mydb2;



鎿嶄綔琛紙閲嶇偣锛�

DDL鏁版嵁瀹氫箟璇█

瀵硅〃鐨勫鍒犳敼

create

璇硶锛�

create table <琛ㄥ悕>(

<鍒楀悕> 鏁版嵁绫诲瀷 [鍒楃骇瀹屾暣鎬х害鏉熸潯浠禲,

<鍒楀悕> 鏁版嵁绫诲瀷 [鍒楃骇瀹屾暣鎬х害鏉熸潯浠禲,

...

[琛ㄧ骇瀹屾暣鎬х害鏉熸潯浠�(鍙悓鏃剁害鏉熷鍒�)]

);


鏁版嵁瀹屾暣鎬�

浣滅敤锛氫繚璇佺敤鎴疯緭鍏ョ殑鏁版嵁淇濆瓨鍒版暟鎹簱涓槸姝g‘鐨勩��

瀹屾暣鎬х殑鍒嗙被锛�

1.瀹炰綋瀹屾暣鎬�:

2.鍩熷畬鏁存��:

3.寮曠敤瀹屾暣鎬�:


鍒楃骇绾︽潫鏉′欢

1.瀹炰綋瀹屾暣鎬�

瀹炰綋锛氬嵆琛ㄤ腑鐨勪竴琛�(涓�鏉¤褰�)浠h〃涓�涓疄浣擄紙entity锛�

瀹炰綋瀹屾暣鎬х殑浣滅敤锛氭爣璇嗘瘡涓�琛屾暟鎹笉閲嶅銆�

绾︽潫绫诲瀷锛�涓婚敭绾︽潫锛坧rimary key锛�鍞竴绾︽潫(unique)鑷姩澧為暱鍒�(auto_increment)

(1)涓婚敭绾︽潫锛坧rimary key锛�

娉細姣忎釜琛ㄤ腑瑕佹湁涓�涓富閿�,涓斿彧鑳芥湁涓�涓�

鐗圭偣锛氭暟鎹敮涓�锛屼笖涓嶈兘涓簄ull,鎻掑叆鏁版嵁鐨勭粍鍚堜腑浠讳綍涓�涓暟鎹兘涓嶈兘鏄痭ull

锛�2锛夊敮涓�绾︽潫(unique)锛氱壒鐐癸細鏁版嵁涓嶈兘閲嶅銆傚彲null

锛�3锛夎嚜鍔ㄥ闀垮垪(auto_increment)锛屼竴涓〃鍙兘鏈変竴涓嚜澧炲垪锛堝彧鑳芥坊鍔犲埌涓婚敭绾︽潫鎴栧敮涓�绾︽潫涓婏級

2.鍩熷畬鏁存��

鍩熷畬鏁存�х殑浣滅敤锛氬煙浠h〃褰撳墠鍗曞厓鏍硷紝闄愬埗姝ゅ崟鍏冩牸鐨勬暟鎹纭紝

鍩熷畬鏁存�х害鏉燂細鏁版嵁绫诲瀷闈炵┖绾︽潫锛坣ot聽null锛夐粯璁ゅ�肩害鏉�(default)聽check绾︽潫锛坢ysql涓嶆敮鎸侊級check(sex='鐢�'orsex='濂�')

锛�1锛塶ot null 涓嶄负绌� 聽null鍙负绌�

锛�2锛塪efault 榛樿鍊� 聽 聽 聽 聽缁欏彉閲忚祴榛樿鍊�

3.寮曠敤瀹屾暣鎬�锛堝弬鐓у畬鏁存�э級

澶栭敭绾︽潫

鍒楃骇绾︽潫鐩存帴澹版槑鍚庢坊鍔狅細deptno int REFERENCES dept(deptno)

琚害鏉熺殑鍒楀繀椤绘槸鍙︿竴涓〃涓殑鍏锋湁鍞竴鎬х殑涓�鍒�

娉細1.寰�鍏锋湁澶栭敭绾︽潫鐨勫瓧娈典腑娣诲姞鏁版嵁锛岃繖涓暟鎹繀椤绘槸鍙︿竴寮犺〃涓寚瀹氬瓧娈典腑鍑虹幇鐨勬暟鎹�

2.琚害鏉熺殑瀛楁鍜岀害鏉熺殑瀛楁涓嶄竴瀹氳鍚嶅瓧鐩稿悓

3.鍒犻櫎琛ㄧ殑鏃跺�欏厛鍒犻櫎娣诲姞澶栭敭绾︽潫鐨勮〃锛屽啀鍒犻櫎鍙傜収鐨勮〃

瀹為檯寮�鍙戜腑寰堝皯浣跨敤澶栭敭绾︽潫锛岃�屾洿澶氫娇鐢ㄧ殑鏄笟鍔¢�昏緫杩涜鍒ゆ柇


琛ㄧ骇绾︽潫鏉′欢锛�

锛�1锛塩onstraint <绾︽潫鍚�> unique(鍒�1锛屽垪2) 琛ㄧず缁勫悎鍞竴

锛�2锛塸rimary key(classid锛宻tuid) 聽鑱斿悎涓婚敭锛屼袱涓�煎喅瀹氫富閿�硷紝

锛�3锛塁ONSTRAINT 聽 澶栭敭绾︽潫鍚� 聽foreign key (sid) references student(sid涓婚敭));

鏈〃鐨剆id渚濊禆student琛ㄤ腑鐨勪富閿畇id


Drop

Drop Table <琛ㄥ悕> 聽鏈夊叧鑱旇〃鍏堣В闄ゅ叧鑱斿啀鍒犺〃


Alter锛堟槸閽堝琛ㄧ粨鏋勭殑鏇存敼锛�

娉細alter閽堝琛ㄧ粨鏋勭殑鏇存敼锛屽彲浠ラ�氳繃鍥惧舰鍖栧伐鍏风洿鎺ユ搷浣滐紝浣滀负浜嗚В

1.娣诲姞鍒� 聽

Alter Table <琛ㄥ悕> ADD <鏂板垪鍚�><鏁版嵁绫诲瀷>[瀹屾暣鎬х害鏉熸潯浠禲 聽

(after 瀛楁鍚�(鎸囧畾瀛楁鍚庢坊鍔�)/first(绗竴鍒楁坊鍔�))

2.鍒犻櫎鍒�

ALTER TABLE <琛ㄥ悕> DROP <鍒楀悕>

3.淇敼鍒楁暟鎹被鍨�

ALTER TABLE <琛ㄥ悕> MODIFY <鍒楀悕> <鏁版嵁绫诲瀷>[绾︽潫鏉′欢]

4.淇敼琛ㄥ悕

ALTER TABLE <鏃ц〃鍚�> RENAME TO <鏂拌〃鍚�>

5.淇敼瀛楁鍚�

ALTER TABLE <琛ㄥ悕> 聽 聽CHANGE <鏃у垪鍚�> <鏂板垪鍚�> <鏂版暟鎹被鍨�>

6.澧炶ˉ绾︽潫

锛�1锛変富閿� 聽ALTER TABLE <琛ㄥ悕> ADD CONSTRAINT <绾︽潫鍚�> 锛堢害鏉熺被_琛ㄥ悕_鍒楀悕锛塒RIMARY KEY[UNIQUE] <(鍒楀悕)>

锛�2锛夊閿� ALTER TABLE <琛ㄥ悕> ADD CONSTRAINT <绾︽潫鍚�> FOREIGN KEY <(鍒楀悕)> REFERENCES <琛ㄥ悕><(鍒楀悕)>

锛�3锛夐粯璁ゅ�� ALTER TABLE <琛ㄥ悕> ALTER <鍒楀悕> SET DEFAULT 榛樿鍊�

锛�4锛夎嚜澧炲垪 聽ALTER 聽TABLE <琛ㄥ悕> MODIFY 聽COLUMN<鍒楀悕> 聽<鏁版嵁绫诲瀷> 聽NOT NULL聽 <绾︽潫锛堜富閿垨鑰呭敮涓�鎬х害鏉燂級> 聽 聽 AUTO_INCREMENT


7.鍒犻櫎绾︽潫

1.涓婚敭 ALTER TABLE<琛ㄥ悕> DROP PRIMARY KEY

2.鍞竴鍊� ALTER TABLE <琛ㄥ悕> DROP INDEX <鍒楀悕>

3. 澶栭敭 ALTER TABLE <琛ㄥ悕> DROP FOREIGN KEY 绾︽潫鍚�

4.鍒犻櫎鑷闀� 聽ALTER TABLE <琛ㄥ悕> MODIFY <鍒楀悕> <鏁版嵁绫诲瀷>


DML鏁版嵁鎿嶄綔璇█

DML鏄琛ㄤ腑鐨勬暟鎹繘琛屽銆佸垹銆佹敼鐨勬搷浣溿�備笉瑕佷笌DDL娣锋穯浜嗐��

INSERT聽

1.鎻掑叆涓嶅瓨鍦ㄧ殑鏁版嵁 聽INSERT INTO <琛ㄥ悕> [(鍒�1锛屽垪2锛屽垪3...)] values 锛�' 鍊�1','鍊�2 ','鍊�3 '锛�,锛�' 鍊�1','鍊�2 ','鍊�3 '锛�..

娉細鎻掑叏閮ㄦ暟鎹殑鏃跺�欏彲浠ヤ笉鍐欏垪鍚�

2.灏嗘煡璇㈢殑鏁版嵁鏌ュ埌鏁版嵁琛�

INSERT INTO <琛ㄥ悕> [(鍒�1锛屽垪2....)] 聽SELECT 鏌ヨ璇彞


UPDATE锛堥拡瀵瑰�肩殑鏇存敼锛�

UPDATE <琛ㄥ悕> SET 鍒楀悕1=鍊艰〃杈惧紡[,鍒楀悕2=鍊艰〃杈惧紡].....[WHERE 鏉′欢琛ㄨ揪寮廬


DELETE

DELETE FROM <琛ㄥ悕> [WHERE 琛ㄨ揪寮廬

渚嬶細鍒犻櫎id灏忎簬10鐨勬暟鎹褰�

DELETE 鍒犻櫎鐨勬暟鎹� FROM student WHERE ID<10

TRUNCATE鍒犻櫎鏄妸琛ㄧ洿鎺ROP鎺夛紝鐒跺悗鍐嶅垱寤轰竴涓悓鏍风殑鏂拌〃銆傚垹闄ょ殑鏁版嵁涓嶈兘鎵惧洖銆傛墽琛岄�熷害姣擠ELETE蹇��

Truncate Table<琛ㄥ悕> 娓呯┖琛ㄦ暟鎹�


DQL鏁版嵁鏌ヨ璇█

鏌ヨ杩斿洖鐨勭粨鏋滈泦鏄竴寮犺櫄鎷熻〃銆�

SELECT

瀹為檯搴旂敤鏃讹紝鏌ヨ鏄笉鍙敤select * 骞朵笖涓�瀹氳鍐欐潯浠讹紝鍥犱负鍏徃鏁版嵁寰堝锛屼笉鍐欐潯浠朵細寮曡捣姝绘満

SELECT [ALL|DISTINCT] <鍒楀悕>,[<鍒楀悕>...]

FROM <琛ㄥ悕鎴栬鍥惧悕> [琛ㄥ悕鎴栬鍥惧悕]

[WHERE <鏉′欢琛ㄨ揪寮�>]

[GROUP BY <鍒楀悕> [HAVING] <鏉′欢琛ㄨ揪寮�>]]

[ORDER BY <鍒楀悕>[ASC|DESC]] 聽 聽[LIMIT m,n];

*---->浠h〃鎵�鏈�

DISTINCT锛氭秷闄ゆ煡璇㈠埌鐨勯噸澶嶇粨鏋滐紙鑻ュ悗闈㈣窡鐫�澶氫釜鍒楋紝鍘婚櫎鐨勬槸閲嶅缁勫悎锛�

LIMIT m,n 聽 聽m琛ㄧず寮�濮嬩綅缃紝n琛ㄧず闀垮害锛堣幏鍙栨煡璇㈢粨鏋滅殑涓�閮ㄥ垎鍐呭锛夛紝娉細绗竴琛屾暟鎹负0

鎵ц椤哄簭锛�FROM 聽聽WHERE 聽聽GROUP BY 聽聽HAVING 聽SELECT 聽DISTINCT 聽聽ORDER BY 聽LIMIT


鏉′欢鏌ヨWHERE聽

=銆�<=>瀹夊叏绛夊彲鍒ゆ柇涓よ�呮槸鍚﹂兘涓簄ull锛�!=銆�<>涓嶇瓑銆�<銆�<=銆�>銆�>=锛�

AND 骞朵笖聽聽OR鎴� 聽聽聽NOT 聽闈灺�

BETWEEN 鏉′欢1 聽AND 鏉′欢2 琛ㄧず涓よ�呬箣闂� 聽 聽 / NOT聽BETWEEN 鏉′欢1 聽AND 鏉′欢2

<鍒楀悕> in 锛堬級鍦ㄦ嫭鍙烽噷鐨勫�煎彇锛宯ot in

IS NULL 聽 琛ㄧず涓虹┖ 聽 IS NOT NULL 涓嶄负绌�


閫氶厤绗IKE锛圢ot Like涓嶅寘鍚級

_ 涓嬪垝绾胯〃绀轰竴涓瓧绗� 聽渚嬶細 LIKE '瀛檁'

%琛ㄧず浠绘剰闀垮害 聽 聽 聽 聽 聽渚嬶細 LIKE '瀛�%' 聽 聽 LIKE '%M%'鍑虹幇杩嘙

[m,n]m鍒皀涓暱搴�


鑱氬悎鍑芥暟

sum() 聽 avg() 聽 min() 聽 max() count()

鑱氬悎鍑芥暟鍙互宓屽 聽 max(avg(sal)) 聽鏌ヨ骞冲潎鍊兼渶澶х殑涓�缁�

娉細鑱氬悎鍑芥暟缁熻鐨勬槸闈濶ULL鐨勬暟鎹紝

濡傛灉涓�涓垪鏈夊緢澶氳褰曠殑鍊间负NULL锛屽洜涓轰换浣曚笢瑗夸笌NULL鐩稿姞缁撴灉杩樻槸NULL锛屾墍浠ョ粨绠楃粨鏋滃彲鑳戒細鍑虹幇NULL銆備笅闈娇鐢ㄤ簡鎶奛ULL杞崲鎴愭暟鍊�0鐨勫嚱鏁癐FNULL锛�

SELECT *,sal+IFNULL(鍒楀悕,0) FROM emp;


鍒嗙粍

GROUD BY (鏍规嵁鏌愪釜鏉′欢瀵规暟鎹繘琛屽垎缁�) 聽铏借鏄垎缁勪絾鏄垎缁勫悗鐨勬暟鎹槸鏃犳硶鐩存帴鍏ㄩ儴鎵撳嵃鍑烘潵鐨勶紝寮鸿鎵撳嵃姣忕粍鍙細鏄剧ず姣忕粍鐨勪竴涓暟鎹�傦紙鍙互浣跨敤GROUP_CONCAT(瀛楁)鍑芥暟鏉ユ樉绀哄垎缁勬暟鎹級

GROUD BY鍙互瀵逛袱涓睘鎬ц繘琛屽垎缁勶紝杩欐椂鍒嗙殑缁勬槸浠ヤ袱涓睘鎬х殑缁勫悎杩涜鍒嗙殑缁勶紝缁勫拰鐩稿悓涓轰竴缁勩��

鍒嗙粍鍙互鐞嗚В鎴愪负锛屾妸浣爂roup聽by鍚庨潰鐨勫瓧娈典綔涓轰竴涓暣浣擄紝杈撳嚭涓�鏉′綘鎯冲緱鍒扮殑涓�绉嶈仛鍚堝嚱鏁扮被鍨嬬殑鏁版嵁锛屾墍浠�鍒嗙粍鍜岃仛鍚堝嚱鏁�鎭伅鐩稿叧銆�

鍒嗙粍鍑芥暟鐨勬槸姣忎竴缁勫琛岃緭鍏ワ紝鍗曡杈撳嚭 杩欓噷灏辫В閲婁簡 聽select ename max锛坰al锛� from emp 聽锛屽洜涓篹name鍙兘鏄涓紝灏变骇鐢熶簡澶氳杈撳嚭锛屼骇鐢熶簡鏃犳硶鍖归厤鐨勯棶棰�

娉細鍑虹幇鍦ㄦ煡璇㈣鍙ラ噷鐨勫瓧娈碉紝濡傛灉娌″嚭鐜板湪鑱氬悎鍑芥暟涓紝灏变竴瀹氳鍑虹幇鍦╣roud by涓笉鐒朵細鍑虹幇涓婅堪闂

SELECT s_id,count(*) AS total

from fruits

group by s_id with rollup 聽 聽

娉細with rollup琛ㄧず鐨勬槸鎵�鏈塩ount(*)鐨勫拰锛屼篃灏辨槸鎵�璇寸殑鎬昏锛�


HAVING锛堝浜庤仛鍚堝嚱鏁扮殑缁熻缁撴灉杩涜绛涢�夛級


having涓巜here鐨勫尯鍒�:

1.having鏄湪鍒嗙粍鍚庡鏁版嵁杩涜杩囨护.

where鏄湪鍒嗙粍鍓嶅鏁版嵁杩涜杩囨护

2.having鍚庨潰鍙互浣跨敤鍒嗙粍鍑芥暟(缁熻鍑芥暟)

where鍚庨潰涓嶅彲浠ヤ娇鐢ㄥ垎缁勫嚱鏁般��

WHERE鏄鍒嗙粍鍓嶈褰曠殑鏉′欢锛屽鏋滄煇琛岃褰曟病鏈夋弧瓒砏HERE瀛愬彞鐨勬潯浠讹紝閭d箞杩欒璁板綍涓嶄細鍙傚姞鍒嗙粍锛涜�孒AVING鏄鍒嗙粍鍚庢暟鎹殑绾︽潫銆�


鎺掑簭

ORDER BY <鍒�1>[ASC|DESC],[<鍒�1>[ASC|DESC]] 聽 鍏堟帓鍒�1锛屽垪1鐩稿悓鍐嶆帓鍒�2

DESC鍊掑簭锛孉SC姝e簭(榛樿)


琛ㄤ笌琛ㄤ箣闂寸殑鍏崇郴

涓�瀵逛竴锛氫緥濡倀_person琛ㄥ拰t_card琛紝鍗充汉鍜岃韩浠借瘉銆傝繖绉嶆儏鍐甸渶瑕佹壘鍑轰富浠庡叧绯伙紝鍗宠皝鏄富琛紝璋佹槸浠庤〃銆備汉鍙互娌℃湁韬唤璇侊紝浣嗚韩浠借瘉蹇呴』瑕佹湁浜烘墠琛岋紝鎵�浠ヤ汉鏄富琛紝鑰岃韩浠借瘉鏄粠琛ㄣ�傝璁′粠琛ㄥ彲浠ユ湁涓ょ鏂规锛�

鍦╰_card琛ㄤ腑娣诲姞澶栭敭鍒楋紙鐩稿t_user琛級锛屽苟涓旂粰澶栭敭娣诲姞鍞竴绾︽潫锛�

缁檛_card琛ㄧ殑涓婚敭娣诲姞澶栭敭绾︽潫锛堢浉瀵箃_user琛級锛屽嵆t_card琛ㄧ殑涓婚敭涔熸槸澶栭敭銆�

涓�瀵瑰锛堝瀵逛竴锛夛細鏈�涓哄父瑙佺殑灏辨槸涓�瀵瑰锛佷竴瀵瑰鍜屽瀵逛竴锛岃繖鏄粠鍝釜瑙掑害鍘荤湅寰楀嚭鏉ョ殑銆倀_user鍜宼_section鐨勫叧绯伙紝浠巘_user鏉ョ湅灏辨槸涓�瀵瑰锛岃�屼粠t_section鐨勮搴︽潵鐪嬪氨鏄瀵逛竴锛佽繖绉嶆儏鍐甸兘鏄湪澶氭柟鍒涘缓澶栭敭锛�

澶氬澶�锛氫緥濡倀_stu鍜宼_teacher琛紝鍗充竴涓鐢熷彲浠ユ湁澶氫釜鑰佸笀锛岃�屼竴涓�佸笀涔熷彲浠ユ湁澶氫釜瀛︾敓銆傝繖绉嶆儏鍐甸�氬父闇�瑕佸垱寤轰腑闂磋〃鏉ュ鐞嗗瀵瑰鍏崇郴銆備緥濡傚啀鍒涘缓涓�寮犺〃t_stu_tea琛紝缁欏嚭涓や釜澶栭敭锛屼竴涓浉瀵箃_stu琛ㄧ殑澶栭敭锛屽彟涓�涓浉瀵箃_teacher琛ㄧ殑澶栭敭銆�


澶氳〃鏌ヨ锛堥噸鐐癸級

澶氳〃鏌ヨ鏈夊涓嬪嚑绉嶏細

1.鍚堝苟缁撴灉闆嗭紱UNION銆乁NION ALL

2.杩炴帴鏌ヨ

鍐呰繛鎺INNER] JOIN ON

澶栬繛鎺UTER JOIN ON

宸﹀杩炴帴LEFT [OUTER] JOIN

鍙冲杩炴帴RIGHT [OUTER] JOIN

鑷劧杩炴帴NATURAL JOIN

3.瀛愭煡璇�


鍚堝苟缁撴灉闆�

1.鍚堝苟缁撴灉闆�

浣滅敤锛氬悎骞剁粨鏋滈泦灏辨槸鎶婁袱涓猻elect璇彞鐨勬煡璇㈢粨鏋滃悎骞跺埌涓�璧凤紒

鍚堝苟缁撴灉闆嗘湁涓ょ鏂瑰紡锛�

UNION锛氬幓闄ら噸澶嶈褰曪紝渚嬪锛歋ELECT* FROM t1 UNION SELECT * FROM t2锛�

UNION ALL锛氫笉鍘婚櫎閲嶅璁板綍锛屼緥濡傦細SELECT * FROM t1 UNION ALL SELECT * FROM t2銆�

瑕佹眰锛�琚悎骞剁殑涓や釜缁撴灉锛氬垪鏁般�佸垪绫诲瀷蹇呴』鐩稿悓銆�


2.杩炴帴鏌ヨ锛堥噸鐐癸級

杩炴帴鏌ヨ灏辨槸姹傚嚭澶氫釜琛ㄧ殑涔樼Н锛屼緥濡倀1杩炴帴t2锛岄偅涔堟煡璇㈠嚭鐨勭粨鏋滃氨鏄痶1*t2銆�

聽 聽 聽 聽 杩炴帴鏌ヨ浼氫骇鐢熺瑳鍗″皵绉紝鍋囪闆嗗悎A={a,b}锛岄泦鍚圔={0,1,2}锛屽垯涓や釜闆嗗悎鐨勭瑳鍗″皵绉负{(a,0),(a,1),(a,2),(b,0),(b,1),(b,2)}銆傚彲浠ユ墿灞曞埌澶氫釜闆嗗悎鐨勬儏鍐点��

聽 聽 聽 聽 閭d箞澶氳〃鏌ヨ浜х敓杩欐牱鐨勭粨鏋滃苟涓嶆槸鎴戜滑鎯宠鐨勶紝閭d箞鎬庝箞鍘婚櫎閲嶅鐨勶紝涓嶆兂瑕佺殑璁板綍鍛紝褰撶劧鏄�氳繃鏉′欢杩囨护銆傞�氬父瑕佹煡璇㈢殑澶氫釜琛ㄤ箣闂撮兘瀛樺湪鍏宠仈鍏崇郴锛岄偅涔堝氨閫氳繃鍏宠仈鍏崇郴鍘婚櫎绗涘崱灏旂Н銆�

聽 聽 聽 聽 聽浣犺兘鎯冲儚鍒癳mp鍜宒ept琛ㄨ繛鎺ユ煡璇㈢殑缁撴灉涔堬紵emp涓�鍏�14琛岃褰曪紝dept琛ㄤ竴鍏�4琛岃褰曪紝閭d箞杩炴帴鍚庢煡璇㈠嚭鐨勭粨鏋滄槸56琛岃褰曘�備篃灏变綘鍙槸鎯冲湪鏌ヨemp琛ㄧ殑鍚屾椂锛屾妸姣忎釜鍛樺伐鐨勬墍鍦ㄩ儴闂ㄤ俊鎭樉绀哄嚭鏉ワ紝閭d箞灏遍渶瑕佷娇鐢ㄤ富澶栭敭鏉ュ幓闄ゆ棤鐢ㄤ俊鎭簡銆�


浣跨敤涓诲閿叧绯诲仛涓烘潯浠舵潵鍘婚櫎鏃犵敤淇℃伅

SELECT * FROM emp,dept WHERE emp.deptno=dept.deptno;

娉細鍦ㄥ琛ㄦ煡璇腑锛屽湪浣跨敤鍒楁椂蹇呴』鎸囧畾鍒楁墍浠庡睘鐨勮〃锛屼緥濡俥mp.deptno琛ㄧずemp琛ㄧ殑deptno鍒椼��

绗竴閮ㄥ垎 鏁版嵁搴撲箣SQL璇彞_第1张图片

锛�1锛夊唴杩炴帴

鍒嗕负闅愬紡鍜屾樉绀猴紝鏄剧ず鍐呰繛鎺ワ細灏嗚〃闂寸殑閫楀彿鐢↖nner join琛ㄧず锛寃here鐢╫n琛ㄧず锛堜竴涓猨oin璺熶竴涓猳n锛�

SELECT *

FROM emp e Inner JOIN dept d

ON聽e.deptno=d.deptno;

鐗圭偣锛氭煡璇㈢粨鏋滃繀椤绘弧瓒虫潯浠讹紝涓や釜琛ㄤ腑閮芥湁鐨勬暟鎹墠浼氭樉绀哄嚭鏉�


鑷繛鎺�锛氭妸鍚屼竴寮犺〃褰撳仛澶氬紶琛ㄥ寰�(鐗规畩鍐呰繛鎺�)

姹�7369鍛樺伐缂栧彿銆佸鍚嶃�佺粡鐞嗙紪鍙峰拰缁忕悊濮撳悕

鍚屼竴寮犺〃鏃犳硶鎺у埗涓嶅悓琛岀殑mgr鍜宔mpno鐩稿悓锛屽鏋滃彧鏌ヤ竴寮犺〃鍙gr鍜宔mpno鐩哥瓑姣旂殑鏄悓涓�琛屻��

鎵�浠ュ鏋滄瘮杈冩潯浠舵槸鍚屼竴寮犺〃鍐呬笉鍚岃鐨勬暟鎹娇鐢ㄨ嚜杩炴帴锛屽皢涓�寮犺〃鎷疯礉涓�浠�

SELECTe1.empno , e1.ename,e2.mgr,e2.ename

FROM emp e1, emp e2

WHERE e1.mgr = e2.empno ANDe1.empno = 7369;

娉ㄦ剰锛氬唴杩炴帴涓�瀹氳缁欒〃璧峰埆鍚嶏紝璁╂暟鎹簱鐭ラ亾浣犲湪璋冪敤琛ㄥ悕鏃跺埌搴曡皟鐢ㄧ殑鏄袱寮犵浉鍚岃〃鐨勫摢涓�寮�


锛�2锛夊杩炴帴锛堝熀纭�琛ㄤ腑鐨勬暟鎹繀椤婚兘鍑虹幇锛屽彟涓�寮犳病鏈夌殑鐢╪ull濉厖锛�

宸﹀杩炴帴锛氬乏杈瑰熀纭�琛� 聽 聽 鍙冲杩炴帴锛氬彸杈瑰熀纭�琛�

聽 聽 聽 聽杩樼敤涓婇潰鐨勪緥瀛愬叾涓璭mp琛ㄤ腑鈥滃紶涓夆�濊繖鏉¤褰曚腑锛岄儴闂ㄧ紪鍙蜂负50锛岃�宒ept琛ㄤ腑涓嶅瓨鍦ㄩ儴闂ㄧ紪鍙蜂负50鐨勮褰曪紝鎵�浠モ�滃紶涓夆�濊繖鏉¤褰曪紝涓嶈兘婊¤冻e.deptno=d.deptno杩欐潯浠躲�備絾鍦ㄥ乏杩炴帴涓紝鍥犱负emp琛ㄦ槸宸﹁〃锛屾墍浠ュ乏琛ㄤ腑鐨勮褰曢兘浼氭煡璇㈠嚭鏉ワ紝鍗斥�滃紶涓夆�濊繖鏉¤褰曚篃浼氭煡鍑猴紝浣嗙浉搴旂殑鍙宠〃閮ㄥ垎鏄剧ずNULL銆�

绗竴閮ㄥ垎 鏁版嵁搴撲箣SQL璇彞_第2张图片

SELECT <鍒楀悕>

FROM TABLE1 LEFT | RIGHT JOIN TABLE2

ON TABLE1.鍒�=TABLE2.鍒�

杩炴帴鍚庣殑琛ㄦ槸涓や釜琛ㄧ瑳鍗″皵绉殑缁撴灉锛屾敞鎰忎娇鐢ㄤ富澶栭敭鍏崇郴鍘婚櫎涓嶉渶瑕佺殑璁板綍


锛�3锛夎嚜鐒惰繛鎺�

鑷姩鎵惧埌涓诲閿叧绯伙紙鑷繁鎵惧埌杩炴帴鏉′欢锛�

SELECT * FROM emp NATURAL JOIN dept; 聽 聽//鍐呰繛鎺�

SELECT * FROM emp NATURAL LEFT JOIN dept; 聽 聽//宸﹂摼鎺�

SELECT * FROM emp NATURAL RIGHT JOIN dept; 聽 聽//鍙抽摼鎺�


锛�4锛夊瓙鏌ヨ锛堥噸鐐癸級

涓�涓猻elect璇彞涓寘鍚彟涓�涓畬鏁寸殑select璇彞銆�

瀛愭煡璇㈠氨鏄祵濂楁煡璇紝鍗砈ELECT涓寘鍚玈ELECT锛屽鏋滀竴鏉¤鍙ヤ腑瀛樺湪涓や釜锛屾垨涓や釜浠ヤ笂SELECT锛岄偅涔堝氨鏄瓙鏌ヨ璇彞浜嗐��

姹傛瘡涓粍閲屾渶澶у伐璧勭殑鍛樺伐

聽聽聽聽聽聽聽聽鍑虹幇鐨勯棶棰橈細褰撹柂姘村彧瑕佹弧瓒虫槸鍒嗙粍閲屼笁缁勪腑浠绘剰涓�缁勫伐璧勬渶澶у�煎氨鍙互閫夊嚭鏉ワ紝涔熷氨鏄绗竴缁勭殑鏌愪釜浜虹殑钖祫鍙兘鏄浜岀粍鐨勬渶澶у�硷紝杩欐椂涔熶細琚�変簡鍑烘潵

瑙e喅鍔炴硶锛氳繖鏄簲璇ュ皢瀛愭煡璇㈢粨鏋滄牴鎹儴闂ㄧ紪鍙峰拰鏈�澶у伐璧勫缓涓�寮犳柊琛紝鐒跺悗杩涜杩炴帴鏌ヨ

绗竴閮ㄥ垎 鏁版嵁搴撲箣SQL璇彞_第3张图片

瀛愭煡璇㈠父鍑虹幇鐨勪綅缃�锛�

where鍚庯紝浣滀负琚煡璇㈢殑涓�鏉′欢鐨勪竴閮ㄥ垎锛�

from鍚庯紝浣滆〃锛�

飪� 鍗曡鍗曞垪锛堢敤浜庢潯浠讹級

飪� 鍗曡澶氬垪锛堢敤浜庢潯浠讹級

飪� 澶氳鍗曞垪锛堢敤浜庢潯浠讹級

飪� 澶氳澶氬垪锛堢敤浜庤〃锛屽垎缁勶級

>all琛ㄧず澶т簬鎵�鏈� 聽 聽>any琛ㄧず澶т簬浠绘剰涓�涓紝鍙澶т簬涓�涓氨琛岋紙瀛愭煡璇綅浜巜here鍚庯級

exists鍜宨n 鐨勫尯鍒紝

exists鏄厛鎵ц涓绘煡璇㈢劧鍚庣湅涓绘煡璇㈢殑鏉′欢鏄惁鍦ㄥ瓙鏌ヨ涓瓨鍦�

in鏄厛鎵ц瀛愭煡璇紝鐒跺悗鐪嬩富鏌ヨ鐨勬潯浠舵槸鍚﹀湪瀛愭煡璇腑

娉細1.瀛愭煡璇㈢粨鏋滈泦灏戯紝涓绘煡璇㈢粨鏋滈泦澶氾紝鐢╥n

聽 聽 聽 2.瀛愭煡璇㈢粨鏋滈泦澶氾紝涓绘煡璇㈢粨鏋滈泦灏戯紝鐢╡xists

聽 聽 聽3 .in涓�鑸〃绀虹殑鏄煇涓�瀛楁鐨勬椂鍦ㄥ瓙鏌ヨ涓氨杩涜涓绘煡璇紝鑰宔xist琛ㄧず鐨勬槸鍙瀛愭煡璇㈡煡璇㈠埌浜嗘暟鎹氨杩涜涓绘煡璇�


渚嬮锛�

1. 聽 聽鏌ヨ宸ヨ祫鏈�楂樼殑浜哄悕鍜岃柂姘达細瀛愭煡璇㈡煡鍑烘渶楂樺伐璧勶紝涓绘煡璇㈡煡鍑烘湁鏈�楂樿柂姘寸殑浜哄悕鍜屽伐璧�

锛�1锛塖ELECT NAME,SAL 聽 聽FROM EMP 聽WHERE SAL=(SELECT MAX(SAL) FROM EMP)

鑻ヤ笉浣跨敤瀛愭煡璇細SELETE NAME,MAX(SAL) 聽杩欐牱鏄笉瀵圭殑锛屾渶澶ф暟鎹彧鏈変竴涓紝鎵�浠ヨ繖涓煡璇㈠彧鑳芥煡璇竴涓搴旂殑鍚嶅瓧锛岃�屽疄闄呬笂鏈�澶ц柂姘村搴旂殑涓嶆槸涓�涓汉

锛�2锛塖ELECT NAME,SAL 聽 聽FROM EMP 聽WHERE SAL>(SELECT MAX(SAL) FROM EMP)

锛�3锛塖ELECT NAME,SAL 聽 聽FROM EMP 聽WHERE SAL in(SELECT MAX(SAL) FROM EMP)

琛ㄧずsal鏄瓙鏌ヨ鏌ヨ鍑烘潵浼楀鍊肩殑涓�涓�

2.宸ヨ祫楂樹簬JONES鐨勫憳宸ャ��

SELECT * FROM emp WHERE sal > (SELECT sal FROM emp聽WHERE ename='JONES')

3.鏌ヨ宸ヤ綔鍜屽伐璧勪笌MARTIN锛堥┈涓侊級瀹屽叏鐩稿悓鐨勫憳宸ヤ俊鎭�(閲嶇偣锛侊紒锛�)

SELECT * FROM emp WHERE (job,sal) IN (SELECT job,sal聽FROM emp WHERE ename='MARTIN')

3.鏈�2涓互涓婄洿鎺ヤ笅灞炵殑鍛樺伐淇℃伅(褰撳瓙鏌ヨ鐨勭粨鏋滄煡璇㈢殑鏄垎缁勫悗鐨勭粨鏋滆繑鍥炵殑鏄鍒椾笉鍙敤绛夊彿鐢╥n)

SELECT * FROM emp WHERE empno IN(

SELECT mgr FROM emp GROUP BY mgr HAVING COUNT(mgr)>=2);

4.涓嶇敤缁勫嚱鏁帮紝姹傝柂姘存渶澶у�硷紙闈㈣瘯棰橈級

鎬濊矾锛氫娇鐢ㄨ嚜杩炴帴锛屾煡璇�1琛ㄤ腑宸ヨ祫涓皬浜�2琛ㄤ腑宸ヨ祫鐨勬暟缁勭粍鎴愭柊琛紝杩欐槸浠庡師琛ㄤ腑鏌ヨ宸ヨ祫涓嶅啀鏂拌〃閲岀殑鏁版嵁灏辨槸鏈�楂樺伐璧�

鎴栬�呰〃1鐨勬煇涓伐璧勫�煎ぇ浜庣瓑浜庤〃2涓墍鏈夌殑宸ヨ祫

5.鏌ヨ瀛楁涓簄ull鐨勪俊鎭�

SELECT * FROM EMP WHERE loc


灏嗘煡璇㈢粨鏋滄彃鍏ュ埌琛ㄤ腑

INSERT INTO 琛ㄥ悕 (column_list1)

SELECT (column_list2) FROM table_name2 where (condition)

column_list1琛ㄧず瑕佹彃鍏ュ摢浜涘垪

column_list2鐨勫瓧娈垫暟蹇呴』鍜宑olumn_list1鐩稿悓

你可能感兴趣的:(绗竴閮ㄥ垎 鏁版嵁搴撲箣SQL璇彞)