postgresql 枚举_PostgreSQL替换枚举的3种方法

本文介绍了如何在PostgreSQL中替换枚举类型,提供了三种不同的方法,详细探讨了在数据库设计中处理枚举类型的策略。
摘要由CSDN通过智能技术生成

postgresql 枚举

技术评估 (Technical Evaluations)

It is a common situation where the value of a field is restricted to a list of valid options.

在一种常见的情况下,字段的值仅限于有效选项的列表。

For examples,

举些例子,

  • Genders: male, female , …

    性别malefemale ,…

  • Currencies: USD, SGD, CAD, AUD, …

    货币USDSGDCADAUD

  • Countries: us, uk , sg… (200+ options)

    国家usuksg ...(200+选项)

  • Statuses: pending , processing , failed , completed , …

    状态pendingprocessingfailedcompleted ...

  • Genres: romance , comedy , action , …

    类型romancecomedyaction ……

数据验证和完整性 (Data validation and integrity)

Although data validation can be done at the APPLICATION LOGIC LAYER either

尽管可以在APPLICATION LOGIC LAYER上完成数据验证

  • on the client-side (form validations)

    在客户端(表单验证)
  • OR on the server-side (API or model validations),

    或在服务器端(API或模型验证),

such validations can be bypassed by going straight to the lower layer.

可以直接进入下一层来绕过此类验证。

The safer way to ensure data integrity is by using DATABASE LEVEL CONSTRAINTS or VALIDATIONS.

确保数据完整性更安全方法是使用“ 数据库级别约束”或“ 验证”

This article would be discussing 4 techniques in which we can restrict the values to a set of valid options in PostgreSQL (also applicable to other databases)

本文将讨论4种技巧 ,其中我们可以将值限制为PostgreSQL中的一组有效选项(也适用于其他数据库)

  1. Enumerated Types

    枚举类型

  2. Simple CHECK constraints

    简单的CHECK约束

  3. CHECK constraints with FUNCTION

    使用FUNCTION检查约束

  4. FOREIGN KEYS

    外键

Each of the approaches has distinguished PROS, CONS, and therefore, is suitable for different use cases.

每种方法都具有不同的PROSCONS ,因此适用于不同的用例

评估方面 (Evaluation aspects)

  • ReadabilityHow easy to read and understand the validation

    可读性 多么容易 阅读 理解 验证

  • UsabilityHow easy to retrieve the list of options.The number of options

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值