MySQL基本知識ポイント

3542 ワード

1,MySQLは大規模なデータベースをサポートし、最大5千万行以上を1つのテーブルに収容できます.各テーブルのデフォルトファイルサイズは4 GBに制限されていますが、オペレーティングシステムがサポートしている場合は、理論的な制限を800万TBに増やすことができます.
2,MySQLの列の自増、ある列に自増列を設定した場合、データを挿入するときにこの列を設定する必要がなく、デフォルトでは自増(表には1つの自増列しかありません)
create table tb_name(
    nid int not null auto_increment primary key,
    num int null
)ENGINE=InnoDB DEFAULT CHARSET=utf8

または
create table tb_name(
    nid int not null auto_increment,
    num int null,
    index(nid)
)ENGINE=InnoDB DEFAULT CHARSET=utf8
  • 自己増分列については、インデックス(プライマリ・キーを含む)である必要があります.
  • 自己増加について、ステップ長および開始値
  • を設定することができる.
    show session variables like 'auto_inc%';
    set session auto_increment_increment=2;
    set session auto_increment_offset=10;
    
    shwo global variables like 'auto_inc%';
    set global auto_increment_increment=2;
    set global auto_increment_offset=10;
    

    3,MySQLのプライマリ・キーは、特殊な一意のインデックスであり、NULL値は許可されません.プライマリ・キーが単一のカラムを使用する場合、その値は一意でなければなりません.複数のカラムの場合、その組合せは一意でなければなりません.
    create table tb1(
        nid int not null auto_increment primary key,
        num int null
    )
    

    または
    create table tb1(
        nid int not null,
        num int not null,
        primary key(nid,num)
    )
    

    4,MySQLの空のテーブル
    --           ,                    
    delete from tb_name;
    --           ,                        
    truncate table tb_name;
    

    5,MySQLでの変更表
    --    
    alter table    add      ;
    
    --    
    alter table    drop column   ;
    
    --    
    alter table    modify column      ;  --   
    alter table    change           ; --   ,  
    
    --     
    alter table    add primary key(  );
    
    --     
    alter table    drop primary key;
    alter table     modify     int, drop primary key;
    
    --     
    alter table    add constraint     (  :FK_  _  ) foreign key   (    ) references   (    );
    
    --     
    alter table    drop foreign key     ;
    
    --      
    ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
    
    --      
    ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
    

    6,MySQLの列挙:ENUM('value 1','value 2',...)
    CREATE TABLE shirts (
        name VARCHAR(40),
        size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
    );
    
    INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),
      ('polo shirt','small');
    
    mysql> SELECT name, size FROM shirts WHERE size = 'medium';
    +---------+--------+
    | name    | size   |
    +---------+--------+
    | t-shirt | medium |
    +---------+--------+
    1 row in set (0.00 sec)
    
    UPDATE shirts SET size = 'small' WHERE size = 'large';
    
    COMMIT;
    

    7,MySQLでの集合:SET('value 1','value 2',...)
    mysql> CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
    Query OK, 5 rows affected (0.01 sec)
    Records: 5  Duplicates: 0  Warnings: 0
    
    mysql> SELECT col FROM myset;
    +------+
    | col  |
    +------+
    | a,d  |
    | a,d  |
    | a,d  |
    | a,d  |
    | a,d  |
    +------+
    5 rows in set (0.00 sec)
    

    8,MySQL Full Joinの実装左右の2つのテーブルのデータを取り出し、MySQL Full Joinの実装に一致するかどうかにかかわらずMySQLはFULL JOINをサポートしていないので、次の代替方法です.
    select * from A left join B on A.id = B.id (where   )
    union --all  
    select * from A right join B on A.id = B.id (where  );