MySql

//杩炴帴mysql

mysql -u root -h 47.101.205.200 -p //杩炴帴mysql

exit; //閫�鍑簃ysql

//鍚姩鍋滄mysql

mysqld --console //window绯荤粺涓� - 鍚姩mysql

mysqladmin -uroot shutdown //window绯荤粺涓� - 鍋滄mysql

service mysqld start //centos绯荤粺涓� - 鍚姩mysql

service mysqld stop //centos绯荤粺涓� - 鍏抽棴mysql

service mysqld restart //centos绯荤粺涓� - 閲嶅惎mysql

service mysqld status //centos绯荤粺涓� - 鏌ョ湅鏈嶅姟鐘舵��

//灞曠ず鏁版嵁搴撱�佹暟鎹簱琛ㄧ瓑...

SHOW DATABASES; //鍒楀嚭 MySQL 鏁版嵁搴撶鐞嗙郴缁熺殑鏁版嵁搴撳垪琛ㄣ��

SHOW TABLES; //鏄剧ず鎸囧畾鏁版嵁搴撶殑鎵�鏈夎〃

select * from 鏁版嵁琛ㄥ悕; //璇诲彇鏁版嵁搴撹〃

SHOW COLUMNS FROM 鏁版嵁琛ㄥ悕; //鏄剧ず鏁版嵁琛ㄧ殑灞炴�э紝灞炴�х被鍨嬶紝涓婚敭淇℃伅 锛屾槸鍚︿负 NULL锛岄粯璁ゅ�肩瓑鍏朵粬淇℃伅銆�

//鍒涘缓銆佸垹闄ゃ�佷娇鐢ㄣ�佹暟鎹簱

CREATE DATABASE 鏁版嵁搴撳悕; //鍒涘缓鏁版嵁搴�

drop database <鏁版嵁搴撳悕>; //鍒犻櫎鏁版嵁搴�

use 鏁版嵁搴撳悕; //閫夋嫨鏁版嵁搴�

//鍒涘缓銆佸垹闄ゆ暟鎹簱琛�

ALTER TABLE store RENAME TO store999; //淇敼琛ㄥ悕绉�

DROP TABLE table_name; //鍒犻櫎鏁版嵁搴撹〃

//鍒涘缓鏁版嵁搴撹〃

CREATE TABLE IF NOT EXISTS `table_name`(

聽 `table_id` INT UNSIGNED AUTO_INCREMENT,

聽 `table_title` VARCHAR(100) NOT NULL,

聽 `table_author` VARCHAR(40) NOT NULL,

聽 `submission_date` DATE,

聽 PRIMARY KEY ( `table_id` )

)ENGINE=InnoDB DEFAULT CHARSET=utf8;

// NOT NULL 鍦ㄦ搷浣滄暟鎹簱鏃跺鏋滆緭鍏ュ瓧娈典负null鎶ラ敊

// AUTO_INCREMENT瀹氫箟鍒椾负鑷鐨勫睘鎬э紝涓�鑸敤浜庝富閿紝鏁板�间細鑷姩鍔�1銆�

// PRIMARY KEY鍏抽敭瀛楃敤浜庡畾涔夊垪涓轰富閿�� 鎮ㄥ彲浠ヤ娇鐢ㄥ鍒楁潵瀹氫箟涓婚敭锛屽垪闂翠互閫楀彿鍒嗛殧銆�

// ENGINE 璁剧疆瀛樺偍寮曟搸锛孋HARSET 璁剧疆缂栫爜銆�

//澧炲垹鏀规煡琛ㄦ暟鎹�

*** where澶氬垽鏂� where id=1 or id=2 or id=3 .....;聽 where id in (1,2,3...);

*** SELECT name FROM person_tbl WHERE name REGEXP '^st' || 'ok$' || 'mar'; // 姝e垯鍖归厤寮�澶� 缁撳熬 鍖呭惈

*** like鍖归厤/妯$硦鍖归厤 select * from store where info like '%鏈嶄笉鏈�%';

'%a'聽 聽 //浠缁撳熬鐨勬暟鎹�

'a%'聽 聽 //浠寮�澶寸殑鏁版嵁

'%a%'聽 聽 //鍚湁a鐨勬暟鎹�

'_a_'聽 聽 //涓変綅涓斾腑闂村瓧姣嶆槸a鐨�

'_a'聽 聽 //涓や綅涓旂粨灏惧瓧姣嶆槸a鐨�

'a_'聽 聽 //涓や綅涓斿紑澶村瓧姣嶆槸a鐨�

------------------------------------------------------------------------------------------------------------

insert into store(id,imgUrl,title,info,price,count) values (22,'imgUrl22','title22','info22',22,22); //鍦ㄨ〃涓彃鍏ユ暟鎹�

update store set id=999 where id=212; //鍦ㄨ〃涓慨鏀规暟鎹�

delete from store where id=1; //鍦ㄨ〃涓垹闄ゆ暟鎹�

//鍙互閫夋嫨澶氫釜瀛楁鎴栬〃 , 鍒嗛殧, where璇彞鍖呭惈鏉′欢, limit璁惧畾杩斿洖鐨勬暟閲�, offset璁剧疆寮�濮嬫煡璇㈢殑浣嶇疆, BINARY 鍖哄垎澶у皬鍐�,

select id from store where BINARY id = 5 limit 3 offset 0; // 鏍规嵁鏉′欢鏌ヨ鏁版嵁

------------------------------------------------------------------------------------------------------------

//璁剧疆涓婚敭 鏀剧疆閲嶅 (閲嶅鎶ラ敊)

ALTER TABLE store ADD PRIMARY KEY ( id );

//UNION [ALL | DISTINCT] 鍒犻櫎缁撴灉閲嶅鏁版嵁********************(涓嶅湪mysql淇敼)

select id from store union distinct select id from store;

//ORDER BY璇诲彇鐨勬暟鎹繘琛屾帓搴� asc鍗囧簭 desc闄嶅簭********************锛堜笉鍦╩ysql淇敼锛�

select id from store order by id desc;

//鏍规嵁鏌愪釜瀛楁鍒嗙粍,骞剁粺璁℃瘡涓汉鏈夊灏戞潯璁板綍

SELECT id, COUNT(*) FROM store GROUP BY id;

//鍙互瀹炵幇鍦ㄥ垎缁勭粺璁℃暟鎹熀纭�涓婂啀杩涜鐩稿悓鐨勭粺璁�, 渚嬪鎴戜滑灏嗕互涓婄殑鏁版嵁琛ㄦ寜鍚嶅瓧杩涜鍒嗙粍锛屽啀缁熻姣忎釜浜虹櫥褰曠殑娆℃暟锛� NULL 琛ㄧず鎵�鏈変汉鐨勭櫥褰曟鏁般��

SELECT id, SUM(singin) as singin_count FROM store GROUP BY id WITH ROLLUP;

// coalesce 鏉ヨ缃竴涓彲浠ュ彇浠� NUll 鐨勫悕绉�, 鍙傛暟璇存槑锛氬鏋渁==null,鍒欓�夋嫨b锛涘鏋渂==null,鍒欓�夋嫨c锛涘鏋渁!=null,鍒欓�夋嫨a锛涘鏋渁 b c 閮戒负null 锛屽垯杩斿洖涓簄ull锛堟病鎰忎箟锛夈��

SELECT coalesce(id,'鎬绘暟'), SUM(count) FROM store GROUP BY id WITH ROLLUP;

//濡傛灉瀛楃闆嗛噰鐢ㄧ殑鏄� utf8(涓囧浗鐮�)锛岄渶瑕佸厛瀵瑰瓧娈佃繘琛岃浆鐮佺劧鍚庢帓搴忥細

select id from store order by CONVERT(id using gbk);

//鏌ヨ鏁版嵁搴撹〃涓槸鍚︿负NULL銆�

WHERE id IS NULL; <==>聽 WHERE id IS NOT NULL;

//鏌ユ壘澶氫釜琛ㄥ啓娉�

SELECT a.runoob_id, a.runoob_author, b.runoob_count FROM runoob_tbl a, tcount_tbl b WHERE a.runoob_author = b.runoob_author;

//鍒犻櫎锛屾坊鍔犳垨淇敼琛ㄥ瓧娈�

ALTER TABLE store DROP price; //鍒犻櫎琛ㄥ瓧娈�

ALTER TABLE store ADD table_id INT; //娣诲姞table_id瀛楁锛屽苟瀹氫箟鏁版嵁绫诲瀷:

ALTER TABLE store change table_id price111 INT; //淇敼table_id瀛楁涓簆rice111, 骞跺畾涔夋暟鎹被鍨�;鑷姩璇ュ瓧娈甸粯璁や负 NULL銆�

alter table store modify price INT NOT NULL DEFAULT 100; //淇敼瀛楁榛樿鍊�

ALTER TABLE store ALTER price SET DEFAULT 9999; //淇敼瀛楁榛樿鍊�

//淇敼鏁版嵁琛ㄧ被鍨�

SHOW TABLE STATUS like 'store'\G; //鏌ョ湅绫诲瀷

ALTER TABLE store ENGINE = InnoDB; //淇敼鏁版嵁琛ㄧ被鍨� MYISAM

你可能感兴趣的:(MySql)