简体   繁体   English

如何在SQL TYPE上添加约束

[英]How to add constraints on a SQL TYPE

I'm a beginner, learning Oracle SQL. 我是一个初学者,正在学习Oracle SQL。

I need to create a type, with constraints on the variables it contains (for example, create a boolean type constrained to true/false/null. Don't tell me that boolean exists. this is just an illustrative example) 我需要创建一个类型,对其包含的变量进行约束(例如,创建一个约束为true / false / null的布尔类型。不要告诉我布尔存在。这只是一个示例)

I do not want to add constrains on each table declaration, because these problems: 我不想在每个表声明上添加约束,因为这些问题:

  • Needs to repeat checkings each time I use the type. 每次我使用该类型时都需要重复检查。
  • Needs to edit each type implementation on each table each time a bug is found. 每次发现错误时,都需要在每个表上编辑每种类型的实现。

Is there a way to define a type with his own checkings? 有没有一种方法可以通过自己的检查来定义类型?

I'm a newbie, please add an example. 我是新手,请添加示例。

I had read the documentation for declare type, and I find nothing (aside, the documentation is awful) 我已经阅读了有关声明类型的文档,但没有发现任何东西(此外,文档太糟糕了)

What you're looking for are SQL domains . 您正在寻找的是SQL Oracle doesn't support these; Oracle不支持这些。 instead you have to put the constraints on the table definitions. 相反,您必须将约束放在表定义上。

Oracle does support object types but personally I avoid them as they seem to create more trouble than they're worth. Oracle确实支持对象类型,但我个人避免使用它们,因为它们似乎造成了超出其价值的麻烦。

One way we make this easier is to model using a separate tool like SQL Developer Data Modeler, which allows you to define custom domains. 我们简化此过程的一种方法是使用单独的工具(例如SQL Developer Data Modeler)进行建模,该工具可用于定义自定义域。 The tool then generates the DDL to create the tables and columns with appropriate data types and constraints. 然后,该工具将生成DDL以创建具有适当数据类型和约束的表和列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM