MySQL 甯哥敤鍛戒护鍜屽熀纭€璇硶

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 甯哥敤鍛戒护鍜屽熀纭€璇硶相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/one' title='one'>one   rem   asc   abc   鍒嗘敮璇彞   character   鏃犳硶   鏀粯   

-- mysql 鍛戒护
SHOW DATABASES;     #鏌ョ湅鐩墠绯荤粺涓瓨鍦ㄧ殑鏁版嵁搴?use database_name;      #鍒囨崲鏁版嵁搴?SHOW TABLES;        #鏄剧ず褰撳墠鏁版嵁搴撲笅闈㈢殑鎵€鏈夊彲鐢ㄧ殑琛?SELECT * FROM table_name;       #鏄剧ず璇ヨ〃涓殑鎵€鏈夋暟鎹?DESC table_name;        #鏄剧ず璇ヨ〃鐨勭粨鏋?EXIT;       #閫€鍑篗YSQL鍛戒护琛屾搷浣?

-- DDL-鏁版嵁瀹氫箟璇彞

#鍒涘缓鏁版嵁搴?CREATE DATABASE [IF NOT EXISTS] DATABASE_name;

#淇敼鏁版嵁搴撳瓧绗﹂泦
ALTER DATABASE DATABASE_name 
CHARACTER SET = charset_name;

#鍒犻櫎鏁版嵁搴?DROP DATABASE DATABASE_name;

#鍒涘缓琛?CREATE TABLE table_name(
    col_name data_type CONSTRAINT,
    col_name data_type CONSTRAINT,
    col_name data_type CONSTRAINT
) [ENGINE = ENGINE_name | [DEFAULT] CHARACTER SET [=] charset_name];


-- 琛ㄧ殑甯哥敤鎿嶄綔

SHOW TABLES;        #鏄剧ず褰撳墠鏁版嵁搴撲腑鐨勮〃
DESC table_name;        #鏌ョ湅琛ㄧ殑缁撴瀯
DROP TABLE table_name;      #鍒犻櫎琛?CREATE TABLE new_table_name LIKE copied_table_name;     #鍙鍒朵竴寮犺〃鐨勭粨鏋?CREATE TABLE new_table_name AS (SELECT * FROM copied_table_name);#澶嶅埗涓€寮犺〃鐨勭粨鏋勫強鏁版嵁

ALTER TABLE table_name RENAME [AS|TO] new_table_name;       #閲嶅懡鍚嶈〃
ALTER TABLE table_name ADD [COLUMN] col_name col_definition;        #涓鸿〃娣诲姞涓€涓柊鍒?ALTER TABLE table_name CHANGE [COLUMN] old_col_name new_col_name col_definition;#淇敼鍒楀悕鍙婂垪绫诲瀷
ALTER TABLE table_name MODIFY [COLUMN] col_name col_definiton;      #鍙慨鏀瑰垪绫诲瀷
ALTER TABLE table_name DROP [COLUMN] col_name;      #鍒犻櫎鍒?
-- 鍒涘缓涓婚敭鐨勬柟娉?
#鐩存帴鍦ㄥ瓧娈靛畾涔夊悗闈㈠0鏄庝富閿?CREATE TABLE demo(
    id INT PRIMARY KEY,
    ......
);

#鐢╟onstraint澹版槑涓婚敭
CREATE TABLE demo(
    id INT,
    ......,
    CONSTRAINT pk_demo PRIMARY KEY (id)     #澶栭敭鍛藉悕瑙勮寖锛歱k_table_name
);

CREATE TABLE demo(
    id INT
    `name` VARCHAR(10),
    ......,
    PRIMARY KEY (id,`name`)     #澶嶅悎涓婚敭
);

#鐢ˋLTER璇彞琛ュ厖澹版槑涓婚敭
ALTER TABLE demo ADD CONSTRAINT pk_demo PRIMARY KEY (sid);

#鍒犻櫎涓婚敭鐨勬柟娉?ALTER TABLE demo DROP PRIMARY KEY;


-- 鍒涘缓澶栭敭鐨勬柟娉?CREATE TABLE demo(
    cid INT PRIMARY KEY,
    sid INT,
    FOREIGN KEY (sid) REFERENCES demo (sid)
);

