MySQL之修改表和复制表

修改表

# MySQL对大小写是不敏感的
1. 修改表名
	alter table 表名 rename 新表名
2. 增加字段
	alter table 表名 add 字段名 字段类型(宽度) 约束条件;
	# 将字段直接添加在标的最前面
	alter table 表名 add 字段名 字段类型(宽度) 约束条件 first;
	# 将字段指定跟在谁的后面
	alter table 表名 add 字段名 字段类型(宽度) 约束条件 after 字段名
3. 删除字段
	alter table 表名 drop 字段名;
4. 修改字段
	alter table 表名 modify 字段名 字段类型(宽度) 约束条件;
	alter table 表名 change 旧字段名 新字段名 字段类型(宽度) 约束条件;

复制表

我们SQL语句查询的结果其实也是一张虚拟表

create table 表名 select * from 旧表;
# 不能复制原表中的任何键,只复制了原表的结构
create table new_dep select * from dep where id >3;

select * from dep;
+-----+-------------+--------------------------+
| id  | dep_name    | dep_desc                 |
+-----+-------------+--------------------------+
|   3 | nb技术部    | 技术能力有限部门         |
| 200 | 外交部      | 多人外交                 |
+-----+-------------+--------------------------+
select * from new_dep;
+-----+-----------+--------------+
| id  | dep_name  | dep_desc     |
+-----+-----------+--------------+
| 200 | 外交部    | 多人外交     |
+-----+-----------+--------------+

desc dep;
+----------+----------+------+-----+---------+----------------+
| Field    | Type     | Null | Key | Default | Extra          |
+----------+----------+------+-----+---------+----------------+
| id       | int(11)  | NO   | PRI | NULL    | auto_increment |
| dep_name | char(16) | YES  |     | NULL    |                |
| dep_desc | char(32) | YES  |     | NULL    |                |
+----------+----------+------+-----+---------+----------------+
desc new_dep;
+----------+----------+------+-----+---------+-------+
| Field    | Type     | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| id       | int(11)  | NO   |     | 0       |       |
| dep_name | char(16) | YES  |     | NULL    |       |
| dep_desc | char(32) | YES  |     | NULL    |       |
+----------+----------+------+-----+---------+-------+
posted @ 2024-03-03 18:38  Xiao0101  阅读(30)  评论(0)    收藏  举报