2020年4月25日

    1. CREATE TABLE habitat_codes{
    2. habitat_id INT AUTO_INCREMENT PRIMARY KEY,
    3. habitat VARCHAR(25)
    4. };
    5. INSERT INTO habitat_codes(habitat)
    6. VALUES('coasts'),('deserts'),('forsets'),('grasslands'),('lakes,rivers,ponds'),('marshes,swamps'),('white'),('black')
    7. CREATE TABLE bird_habitats(bird_id INT,habitat_id INT);
    8. DESC bird_habitat;
    9. RENAME TABLE bird_habitat TO birds_habitat;
    10. DESC bird_habitat;
    11. ALTER TABLE birds_habitats ADD UNIQUE birds_habitats(bird_id,habitat_id);
    12. SHOW CREATE TABLE birds_habitat;
    13. ALTER TABLE birds_habitat DROP INDEX,ADD COLUMN bird_habitat(bird_id,habitat_id);
    14. ALTER TABLE birds_habitats ADD COLUMN(contry_id CHAR(2),men_type VARCHAR(10),men_expiration DATA);
    15. \
    16. CREATE TABLE habitat_codes{
    17. habitat_id INT AUTO_INCREMENT PRIMARY KEY,
    18. habitat VARCHAR(25)
    19. };
    20. INSERT INTO habitat_codes(habitat)
    21. VALUES('coasts'),('deserts'),('forsets'),('grasslands'),('lakes,rivers,ponds'),('marshes,swamps'),('white'),('black')
    22. CREATE TABLE bird_habitats(bird_id INT,habitat_id INT);
    23. DESC bird_habitat;
    24. RENAME TABLE bird_habitat TO birds_habitat;
    25. DESC bird_habitat;
    26. ALTER TABLE birds_habitats ADD UNIQUE birds_habitats(bird_id,habitat_id);
    27. SHOW CREATE TABLE birds_habitat;
    28. ALTER TABLE birds_habitat DROP INDEX,ADD COLUMN bird_habitat(bird_id,habitat_id);
    29. ALTER TABLE birds_habitats ADD COLUMN(contry_id CHAR(2),men_type VARCHAR(10),men_expiration DATA);
    30. \
    31. CREATE TABLE habitat_codes{
    32. habitat_id INT AUTO_INCREMENT PRIMARY KEY,
    33. habitat VARCHAR(25)
    34. };
    35. INSERT INTO habitat_codes(habitat)
    36. VALUES('coasts'),('deserts'),('forsets'),('grasslands'),('lakes,rivers,ponds'),('marshes,swamps'),('white'),('black')
    37. CREATE TABLE bird_habitats(bird_id INT,habitat_id INT);
    38. DESC bird_habitat;
    39. RENAME TABLE bird_habitat TO birds_habitat;
    40. DESC bird_habitat;
    41. ALTER TABLE birds_habitats ADD UNIQUE birds_habitats(bird_id,habitat_id);
    42. SHOW CREATE TABLE birds_habitat;
    43. ALTER TABLE birds_habitat DROP INDEX,ADD COLUMN bird_habitat(bird_id,habitat_id);
    44. ALTER TABLE birds_habitats ADD COLUMN(contry_id CHAR(2),men_type VARCHAR(10),men_expiration DATA);

    索引:
    字段不等于索引
    字段就只是字段,索引也只是索引,把索引添加到字段上只是一个绑定
    ALTER TABLE
    显示表的所有索引:
    SHOW INDEX FORM 表名;
    查看语句是否使用索引:
    EXPLAIN 语句;
    查看建表语句:
    SHOW CREATE TABLE 表名;
    添加索引:
    INDEX 普通索引
    UNIQUE 唯一索引

    ALTER TABLE 表名 ADD INDEX 索引名 (字段1[,字段2….]);
    ALTER TABLE users ADD INDEX ziji(name,password);

    删除索引:
    INDEX PRIMARY KEY
    ALTER TABLE 表名 DROP[INDEX | PRIMARY KEY] [索引名];
    ALTER TABLE users DROP INDEX phone;
    ALTER TABLE users DROP INDEX ziji;

    删除主键索引,同时也要删除自增:
    ALTER TABLE users DROP PRIMARY KEY,CHANGE COLUMN user_id INT;

    修改主键的名字

    ALTER TABLE users DROP PRIMARY KEY,CHANGE COLUMN id user_id INT AUTO_INCREMENT PRIIMARY KEY;