标签:postgresql sql mysql
MySQL 有一个和优秀的语法 create table ... like , 可以快速复制一张表,创建其副本。 PostgreSQL 也有类似的语法,而且更加灵活,不过要注意些细节。+----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | log_time | datetime(6) | YES | | NULL | | +----------+------------------+------+-----+---------+----------------+
mysql> create table t2 like t1; Query OK, 0 rows affected (0.03 sec)
+----------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | log_time | datetime(6) | YES | | NULL | | +----------+------------------+------+-----+---------+----------------+
mysql> insert into t2 (log_time) select now(); Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from t2; +----+----------------------------+ | id | log_time | +----+----------------------------+ | 1 | 2014-11-27 13:44:12.000000 | +----+----------------------------+ 1 row in set (0.00 sec)
Table "ytt_sql.t1" Column | Type | Modifiers ----------+-----------------------------+------------------------------------------------- id | integer | not null default nextval(‘t1_id_seq‘::regclass) log_time | timestamp without time zone | Indexes: "t1_pkey" PRIMARY KEY, btree (id)
t_girl=# create table t2 (like t1 including all); CREATE TABLE Time: 50.035 ms
Table "ytt_sql.t2" Column | Type | Modifiers ----------+-----------------------------+------------------------------------------------- id | integer | not null default nextval(‘t1_id_seq‘::regclass) log_time | timestamp without time zone | Indexes: "t2_pkey" PRIMARY KEY, btree (id)
t_girl=# select currval(‘t1_id_seq‘); currval --------- 120 (1 row) Time: 3.771 ms
t_girl=# create sequence t2_id_seq; CREATE SEQUENCE Time: 12.744 ms
t_girl=# alter table t2 alter id set default nextval(‘t2_id_seq‘); ALTER TABLE Time: 5.002 ms
t_girl=# insert into t2 (log_time) values ....; INSERT 0 10 Time: 10.331 ms
t_girl=# select * from t2; id | log_time ----+---------------------------- 1 | 2014-03-09 06:49:14.393962 2 | 2005-12-30 05:49:14.393962 3 | 2014-05-17 20:49:14.393962 4 | 2004-06-15 22:49:14.393962 5 | 2010-06-19 03:49:14.393962 ... 10 | 2009-09-07 23:49:14.393962 (10 rows) Time: 4.958 ms
t_girl=# create table t2 (like t1 including all excluding defaults); CREATE TABLE Time: 40.292 ms Table "ytt_sql.t2" Column | Type | Modifiers ----------+-----------------------------+----------- id | integer | not null log_time | timestamp without time zone | Indexes: "t2_pkey" PRIMARY KEY, btree (id)
t_girl=# create table t2 as table t1 with no data; SELECT 0 Time: 15.562 ms 或者 t_girl=# create table t2 as select * from t1 where false; SELECT 0 Time: 14.181 ms
我们手动给添加主键以及默认值。
t_girl=# alter table t2 add constraint pk_t2_id primary key (id), alter id set default nextval(‘t2_id_seq‘); ALTER TABLE Time: 41.105 ms
Table "ytt_sql.t2" Column | Type | Modifiers ----------+-----------------------------+------------------------------------------------- id | integer | not null default nextval(‘t2_id_seq‘::regclass) log_time | timestamp without time zone | Indexes: "pk_t2_id" PRIMARY KEY, btree (id)
标签:postgresql sql mysql
原文地址:http://blog.csdn.net/yueliangdao0608/article/details/41800199