POSTGRESQL和 MYSQL的自增字段比较

POSTGRESQL和 MYSQL的自增字段比较,第1张

概述最近学习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 affec 最近学习PGsql。来比较一下他和MysqL自增字段的不同点。
1、自增序列。MysqL从最后一个ID自增。
测试数据。

1, I love this girl.
2.
3is my girl.
4is your girl.
MysqL:
MysqL> create database test;
query OKrow affected (0.10 sec)

MysqL> use test
Database changed
MysqLtable t(ID int not null auto_increment primary key-> username char(20) null)rows affected .02 sec)
MysqL> load data infile '/tmp/test.sql' into table t fIElds terminated by ','.00 sec)
Records: 4 Deleted: 0 Skipped: 0 Warnings: 0

MysqLselect * from t;
+----+-------------------+

| ID | username |
| 1 | I love this girl. | 2 | I hate this girl| 3 | She | 4 is your girl----+-------------------+

4 rows in set insert into t values (6'This is inserted'into t(usernamevalues('This is last'| 6 | This is inserted | 7 is last ----+-------------------+


MysqLtruncate ----+--------------+

----+--------------+

3 )

@H_403_229@PGsql从1开始逐个尝试。


[root@localhost ~]# psql -Upostgres -hlocalhost
。。。
postgres=# CREATE DATABASE
postgres\c test
You are Now connected to database "test".
test(ID serial ;
NOTICE: table will create implicit sequence "t_ID_seq" for serial column "t.ID"
table
test\d ttable "public.t"
Column | Type | ModifIErs
----------+---------------+------------------------------------------------

ID integer null default nextval't_ID_seq'::regclass)
username | characternull
test# copy t from with csv;
copY 4
test;
ID | username
----+----------------------

1 .
2 .
3 .
4 .
(4 rows)

testINSERT 0 1
test;
ID1重复
ERROR: duplicate key violates unique constraint "t_pkey"
test;
ID2重复
ERROR;
。。。
ID5没有。插入
;
ID6又重复
ERROR..
.
6 is inserted
5 is last
7 is last
8 is last
9 is last
(9 )
看一下DELETE *** 作。
testdelete DELETE 9
test----+----------------------

10 is last
11 is last
12 (3 )
这个和MysqL一样的。
TruncATE虽然和MysqL一样可以快速清空表数据。可是ID还是从最后一个开始增加的,如果想从1开始的话,就得用setval函数来设置。
testTruncATE ----+----------------------

13 is last
14 is last
15 )
至于怎么从1重新开始。还在学习中。。。


2、得到刚刚插入的自增ID。

@H_403_229@在MysqL里面:
MysqLselect last_insert_ID(------------------+

| last_insert_ID------------------+

1 row )
@H_403_229@在POSTGREsql里面:

testdrop table t
testDROP "t.ID"
NOTICEtable / PRIMARY KEY will index "t_pkey" for table "t"
\d t
null
Indexes:
KEY(ID'This is test name'is test name
(1 rowselect currval;
currval
---------

1
#
3、设置自增ID的开始值。
@H_403_229@MysqL:

MysqLalter table t auto_increment = 3.01 sec: 1 Duplicates----+--------------+

2 POSTGREsql:

t_girlselect setvalfalse;
setval
--------

1
)

Time: 19.554 ms
t_girl'wangwei''meimei'INSERT 0 2
Time: 1.882 ms
t_girl| wangwei
2 | meimei
(2 : 0.598 ms
总结

以上是内存溢出为你收集整理的POSTGRESQL和 MYSQL的自增字段比较全部内容,希望文章能够帮你解决POSTGRESQL和 MYSQL的自增字段比较所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1180926.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存