#閫氳繃constraint娣诲姞澶栭敭
ALTER TABLE demo ADD CONSTRAINT fk_1 FOREIGN KEY (sid) REFERENCES demo (sid) 
ON DELETE CASCADE ON UPDATE CASCADE;
/*
CASCADE: 浠庣埗琛ㄤ腑鍒犻櫎鎴栨洿鏂板搴旂殑琛岋紝鍚屾椂鑷姩鐨勫垹闄ゆ垨鏇存柊瀛愯〃涓尮閰嶇殑琛屻€?                    ON DELETE CASCADE 鍜?on  UPDATE CASCADE 閮借 InnoDB 鎵€鏀寔銆?SET NULL: 浠庣埗琛ㄤ腑鍒犻櫎鎴栨洿鏂板搴旂殑琛岋紝鍚屾椂灏嗗瓙琛ㄤ腑鐨勫閿垪璁句负绌恒€?                    杩欎簺鍦ㄥ閿垪娌℃湁琚涓?NOT NULL 鏃舵墠鏈夋晥銆?                    ON DELETE SET NULL 鍜?ON UPDATE SET NULL 閮借鏀寔銆?NO action:榛樿涓簉estrict,鎷掔粷鍒犻櫎鎴栬€呮洿鏂扮埗琛?*/

#鍒犻櫎澶栭敭鐨勬柟娉?ALTER TABLE table_name DROP FOREIGN KEY fk_name;

-- 鑷闀?auto_increment
CREATE TABLE demo(
    id INT auto_increment PRIMARY KEY,
    `name` VARCHAR(10),
    ......ALTER);

-- 鍞竴绾︽潫  UNIQUE

-- 闈炵┖绾︽潫 NOT NULL

