mysql 复制一条记录

新建一张表:

create table t_coffee
(
	id int auto_increment
		primary key,
	name varchar(111) null,
	price double(5,2) null,
	create_time datetime null,
	update_time datetime null
)
;

插入两条记录:

INSERT INTO t_coffee (id, name, price, create_time, update_time) VALUES (1, 'espresso', 20, '2019-05-19 22:18:14', '2019-05-19 22:18:14');
INSERT INTO t_coffee (id, name, price, create_time, update_time) VALUES (2, 'latte', 25, '2019-05-19 22:18:14', '2019-05-19 22:18:14');

在这里插入图片描述
复制记录:

insert into t_coffee (name,price) select name,price from t_coffee

在这里插入图片描述

如果是两张表的操作:

insert into table_name1  select * from table_name2

如果是同一张表,并且 id 是主键唯一的,那么这样就会冲突,
就可以使用对字段进行添加

insert into table_name1 (column1,column2)  select column1,column2 from table_name2 where condition
表的复制

在创建表时,就插入另一张表里面的某些数据

create table table_name1 as  select * from table_name2 where condition; 

​as创建出来的表table_name1(新表)缺少表table_name2(源表)的索引信息,只有表结构相同,没有索引。

create table  table_name1  like  table_name2;

like 创建出来的新表包含源表的完整表结构和索引信息
as用来创建相同表结构并复制源表数据
like用来创建完整表结构和全部索引

oracle支持as,也是只有表结构没有索引;oracle不支持like。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值