postgres create table as select & create table like
2015-01-16 08:54
543 查看
1. 准备
先建立一个基础表:
查看mytb1的表结构:
插入两条记录:
2. 使用created table as select
3. 使用create table like
create table like的时候通过including来包含索引,约束等。
结论:
create table as select : 只会复制表中的数据,表结构中的索引,约束等不会复制过来。
create table like: 不复制数据,需要复制索引,约束等还要自己including进去。
先建立一个基础表:
create table mytb1(id serial,name character varying,age integer);在name字段上创建索引:
create index mytb1_name_index on mytb1(name);
查看mytb1的表结构:
postgres=# \d mytb1; Table "public.mytb1" Column | Type | Modifiers --------+-------------------+--------------------------------------------------- id | integer | not null default nextval('mytb1_id_seq'::regclass) name | character varying | age | integer | Indexes: "mytb1_name_index" btree (name)
插入两条记录:
postgres=# insert into mytb1(name,age) values('zhangsan',12),('lisi',23);
2. 使用created table as select
postgres=# create table mytb2 as select * from mytb1; SELECT 2查看mytb2的表结构及表中的数据:
postgres=# \d mytb2 Table "public.mytb2" Column | Type | Modifiers --------+-------------------+----------- id | integer | name | character varying | age | integer |
postgres=# select * from mytb2; id | name | age ----+----------+----- 1 | zhangsan | 12 2 | lisi | 23 (2 rows)可以看到,使用create table as select ,表中的数据会全部复制过去,表结构中的主键,索引,约束等都没有移过去,仅仅是字段复制过去。
3. 使用create table like
postgres=# create table mytb3 (like mytb1); CREATE TABLE查看mytb3的表结构及表中的数据:
postgres=# \d mytb3 Table "public.mytb3" Column | Type | Modifiers --------+-------------------+----------- id | integer | not null name | character varying | age | integer |
postgres=# select * from mytb3; id | name | age ----+------+----- (0 rows)表结构中的索引主键也没有一起过来。
create table like的时候通过including来包含索引,约束等。
postgres=# create table mytb5 (like mytb1 including defaults including constraints including indexes); CREATE TABLE看看表结构
postgres=# \d mytb5 Table "public.mytb5" Column | Type | Modifiers --------+-------------------+--------------------------------------------------- id | integer | not null default nextval('mytb1_id_seq'::regclass) name | character varying | age | integer | Indexes: "mytb5_name_idx" btree (name)现在索引,约束等都全部复制过来。
结论:
create table as select : 只会复制表中的数据,表结构中的索引,约束等不会复制过来。
create table like: 不复制数据,需要复制索引,约束等还要自己including进去。
相关文章推荐
- create table xxx as select 与 create table xxx like
- create table tb as select 和create table tb like的区别
- MySQL中表复制:create table like 与 create table as select
- oracle数据迁移--复制表--create table as select & select into & insert into
- MySQL中表复制:create table like 与 create table as select
- MySQL中表复制:create table like 与 create table as select
- Mysql create table tb as select 和create table tb like的区别
- MySQL中表复制:create table like 与 create table as select
- "CREATE TABLE AS SELECT"以及"INSERT INTO AS SELECT"语句的优化
- MySQL中表复制:create table like 与 create table as select
- 如何使‘CREATE TABLE AS SELECT’能支持ORDER BY ?
- create table like 和create table select 比较
- MySQL的create table as 与 like区别
- hive基本的操作语句(实例简单易懂,create table XX as select XX)
- create table as …
- Oracle Create Table as Select
- insert into select 与 create table as的用法和区别
- oracle 中 create table tb_content_bak as select * from tb_content where 1=2 long字段解决办法
- hive基本的操作语句(实例简单易懂,create table XX as select XX)
- MySQL create table as 与 like区别