net start mysql
mysql -h å°å -P ç«¯å£ -u ç¨æ·å -p å¯ç
mysqld --skip-grant-tables
â ä¿®æ¹rootå¯ç
å¯ç å å¯å½æ°password()
update mysql.user set password=password(ârootâ);
SHOW PROCESSLIST â æ¾ç¤ºåªäºçº¿ç¨æ£å¨è¿è¡
SHOW VARIABLES â
------------------
â æ¥çå½åæ°æ®åº
select database();
â æ¾ç¤ºå½åæ¶é´ãç¨æ·åãæ°æ®åºçæ¬
select now(), user(), version();
â å建åº
create database[ if not exists] æ°æ®åºå æ°æ®åºé项
æ°æ®åºé项ï¼
CHARACTER SET charset_name
COLLATE collation_name
â æ¥çå·²æåº
show databases[ like âpatternâ]
â æ¥çå½ååºä¿¡æ¯
show create database æ°æ®åºå
â ä¿®æ¹åºçé项信æ¯
alter database åºå é项信æ¯
â å é¤åº
drop database[ if exists] æ°æ®åºå
åæ¶å é¤è¯¥æ°æ®åºç¸å ³çç®å½åå ¶ç®å½å 容
------------------
â å建表
create [temporary] table[ if not exists] [åºå.]表å ( 表çç»æå®ä¹ )[ 表é项]
æ¯ä¸ªåæ®µå¿ é¡»ææ°æ®ç±»å
æåä¸ä¸ªå段åä¸è½æéå·
temporary 临æ¶è¡¨ï¼ä¼è¯ç»ææ¶è¡¨èªå¨æ¶å¤±
对äºå段çå®ä¹ï¼
å段å æ°æ®ç±»å [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY] [COMMENT âstringâ]
â 表é项
â å符é
CHARSET = charset_name
å¦æ表没æ设å®ï¼å使ç¨æ°æ®åºå符é
â åå¨å¼æ
ENGINE = engine_name
表å¨ç®¡çæ°æ®æ¶éç¨çä¸åçæ°æ®ç»æï¼ç»æä¸åä¼å¯¼è´å¤çæ¹å¼ãæä¾çç¹æ§æä½çä¸å
常è§çå¼æï¼InnoDB MyISAM Memory/Heap BDB Merge Example CSV MaxDB Archive
ä¸åçå¼æå¨ä¿å表çç»æåæ°æ®æ¶éç¨ä¸åçæ¹å¼
MyISAM表æ件å«ä¹ï¼.frm表å®ä¹ï¼.MYD表æ°æ®ï¼.MYI表索å¼
InnoDB表æ件å«ä¹ï¼.frm表å®ä¹ï¼è¡¨ç©ºé´æ°æ®åæ¥å¿æ件
SHOW ENGINES â æ¾ç¤ºåå¨å¼æçç¶æä¿¡æ¯
SHOW ENGINE å¼æå {LOGS|STATUS} â æ¾ç¤ºåå¨å¼æçæ¥å¿æç¶æä¿¡æ¯
â æ°æ®æ件ç®å½
DATA DIRECTORY = âç®å½â
â ç´¢å¼æ件ç®å½
INDEX DIRECTORY = âç®å½â
â 表注é
COMMENT = âstringâ
â æ¥çææ表
SHOW TABLES[ LIKE âpatternâ]
SHOW TABLES FROM 表å
â æ¥ç表æºæ
SHOW CREATE TABLE 表å ï¼ä¿¡æ¯æ´è¯¦ç»ï¼
DESC 表å / DESCRIBE 表å / EXPLAIN 表å / SHOW COLUMNS FROM 表å [LIKE âPATTERNâ]
SHOW TABLE STATUS [FROM db_name] [LIKE âpatternâ]
â ä¿®æ¹è¡¨
â ä¿®æ¹è¡¨æ¬èº«çé项
ALTER TABLE 表å 表çé项
EG: ALTER TABLE 表å ENGINE=MYISAM;
â 对表è¿è¡éå½å
RENAME TABLE å表å TO æ°è¡¨å
RENAME TABLE å表å TO åºå.表å ï¼å¯å°è¡¨ç§»å¨å°å¦ä¸ä¸ªæ°æ®åºï¼
â RENAMEå¯ä»¥äº¤æ¢ä¸¤ä¸ªè¡¨å
â ä¿®æ¹è¡¨çå段æºæ
ALTER TABLE 表å æä½å
â æä½å
ADD[ COLUMN] å段å â å¢å å段
AFTER å段å â 表示å¢å å¨è¯¥å段ååé¢
FIRST â 表示å¢å å¨ç¬¬ä¸ä¸ª
ADD PRIMARY KEY(å段å) â å建主é®
ADD UNIQUE [ç´¢å¼å] (å段å)-- å建å¯ä¸ç´¢å¼
ADD INDEX [ç´¢å¼å] (å段å) â å建æ®éç´¢å¼
ADD
DROP[ COLUMN] å段å â å é¤å段
MODIFY[ COLUMN] å段å å段å±æ§ â æ¯æ对å段å±æ§è¿è¡ä¿®æ¹ï¼ä¸è½ä¿®æ¹å段å(ææåæå±æ§ä¹éåä¸)
CHANGE[ COLUMN] åå段å æ°å段å å段å±æ§ â æ¯æ对å段åä¿®æ¹
DROP PRIMARY KEY â å é¤ä¸»é®(å é¤ä¸»é®åéå é¤å ¶AUTO_INCREMENTå±æ§)
DROP INDEX ç´¢å¼å â å é¤ç´¢å¼
DROP FOREIGN KEY å¤é® â å é¤å¤é®
â å é¤è¡¨
DROP TABLE[ IF EXISTS] 表å â¦
â æ¸ ç©ºè¡¨æ°æ®
TRUNCATE [TABLE] 表å
â å¤å¶è¡¨ç»æ
CREATE TABLE 表å LIKE è¦å¤å¶ç表å
â å¤å¶è¡¨ç»æåæ°æ®
CREATE TABLE 表å [AS] SELECT * FROM è¦å¤å¶ç表å
â æ£æ¥è¡¨æ¯å¦æé误
CHECK TABLE tbl_name [, tbl_name] ⦠[option] â¦
â ä¼å表
OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] â¦
â ä¿®å¤è¡¨
REPAIR [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ⦠[QUICK] [EXTENDED] [USE_FRM]
â åæ表
ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] â¦
------------------
â å¢
INSERT [INTO] 表å [(å段å表)] VALUES (å¼å表)[, (å¼å表), â¦]
â å¦æè¦æå ¥çå¼å表å å«ææå段并ä¸é¡ºåºä¸è´ï¼åå¯ä»¥çç¥å段å表ã
â å¯åæ¶æå ¥å¤æ¡æ°æ®è®°å½ï¼
REPLACE ä¸ INSERT å®å ¨ä¸æ ·ï¼å¯äºæ¢ã
INSERT [INTO] 表å SET å段å=å¼[, å段å=å¼, â¦]
â æ¥
SELECT å段å表 FROM 表å[ å ¶ä»åå¥]
â å¯æ¥èªå¤ä¸ªè¡¨çå¤ä¸ªå段
â å ¶ä»åå¥å¯ä»¥ä¸ä½¿ç¨
â å段å表å¯ä»¥ç¨*代æ¿ï¼è¡¨ç¤ºææå段
â å
DELETE FROM 表å[ å é¤æ¡ä»¶åå¥]
没ææ¡ä»¶åå¥ï¼åä¼å é¤å ¨é¨
â æ¹
UPDATE 表å SET å段å=æ°å¼[, å段å=æ°å¼] [æ´æ°æ¡ä»¶]
------------------
â MySQLãæ°æ®åºã表ãå段åå¯è®¾ç½®ç¼ç
â æ°æ®ç¼ç ä¸å®¢æ·ç«¯ç¼ç ä¸éä¸è´
SHOW VARIABLES LIKE âcharacter_set_%â â æ¥çææå符éç¼ç 项
character_set_client 客æ·ç«¯åæå¡å¨åéæ°æ®æ¶ä½¿ç¨çç¼ç
character_set_results æå¡å¨ç«¯å°ç»æè¿åç»å®¢æ·ç«¯æ使ç¨çç¼ç
character_set_connection è¿æ¥å±ç¼ç
SET åéå = åéå¼
set character_set_client = gbk;
set character_set_results = gbk;
set character_set_connection = gbk;
SET NAMES GBK; â ç¸å½äºå®æ以ä¸ä¸ä¸ªè®¾ç½®
â æ ¡å¯¹é
æ ¡å¯¹éç¨ä»¥æåº
SHOW CHARACTER SET [LIKE 'pattern']/SHOW CHARSET [LIKE 'pattern'] æ¥çææå符é
SHOW COLLATION [LIKE 'pattern'] æ¥çæææ ¡å¯¹é
charset å符éç¼ç 设置å符éç¼ç
collate æ ¡å¯¹éç¼ç è®¾ç½®æ ¡å¯¹éç¼ç
------------------
- æ°å¼ç±»å
â a. æ´å ---------
ç±»å åè èå´ï¼æ符å·ä½ï¼
tinyint 1åè -128 ~ 127 æ 符å·ä½ï¼0 ~ 255
smallint 2åè -32768 ~ 32767
mediumint 3åè -8388608 ~ 8388607
int 4åè
bigint 8åè
int(M) M表示æ»ä½æ°
- é»è®¤åå¨ç¬¦å·ä½ï¼unsigned å±æ§ä¿®æ¹
- æ¾ç¤ºå®½åº¦ï¼å¦ææ个æ°ä¸å¤å®ä¹å段æ¶è®¾ç½®çä½æ°ï¼ååé¢ä»¥0补填ï¼zerofill å±æ§ä¿®æ¹
ä¾ï¼int(5) æå ¥ä¸ä¸ªæ°â123âï¼è¡¥å¡«å为â00123â
- å¨æ»¡è¶³è¦æ±çæ åµä¸ï¼è¶å°è¶å¥½ã
- 1表示boolå¼çï¼0表示boolå¼åãMySQL没æå¸å°ç±»åï¼éè¿æ´å0å1表示ã常ç¨tinyint(1)表示å¸å°åã
â b. æµ®ç¹å ----------
ç±»å åè èå´
float(å精度) 4åè
double(å精度) 8åè
æµ®ç¹åæ¢æ¯æ符å·ä½ unsigned å±æ§ï¼ä¹æ¯ææ¾ç¤ºå®½åº¦ zerofill å±æ§ã
ä¸åäºæ´åï¼åååä¼è¡¥å¡«0.
å®ä¹æµ®ç¹åæ¶ï¼éæå®æ»ä½æ°åå°æ°ä½æ°ã
float(M, D) double(M, D)
M表示æ»ä½æ°ï¼D表示å°æ°ä½æ°ã
MåDç大å°ä¼å³å®æµ®ç¹æ°çèå´ãä¸åäºæ´åçåºå®èå´ã
Mæ¢è¡¨ç¤ºæ»ä½æ°ï¼ä¸å æ¬å°æ°ç¹åæ£è´å·ï¼ï¼ä¹è¡¨ç¤ºæ¾ç¤ºå®½åº¦ï¼æææ¾ç¤ºç¬¦å·åå æ¬ï¼ã
æ¯æç§å¦è®¡æ°æ³è¡¨ç¤ºã
æµ®ç¹æ°è¡¨ç¤ºè¿ä¼¼å¼ã
â c. å®ç¹æ° ----------
decimal â å¯åé¿åº¦
decimal(M, D) Mä¹è¡¨ç¤ºæ»ä½æ°ï¼D表示å°æ°ä½æ°ã
ä¿åä¸ä¸ªç²¾ç¡®çæ°å¼ï¼ä¸ä¼åçæ°æ®çæ¹åï¼ä¸åäºæµ®ç¹æ°çåèäºå ¥ã
å°æµ®ç¹æ°è½¬æ¢ä¸ºå符串æ¥ä¿åï¼æ¯9ä½æ°åä¿å为4个åèã
- å符串类å
â a. char, varchar ----------
char å®é¿å符串ï¼é度快ï¼ä½æµªè´¹ç©ºé´
varchar åé¿å符串ï¼éåº¦æ ¢ï¼ä½èç空é´
M表示è½åå¨çæ大é¿åº¦ï¼æ¤é¿åº¦æ¯å符æ°ï¼éåèæ°ã
ä¸åçç¼ç ï¼æå ç¨ç空é´ä¸åã
char,æå¤255个å符ï¼ä¸ç¼ç æ å ³ã
varchar,æå¤65535å符ï¼ä¸ç¼ç æå ³ã
ä¸æ¡ææè®°å½æ大ä¸è½è¶ è¿65535个åèã
utf8 æ大为21844个å符ï¼gbk æ大为32766个å符ï¼latin1 æ大为65532个å符
varchar æ¯åé¿çï¼éè¦å©ç¨åå¨ç©ºé´ä¿å varchar çé¿åº¦ï¼å¦ææ°æ®å°äº255个åèï¼åéç¨ä¸ä¸ªåèæ¥ä¿åé¿åº¦ï¼åä¹éè¦ä¸¤ä¸ªåèæ¥ä¿åã
varchar çæ大ææé¿åº¦ç±æ大è¡å¤§å°å使ç¨çå符éç¡®å®ã
æ大ææé¿åº¦æ¯65532åèï¼å 为å¨varcharåå符串æ¶ï¼ç¬¬ä¸ä¸ªåèæ¯ç©ºçï¼ä¸åå¨ä»»ä½æ°æ®ï¼ç¶åè¿é两个åèæ¥åæ¾å符串çé¿åº¦ï¼æ以ææé¿åº¦æ¯64432-1-2=65532åèã
ä¾ï¼è¥ä¸ä¸ªè¡¨å®ä¹ä¸º CREATE TABLE tb(c1 int, c2 char(30), c3 varchar(N)) charset=utf8; é®Nçæ大å¼æ¯å¤å°ï¼ çï¼(65535-1-2-4-30*3)/3
â b. blob, text ----------
blob äºè¿å¶å符串ï¼åèå符串ï¼
tinyblob, blob, mediumblob, longblob
text éäºè¿å¶å符串ï¼å符å符串ï¼
tinytext, text, mediumtext, longtext
text å¨å®ä¹æ¶ï¼ä¸éè¦å®ä¹é¿åº¦ï¼ä¹ä¸ä¼è®¡ç®æ»é¿åº¦ã
text ç±»åå¨å®ä¹æ¶ï¼ä¸å¯ç»defaultå¼
â c. binary, varbinary ----------
类似äºcharåvarcharï¼ç¨äºä¿åäºè¿å¶å符串ï¼ä¹å°±æ¯ä¿ååèå符串èéå符å符串ã
char, varchar, text å¯¹åº binary, varbinary, blob.
- æ¥ææ¶é´ç±»å
ä¸è¬ç¨æ´åä¿åæ¶é´æ³ï¼å 为PHPå¯ä»¥å¾æ¹ä¾¿çå°æ¶é´æ³è¿è¡æ ¼å¼åã
datetime 8åè æ¥æåæ¶é´ 1000-01-01 00:00:00 å° 9999-12-31 23:59:59
date 3åè æ¥æ 1000-01-01 å° 9999-12-31
timestamp 4åè æ¶é´æ³ 19700101000000 å° 2038-01-19 03:14:07
time 3åè æ¶é´ -838:59:59 å° 838:59:59
year 1åè 年份 1901 - 2155
datetime âYYYY-MM-DD hh:mm:ssâ
timestamp âYY-MM-DD hh:mm:ssâ
âYYYYMMDDhhmmssâ
âYYMMDDhhmmssâ
YYYYMMDDhhmmss
YYMMDDhhmmss
date âYYYY-MM-DDâ
âYY-MM-DDâ
âYYYYMMDDâ
âYYMMDDâ
YYYYMMDD
YYMMDD
time âhh:mm:ssâ
âhhmmssâ
hhmmss
year âYYYYâ
âYYâ
YYYY
YY
- æ举åéå
â æ举(enum) ----------
enum(val1, val2, val3â¦)
å¨å·²ç¥çå¼ä¸è¿è¡åéãæ大æ°é为65535.
æ举å¼å¨ä¿åæ¶ï¼ä»¥2个åèçæ´å(smallint)ä¿åãæ¯ä¸ªæ举å¼ï¼æä¿åçä½ç½®é¡ºåºï¼ä»1å¼å§éä¸éå¢ã
表ç°ä¸ºå符串类åï¼åå¨å´æ¯æ´åã
NULLå¼çç´¢å¼æ¯NULLã
空å符串é误å¼çç´¢å¼å¼æ¯0ã
â éåï¼setï¼ ----------
set(val1, val2, val3â¦)
create table tab ( gender set(âç·â, â女â, âæ â) );
insert into tab values (âç·, 女â);
æå¤å¯ä»¥æ64个ä¸åçæåã以bigintåå¨ï¼å ±8个åèãéåä½è¿ç®çå½¢å¼ã
å½å建表æ¶ï¼SETæåå¼çå°¾é¨ç©ºæ ¼å°èªå¨è¢«å é¤ã
â PHPè§åº¦
- åè½æ»¡è¶³
- åå¨ç©ºé´å°½éå°ï¼å¤çæçæ´é«
- èèå ¼å®¹é®é¢
â IPåå¨ ----------
- åªéåå¨ï¼å¯ç¨å符串
- å¦æé计ç®ï¼æ¥æ¾çï¼å¯åå¨ä¸º4个åèçæ 符å·intï¼å³unsigned
- PHPå½æ°è½¬æ¢
ip2longå¯è½¬æ¢ä¸ºæ´åï¼ä½ä¼åºç°æºå¸¦ç¬¦å·é®é¢ãéæ ¼å¼å为æ 符å·çæ´åã
å©ç¨sprintfå½æ°æ ¼å¼åå符串
sprintf("%u", ip2long(â192.168.3.134â));
ç¶åç¨long2ipå°æ´å转åIPå符串
- MySQLå½æ°è½¬æ¢(æ 符å·æ´åï¼UNSIGNED)
INET_ATON(â127.0.0.1â) å°IP转为æ´å
INET_NTOA(2130706433) å°æ´å转为IP
------------------
- 主é®
- è½å¯ä¸æ è¯è®°å½çå段ï¼å¯ä»¥ä½ä¸ºä¸»é®ã
- ä¸ä¸ªè¡¨åªè½æä¸ä¸ªä¸»é®ã
- 主é®å ·æå¯ä¸æ§ã
-
声æå段æ¶ï¼ç¨ primary key æ è¯ã
ä¹å¯ä»¥å¨å段å表ä¹å声æ
ä¾ï¼create table tab ( id int, stu varchar(10), primary key (id));
- 主é®å段çå¼ä¸è½ä¸ºnullã
-
主é®å¯ä»¥ç±å¤ä¸ªåæ®µå ±åç»æãæ¤æ¶éè¦å¨å段å表å声æçæ¹æ³ã
ä¾ï¼create table tab ( id int, stu varchar(10), age int, primary key (stu, age));
- unique å¯ä¸ç´¢å¼ï¼å¯ä¸çº¦æï¼
使å¾æå段çå¼ä¹ä¸è½éå¤ã
3. null 约æ
nullä¸æ¯æ°æ®ç±»åï¼æ¯åçä¸ä¸ªå±æ§ã
表示å½ååæ¯å¦å¯ä»¥ä¸ºnullï¼è¡¨ç¤ºä»ä¹é½æ²¡æã
null, å
许为空ãé»è®¤ã
not null, ä¸å
许为空ã
insert into tab values (null, 'val');
-- æ¤æ¶è¡¨ç¤ºå°ç¬¬ä¸ä¸ªå段çå¼è®¾ä¸ºnull, åå³äºè¯¥å段æ¯å¦å
许为null
- default é»è®¤å¼å±æ§
å½åå段çé»è®¤å¼ã
insert into tab values (default, âvalâ); â æ¤æ¶è¡¨ç¤ºå¼ºå¶ä½¿ç¨é»è®¤å¼ã
create table tab ( add_time timestamp default current_timestamp );
â 表示å°å½åæ¶é´çæ¶é´æ³è®¾ä¸ºé»è®¤å¼ã
current_date, current_time
- auto_increment èªå¨å¢é¿çº¦æ
èªå¨å¢é¿å¿ 须为索å¼ï¼ä¸»é®æuniqueï¼
åªè½åå¨ä¸ä¸ªå段为èªå¨å¢é¿ã
é»è®¤ä¸º1å¼å§èªå¨å¢é¿ãå¯ä»¥éè¿è¡¨å±æ§ auto_increment = xè¿è¡è®¾ç½®ï¼æ alter table tbl auto_increment = x;
- comment 注é
ä¾ï¼create table tab ( id int ) comment â注éå 容â;
- foreign key å¤é®çº¦æ
ç¨äºéå¶ä¸»è¡¨ä¸ä»è¡¨æ°æ®å®æ´æ§ã
alter table t1 add constraint
t1_t2_fk
foreign key (t1_id) references t2(id);
â å°è¡¨t1çt1_idå¤é®å ³èå°è¡¨t2çidå段ã
â æ¯ä¸ªå¤é®é½æä¸ä¸ªååï¼å¯ä»¥éè¿ constraint æå®
åå¨å¤é®ç表ï¼ç§°ä¹ä¸ºä»è¡¨ï¼å表ï¼ï¼å¤é®æåç表ï¼ç§°ä¹ä¸ºä¸»è¡¨ï¼ç¶è¡¨ï¼ã
ä½ç¨ï¼ä¿ææ°æ®ä¸è´æ§ï¼å®æ´æ§ï¼ä¸»è¦ç®çæ¯æ§å¶åå¨å¨å¤é®è¡¨ï¼ä»è¡¨ï¼ä¸çæ°æ®ã
MySQLä¸ï¼å¯ä»¥å¯¹InnoDBå¼æ使ç¨å¤é®çº¦æï¼
è¯æ³ï¼
foreign key (å¤é®åæ®µï¼ references 主表å (å ³èå段) [主表记å½å é¤æ¶çå¨ä½] [主表记å½æ´æ°æ¶çå¨ä½]
æ¤æ¶éè¦æ£æµä¸ä¸ªä»è¡¨çå¤é®éè¦çº¦æ为主表çå·²åå¨çå¼ãå¤é®å¨æ²¡æå ³èçæ åµä¸ï¼å¯ä»¥è®¾ç½®ä¸ºnull.åææ¯è¯¥å¤é®åï¼æ²¡ænot nullã
å¯ä»¥ä¸æå®ä¸»è¡¨è®°å½æ´æ¹ææ´æ°æ¶çå¨ä½ï¼é£ä¹æ¤æ¶ä¸»è¡¨çæä½è¢«æç»ã
å¦ææå®äº on update æ on deleteï¼å¨å é¤ææ´æ°æ¶ï¼æå¦ä¸å 个æä½å¯ä»¥éæ©ï¼
- cascadeï¼çº§èæä½ã主表æ°æ®è¢«æ´æ°ï¼ä¸»é®å¼æ´æ°ï¼ï¼ä»è¡¨ä¹è¢«æ´æ°ï¼å¤é®å¼æ´æ°ï¼ã主表记å½è¢«å é¤ï¼ä»è¡¨ç¸å ³è®°å½ä¹è¢«å é¤ã
- set nullï¼è®¾ç½®ä¸ºnullã主表æ°æ®è¢«æ´æ°ï¼ä¸»é®å¼æ´æ°ï¼ï¼ä»è¡¨çå¤é®è¢«è®¾ç½®ä¸ºnullã主表记å½è¢«å é¤ï¼ä»è¡¨ç¸å ³è®°å½å¤é®è¢«è®¾ç½®ænullãä½æ³¨æï¼è¦æ±è¯¥å¤é®åï¼æ²¡ænot nullå±æ§çº¦æã
- restrictï¼æç»ç¶è¡¨å é¤åæ´æ°ã
注æï¼å¤é®åªè¢«InnoDBåå¨å¼æææ¯æãå ¶ä»å¼ææ¯ä¸æ¯æçã
------------------
â Normal Format, NF
- æ¯ä¸ªè¡¨ä¿åä¸ä¸ªå®ä½ä¿¡æ¯
- æ¯ä¸ªå ·æä¸ä¸ªIDå段ä½ä¸ºä¸»é®
- IDä¸»é® + åå表
â 1NF, 第ä¸èå¼
å段ä¸è½ååï¼å°±æ»¡è¶³ç¬¬ä¸èå¼ã
â 2NF, 第äºèå¼
满足第ä¸èå¼çåæä¸ï¼ä¸è½åºç°é¨åä¾èµã
æ¶é¤ç¬¦å主é®å°±å¯ä»¥é¿å é¨åä¾èµãå¢å ååå ³é®åã
â 3NF, 第ä¸èå¼
满足第äºèå¼çåæä¸ï¼ä¸è½åºç°ä¼ éä¾èµã
æ个å段ä¾èµäºä¸»é®ï¼èæå ¶ä»å段ä¾èµäºè¯¥å段ãè¿å°±æ¯ä¼ éä¾èµã
å°ä¸ä¸ªå®ä½ä¿¡æ¯çæ°æ®æ¾å¨ä¸ä¸ªè¡¨å å®ç°ã
------------------
select [all|distinct] select_expr from -> where -> group by [å计å½æ°] -> having -> order by -> limit
a. select_expr
â å¯ä»¥ç¨ * 表示ææå段ã
select * from tb;
â å¯ä»¥ä½¿ç¨è¡¨è¾¾å¼ï¼è®¡ç®å ¬å¼ãå½æ°è°ç¨ãå段ä¹æ¯ä¸ªè¡¨è¾¾å¼ï¼
select stu, 29+25, now() from tb;
â å¯ä»¥ä¸ºæ¯ä¸ªå使ç¨å«åãéç¨äºç®ååæ è¯ï¼é¿å å¤ä¸ªåæ è¯ç¬¦éå¤ã
- ä½¿ç¨ as å ³é®åï¼ä¹å¯çç¥ as.
select stu+10 as add10 from tb;
b. from åå¥
ç¨äºæ è¯æ¥è¯¢æ¥æºã
â å¯ä»¥ä¸ºè¡¨èµ·å«åã使ç¨aså ³é®åã
select * from tb1 as tt, tb2 as bb;
â fromåå¥åï¼å¯ä»¥åæ¶åºç°å¤ä¸ªè¡¨ã
â å¤ä¸ªè¡¨ä¼æ¨ªåå å å°ä¸èµ·ï¼èæ°æ®ä¼å½¢æä¸ä¸ªç¬å¡å°ç§¯ã
select * from tb1, tb2;
c. where åå¥
â ä»fromè·å¾çæ°æ®æºä¸è¿è¡çéã
â æ´å1表示çï¼0表示åã
â 表达å¼ç±è¿ç®ç¬¦åè¿ç®æ°ç»æã
â è¿ç®æ°ï¼åéï¼å段ï¼ãå¼ãå½æ°è¿åå¼
â è¿ç®ç¬¦ï¼
=, <=>, <>, !=, <=, <, >=, >, !, &&, ||,
in (not) null, (not) like, (not) in, (not) between and, is (not), and, or, not, xor
is/is not å ä¸ture/false/unknownï¼æ£éªæ个å¼ççå
<=>ä¸<>åè½ç¸åï¼<=>å¯ç¨äºnullæ¯è¾
d. group by åå¥, åç»åå¥
group by å段/å«å [æåºæ¹å¼]
åç»åä¼è¿è¡æåºãååºï¼ASCï¼éåºï¼DESC
以ä¸[å计å½æ°]éé
å group by 使ç¨ï¼
count è¿åä¸åçéNULLå¼æ°ç® count(*)ãcount(å段)
sum æ±å
max æ±æ大å¼
min æ±æå°å¼
avg æ±å¹³åå¼
group_concat è¿å带ææ¥èªä¸ä¸ªç»çè¿æ¥çéNULLå¼çå符串ç»æãç»å
å符串è¿æ¥ã
e. having åå¥ï¼æ¡ä»¶åå¥
ä¸ where åè½ãç¨æ³ç¸åï¼æ§è¡æ¶æºä¸åã
where å¨å¼å§æ¶æ§è¡æ£æµæ°æ®ï¼å¯¹åæ°æ®è¿è¡è¿æ»¤ã
having 对çéåºçç»æå次è¿è¡è¿æ»¤ã
having åæ®µå¿ é¡»æ¯æ¥è¯¢åºæ¥çï¼where åæ®µå¿ é¡»æ¯æ°æ®è¡¨åå¨çã
where ä¸å¯ä»¥ä½¿ç¨å段çå«åï¼having å¯ä»¥ãå 为æ§è¡WHERE代ç æ¶ï¼å¯è½å°æªç¡®å®åå¼ã
where ä¸å¯ä»¥ä½¿ç¨å计å½æ°ãä¸è¬éç¨å计å½æ°æä¼ç¨ having
SQLæ åè¦æ±HAVINGå¿ é¡»å¼ç¨GROUP BYåå¥ä¸çåæç¨äºå计å½æ°ä¸çåã
f. order by åå¥ï¼æåºåå¥
order by æåºå段/å«å æåºæ¹å¼ [,æåºå段/å«å æåºæ¹å¼]â¦
ååºï¼ASCï¼éåºï¼DESC
æ¯æå¤ä¸ªå段çæåºã
g. limit åå¥ï¼éå¶ç»ææ°éåå¥
ä» å¯¹å¤ç好çç»æè¿è¡æ°ééå¶ãå°å¤ç好çç»æççä½æ¯ä¸ä¸ªéåï¼æç §è®°å½åºç°ç顺åºï¼ç´¢å¼ä»0å¼å§ã
limit èµ·å§ä½ç½®, è·åæ¡æ°
çç¥ç¬¬ä¸ä¸ªåæ°ï¼è¡¨ç¤ºä»ç´¢å¼0å¼å§ãlimit è·åæ¡æ°
h. distinct, all é项
distinct å»é¤éå¤è®°å½
é»è®¤ä¸º all, å ¨é¨è®°å½
------------------
å°å¤ä¸ªselectæ¥è¯¢çç»æç»åæä¸ä¸ªç»æéåã
SELECT ⦠UNION [ALL|DISTINCT] SELECT â¦
é»è®¤ DISTINCT æ¹å¼ï¼å³ææè¿åçè¡é½æ¯å¯ä¸ç
建议ï¼å¯¹æ¯ä¸ªSELECTæ¥è¯¢å ä¸å°æ¬å·å 裹ã
ORDER BY æåºæ¶ï¼éå ä¸ LIMIT è¿è¡ç»åã
éè¦åselectæ¥è¯¢çå段æ°éä¸æ ·ã
æ¯ä¸ªselectæ¥è¯¢çå段å表(æ°éãç±»å)åºä¸è´ï¼å 为ç»æä¸çå段å以第ä¸æ¡selectè¯å¥ä¸ºåã
------------------
-
åæ¥è¯¢éç¨æ¬å·å 裹ã
â fromå
fromåè¦æ±æ¯ä¸ä¸ªè¡¨ï¼å¿ é¡»ç»åæ¥è¯¢ç»æå个å«åã
- ç®åæ¯ä¸ªæ¥è¯¢å çæ¡ä»¶ã
- fromåéå°ç»æçæä¸ä¸ªä¸´æ¶è¡¨æ ¼ï¼å¯ç¨ä»¥å表çéå®çéæ¾ã
- åæ¥è¯¢è¿åä¸ä¸ªè¡¨ï¼è¡¨ååæ¥è¯¢ã
select * from (select * from tb where id>0) as subfrom where id>1;
â whereå
- åæ¥è¯¢è¿åä¸ä¸ªå¼ï¼æ éåæ¥è¯¢ã
- ä¸éè¦ç»åæ¥è¯¢åå«åã
-
whereåæ¥è¯¢å ç表ï¼ä¸è½ç´æ¥ç¨ä»¥æ´æ°ã
select * from tb where money = (select max(money) from tb);
â ååæ¥è¯¢
å¦æåæ¥è¯¢ç»æè¿åçæ¯ä¸åã
ä½¿ç¨ in æ not in å®ææ¥è¯¢
exists å not exists æ¡ä»¶
å¦æåæ¥è¯¢è¿åæ°æ®ï¼åè¿å1æ0ã常ç¨äºå¤ææ¡ä»¶ã
select column1 from t1 where exists (select * from t2);
â è¡åæ¥è¯¢
æ¥è¯¢æ¡ä»¶æ¯ä¸ä¸ªè¡ã
select * from t1 where (id, gender) in (select id, gender from t2);
è¡æé 符ï¼(col1, col2, â¦) æ ROW(col1, col2, â¦)
è¡æé 符é常ç¨äºä¸å¯¹è½è¿å两个æ两个以ä¸åçåæ¥è¯¢è¿è¡æ¯è¾ã
-- ç¹æ®è¿ç®ç¬¦
!= all() ç¸å½äº not in
= some() ç¸å½äº inãany æ¯ some çå«å
!= some() ä¸çåäº not inï¼ä¸çäºå
¶ä¸æä¸ä¸ªã
all, some å¯ä»¥é
åå
¶ä»è¿ç®ç¬¦ä¸èµ·ä½¿ç¨ã
------------------
å°å¤ä¸ªè¡¨çå段è¿è¡è¿æ¥ï¼å¯ä»¥æå®è¿æ¥æ¡ä»¶ã
â å è¿æ¥(inner join)
- é»è®¤å°±æ¯å è¿æ¥ï¼å¯çç¥innerã
- åªææ°æ®åå¨æ¶æè½åéè¿æ¥ãå³è¿æ¥ç»æä¸è½åºç°ç©ºè¡ã
on 表示è¿æ¥æ¡ä»¶ãå ¶æ¡ä»¶è¡¨è¾¾å¼ä¸where类似ãä¹å¯ä»¥çç¥æ¡ä»¶ï¼è¡¨ç¤ºæ¡ä»¶æ°¸è¿ä¸ºçï¼
ä¹å¯ç¨where表示è¿æ¥æ¡ä»¶ã
è¿æ using, ä½éå段åç¸åã using(å段å)
-- 交åè¿æ¥ cross join
å³ï¼æ²¡ææ¡ä»¶çå
è¿æ¥ã
select * from tb1 cross join tb2;
â å¤è¿æ¥(outer join)
- å¦ææ°æ®ä¸åå¨ï¼ä¹ä¼åºç°å¨è¿æ¥ç»æä¸ã
â å·¦å¤è¿æ¥ left join
å¦ææ°æ®ä¸åå¨ï¼å·¦è¡¨è®°å½ä¼åºç°ï¼èå³è¡¨ä¸ºnullå¡«å
â å³å¤è¿æ¥ right join
å¦ææ°æ®ä¸åå¨ï¼å³è¡¨è®°å½ä¼åºç°ï¼è左表为nullå¡«å
â èªç¶è¿æ¥(natural join)
èªå¨å¤æè¿æ¥æ¡ä»¶å®æè¿æ¥ã
ç¸å½äºçç¥äºusingï¼ä¼èªå¨æ¥æ¾ç¸åå段åã
natural join
natural left join
natural right join
select info.id, info.name, info.stu_num, extra_info.hobby, extra_info.sex from info, extra_info where info.stu_num = extra_info.stu_id;
------------------
select * into outfile æ件å°å [æ§å¶æ ¼å¼] from 表å; â 导åºè¡¨æ°æ®
load data [local] infile æ件å°å [replace|ignore] into table 表å [æ§å¶æ ¼å¼]; â å¯¼å ¥æ°æ®
çæçæ°æ®é»è®¤çåé符æ¯å¶è¡¨ç¬¦
localæªæå®ï¼åæ°æ®æä»¶å¿ é¡»å¨æå¡å¨ä¸
replace å ignore å ³é®è¯æ§å¶å¯¹ç°æçå¯ä¸é®è®°å½çéå¤çå¤ç
â æ§å¶æ ¼å¼
fields æ§å¶åæ®µæ ¼å¼
é»è®¤ï¼fields terminated by â\tâ enclosed by ââ escaped by â\â
terminated by âstringâ â ç»æ¢
enclosed by âcharâ â å 裹
escaped by âcharâ â 转ä¹
â 示ä¾ï¼
SELECT a,b,a+b INTO OUTFILE â/tmp/result.textâ
FIELDS TERMINATED BY â,â OPTIONALLY ENCLOSED BY â"â
LINES TERMINATED BY â\nâ
FROM test_table;
lines æ§å¶è¡æ ¼å¼
é»è®¤ï¼lines terminated by â\nâ
terminated by âstringâ â ç»æ¢
------------------
selectè¯å¥è·å¾çæ°æ®å¯ä»¥ç¨insertæå ¥ã
å¯ä»¥çç¥å¯¹åçæå®ï¼è¦æ± values () æ¬å·å ï¼æä¾ç»äºæç §å顺åºåºç°çææå段çå¼ã
æè 使ç¨setè¯æ³ã
insert into tbl_name set field=value,â¦ï¼
å¯ä»¥ä¸æ¬¡æ§ä½¿ç¨å¤ä¸ªå¼ï¼éç¨(), (), ();çå½¢å¼ã
insert into tbl_name values (), (), ();
å¯ä»¥å¨åå¼æå®æ¶ï¼ä½¿ç¨è¡¨è¾¾å¼ã
insert into tbl_name values (field_value, 10+10, now());
å¯ä»¥ä½¿ç¨ä¸ä¸ªç¹æ®å¼ defaultï¼è¡¨ç¤ºè¯¥å使ç¨é»è®¤å¼ã
insert into tbl_name values (field_value, default);
å¯ä»¥éè¿ä¸ä¸ªæ¥è¯¢çç»æï¼ä½ä¸ºéè¦æå ¥çå¼ã
insert into tbl_name select â¦;
å¯ä»¥æå®å¨æå ¥çå¼åºç°ä¸»é®ï¼æå¯ä¸ç´¢å¼ï¼å²çªæ¶ï¼æ´æ°å ¶ä»é主é®åçä¿¡æ¯ã
insert into tbl_name values/set/select on duplicate key update å段=å¼, â¦;
------------------
DELETE FROM tbl_name [WHERE where_definition] [ORDER BY â¦] [LIMIT row_count]
æç §æ¡ä»¶å é¤
æå®å é¤çæå¤è®°å½æ°ãLimit
å¯ä»¥éè¿æåºæ¡ä»¶å é¤ãorder by + limit
æ¯æå¤è¡¨å é¤ï¼ä½¿ç¨ç±»ä¼¼è¿æ¥è¯æ³ã
delete from éè¦å é¤æ°æ®å¤è¡¨1ï¼è¡¨2 using 表è¿æ¥æä½ æ¡ä»¶ã
------------------
TRUNCATE [TABLE] tbl_name
æ¸ ç©ºæ°æ®
å é¤é建表
åºå«ï¼
1ï¼truncate æ¯å é¤è¡¨åå建ï¼delete æ¯éæ¡å é¤
2ï¼truncate éç½®auto_incrementçå¼ãèdeleteä¸ä¼
3ï¼truncate ä¸ç¥éå é¤äºå æ¡ï¼èdeleteç¥éã
4ï¼å½è¢«ç¨äºå¸¦ååºç表æ¶ï¼truncate ä¼ä¿çååº
------------------
å¤ä»½ï¼å°æ°æ®çç»æä¸è¡¨å æ°æ®ä¿åèµ·æ¥ã
å©ç¨ mysqldump æ令å®æã
â 导åº
-
导åºä¸å¼ 表
ããmysqldump -uç¨æ·å -på¯ç åºå 表å > æ件å(D:/a.sql)
-
导åºå¤å¼ 表
ããmysqldump -uç¨æ·å -på¯ç åºå 表1 表2 表3 > æ件å(D:/a.sql)
-
导åºææ表
ããmysqldump -uç¨æ·å -på¯ç åºå > æ件å(D:/a.sql)
-
导åºä¸ä¸ªåº
ããmysqldump -uç¨æ·å -på¯ç -B åºå > æ件å(D:/a.sql)
å¯ä»¥-wæºå¸¦å¤ä»½æ¡ä»¶
â å¯¼å ¥
-
å¨ç»å½mysqlçæ åµä¸ï¼
ããsource å¤ä»½æ件
-
å¨ä¸ç»å½çæ åµä¸
ããmysql -uç¨æ·å -på¯ç åºå < å¤ä»½æ件
------------------
ä»ä¹æ¯è§å¾ï¼
è§å¾æ¯ä¸ä¸ªèæ表ï¼å ¶å 容ç±æ¥è¯¢å®ä¹ãåçå®ç表ä¸æ ·ï¼è§å¾å å«ä¸ç³»å带æå称çååè¡æ°æ®ãä½æ¯ï¼è§å¾å¹¶ä¸å¨æ°æ®åºä¸ä»¥åå¨çæ°æ®å¼éå½¢å¼åå¨ãè¡ååæ°æ®æ¥èªç±å®ä¹è§å¾çæ¥è¯¢æå¼ç¨ç表ï¼å¹¶ä¸å¨å¼ç¨è§å¾æ¶å¨æçæã
è§å¾å ·æ表ç»ææ件ï¼ä½ä¸åå¨æ°æ®æ件ã
å¯¹å ¶ä¸æå¼ç¨çåºç¡è¡¨æ¥è¯´ï¼è§å¾çä½ç¨ç±»ä¼¼äºçéãå®ä¹è§å¾ççéå¯ä»¥æ¥èªå½åæå ¶å®æ°æ®åºçä¸ä¸ªæå¤ä¸ªè¡¨ï¼æè å ¶å®è§å¾ãéè¿è§å¾è¿è¡æ¥è¯¢æ²¡æä»»ä½éå¶ï¼éè¿å®ä»¬è¿è¡æ°æ®ä¿®æ¹æ¶çéå¶ä¹å¾å°ã
è§å¾æ¯åå¨å¨æ°æ®åºä¸çæ¥è¯¢çsqlè¯å¥ï¼å®ä¸»è¦åºäºä¸¤ç§åå ï¼å®å ¨åå ï¼è§å¾å¯ä»¥éèä¸äºæ°æ®ï¼å¦ï¼ç¤¾ä¼ä¿é©åºé表ï¼å¯ä»¥ç¨è§å¾åªæ¾ç¤ºå§åï¼å°åï¼èä¸æ¾ç¤ºç¤¾ä¼ä¿é©å·åå·¥èµæ°çï¼å¦ä¸åå æ¯å¯ä½¿å¤æçæ¥è¯¢æäºç解å使ç¨ã
â å建è§å¾
CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] VIEW view_name [(column_list)] AS select_statement
- è§å¾åå¿ é¡»å¯ä¸ï¼åæ¶ä¸è½ä¸è¡¨éåã
- è§å¾å¯ä»¥ä½¿ç¨selectè¯å¥æ¥è¯¢å°çååï¼ä¹å¯ä»¥èªå·±æå®ç¸åºçååã
- å¯ä»¥æå®è§å¾æ§è¡çç®æ³ï¼éè¿ALGORITHMæå®ã
- column_listå¦æåå¨ï¼åæ°ç®å¿ é¡»çäºSELECTè¯å¥æ£ç´¢çåæ°
â æ¥çç»æ
SHOW CREATE VIEW view_name
â å é¤è§å¾
- å é¤è§å¾åï¼æ°æ®ä¾ç¶åå¨ã
-
å¯åæ¶å é¤å¤ä¸ªè§å¾ã
DROP VIEW [IF EXISTS] view_name â¦
â ä¿®æ¹è§å¾ç»æ
-
ä¸è¬ä¸ä¿®æ¹è§å¾ï¼å 为ä¸æ¯ææçæ´æ°è§å¾é½ä¼æ å°å°è¡¨ä¸ã
ALTER VIEW view_name [(column_list)] AS select_statement
â è§å¾ä½ç¨
- ç®åä¸å¡é»è¾
- 对客æ·ç«¯éèçå®ç表ç»æ
â è§å¾ç®æ³(ALGORITHM)
MERGE å并
å°è§å¾çæ¥è¯¢è¯å¥ï¼ä¸å¤é¨æ¥è¯¢éè¦å å并åæ§è¡ï¼
TEMPTABLE 临æ¶è¡¨
å°è§å¾æ§è¡å®æ¯åï¼å½¢æ临æ¶è¡¨ï¼ååå¤å±æ¥è¯¢ï¼
UNDEFINED æªå®ä¹(é»è®¤)ï¼æçæ¯MySQLèªä¸»å»éæ©ç¸åºçç®æ³ã
------------------
äºå¡æ¯æé»è¾ä¸çä¸ç»æä½ï¼ç»æè¿ç»æä½çå个åå ï¼è¦ä¸å ¨æåè¦ä¸å ¨å¤±è´¥ã
- æ¯æè¿ç»SQLçéä½æåæéä½æ¤éã
- äºå¡æ¯æ°æ®åºå¨æ°æ®æèªä¹ æ¹é¢çä¸ä¸ªåè½ã
- éè¦å©ç¨ InnoDB æ BDB åå¨å¼æï¼å¯¹èªå¨æ交çç¹æ§æ¯æå®æã
- InnoDB被称为äºå¡å®å ¨åå¼æã
â äºå¡å¼å¯
START TRANSACTION; æè BEGIN;
å¼å¯äºå¡åï¼ææ被æ§è¡çSQLè¯å¥å被认ä½å½åäºå¡å çSQLè¯å¥ã
â äºå¡æ交
COMMIT;
â äºå¡åæ»
ROLLBACK;
å¦æé¨åæä½åçé®é¢ï¼æ å°å°äºå¡å¼å¯åã
â äºå¡çç¹æ§
1. ååæ§ï¼Atomicityï¼
äºå¡æ¯ä¸ä¸ªä¸å¯åå²çå·¥ä½åä½ï¼äºå¡ä¸çæä½è¦ä¹é½åçï¼è¦ä¹é½ä¸åçã
2. ä¸è´æ§ï¼Consistencyï¼
äºå¡ååæ°æ®çå®æ´æ§å¿ é¡»ä¿æä¸è´ã
- äºå¡å¼å§åç»ææ¶ï¼å¤é¨æ°æ®ä¸è´
- å¨æ´ä¸ªäºå¡è¿ç¨ä¸ï¼æä½æ¯è¿ç»ç
3. é离æ§ï¼Isolationï¼
å¤ä¸ªç¨æ·å¹¶å访é®æ°æ®åºæ¶ï¼ä¸ä¸ªç¨æ·çäºå¡ä¸è½è¢«å ¶å®ç¨æ·çäºç©æå¹²æ°ï¼å¤ä¸ªå¹¶åäºå¡ä¹é´çæ°æ®è¦ç¸äºé离ã
4. æä¹ æ§ï¼Durabilityï¼
ä¸ä¸ªäºå¡ä¸æ¦è¢«æ交ï¼å®å¯¹æ°æ®åºä¸çæ°æ®æ¹åå°±æ¯æ°¸ä¹ æ§çã
â äºå¡çå®ç°
1. è¦æ±æ¯äºå¡æ¯æç表类å
2. æ§è¡ä¸ç»ç¸å ³çæä½åå¼å¯äºå¡
3. æ´ç»æä½å®æåï¼é½æåï¼åæ交ï¼å¦æåå¨å¤±è´¥ï¼éæ©åæ»ï¼åä¼åå°äºå¡å¼å§çå¤ä»½ç¹ã
â äºå¡çåç
å©ç¨InnoDBçèªå¨æ交(autocommit)ç¹æ§å®æã
æ®éçMySQLæ§è¡è¯å¥åï¼å½åçæ°æ®æ交æä½åå¯è¢«å ¶ä»å®¢æ·ç«¯å¯è§ã
èäºå¡æ¯ææ¶å ³éâèªå¨æ交âæºå¶ï¼éè¦commitæ交æä¹ åæ°æ®æä½ã
â 注æ
1. æ°æ®å®ä¹è¯è¨ï¼DDLï¼è¯å¥ä¸è½è¢«åæ»ï¼æ¯å¦å建æåæ¶æ°æ®åºçè¯å¥ï¼åå建ãåæ¶ææ´æ¹è¡¨æåå¨çåç¨åºçè¯å¥ã
2. äºå¡ä¸è½è¢«åµå¥
â ä¿åç¹
SAVEPOINT ä¿åç¹å称 â 设置ä¸ä¸ªäºå¡ä¿åç¹
ROLLBACK TO SAVEPOINT ä¿åç¹å称 â åæ»å°ä¿åç¹
RELEASE SAVEPOINT ä¿åç¹å称 â å é¤ä¿åç¹
â InnoDBèªå¨æ交ç¹æ§è®¾ç½®
SET autocommit = 0|1; 0è¡¨ç¤ºå ³éèªå¨æ交ï¼1表示å¼å¯èªå¨æ交ã
- å¦æå ³éäºï¼é£æ®éæä½çç»æå¯¹å ¶ä»å®¢æ·ç«¯ä¹ä¸å¯è§ï¼éè¦commitæ交åæè½æä¹ åæ°æ®æä½ã
- ä¹å¯ä»¥å ³éèªå¨æ交æ¥å¼å¯äºå¡ãä½ä¸START TRANSACTIONä¸åçæ¯ï¼
SET autocommitæ¯æ°¸ä¹ æ¹åæå¡å¨ç设置ï¼ç´å°ä¸æ¬¡å次修æ¹è¯¥è®¾ç½®ã(é对å½åè¿æ¥)
èSTART TRANSACTIONè®°å½å¼å¯åçç¶æï¼èä¸æ¦äºå¡æ交æåæ»åå°±éè¦å次å¼å¯äºå¡ã(é对å½åäºå¡)
表éå®åªç¨äºé²æ¢å ¶å®å®¢æ·ç«¯è¿è¡ä¸æ£å½å°è¯»åååå ¥
MyISAM æ¯æ表éï¼InnoDB æ¯æè¡é
â éå®
LOCK TABLES tbl_name [AS alias]
â 解é
UNLOCK TABLES
------------------
触åç¨åºæ¯ä¸è¡¨æå ³çå½åæ°æ®åºå¯¹è±¡ï¼å½è¯¥è¡¨åºç°ç¹å®äºä»¶æ¶ï¼å°æ¿æ´»è¯¥å¯¹è±¡
çå¬ï¼è®°å½çå¢å ãä¿®æ¹ãå é¤ã
â å建触åå¨
CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EACH ROW trigger_stmt
åæ°ï¼
trigger_timeæ¯è§¦åç¨åºçå¨ä½æ¶é´ãå®å¯ä»¥æ¯ before æ afterï¼ä»¥ææ触åç¨åºæ¯å¨æ¿æ´»å®çè¯å¥ä¹åæä¹å触åã
trigger_eventææäºæ¿æ´»è§¦åç¨åºçè¯å¥çç±»å
INSERTï¼å°æ°è¡æå ¥è¡¨æ¶æ¿æ´»è§¦åç¨åº
UPDATEï¼æ´æ¹æä¸è¡æ¶æ¿æ´»è§¦åç¨åº
DELETEï¼ä»è¡¨ä¸å é¤æä¸è¡æ¶æ¿æ´»è§¦åç¨åº
tbl_nameï¼çå¬ç表ï¼å¿ é¡»æ¯æ°¸ä¹ æ§ç表ï¼ä¸è½å°è§¦åç¨åºä¸TEMPORARY表æè§å¾å ³èèµ·æ¥ã
trigger_stmtï¼å½è§¦åç¨åºæ¿æ´»æ¶æ§è¡çè¯å¥ãæ§è¡å¤ä¸ªè¯å¥ï¼å¯ä½¿ç¨BEGINâ¦ENDå¤åè¯å¥ç»æ
â å é¤
DROP TRIGGER [schema_name.]trigger_name
å¯ä»¥ä½¿ç¨oldånew代æ¿æ§çåæ°çæ°æ®
æ´æ°æä½ï¼æ´æ°åæ¯oldï¼æ´æ°åæ¯new.
å é¤æä½ï¼åªæold.
å¢å æä½ï¼åªænew.
â 注æ
1. 对äºå ·æç¸å触åç¨åºå¨ä½æ¶é´åäºä»¶çç»å®è¡¨ï¼ä¸è½æ两个触åç¨åºã
â å符è¿æ¥å½æ°
concat(str1[, str2,â¦])
â åæ¯è¯å¥
if æ¡ä»¶ then
æ§è¡è¯å¥
elseif æ¡ä»¶ then
æ§è¡è¯å¥
else
æ§è¡è¯å¥
end if;
â ä¿®æ¹æå¤å±è¯å¥ç»æ符
delimiter èªå®ä¹ç»æ符å·
SQLè¯å¥
èªå®ä¹ç»æ符å·
delimiter ; â ä¿®æ¹ååæ¥çåå·
â è¯å¥åå 裹
begin
è¯å¥å
end
â ç¹æ®çæ§è¡
- åªè¦æ·»å è®°å½ï¼å°±ä¼è§¦åç¨åºã
-
Insert into on duplicate key update è¯æ³ä¼è§¦åï¼
å¦æ没æéå¤è®°å½ï¼ä¼è§¦å before insert, after insert;
å¦ææéå¤è®°å½å¹¶æ´æ°ï¼ä¼è§¦å before insert, before update, after update;
å¦ææéå¤è®°å½ä½æ¯æ²¡æåçæ´æ°ï¼å触å before insert, before update
- Replace è¯æ³ å¦ææè®°å½ï¼åæ§è¡ before insert, before delete, after delete, after insert
------------------
â// å±é¨åé ----------
â åé声æ
declare var_name[,â¦] type [default value]
è¿ä¸ªè¯å¥è¢«ç¨æ¥å£°æå±é¨åéãè¦ç»åéæä¾ä¸ä¸ªé»è®¤å¼ï¼è¯·å å«ä¸ä¸ªdefaultåå¥ãå¼å¯ä»¥è¢«æå®ä¸ºä¸ä¸ªè¡¨è¾¾å¼ï¼ä¸éè¦ä¸ºä¸ä¸ªå¸¸æ°ãå¦æ没ædefaultåå¥ï¼åå§å¼ä¸ºnullã
â èµå¼
ä½¿ç¨ set å select into è¯å¥ä¸ºåéèµå¼ã
- 注æï¼å¨å½æ°å
æ¯å¯ä»¥ä½¿ç¨å
¨å±åéï¼ç¨æ·èªå®ä¹çåéï¼
â// å ¨å±åé ----------
â å®ä¹ãèµå¼
set è¯å¥å¯ä»¥å®ä¹å¹¶ä¸ºåéèµå¼ã
set @var = value;
ä¹å¯ä»¥ä½¿ç¨select intoè¯å¥ä¸ºåéåå§å并èµå¼ãè¿æ ·è¦æ±selectè¯å¥åªè½è¿åä¸è¡ï¼ä½æ¯å¯ä»¥æ¯å¤ä¸ªå段ï¼å°±æå³çåæ¶ä¸ºå¤ä¸ªåéè¿è¡èµå¼ï¼åéçæ°ééè¦ä¸æ¥è¯¢çåæ°ä¸è´ã
è¿å¯ä»¥æèµå¼è¯å¥çä½ä¸ä¸ªè¡¨è¾¾å¼ï¼éè¿selectæ§è¡å®æãæ¤æ¶ä¸ºäºé¿å =被å½ä½å ³ç³»è¿ç®ç¬¦çå¾ ï¼ä½¿ç¨:=代æ¿ãï¼setè¯å¥å¯ä»¥ä½¿ç¨= å :=ï¼ã
select @var:=20;
select @v1:=id, @v2=name from t1 limit 1;
select * from tbl_name where @var:=30;
select into å¯ä»¥å°è¡¨ä¸æ¥è¯¢è·å¾çæ°æ®èµç»åéã
-| select max(height) into @max_height from tb;
â èªå®ä¹åéå
为äºé¿å selectè¯å¥ä¸ï¼ç¨æ·èªå®ä¹çåéä¸ç³»ç»æ è¯ç¬¦ï¼é常æ¯å段åï¼å²çªï¼ç¨æ·èªå®ä¹åéå¨åéåå使ç¨@ä½ä¸ºå¼å§ç¬¦å·ã
@var=10;
- åé被å®ä¹åï¼å¨æ´ä¸ªä¼è¯å¨æé½ææï¼ç»å½å°éåºï¼
â// æ§å¶ç»æ ----------
â ifè¯å¥
if search_condition then
statement_list
[elseif search_condition then
statement_list]
â¦
[else
statement_list]
end if;
â caseè¯å¥
CASE value WHEN [compare-value] THEN result
[WHEN [compare-value] THEN result â¦]
[ELSE result]
END
â while循ç¯
[begin_label:] while search_condition do
statement_list
end while [end_label];
-
å¦æéè¦å¨å¾ªç¯å æåç»æ¢ while循ç¯ï¼åéè¦ä½¿ç¨æ ç¾ï¼æ ç¾éè¦æ对åºç°ã
â éåºå¾ªç¯
éåºæ´ä¸ªå¾ªç¯ leave
éåºå½åå¾ªç¯ iterate
éè¿éåºçæ ç¾å³å®éåºåªä¸ªå¾ªç¯
â// å ç½®å½æ° ----------
â æ°å¼å½æ°
abs(x) â ç»å¯¹å¼ abs(-10.9) = 10
format(x, d) â æ ¼å¼åååä½æ°å¼ format(1234567.456, 2) = 1,234,567.46
ceil(x) â åä¸åæ´ ceil(10.1) = 11
floor(x) â åä¸åæ´ floor (10.1) = 10
round(x) â åèäºå ¥å»æ´
mod(m, n) â m%n m mod n æ±ä½ 10%3=1
pi() â è·å¾åå¨ç
pow(m, n) â m^n
sqrt(x) â ç®æ¯å¹³æ¹æ ¹
rand() â éæºæ°
truncate(x, d) â æªådä½å°æ°
â æ¶é´æ¥æå½æ°
now(), current_timestamp(); â å½åæ¥ææ¶é´
current_date(); â å½åæ¥æ
current_time(); â å½åæ¶é´
date(âyyyy-mm-dd hh:ii:ssâ); â è·åæ¥æé¨å
time(âyyyy-mm-dd hh:ii:ssâ); â è·åæ¶é´é¨å
date_format(âyyyy-mm-dd hh:ii:ssâ, â%d %y %a %d %m %b %jâ); â æ ¼å¼åæ¶é´
unix_timestamp(); â è·å¾unixæ¶é´æ³
from_unixtime(); â ä»æ¶é´æ³è·å¾æ¶é´
â å符串å½æ°
length(string) â stringé¿åº¦ï¼åè
char_length(string) â stringçå符个æ°
substring(str, position [,length]) â ä»strçpositionå¼å§,ålength个å符
replace(str ,search_str ,replace_str) â å¨strä¸ç¨replace_stræ¿æ¢search_str
instr(string ,substring) â è¿åsubstringé¦æ¬¡å¨stringä¸åºç°çä½ç½®
concat(string [,â¦]) â è¿æ¥å串
charset(str) â è¿åå串å符é
lcase(string) â 转æ¢æå°å
left(string, length) â ä»string2ä¸ç左边起ålength个å符
load_file(file_name) â ä»æ件读åå 容
locate(substring, string [,start_position]) â åinstr,ä½å¯æå®å¼å§ä½ç½®
lpad(string, length, pad) â éå¤ç¨padå å¨stringå¼å¤´,ç´å°å串é¿åº¦ä¸ºlength
ltrim(string) â å»é¤åç«¯ç©ºæ ¼
repeat(string, count) â éå¤count次
rpad(string, length, pad) --å¨stråç¨padè¡¥å ,ç´å°é¿åº¦ä¸ºlength
rtrim(string) â å»é¤åç«¯ç©ºæ ¼
strcmp(string1 ,string2) â éå符æ¯è¾ä¸¤å串大å°
â æµç¨å½æ°
case when [condition] then result [when [condition] then result â¦] [else result] end å¤åæ¯
if(expr1,expr2,expr3) ååæ¯ã
â èåå½æ°
count()
sum();
max();
min();
avg();
group_concat()
â å ¶ä»å¸¸ç¨å½æ°
md5();
default();
â// åå¨å½æ°ï¼èªå®ä¹å½æ° ----------
â æ°å»º
CREATE FUNCTION function_name (åæ°å表) RETURNS è¿åå¼ç±»å
å½æ°ä½
- å½æ°åï¼åºè¯¥åæ³çæ è¯ç¬¦ï¼å¹¶ä¸ä¸åºè¯¥ä¸å·²æçå
³é®åå²çªã
- ä¸ä¸ªå½æ°åºè¯¥å±äºæ个æ°æ®åºï¼å¯ä»¥ä½¿ç¨db_name.funciton_nameçå½¢å¼æ§è¡å½åå½æ°æå±æ°æ®åºï¼å¦å为å½åæ°æ®åºã
- åæ°é¨åï¼ç±"åæ°å"å"åæ°ç±»å"ç»æãå¤ä¸ªåæ°ç¨éå·éå¼ã
- å½æ°ä½ç±å¤æ¡å¯ç¨çmysqlè¯å¥ï¼æµç¨æ§å¶ï¼åé声æçè¯å¥ææã
- å¤æ¡è¯å¥åºè¯¥ä½¿ç¨ begin...end è¯å¥åå
å«ã
- ä¸å®è¦æ return è¿åå¼è¯å¥ã
â å é¤
DROP FUNCTION [IF EXISTS] function_name;
â æ¥ç
SHOW FUNCTION STATUS LIKE âparttenâ
SHOW CREATE FUNCTION function_name;
â ä¿®æ¹
ALTER FUNCTION function_name å½æ°é项
â// åå¨è¿ç¨ï¼èªå®ä¹åè½ ----------
â å®ä¹
åå¨åå¨è¿ç¨ æ¯ä¸æ®µä»£ç ï¼è¿ç¨ï¼ï¼åå¨å¨æ°æ®åºä¸çsqlç»æã
ä¸ä¸ªåå¨è¿ç¨é常ç¨äºå®æä¸æ®µä¸å¡é»è¾ï¼ä¾å¦æ¥åï¼äº¤çè´¹ï¼è®¢åå ¥åºçã
èä¸ä¸ªå½æ°é常ä¸æ³¨ä¸æ个åè½ï¼è§ä¸ºå ¶ä»ç¨åºæå¡çï¼éè¦å¨å ¶ä»è¯å¥ä¸è°ç¨å½æ°æå¯ä»¥ï¼èåå¨è¿ç¨ä¸è½è¢«å ¶ä»è°ç¨ï¼æ¯èªå·±æ§è¡ éè¿callæ§è¡ã
â å建
CREATE PROCEDURE sp_name (åæ°å表)
è¿ç¨ä½
åæ°å表ï¼ä¸åäºå½æ°çåæ°å表ï¼éè¦ææåæ°ç±»å
INï¼è¡¨ç¤ºè¾å ¥å
OUTï¼è¡¨ç¤ºè¾åºå
INOUTï¼è¡¨ç¤ºæ··åå
注æï¼æ²¡æè¿åå¼ã
------------------
åå¨è¿ç¨æ¯ä¸æ®µå¯æ§è¡æ§ä»£ç çéåãç¸æ¯å½æ°ï¼æ´ååäºä¸å¡é»è¾ã
è°ç¨ï¼CALL è¿ç¨å
â 注æ
- 没æè¿åå¼ã
- åªè½åç¬è°ç¨ï¼ä¸å¯å¤¹æå¨å ¶ä»è¯å¥ä¸
â åæ°
IN|OUT|INOUT åæ°å æ°æ®ç±»å
IN è¾å ¥ï¼å¨è°ç¨è¿ç¨ä¸ï¼å°æ°æ®è¾å ¥å°è¿ç¨ä½å é¨çåæ°
OUT è¾åºï¼å¨è°ç¨è¿ç¨ä¸ï¼å°è¿ç¨ä½å¤çå®çç»æè¿åå°å®¢æ·ç«¯
INOUT è¾å ¥è¾åºï¼æ¢å¯è¾å ¥ï¼ä¹å¯è¾åº
â è¯æ³
CREATE PROCEDURE è¿ç¨å (åæ°å表)
BEGIN
è¿ç¨ä½
END
------------------
ç¨æ·ä¿¡æ¯è¡¨ï¼mysql.user
â å·æ°æé
FLUSH PRIVILEGES
â å¢å ç¨æ·
CREATE USER ç¨æ·å IDENTIFIED BY [PASSWORD] å¯ç (å符串)
- å¿ é¡»æ¥æmysqlæ°æ®åºçå ¨å±CREATE USERæéï¼ææ¥æINSERTæéã
- åªè½å建ç¨æ·ï¼ä¸è½èµäºæéã
- ç¨æ·åï¼æ³¨æå¼å·ï¼å¦ âuser_nameâ@â192.168.1.1â
- å¯ç ä¹éå¼å·ï¼çº¯æ°åå¯ç ä¹è¦å å¼å·
- è¦å¨çº¯ææ¬ä¸æå®å¯ç ï¼é忽ç¥PASSWORDå ³é®è¯ãè¦æå¯ç æå®ä¸ºç±PASSWORD()å½æ°è¿åçæ··ç¼å¼ï¼éå å«å ³é®åPASSWORD
â éå½åç¨æ·
RENAME USER old_user TO new_user
â 设置å¯ç
SET PASSWORD = PASSWORD(âå¯ç â) â 为å½åç¨æ·è®¾ç½®å¯ç
SET PASSWORD FOR ç¨æ·å = PASSWORD(âå¯ç â) â 为æå®ç¨æ·è®¾ç½®å¯ç
â å é¤ç¨æ·
DROP USER ç¨æ·å
â åé æé/æ·»å ç¨æ·
GRANT æéå表 ON 表å TO ç¨æ·å [IDENTIFIED BY [PASSWORD] âpasswordâ]
- all privileges 表示æææé
- . 表示ææåºçææ表
- åºå.表å 表示æåºä¸é¢çæ表
â æ¥çæé
SHOW GRANTS FOR ç¨æ·å
â æ¥çå½åç¨æ·æé
SHOW GRANTS; æ SHOW GRANTS FOR CURRENT_USER; æ SHOW GRANTS FOR CURRENT_USER();
â æ¤æ¶æé
REVOKE æéå表 ON 表å FROM ç¨æ·å
REVOKE ALL PRIVILEGES, GRANT OPTION FROM ç¨æ·å â æ¤éæææé
â æéå±çº§
â è¦ä½¿ç¨GRANTæREVOKEï¼æ¨å¿ é¡»æ¥æGRANT OPTIONæéï¼å¹¶ä¸æ¨å¿ é¡»ç¨äºæ¨æ£å¨æäºææ¤éçæéã
å ¨å±å±çº§ï¼å ¨å±æééç¨äºä¸ä¸ªç»å®æå¡å¨ä¸çæææ°æ®åºï¼mysql.user
GRANT ALL ON *.*å REVOKE ALL ON *.*åªæäºåæ¤éå ¨å±æéã
æ°æ®åºå±çº§ï¼æ°æ®åºæééç¨äºä¸ä¸ªç»å®æ°æ®åºä¸çææç®æ ï¼mysql.db, mysql.host
GRANT ALL ON db_name.*åREVOKE ALL ON db_name.*åªæäºåæ¤éæ°æ®åºæéã
表å±çº§ï¼è¡¨æééç¨äºä¸ä¸ªç»å®è¡¨ä¸çææåï¼mysql.talbes_priv
GRANT ALL ON db_name.tbl_nameåREVOKE ALL ON db_name.tbl_nameåªæäºåæ¤é表æéã
åå±çº§ï¼åæééç¨äºä¸ä¸ªç»å®è¡¨ä¸çåä¸åï¼mysql.columns_priv
å½ä½¿ç¨REVOKEæ¶ï¼æ¨å¿ é¡»æå®ä¸è¢«ææåç¸åçåã
â æéå表
ALL [PRIVILEGES] â 设置é¤GRANT OPTIONä¹å¤çææç®åæé
ALTER â å 许使ç¨ALTER TABLE
ALTER ROUTINE â æ´æ¹æåæ¶å·²åå¨çåç¨åº
CREATE â å 许使ç¨CREATE TABLE
CREATE ROUTINE â å建已åå¨çåç¨åº
CREATE TEMPORARY TABLES â å 许使ç¨CREATE TEMPORARY TABLE
CREATE USER â å 许使ç¨CREATE USER, DROP USER, RENAME USERåREVOKE ALL PRIVILEGESã
CREATE VIEW â å 许使ç¨CREATE VIEW
DELETE â å 许使ç¨DELETE
DROP â å 许使ç¨DROP TABLE
EXECUTE â å 许ç¨æ·è¿è¡å·²åå¨çåç¨åº
FILE â å 许使ç¨SELECTâ¦INTO OUTFILEåLOAD DATA INFILE
INDEX â å 许使ç¨CREATE INDEXåDROP INDEX
INSERT â å 许使ç¨INSERT
LOCK TABLES â å 许对æ¨æ¥æSELECTæéç表使ç¨LOCK TABLES
PROCESS â å 许使ç¨SHOW FULL PROCESSLIST
REFERENCES â æªè¢«å®æ½
RELOAD â å 许使ç¨FLUSH
REPLICATION CLIENT â å 许ç¨æ·è¯¢é®ä»å±æå¡å¨æ主æå¡å¨çå°å
REPLICATION SLAVE â ç¨äºå¤å¶åä»å±æå¡å¨ï¼ä»ä¸»æå¡å¨ä¸è¯»åäºè¿å¶æ¥å¿äºä»¶ï¼
SELECT â å 许使ç¨SELECT
SHOW DATABASES â æ¾ç¤ºæææ°æ®åº
SHOW VIEW â å 许使ç¨SHOW CREATE VIEW
SHUTDOWN â å 许使ç¨mysqladmin shutdown
SUPER â å 许使ç¨CHANGE MASTER, KILL, PURGE MASTER LOGSåSET GLOBALè¯å¥ï¼mysqladmin debugå½ä»¤ï¼å 许æ¨è¿æ¥ï¼ä¸æ¬¡ï¼ï¼å³ä½¿å·²è¾¾å°max_connectionsã
UPDATE â å 许使ç¨UPDATE
USAGE â âæ æéâçåä¹è¯
GRANT OPTION â å 许æäºæé
â åæååå¨è¡¨çå ³é®ååå¸
ANALYZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE 表å â¦
â æ£æ¥ä¸ä¸ªæå¤ä¸ªè¡¨æ¯å¦æé误
CHECK TABLE tbl_name [, tbl_name] ⦠[option] â¦
option = {QUICK | FAST | MEDIUM | EXTENDED | CHANGED}
â æ´çæ°æ®æ件çç¢ç
OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] â¦
------------------
- å¯ç¨åå¼å·ï¼`ï¼ä¸ºæ è¯ç¬¦ï¼åºåã表åãå段åãç´¢å¼ãå«åï¼å 裹ï¼ä»¥é¿å ä¸å ³é®åéåï¼ä¸æä¹å¯ä»¥ä½ä¸ºæ è¯ç¬¦ï¼
- æ¯ä¸ªåºç®å½åå¨ä¸ä¸ªä¿åå½åæ°æ®åºçé项æ件db.optã
- 注éï¼
åè¡æ³¨é # 注éå 容
å¤è¡æ³¨é
åè¡æ³¨é â 注éå 容 (æ åSQL注éé£æ ¼ï¼è¦æ±åç ´æå·åå ä¸ç©ºæ ¼ç¬¦ï¼ç©ºæ ¼ãTABãæ¢è¡çï¼)
- 模å¼éé 符ï¼
_ ä»»æå个å符
% ä»»æå¤ä¸ªå符ï¼çè³å æ¬é¶å符
åå¼å·éè¦è¿è¡è½¬ä¹ â
- CMDå½ä»¤è¡å çè¯å¥ç»æ符å¯ä»¥ä¸º â;â, â\Gâ, â\gâï¼ä» å½±åæ¾ç¤ºç»æãå ¶ä»å°æ¹è¿æ¯ç¨åå·ç»æãdelimiter å¯ä¿®æ¹å½å对è¯çè¯å¥ç»æ符ã
- SQL对大å°åä¸ææ
- æ¸ é¤å·²æè¯å¥ï¼\c
以ä¸è¿ç¯å¯¹MySqlç»å¸¸ä½¿ç¨è¯å¥çå ¨é¢æ»ç»(å¿ çç¯)å°±æ¯å°ç¼å享ç»å¤§å®¶çå ¨é¨å 容äºï¼å¸æè½ç»å¤§å®¶ä¸ä¸ªåèï¼ä¹å¸æ大家å¤å¤æ¯æèæ¬ä¹å®¶ã