半天搞定MySQL(全)
半天搞定MySQL(全)一
半天搞定MySQL(全)二
半天搞定MySQL(全)三
半天搞定MySQL(全)五
半天搞定MySQL(全)六
博主用的是8.0版本的MySQL,储存引擎是InnoDB,关于InnoDB这里不详细解释,需要的话推荐了解这篇博文(或者自行百度)https://www.jianshu.com/p/519fd7747137
7. alter 命令
作用:修改数据表名或者修改数据表字段
添加一个字段 add
mysql> alter table city_food add people varchar(20);
第一个插入
mysql> alter table city_food add people varchar(20) first;
后插
mysql> alter table city_food add pop varchar(20) after city_food;
删除一个字段 delete
mysql> alter table city_food drop people;
修改字段类型及名称 modify 或者 change
mosify
修改字段类型
mysql> alter table city_food modify pop int;
改字段名及类型 pop int->p varchar(20)
mysql> alter table city_food change pop p varchar(20);
修改默认值和not null
mysql> alter table city_food modify p int not null default 0;
添加主键
先确保主键不为空
mysql> alter table c_f modify food_ID int not null;
Query OK, 1 row affected (0.11 sec)
Records: 1 Duplicates: 0 Warnings: 0
添加
mysql> alter table c_f add primary key(food_ID);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
删除主键
mysql> alter table c_f drop primary key;
设置字段默认值
mysql> alter table city_food alter p set default 1;
修改表名
mysql> alter table city_food rename to c_f;
Query OK, 0 rows affected (0.03 sec)
8. 索引
MySQL索引的建立对于MySQL的高效运行是很重要的,索引可以大大提高MySQL的检索速度。
索引分单列索引和组合索引。单列索引,即一个索引只包含单个列,一个表可以有多个单列索引,但这不是组合索引。组合索引,即一个索引包含多个列。
实际上,索引也是一张表,该表保存了主键与索引字段,并指向实体表的记录。
上面都在说使用索引的好处,但过多的使用索引将会造成滥用。因此索引也会有它的缺点:虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行INSERT、UPDATE和DELETE。因为更新表时,MySQL不仅要保存数据,还要保存一下索引文件。
建立索引会占用磁盘空间的索引文件。
创建索引
mysql> create index city on city_spots(city_name);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
修改表索引
mysql> alter table city_play add index play(city_thing);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> create table city_food (
-> city_name varchar(20) not null,
-> city_food varchar(20),
-> food_kinds int,
-> food_ID int auto_increment,
-> index(food_ID));
Query OK, 0 rows affected (0.06 sec)
删除索引 drop
mysql> drop index play on city_play;
或者
mysql> ALTER table city_play drop index city_name;
唯一索引
索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必须唯一。
创建方式与普通索引相似,但是需要加上关键字:unique
例:
创建索引
mysql> create unique index city on city_play(city_name);
或者
mysql> alter table city_play add unique index city_name(city_name);
也许想问,我会创建索引了,那索引怎么使用啊?但是索引不需要人去使用的,也不能主动调用,就只能系统自己调用。
比如刚才给表c_f创建了people的索引ppl
此时进行查找,系统就会自动使用索引
mysql> select * from c_f where people regexp 'a$';
9. 临时表
MySQL 临时表在我们需要保存一些临时数据时是非常有用的。临时表只在当前连接可见,当关闭连接时,Mysql会自动删除表并释放所有空间。
创建临时表:
mysql> create temporary table ppp(
-> p1 int not null default 0,
-> p2 int,
-> p3 int);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into ppp values(1,2,3),(2,3,4),(3,4,5);
mysql> select * from ppp;
+----+------+------+
| p1 | p2 | p3 |
+----+------+------+
| 1 | 2 | 3 |
| 2 | 3 | 4 |
| 3 | 4 | 5 |
+----+------+------+
3 rows in set (0.00 sec)
手动删除临时表:
mysql> drop table ppp;
Query OK, 0 rows affected (0.00 sec)
用查询直接创建临时表的方式:
mysql> create temporary table xxx as(
-> select * from city_spots
-> limit 0,10000);
Query OK, 11 rows affected (0.01 sec)
Records: 11 Duplicates: 0 Warnings: 0
查询新建的临时表
mysql> select * from xxx;
+-----------+--------------------+------------+----------+----------+-----------+
| city_name | place_of_interest' | city_score | cost_rmb | time_way | time_stay |
+-----------+--------------------+------------+----------+----------+-----------+
| America | 未知 | 82 | 20000 | 1.5 | 4 |
| Iceland | Ice | 88 | 19000 | 1.5 | 5 |
| Italy | food | 79 | 20000 | 1 | 4 |
| Janpan | 未知 | 85 | 15000 | 1 | 3 |
| 上海 | 购物 | 80 | 13000 | 1 | 2.5 |
| 东北 | 雪 | 86 | 10000 | 1.5 | 5 |
| 北京 | 长城 | 93 | 15000 | 1 | 3 |
| 新疆 | 水果 | 90 | 12000 | 1.5 | 5 |
| 海南 | 沙滩 | 78 | 9000 | 1 | 4 |
| 西安 | 兵马俑 | 83 | 5500 | 1 | 3 |
| 香港 | 未知 | 76 | 8000 | 1 | 3 |
+-----------+--------------------+------------+----------+----------+-----------+
11 rows in set (0.00 sec)
退出MySQL服务器后再查询xxx表
mysql> select * from xxx;
ERROR 1146 (42S02): Table 'citys.xxx' doesn't exist
所以临时表只是当前连接可见,当关闭连接时,临时表会被删除并释放空间。
未完待续。。。。。
半天搞定MySQL(全)一
半天搞定MySQL(全)二
半天搞定MySQL(全)三
半天搞定MySQL(全)五
半天搞定MySQL(全)六