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.
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.
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.
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.
Define the Begin Value (0), the End Value (100000) and the Description (Credit Range). Press Add.
Then define an attribute or a column using this Domain as a Data Type.
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.
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.
Define the constraint using %element% notation:
Press OK.
Then Apply and OK.
Apply and Save.
Define an attribute or a column with a Domain Name.
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:
- 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.
CREATE TABLE Orders_Test
(
Customer_CustNO NUMBER (10) NOT NULL ,
OrderNo NUMBER (10) NOT NULL ,
Invoiced NUMBER (1) DEFAULT 0
) ;
Hi Heli, excellent post and thank you for your help!
How did you find out about the %column% variable? Are there more? Typically this is the most relevant used. Can’t think about any valid use case for the table name or model name, etc. Just curious, not important.
Cheers,
~Dietmar.
From Philip Stoyanov, of course. He is my guru 🙂
Don’t forget you can fill in the “comments” property on the domain which will then become a template Column Comment in the model and DDL.
Actully I did not get that part working. Was planning to add (as the last screenshot shows) but had to leave the computer without success. I will test later again.
Forgive me if this is not the place for this question. How can I name a “not null” constraint so that when I define a mandatory column the suffix “_NN” is suitably applied?
In “design, properties, naming standards, templates” I have: {table}_{column}_NN but it’s never generated for not null (mandatory) columns, so defined, at least when I peruse “DDL Preview” at the relational model level, within data modeler. Am using 4.1.0.873. There appear to be some awkward work arounds, but it sure would be nice to see them within DDL Preview.
Hi Paul
of course you can ask here! I am not answering very fast here so it might take a while but I do my best.
Did I understand correctly that your question is: if you define a column to be mandatory how to get the not null contraint named as tablename_columnname_NN?
— Heli
Yes. A template is supposed to help with that default “NN” naming but it does not work. Other “default” constraint naming conventions work, not null constraints do not. This seems to be a bug? So then I have to do it manually later in the design process, one by one, which defeats the purpose. “DDL preview” is a powerful review mechanism that one must forego in this context, it seems?
Thank you.
I will write a blog post about it. Just a few minutes…Thanks for asking!
Thank you for this. Date: Wed, 25 May 2016 12:36:14 +0000 To: paul.berger@outlook.com
Thanks for sharing,
I see that you used : Invoiced NUMBER (1).
Does it mean that , for this purpose, using NUMBER (1) is better than VARCHAR2 (1) — Which is Y / N ?
Yes. 0=no, 1=yes. I depends which one is better, number or varchar2. I have defined it as number in this case because it is also used for calculating how many has been invoiced. I would not normally have this kind of denormalized column but now my invoices are in xml and I want to learn the info from my rel table. A bit complicated example, should have used a better and more simple example.
Thanks for the quick reply,
Actually, my question is mainly about Number (1) or Varchar2 (1). Which one would you recommend ?
It depends. If the company already have their standards I follow them. If not I usually use Varchar2(1) and values “Y” and “N”. And I define it mandatory (not null).
I’ve tried to use %column% variable in Data Modeler 4.0.3.853 with Generic constraint and with constraint for Oracle 11g but it doesn’t work (in final code there is only %column%, not the name of column as it should). What can be wrong?
It must be %COLUMN% (not %column%). Sorry for bothering You and thanks for the very good website.
Thank you for your kind comment 🙂
Heli