新建一张表:
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。