PostgreSQL-主键约束和唯一性约束的区别

在建索引的过程中遇到一些问题,网络上搜索了一下基础知识。一直以为唯一索引就已经是主键了,至少在pg中看来不是这么回事儿。

目录概览

1)主键约束(PRIMARY KEY)

2)唯一性约束(UNIQUE)

3)唯一索引(INDEX)


1)主键约束(PRIMARY KEY)

    1) 主键用于唯一地标识表中的每一条记录,可以定义一列或多列为主键。 
    2) 是不可能(或很难)更新。 
    3) 主键列上没有任何两行具有相同值(即重复值),不允许空(NULL)。
    4) 主健可作外健,唯一索引不可。

2)唯一性约束(UNIQUE)

    1) 唯一性约束用来限制不受主键约束的列上的数据的唯一性,用于作为访问某行的可选手段,一个表上可以放置多个唯一性约束。
    2) 只要唯一就可以更新。 
    3) 即表中任意两行在  指定列上都不允许有相同的值,允许空(NULL)。
    4) 一个表上可以放置多个唯一性约束。

3)唯一索引(INDEX)

    创建唯一索引可以确保任何生成重复键值的尝试都会失败。
    
    唯一性约束和主键约束的区别:
        (1)唯一性约束允许在该列上存在NULL值,而主键约束的限制更为严格,不但不允许有重复,而且也不允许有空值。
        (2)在创建唯一性约束和主键约束时可以创建聚集索引和非聚集索引,但在 默认情况下主键约束产生聚集索引,而唯一性约束产生非聚集索引
    
    约束和索引, 前者是用来检查数据的正确性,后者用来实现数据查询的优化,目的不同。
    
    唯一性约束与唯一索引有所不同:
    
    (1)创建唯一约束会在Oracle中创建一个Constraint,同时也会创建一个该约束对应的唯一索引。
    (2)创建唯一索引只会创建一个唯一索引,不会创建Constraint。
    
    也就是说其实唯一约束是通过创建唯一索引来实现的。
    
    在删除时这两者也有一定的区别: 删除唯一约束时可以只删除约束而不删除对应的索引,所以对应的列还是必须唯一的,而删除了唯一索引的话就可以插入不唯一的值。
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
This release contains a variety of fixes from 14.0. For information about new features in major release 14, see Section E.2. A dump/restore is not required for those running 14.X. However, note that installations using physical replication should update standby servers before the primary server, as explained in the third changelog entry below. Also, several bugs have been found that may have resulted in corrupted indexes, as explained in the next several changelog entries. If any of those cases apply to you, it's recommended to reindex possibly-affected indexes after updating. Make the server reject extraneous data after an SSL or GSS encryption handshake (Tom Lane) A man-in-the-middle with the ability to inject data into the TCP connection could stuff some cleartext data into the start of a supposedly encryption-protected database session. This could be abused to send faked SQL commands to the server, although that would only work if the server did not demand any authentication data. (However, a server relying on SSL certificate authentication might well not do so.) The PostgreSQL Project thanks Jacob Champion for reporting this problem. (CVE-2021-23214) Make libpq reject extraneous data after an SSL or GSS encryption handshake (Tom Lane) A man-in-the-middle with the ability to inject data into the TCP connection could stuff some cleartext data into the start of a supposedly encryption-protected database session. This could probably be abused to inject faked responses to the client's first few queries, although other details of libpq's behavior make that harder than it sounds. A different line of attack is to exfiltrate the client's password, or other sensitive data that might be sent early in the session. That has been shown to be possible with a server vulnerable to CVE-2021-23214. The PostgreSQL Project thanks Jacob Champion for reporting this problem. (CVE-2021-23222) Fix physical replication for cases where the primary crashes after shipping a WAL se

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值