单表操作(修改表字段 复制表 表数据迁移 单表查询)
Posted 我的紫霞辣辣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单表操作(修改表字段 复制表 表数据迁移 单表查询)相关的知识,希望对你有一定的参考价值。
修改表字段
create table t1(id int);
desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
# add默认从最后一个字段增加
alter table t1 add name varchar(5) not null, add age int not null;
desc t1;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(5) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+------------+------+-----+---------+-------+
# add + first在字段开头增加
alter table t1 add haha tinyint first;
desc t1;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| haha | tinyint(4) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(5) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+------------+------+-----+---------+-------+
# add + after指定字段进行插入
alter table t1 add y varchar(3) after id;
desc t1;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| haha | tinyint(4) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| y | varchar(3) | YES | | NULL | |
| name | varchar(5) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+------------+------+-----+---------+-------+
# alter + drop指定字段进行删除
alter table t1 drop y;
desc t1;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| haha | tinyint(4) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(5) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+------------+------+-----+---------+-------+
# alter + modify指定字段进行属性修改
alter table t1 modify name varchar(10);
desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| haha | tinyint(4) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
# alter + change 修改表的字段名
alter table t1 change name new_name varchar(10) not null;
desc t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| haha | tinyint(4) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| new_name | varchar(10) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
复制表
# \\G标准输出(将字段按照每一行显示)
select * from mysql.user\\G
# select默认会将输出结果拼接成一个表格形式
select host,user,password from mysql.user;
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| db01 | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| db01 | | |
+-----------+------+-------------------------------------------+
# 复制表+表数据(创建表,将表mysql.user的输出结果打印到t2表中)
create table t2 select host,user,password from mysql.user;
desc t2;
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| host | char(60) | NO | | | |
| user | char(16) | NO | | | |
| password | char(41) | NO | | | |
+----------+----------+------+-----+---------+-------+
select * from t2;
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | root | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| db01 | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| db01 | | |
+-----------+------+-------------------------------------------+
# 只复制表结构,where后面的条件为false,没有输出结果
select host,user,password from mysql.user where 1>10;
# create + select + where(条件为false)无结果输出
create table t3 select host,user,password from mysql.user where 1>10;
desc t3;
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| host | char(60) | NO | | | |
| user | char(16) | NO | | | |
| password | char(41) | NO | | | |
+----------+----------+------+-----+---------+-------+
select * from t3;
Empty set (0.00 sec)
表中数据的增删改
# 插入数据的几种方式
create table t4(id int,name varchar(10),age int);
# 按照顺序插入数据
insert t4 values(1,"nana",18);
insert t4 values(1,"xixi",19);
# 按照字段插入数据
insert t4(name,id,age) values("haha",3,20);
# 批量插入数据
insert t4 values(2,"lala",16),(4,"dudu",19);
select * from t4;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | nana | 18 |
| 1 | xixi | 19 |
| 3 | haha | 20 |
| 2 | lala | 16 |
| 4 | dudu | 19 |
+------+------+------+
表中数据的迁移
create table test01(
id int primary key auto_increment,
name varchar(16) not null unique,
age int not null,
email varchar(16) not null,
born_year int not null,
reg_time timestamp
);
desc test01;
+-----------+-------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+-------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(16) | NO | UNI | NULL | |
| age | int(11) | NO | | NULL | |
| email | varchar(16) | NO | | NULL | |
| born_year | int(11) | NO | | NULL | |
| reg_time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-----------+-------------+------+-----+-------------------+-----------------------------+
insert into test01(name,age,email,born_year,reg_time) values
("nana",18,"12@qq.com",1999,now()),
("lala",19,"12@qq.com",1998,now()),
("haha",20,"12@qq.com",1997,now()),
("xixi",21,"12@qq.com",1993,now());
select * from test01查询语句 select [单表操作]