Using Domains is very wise: you only need to define the datatype and constraints once for a type of information. For instance username, money, or Boolean values. But how can I define a Domain with Oracle SQL Developer Data Modeler and how to use a domain?

Defining a Domain

Select Domain Administration from the Tools menu.

Press Add, define a Name, Logical type etc. for the Domain.

pic1

Press Apply and Save.

 

What if I want to define a constraint for columns of type BooleanDomain that can only have values 0 or 1?

Select the Domain you want to modify and press Modify. Press Value List.

pic2

Press Add and add the Values wanted and their Descriptions.

Press OK. Press Apply (to confirm the modification to the Domain).

 

To specify an attribute or a column with that Domain select Domain as Data Type and your domain from the list. Apply.

pic3

 

Now when you generate the DDLs for the table the column will be of the type defined in the Domain

CREATE TABLE Orders_Test

(

Customer_CustNO NUMBER (10) NOT NULL ,

OrderNo         NUMBER (10) NOT NULL ,

Invoiced        NUMBER (1)

) ;

And there will also be the DDL for defining the constraint:

ALTER TABLE Orders_Test ADD CHECK ( Invoiced IN (0, 1)) ;

 

What if I want to define a constraint ‘between 0-100 000’ for a Domain?

Define the domain Credit as explained earlier but instead of defining a Value List now press Ranges button.

pic4

Define the Begin Value (0), the End Value (100000) and the Description (Credit Range). Press Add.

pic13

 

Then define an attribute or a column using this Domain as a Data Type.

 

pic5

Now the DDLs you get will have all you defined in the Domain: the data type and the constraint:

 

CREATE TABLE Customers_Test

(

CustNO      NUMBER (10) NOT NULL ,

Name        VARCHAR2 (100) NOT NULL ,

CreditLimit NUMBER (6,2)

) ;

ALTER TABLE Customers_Test ADD CHECK ( CreditLimit BETWEEN 0 AND 100000) ;

 

This is all great but what if I want to define a check constraint with other kind of definition but a list or a range?

Defining a Check Constraint

Let’s define a Domain Name.

pic6

Whenever this Domain is used we want the column to be NOT NULL. Press Check Constraint.

If you want to define different constraints to different RDBMS types you can do that by clicking the Constraint field next to the technology chosen. If you want to define a generic constraint, select the Constraint field next to Generic Constraint.

pic7

 

Define the constraint using %element% notation:

pic8

Press OK.

 

pic9

Then Apply and OK.

pic10

Apply and Save.

 

Define an attribute or a column with a Domain Name.

pic11

And the DDLs

 

CREATE TABLE Customers_Test

(

CustNO      NUMBER (16) NOT NULL ,

Name        VARCHAR2 (200) ,

CreditLimit NUMBER (6,2)

) ;

ALTER TABLE Customers_Test ADD CHECK ( Name IS NOT NULL) ;

 

This might be a bit silly example but just to show how you can define more complex constraints using a variable (%column% in our example).

NOTE that the constraint defined in our example is different from defining a column mandatory (ALTER TABLE Customers_Test MODIFY (Name NOT NULL)) but the effect is the same: you cannot insert a row in that table without giving a value for the column Name.

Name             Null                Type

———– ——– ————-

CUSTNO      NOT NULL     NUMBER(16)

NAME                                    VARCHAR2(200)

CREDITLIMIT                      NUMBER(6,2)

 

In User_Constraints view you will find a constraint defining this column NOT NULL:

“HELI”   “SYS_C0014723”               “C”         “CUSTOMERS_TEST”      ” Name IS NOT NULL”    ” Name IS NOT NULL”    “”                “”            “”            “ENABLED”         “NOT DEFERRABLE”         “IMMEDIATE”    “VALIDATED”     “GENERATED NAME”                “”            “”            10.05.2016           “”            “”            “”            “”            3

 

And you cannot add a row without any value to the Name column:

  1. 00000 – “check constraint (%s.%s) violated”

*Cause:    The values being inserted do not satisfy the named check

*Action:   do not insert values that violate the constraint.

 

NOTE: You can also define a Default Value for a Domain. And if you have defined a Value List for that Domain the value can be chosen from that list.

pic12

CREATE TABLE Orders_Test
(
Customer_CustNO NUMBER (10) NOT NULL ,
OrderNo         NUMBER (10) NOT NULL ,
Invoiced        NUMBER (1) DEFAULT 0
) ;

 

Advertisement