1. 程式人生 > ><資料庫>MySQL補充

<資料庫>MySQL補充

    show create table 表名 \G;(檢視建立的屬性)
    alter table 表名 auto_increment=xx;(修改自增起始值)
    set session auto_increment_offset=xx;(修改步長)
    索引的目的:加速查詢
    約束:
        主鍵
        外來鍵
        唯一索引:unique 名字 (列名) ----不允許重複(可以為空)
        聯合唯一:unique 名字 (列名1,列名2)---不允許一起重複

    sql語句補充
    select * from 表名;
    select 列名 as 別名 from 表名 where 條件;
    select 列名 as 別名,數字 from 表名 where 條件;(加額外的數字列)
    
    select * from 表名 where 列名 != x;(不等於)
    select * from 表名 where 列名 in (x,y,z);(in(在):x,y,z)
    select * from 表名 where 列名 not in (x,y,z);(不在)
    select * from 表1名 where 列名 in (select 列名 from 表2);(和表2中相同的)
    select * from 表名 where 列名 between x and y; (介於x-y,包含邊界)   
    
    select * from 表名 where 列名 like "a%";(%代表所有的值,數量:0-無限多)
    select * from 表名 where 列名 like "a_";(_代表所有的值,資料1個)
    
    select * from 表名 limit x;(前x條)
    
    select * from 表名 limit x,y;(起始位置從x起,往後取y條)
    
    select * from 表名 order by 列名 desc; #大到小
    select * from 表名 order by 列名 asc;  #小到大
    select * from 表名 order by 列名1 desc,列名2 desc;分開排序
    取後y條資料(先排序在取值)
    select * from 表名 order by 列名 desc limit y;  
    分組:
    select count(列名),max(列名),part_id from 表名 group by 列名;(sum:求和,avg:求平均值)
    連表:
    select * from 表1,表2 where 條件;

  select * from 表1 left join 表2 on 列1=列2;