设为首页 收藏本站
查看: 950|回复: 0

[经验分享] POSTGRESQL和 MYSQL的自增字段比较

[复制链接]

尚未签到

发表于 2018-9-29 13:24:19 | 显示全部楼层 |阅读模式
最近学习PGSQL。来比较一下他和MYSQL自增字段的不同点。  1、自增序列。MYSQL从最后一个ID自增。
  测试数据。
  1, I love this girl.
  2, I hate this girl.
  3, She is my girl.
  4, She is your girl.
  MYSQL:
  mysql> create database test;
  Query OK, 1 row affected (0.10 sec)
  mysql> use test
  Database changed
  mysql> create table t(id int not null auto_increment primary key,
  -> username char(20) not null);
  Query OK, 0 rows affected (0.02 sec)
  mysql> load data infile '/tmp/test.sql' into table t fields terminated by ',';
  Query OK, 4 rows affected (0.00 sec)
  Records: 4 Deleted: 0 Skipped: 0 Warnings: 0
  mysql> select * from t;
  +----+-------------------+

  |>  +----+-------------------+
  | 1 | I love this girl. |
  | 2 | I hate this girl. |
  | 3 | She is my girl. |
  | 4 | She is your girl. |
  +----+-------------------+
  4 rows in set (0.00 sec)
  mysql> insert into t values (6,'This is inserted');
  Query OK, 1 row affected (0.00 sec)
  mysql> insert into t(username) values('This is last');
  Query OK, 1 row affected (0.00 sec)
  mysql> select * from t;
  +----+-------------------+

  |>  +----+-------------------+
  | 1 | I love this girl. |
  | 2 | I hate this girl. |
  | 3 | She is my girl. |
  | 4 | She is your girl. |
  | 6 | This is inserted |
  | 7 | This is last |
  +----+-------------------+
  mysql> truncate table t;
  Query OK, 0 rows affected (0.00 sec)
  mysql> insert into t(username) values('This is last');
  Query OK, 1 row affected (0.00 sec)
  mysql> insert into t(username) values('This is last');
  Query OK, 1 row affected (0.00 sec)
  mysql> insert into t(username) values('This is last');
  Query OK, 1 row affected (0.00 sec)
  mysql> select * from t;
  +----+--------------+

  |>  +----+--------------+
  | 1 | This is last |
  | 2 | This is last |
  | 3 | This is last |
  +----+--------------+
  3 rows in set (0.00 sec)
  PGSQL从1开始逐个尝试。
  [root@localhost ~]# psql -Upostgres -hlocalhost
  。。。
  postgres=# create database test;
  CREATE DATABASE
  postgres=# \c test
  You are now connected to database "test".
  test=# create table t(id serial not null,username char(20) not null);
  NOTICE: CREATE TABLE will create implicit sequence "t_id_seq" for serial column "t.id"
  CREATE TABLE
  test=# \d t;
  Table "public.t"
  Column | Type | Modifiers
  ----------+---------------+------------------------------------------------
  id | integer | not null default nextval('t_id_seq'::regclass)
  username | character(20) | not null
  test=# copy t from '/tmp/test.sql' with csv;
  COPY 4
  test=# select * from t;
  id | username
  ----+----------------------
  1 | I love this girl.
  2 | I hate this girl.
  3 | She is my girl.
  4 | She is your girl.
  (4 rows)
  test=# insert into t values (6,'This is inserted');
  INSERT 0 1
  test=# insert into t(username) values('This is last');
  ID1重复
  ERROR: duplicate key violates unique constraint "t_pkey"
  test=# insert into t(username) values('This is last');
  ID2重复
  ERROR: duplicate key violates unique constraint "t_pkey"
  test=# insert into t(username) values('This is last');
  。。。
  ID5没有。插入
  INSERT 0 1
  test=# insert into t(username) values('This is last');
  ID6又重复
  ERROR: duplicate key violates unique constraint "t_pkey"
  test=# insert into t(username) values('This is last');
  ...
  INSERT 0 1
  test=# insert into t(username) values('This is last');
  INSERT 0 1
  test=# insert into t(username) values('This is last');
  INSERT 0 1
  test=# select * from t;
  id | username
  ----+----------------------
  1 | I love this girl.
  2 | I hate this girl.
  3 | She is my girl.
  4 | She is your girl.
  6 | This is inserted
  5 | This is last
  7 | This is last
  8 | This is last
  9 | This is last
  (9 rows)
  看一下DELETE操作。
  test=# delete from t;
  DELETE 9
  test=# insert into t(username) values('This is last');
  INSERT 0 1
  test=# insert into t(username) values('This is last');
  INSERT 0 1
  test=# insert into t(username) values('This is last');
  INSERT 0 1
  test=# select * from t;
  id | username
  ----+----------------------
  10 | This is last
  11 | This is last
  12 | This is last
  (3 rows)
  这个和MYSQL一样的。
  TRUNCATE虽然和MYSQL一样可以快速清空表数据。可是ID还是从最后一个开始增加的,如果想从1开始的话,就得用setval函数来设置。
  test=# truncate table t;
  TRUNCATE TABLE
  test=# insert into t(username) values('This is last');
  INSERT 0 1
  test=# insert into t(username) values('This is last');
  INSERT 0 1
  test=# insert into t(username) values('This is last');
  INSERT 0 1
  test=# select * from t;
  id | username
  ----+----------------------
  13 | This is last
  14 | This is last
  15 | This is last
  (3 rows)
  至于怎么从1重新开始。还在学习中。。。
  2、得到刚刚插入的自增ID。
  在MYSQL里面:
  mysql> truncate table t;
  Query OK, 0 rows affected (0.00 sec)
  mysql> insert into t(username) values('This is last');
  Query OK, 1 row affected (0.00 sec)
  mysql> select last_insert_id();
  +------------------+
  | last_insert_id() |
  +------------------+
  | 1 |
  +------------------+
  1 row in set (0.00 sec)
  在POSTGRESQL里面:
  test=# drop table t
  test-# ;
  DROP TABLE
  test=# create table t(id serial not null primary key,username char(20) not null);
  NOTICE: CREATE TABLE will create implicit sequence "t_id_seq" for serial column "t.id"
  NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_pkey" for table "t"
  CREATE TABLE
  test=# \d t
  Table "public.t"
  Column | Type | Modifiers
  ----------+---------------+------------------------------------------------
  id | integer | not null default nextval('t_id_seq'::regclass)
  username | character(20) | not null
  Indexes:
  "t_pkey" PRIMARY KEY, btree (id)
  test=# insert into t(username) values('This is test name');
  INSERT 0 1
  test=# select * from t;
  id | username
  ----+----------------------
  1 | This is test name
  (1 row)
  test=# select currval('t_id_seq');
  currval
  ---------
  1
  (1 row)
  test=#
  3、设置自增ID的开始值。
  MYSQL:

  mysql>>  Query OK, 1 row affected (0.01 sec)
  Records: 1 Duplicates: 0 Warnings: 0
  mysql> insert into t(username) values('This is last');
  Query OK, 1 row affected (0.00 sec)
  mysql> select * from t;
  +----+--------------+

  |>  +----+--------------+
  | 1 | This is last |
  | 3 | This is last |
  +----+--------------+
  2 rows in set (0.00 sec)
  POSTGRESQL:
  t_girl=# select setval('t_id_seq',1,false);
  setval
  --------
  1
  (1 row)
  Time: 19.554 ms
  t_girl=# insert into t(username) values('wangwei'),('meimei');
  INSERT 0 2
  Time: 1.882 ms
  t_girl=# select * from t;
  id | username
  ----+----------------------
  1 | wangwei
  2 | meimei
  (2 rows)
  Time: 0.598 ms


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-603857-1-1.html 上篇帖子: CentOS 中安装mysql5 下篇帖子: Centos6.2+Open***+Radius+Mysql+daloRADIUS
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表