-- 妫€鏌ョ害鏉?CREATE TABLE demo(
    ......,
    sex SET('鐢?#39;,'濂?#39;锛?
    payment ENUM('璐у埌浠樻','鍦ㄧ嚎鏀粯')
);

-- 榛樿鍊肩害鏉?CREATE TABLE demo(
    tel VARCHAR(50) DEFAULT 'default'
);

/*
鏁版嵁搴撲笁澶ц寖寮?绗竴鑼冨紡锛氱‘淇濇瘡鍒椾繚鎸佸師瀛愭€?绗簩鑼冨紡锛氱‘淇濇瘡琛岀殑鍞竴鎬?绗笁鑼冨紡锛氱‘淇濇瘡鍒楅兘鍜屼富閿垪鐩存帴鐩稿叧锛岃€屼笉鏄棿鎺ョ浉鍏?*/


-- DML-鏁版嵁鎿嶇旱璇彞

#鎻掑叆鏁版嵁
INSERT INTO table_name [col_name] VALUES (value1,value2,...);

-- 鎻掑叆澶氳鏁版嵁鐨勬柟娉?
#浠庡叾浠栬〃浠庨€夋嫨鐩稿簲鐨勫唴瀹规彃鍏ュ埌鏂拌〃
INSERT INTO table_name (col_name) SELECT col_name FROM source_table_name;

#鍒╃敤澶氭潯insert璇彞鎵归噺澶勭悊
INSERT INTO table_name (col1_name,col2_name,...)
VALUES
    (col1_value,col2_value,...),
    (col1_value,col2_value,...),
    (col1_value,col2_value,...),
    (col1_value,col2_value,...);

#淇敼鏁版嵁
UPDATE table_name SET col_name = new_value WHERE update_condition;

#鍒犻櫎鏁版嵁
DELETE FROM table_name [WHERE delete_condition];        #鎸夋潯浠跺垹闄や竴鏉℃垨澶氭潯璁板綍
TRUNCATE TABLE table_name;      #娓呯┖鍏ㄨ〃鏁版嵁锛屽苟閲嶇疆鑷姩澧為暱鍊硷紝鏃犳硶鐢ㄤ簬鏈夊閿害鏉熺殑琛?
-- DQL-鏁版嵁鏌ヨ璇彞

SELECT col_name FROM table_name WHERE select_condition GROUP BY col_name ORDER BY col_name LIMIT 0,1;


/*
mysql 鍙橀噺

mysql涓紝鍙橀噺鍒嗕负涓夌被锛屽眬閮ㄥ彉閲忥紝浼氳瘽鍙橀噺锛屽叏灞€鍙橀噺銆?
灞€閮ㄥ彉閲忎富瑕佺敤鍦ㄥ嚱鏁颁互鍙婂瓨鍌ㄨ繃绋嬩腑锛?瀹氫箟锛歞eclare c int default 0;
璋冪敤锛歴elect c;
璁颁綇鍙橀噺鍚嶅墠涓嶉渶瑕佸姞@锛?
浼氳瘽鍙橀噺浠呭褰撳墠瀹㈡埛绔繛鎺ユ湁鏁堬紝
瀹氫箟锛歴et @var := 鈥榓bc鈥?
璋冪敤锛歴elect name = @var;
璁颁綇鍙橀噺鍓嶅姞涓€涓狜锛?
鍏ㄥ眬鍙橀噺娉曞姏鏃犺竟锛屼粈涔堟椂鍊欓兘鏈夋晥銆?瀹氫箟锛歴et @@var := 鈥榓bc鈥?
璋冪敤锛歴elect name = @@var;
璁颁綇鍙橀噺鍓嶅姞2涓狜

*/


-- 鍒嗘敮璇彞

#鏉′欢缁撴瀯
IF              THEN
ELSEIF    THEN
ELSE
END IF;
#CASE
DECLARE i INT DEFAULT 1;
CASE i
    WHEN 1 THEN ...
    WHEN 2 THEN ...
    ELSE
END CASE;

-- 寰幆璇彞

#REPEAT
REPEAT
    action
    UNTIL condition_
END REPEAT;

#WHILE
WHILE condition_
    DO
        aciton
END WHILE;

#LOOP
a:LOOP
    action
    IF ? THEN ITERATE a锛?#ITERATE 鐩稿綋浜?CONTINUE
    ELSE LEAVE a;       #LEAVE 鐩稿綋浜?break
END LOOP a;

-- PROCEDURE   绫诲瀷 in out inout

#璇硶缁撴瀯
CREATE PROCEDURE proce_name(i INT)
BEGIN



END;


-- FUNCTION 

#璇硶缁撴瀯
CREATE FUNCTION func_name(i int) 
RETURNS data_type
BEGIN

END;


-- 娓告爣

#鍒涘缓娓告爣
DECLARE cursor_name CURSOR FOR select_statement;
#寮€鍚父鏍?OPEN cursor_name;
#璇诲彇
FETCH [from] cursor_name INTO var_name [,var_name]...
#鍏抽棴
CLOSE cursor_name;

#瀹炰緥
CREATE PROCEDURE cursorTest()
    BEGIN
        -- 瀹氫箟鍙橀噺
        DECLARE done INT DEFAULT FALSE;
        DECLARE idd INT;
        DECLARE namea VARCHAR(20);
        -- 瀹氫箟娓告爣
        DECLARE test_cursor CURSOR FOR SELECT id,`name` FROM test;
        -- 瀹氫箟鏉′欢澶勭悊鍣細褰撳嚭鐜皀ot found 鐨勫紓甯稿悗鎵цset done=true 
        DECLARE CONTINUE HANDLER for NOT found SET done = true;

        -- 寮€鍚父鏍?        OPEN test_cursor;
        a:WHILE true DO
        -- 璇诲彇
            FETCH test_cursor INTO idd,namea;
            IF done THEN
                LEAVE a;
            ELSE
                SELECT idd,namea;
            END IF;
        END WHILE a;
        CLOSE test_cursor;
    END;


-- 瑙﹀彂鍣?
#鍩烘湰璇硶缁撴瀯
CREATE [DEFINER = {USER | CURRENT_USER}]
TRIGGER trigger_name
trigger_time        #trigger_time:{BEFORE | AFTER}
trigger_event       #trigger_event:{INSERT | UPDATE |DELETE}
ON table_name FOR EACH ROW
[BEGIN]
trigger_body
[END;]


-- 浜嬪姟
SET @autocommit = OFF;
#SAVEPOINT
SAVEPOINT sp_name;
#ROLLBACK
ROLLBACK TO sp_name;
#COMMIT
COMMIT;

以上是关于MySQL 甯哥敤鍛戒护鍜屽熀纭€璇硶的主要内容,如果未能解决你的问题,请参考以下文章

Linux甯哥敤鍛戒护

GitLab 甯哥敤鍛戒护

Linux甯哥敤鍛戒护

sql甯哥敤鍛戒护

Linux甯哥敤鍛戒护

git甯哥敤鍛戒护