Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL Inline NOT NULL named constraint

Tags:

sql

oracle11g

how can I specify a NOT NULL contraint at the end of my sql statement as I can with a UNIQUE

 CREATE TABLE test ( 
        WORKFLOW_CLASS_ID VARCHAR2(24),
        DEPT_OWNER VARCHAR2(3), 
        NAME VARCHAR2(64), 
        PRODUCT_ID VARCHAR2(24), 
        CONSTRAINT WF_WORKFLOW_CLASS_pk PRIMARY KEY (WORKFLOW_CLASS_ID), 
        CONSTRAINT dup_workflow UNIQUE (DEPT_OWNER, NAME), 
        CONSTRAINT not_null NOT NULL(PRODUCT_ID)) 

The above create string is built dynamically using schema defined in an XML document, which defines constraints in its own tag, so therefore I tack on the contraints at the end of the sql string. When I try to specify NOT NULL I get invalid identifier.

EDIT: Below is the stucture of my xml document. This file has evolved and over time. I use it to generate DAO and Javabean boilerplate code that I cut and paste into Eclipse and for dynamically creating html forms (xml has field, size and label). It is also used for an Admin CRUD application for maintaining the backend. I recently added the <tableConstraints> tag because I wanted to add UNIQUE constraints; UNIQUE constraint could be specified at the end of the create statement. In hind sight, I should have added a <contraint> tag within my <column> tag.

<!DOCTYPE schema PUBLIC "SchemaId" "../../schema.dtd">
<schema>
  <tableName>WF_WORKFLOW_CLASS</tableName>
  <javaBean>WorkflowClass</javaBean>
  <tableAlias>wfc</tableAlias>
  <tableTitle>Workflow process definitions by department</tableTitle>
  <tableConstraints>, CONSTRAINT dup_workflow UNIQUE (DEPT_OWNER, NAME), CONSTRAINT not_null NOT NULL(PRODUCT_ID)</tableConstraints>
  <oneToMany>
        <attName>work_item_list</attName>
        <attName>work_action_list</attName>
  </oneToMany>

  <column>
        <name>WORKFLOW_CLASS_ID</name>
        <type>VARCHAR2</type>
        <size>24</size>
        <label>Work flow Class Unique ID</label>
  </column>

  <primaryKey>      
        <name>WORKFLOW_CLASS_ID</name>
        <type>timestamp</type>
  </primaryKey>

  <foreignKey>
        <name>ADMIN_BY</name>
        <table>EBITPSV.PS_NAMES_V009</table>
        <lookUpKey>badge</lookUpKey>
        <returnField>lname</returnField>
  </foreignKey>

</schema>
like image 908
jeff Avatar asked Dec 28 '22 03:12

jeff


1 Answers

An not null constraint is declared inline, after each individual column name:

CREATE TABLE test ( ... 
        NAME VARCHAR2(64), 
        PRODUCT_ID VARCHAR2(24) not null,
        ... )

If you wish to name the constraint explicitly then you also do this after each column:

CREATE TABLE test ( ... 
        NAME VARCHAR2(64), 
        PRODUCT_ID VARCHAR2(24)  
         CONSTRAINT PRODUCT_ID_NN NOT NULL,
        ... )

You can of course modify the column after the table has been created:

ALTER TABLE test
 MODIFY product_id varchar2(24) not null;

Update:

Judging by your recently posted XML script the best thing to do would be to add a sub-tag <notNull> to your <column> tag. With 2 values - e.g. 1, 0 you could add a not null constraint in the same way as you decide whether something is a varchar or not.

The other way it would be possible is to loop through the values in your <tableConstraints> tag and run multiple alter table add constraint ... or alter table modify ... after you have created the table.

You suggestion of adding <constraint> sub-tag in <column> and creating them all at the column level will only work for single column constraints not multiple as you need here.

like image 195
Ben Avatar answered Dec 29 '22 16:12

Ben