当前位置:网站首页>postgresql 数据库如何设置主键自增长
postgresql 数据库如何设置主键自增长
2022-07-17 05:21:00 【是一个菜鸟程序员啊】
今天第一次使用postgresql数据库,发现它和mysql很不一样,不能使用视图设置主键id自增长,因此可以添加一个序列到主键上面去,这样就可以实现自增长啦!
以下,是添加序列化和把序列化设置到id上面的sql语句:
CREATE SEQUENCE "employee_management_emp_seq" //设置序列名
INCREMENT 1 //增长值
MINVALUE 1 //最小值
MAXVALUE 999 //最大值
START 1 //初始值
CACHE 1; //缓存以上的值对应这张图,也可以直接在这里添加序列
点击其他,然后选择序列

最后一步就是把刚刚添加好的序列设置到主键上
alter table 表名 alter column 主键字段名 set default nextval('employee_management_emp_seq'(刚刚添加的序列名))到这里就大功告成啦!这个时候只需回去看一下你刚刚的表,就会发现主键的字段下面的默认约束条件从NULL变成序列了,如下图:

边栏推荐
猜你喜欢

伺服电机的电子齿轮比如何确定?

OLED data display based on spi/iic interface

用Altium Designer绘制PCB图

阿拉伯数字转中文

开发板训练:STM32下的多任务程序

Wu Enda machine learning chapter 14-15

day01(Flume)

No application for domain name SSL certificate under ports 80 and 443 (applicable to acme.sh and certbot)

Wu Enda machine learning chapter 10-11

Wu Enda machine learning chapter 12-13
随机推荐
redis
Some problems encountered in work
总价合同、成本补偿合同、工料合同
高并发day04(ZAB协议,观察者,nc,AVRO,RPC)
BigDecimal中divide方法
Wireshark packet capture: message information
STM32串口通信相关学习
Slag learning road (2) pure white direction: win Server 2003 server building
notepad++下划线以及大小写字母置换
top命令
Full experience of soft examination at the beginning, middle and advanced levels
Thales安全解决方案:怎么提高国家网络安全的关键步骤
企业数据治理之勒索病毒篇
基于Ardunio IDE软件开发编程学习
ARM开发环境入门
Wu Enda machine learning chapter 10-11
Application case of CS brand SD NAND in air quality inspection industry
libevent 报 undefined reference to `getrandom‘
ANAME
Part of the second Shanxi Network Security Skills Competition (Enterprise Group) WP (II)