2020年4月25日
CREATE TABLE habitat_codes{
habitat_id INT AUTO_INCREMENT PRIMARY KEY,
habitat VARCHAR(25)
};
INSERT INTO habitat_codes(habitat)
VALUES('coasts'),('deserts'),('forsets'),('grasslands'),('lakes,rivers,ponds'),('marshes,swamps'),('white'),('black')
CREATE TABLE bird_habitats(bird_id INT,habitat_id INT);
DESC bird_habitat;
RENAME TABLE bird_habitat TO birds_habitat;
DESC bird_habitat;
ALTER TABLE birds_habitats ADD UNIQUE birds_habitats(bird_id,habitat_id);
SHOW CREATE TABLE birds_habitat;
ALTER TABLE birds_habitat DROP INDEX,ADD COLUMN bird_habitat(bird_id,habitat_id);
ALTER TABLE birds_habitats ADD COLUMN(contry_id CHAR(2),men_type VARCHAR(10),men_expiration DATA);
\
CREATE TABLE habitat_codes{
habitat_id INT AUTO_INCREMENT PRIMARY KEY,
habitat VARCHAR(25)
};
INSERT INTO habitat_codes(habitat)
VALUES('coasts'),('deserts'),('forsets'),('grasslands'),('lakes,rivers,ponds'),('marshes,swamps'),('white'),('black')
CREATE TABLE bird_habitats(bird_id INT,habitat_id INT);
DESC bird_habitat;
RENAME TABLE bird_habitat TO birds_habitat;
DESC bird_habitat;
ALTER TABLE birds_habitats ADD UNIQUE birds_habitats(bird_id,habitat_id);
SHOW CREATE TABLE birds_habitat;
ALTER TABLE birds_habitat DROP INDEX,ADD COLUMN bird_habitat(bird_id,habitat_id);
ALTER TABLE birds_habitats ADD COLUMN(contry_id CHAR(2),men_type VARCHAR(10),men_expiration DATA);
\
CREATE TABLE habitat_codes{
habitat_id INT AUTO_INCREMENT PRIMARY KEY,
habitat VARCHAR(25)
};
INSERT INTO habitat_codes(habitat)
VALUES('coasts'),('deserts'),('forsets'),('grasslands'),('lakes,rivers,ponds'),('marshes,swamps'),('white'),('black')
CREATE TABLE bird_habitats(bird_id INT,habitat_id INT);
DESC bird_habitat;
RENAME TABLE bird_habitat TO birds_habitat;
DESC bird_habitat;
ALTER TABLE birds_habitats ADD UNIQUE birds_habitats(bird_id,habitat_id);
SHOW CREATE TABLE birds_habitat;
ALTER TABLE birds_habitat DROP INDEX,ADD COLUMN bird_habitat(bird_id,habitat_id);
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;