天天看點

NoSQL的基本操作NoSQL的基本操作

NoSQL的基本操作

day01

删除索引:

drop index 索引字段 on 表名;

drop index idnumber on student;

删除自增:

alter table 表名 modify 字段 字段限制;

alter table student modify id int;

删除主鍵:

alter table 表名 drop primary key;

alter table student drop primary key;

一.索引

    (1)MySQL中表的某些字段通過排序後生成一個固定格式的資料檔案,這個檔案稱作索引檔案,能夠提高查詢的速度

    (2)為什麼有了索引,查詢的速度就會加快?

        原理: B + 樹

    (3)建立索引:

        普通索引:create index 索引名稱 on 表名(字段清單);   所有字段都可以設定主鍵

        唯一索引:create unique index 索引名稱 on 表名(字段清單);   字段的值要有唯一性

        主鍵索引:alter table 表名 add primary key(字段名);     主鍵一般設定一個,在id上,自增

        全文索引:alter table 表名 add fulltext index 索引名稱(字段名);

    (4)優化select語句

        檢視查詢語句的性能:explain

            explain select...;

                type---All

                possible_keys---null

                key---null

                rows---1234  (周遊資料條數)

                extra---using where

                        using filesort(表外排序,使用order by時出現,需要優化掉)

                        using temporary(使用group by時出現,需要優化掉)

        1.查詢5班男生中,年齡最小的10人:

            create index i_classid_sex_age on student(classid,sex,age); 建立聯合索引

            explain select * from student where classid=5 and sex=1 order by age asc limit 10;

        2.查詢5班每個年齡對應多少學生:

            create index i_classid_age on student(classid,age); 

            explain select age,count(`age`) as num  from student where classid=5 group by age;

        強制使用某索引:    

            select * from student force index(i_classid_age) where......;

        3.查詢年齡最小的10人,要求顯示班級的名稱

            create index i_age on student(age);

            explain select s.sname,s.age,c.cname from student as s

            join class as c 

            on s.classid=c.id 

            order by s.age 

            limit 10;

    索引建立的原則:

        1.where子句中出現的字段要建立索引

        2.order by子句中出現的字段要建立索引

        3.group by 多個子句同時出現或where ,order by, group by 中一個子句出現多個字段都要建立聯合索引

        4.join要優化每個表

        5.如果extra中using filesort ,using temporary 一定要優化掉

day02

1.分區的原則:

    頁面展示資料根據哪個字段查詢,就根據哪個字段進行拆分

2.分區表類型及建立

    1.RANGE分區:根據一個字段值的範圍,配置設定給不同的分區進行存儲

    2.LIST分區:把一個字段相同值對應的記錄,放到一個分區進行存儲

    3.HASH分區:把一個字段的值進行hash散列,然後根據餘數值分區存儲

    4.KEY分區:類似hash分區,key函數中包含字段至少有一個是整數字段

3.存儲過程

    1.建立存儲過程:

        create procedure 名稱(in|out|inout 名稱 類型,......)

        begin

            過程體;

        end

        過程體中可以使用所有的SQL,變量,運算,流程控制器語句,函數,

        存儲過程沒有傳回值.

        delimiter(定界符) 改變sql語句的結束符

    2.聲明變量

        declare 變量名稱 類型(長度) default 預設值;

    3.運算

        比較運算:

            =(等于) 

            =(指派,使用set):set 變量=值;

        邏輯運算:

            and  or  not

    4.條件語句

        if 條件 then

            select xxx;

        elseif 條件

            select yyy;

        else

            select zzz;

        end if;

        case 變量

        when 值1 then 

            select '男';

        when 值2 then

            select '女';

            select '保密'; 

        end case;

    5.循環語句

        while 循環條件 do

            變換步長;

        end while;

        repeat

        變換步長;

        until 終止條件;

        end repeat;

    6.函數

        1.字元串函數

            CONCAT(string2 [,...])                 連接配接子串

            REPLACE(str,search_str,replace_str)   在str中用replace_str替換search_str

            SUBSTRING(str,position[,length])      從str的position開始,取length個字元

        2.數學函數

            CEILING(num2)             向上取整

            FLOOR(num2)               向下取整

            RAND([seed])              随機數

            ROUND(num[,decimals])  四舍五入,decimals為小數位數

        3.時間日期函數

            CURRENT_DATE()      目前日期

            CURRENT_TIME()      目前時間

            NOW()                目前日期+時間

            CURRENT_TIMESTAMP() 目前時間戳

day03

1.觸發器

    1.建立觸發器

        create trigger 觸發器名稱 before|after 事件 on 表名 for each row

            事件觸發語句;

        end//

2.事務

    事務的特點:

        原子性  一緻性  隔離性  持久性

    $pdo->beginTransaction(); //開啟事務

    $pdo->commit();    //送出事務

    $pdo->rollback();  //事務復原

3.使用者權限    

    1.建立使用者

        create user 使用者名@localhost identified by '密碼';

    2.配置設定權限

        grant 權限 on 庫.表 to 使用者@localhost';

        權限值:

            all

            insert,update,delete,select,create,alter,drop...

            庫.*   //某個庫中所有的表

            *.*    //所有庫的所有表

    3.回收權限

        revoke 權限 on 庫.表 from 使用者@localhost;

    4.重置密碼

        set password = password('新密碼');

    5.删除使用者

        drop user 使用者名@localhost;

4.資料庫的備份及恢複

    備份資料庫

        mysqldump -u使用者名 -p密碼 資料庫名>path/名稱.sql

    恢複資料庫

        use 資料庫

        source path/名稱.sql

5.視圖

    一個虛拟的表,由一個查詢語句産生.

    create view 視圖名稱 as select 語句;

    優點:簡化查詢語句,保護資料

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

如有錯誤文法,請指正!!謝謝!!!

原文位址https://blog.csdn.net/qq_42781185/article/details/81187380