MySQL命令大全 - 從小白 → 大白之路

/ 前言 /

       本文持續更新中 . . .

/ SQL /

  • 數據庫連接

    # 連接到本地MySQL
    mysql -uroot -ppassword
    
    # 連接到遠程MySQL服務
    mysql -hip -uroot -ppassword
    
  • 數據庫密碼

    查詢MySQL默認密碼(5.7⤴️)

    sudo grep 'temporary password' /var/log/mysqld.log
    

    修改密碼

    ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
    

    修改密碼策略

    # 修改策略爲最低級別
    set global validate_password_policy=0;
    # 修改密碼長度
    set global validate_password_length=4;
    
  • 數據庫用戶

    # 創建用戶
    CREATE USER 'test_user'@ 'ip' IDENTIFIED BY 'MaRePass4!';
    
    # 爲用戶添加權限 GRANT 權限 ON 數據庫名稱(全部使用*.*代替) TO '用戶名'@'ip';
    GRANT select,insert,update,delete ON *.* TO 'test_user'@'ip|%|localhost';
    
    # 允許外部訪問
    update mysql.user set Host = "%" where user = "root";
    
    # 刪除用戶
    delete from mysql.user where user = "test_user";
    
  • 操作數據庫、表

    數據庫(database)
    # 查看數據庫
    show databases;
    
    # 創建數據庫
    create database test; 
    
    # 選擇數據庫
    use test;
    
    # 刪除數據庫
    drop database test;
    
    表(table)
    # 查看錶
    show tables;
    
    # 創建表
    CREATE TABLE "test_table" (
      "id" bigint(20) NOT NULL AUTO_INCREMENT,
      "description" varchar(200) COLLATE utf8_bin DEFAULT NULL COMMENT '簡介',
      "create_date" datetime NOT NULL COMMENT '創建時間',
      PRIMARY KEY ("id")
    ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='測試表';
    
    # 刪除表
    drop table test_table;
    
  • 表的增、刪、改語法

    增(insert)
    # 語法 : insert 表名 (字段) value (值);
    
    # 增加一條數據
    insert test_table (description,create_date) value ('這是一條測試數據~.~',now())
    
    # 增加多條數據 insert 表名 (字段1、字段2) values (數據,數據),(數據,數據)
    insert test_table (description,create_date) values ('這是批量插入第一條測試數據~.~',now()),('這是批量插入第二條測試數據~.~',now())
    
    刪(delete)
    # 語法 : delete from 表名 where 條件語句;
    
    # 刪除指定數據
    delete from test_table where id = 1;
    
    # 關聯表刪除數據,刪除t1,t2表中相關聯的數據
    # 寫法一 delete 表名(多個以逗號分隔) from 表1 left join 表2 on 等值連接 where 條件語句
    delete t1 from t1 left join t2 on t1.id = t2.t1_id where t2.t1_id = 1;
    delete t1,t2 from t1 left join t2 on t1.id = t2.t1_id where t2.t1_id = 1;
    
    # 寫法二 delete 表名(多個以逗號分隔) from 表名(多個以逗號分隔) where 條件語句
    delete t1 form t1,t2 where t2.t1_id = 1;
    delete t1,t2 form t1,t2 where t2.t1_id = 1;
    
    # 刪除整個表的數據,慎用哦~
    delete from test_table;
    
    改(update)
    # 語法 : update 表名 set 字段 = 值 where 條件語句;
    
    # 修改指定數據
    update test_table set description = '測試修改' where id = 1;
    
    # 關聯表修改, 修改指定數據的值爲另一個表的值
    # 寫法一 update 表1,表2 set 表1.字段 = 表2.字段 where 條件語句
    update t1 as a ,t2 as b set a.description = b.description where a.id = 1;
    
    # 寫法二 update 表1 set 字段 = 子查詢 where 條件語句
    update t1 set description = (SELECT description from t2 where id = 1) where id = 1;
    
  • 表的查詢語法

    select
    # 語法 : select 字段(全部用*代替) from 表名 where 條件語句
    # 查詢全表
    select * from t1;
    # 查詢指定數據
    select * from t1 where id = 1;
    # 關聯表查詢數據
    select t1.id, t2.id from t1
    left join t2 on t1.id = t2.t1_id
    where t1.id = 1
    
    union和union all

    unionunion all在使用時必須保證查詢的列數量是一致的

    select column1 , column2 from t1
    union
    select column1 , column2 from t2
    

    如果像下面這樣寫的話會就報錯

    select column1 , column2 from t1
    union
    select column1 , column2, column2 from t2
    
    Error : The used SELECT statements have a different number of columns 
    

    unionunion all都是將倆個結果集進行合併, 類似與生成一張臨時表, 我們來看下它們的區別
    union : 會篩選掉重複的數據, 並按照查詢字段順序進行排序, 但是效率較低
    union all : 不會篩選掉重複的數據, 也不會按照查詢字段進行排序, 但是效率較高

  • MySQL索引

    創建索引
    # 語法1
    create [UNIQUE|FULLTEXT|SPATIAL] index 索引名稱 on test_table();
    # 創建普通索引
    create index test_index on test_table(description);
    # 創建唯一索引
    CREATE UNIQUE INDEX test_index ON test_table(description);
    # 創建複合索引
    CREATE INDEX id_description ON test_table(id,description);
    
    # 語法2
    alter table 表名 add [UNIQUE|FULLTEXT|SPATIAL] index 索引名稱();
    # 創建普通索引
    alter table test_table add index test_index(description);
    # 創建唯一索引
    ALTER table test_table add UNIQUE index test_index(description);
    # 創建複合索引
    ALTER table test_table add index id_description(id,description);
    
    刪除索引
    # 語法1
    drop index 索引名稱 on 表名;   
    drop index test_index on test_table;   
    
    # 語法2
    alter table 表名 drop index 索引名稱;
    alter table test_table drop index test_index;
    
    查詢索引
    show index from test_table;
    
  • MySQL函數

    生成隨機數( rand )
    # 生成1以內的小數
    select rand();
    
    # 生成5以內的整數
    # select floor(x)這個函數是用來獲取小於x的最大整數值
    select floor(rand() * 5);
    
    # 生成20 - 70以內的整數
    select floor((rand() * 50) + 20)
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章