How to drop Not Null constraint (system generated)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dgirdhar
    New Member
    • May 2007
    • 1

    How to drop Not Null constraint (system generated)

    Hello,

    I created one table say "Table1" with one column say "Column1" with constraint Not Null. Oracle generated its own name for this constraint. Say SYS_C#########.

    I want to delete this constraint using one sql script. But how i will find out the constraint name to drop this constraint?

    -Dhiraj
  • chandu031
    Recognized Expert New Member
    • Mar 2007
    • 77

    #2
    Originally posted by dgirdhar
    Hello,

    I created one table say "Table1" with one column say "Column1" with constraint Not Null. Oracle generated its own name for this constraint. Say SYS_C#########.

    I want to delete this constraint using one sql script. But how i will find out the constraint name to drop this constraint?

    -Dhiraj
    Hi,

    All the Not null constraints will be stored in a table called ALL_CONS_COLUMN S which has Owner, table_name,colu mn_name,constra int_name and position(in case of primary key)

    So to get the constraint name for just a NOT NULL constraint your query would look like this :

    [code=sql]

    SELECT CONSTRAINT_NAME FROM ALL_CONS_COLUMN S WHERE OWNER = <user_name> AND TABLE_NAME = <table_name>
    AND COLUMN_NAME = <column_name>
    [/code]

    Comment

    • frozenmist
      Recognized Expert New Member
      • May 2007
      • 179

      #3
      Hi,
      A simple solution that I can think of if you know the column name is that
      you can just alter the column to allow null. This would take out the constraint , wouldn't it?
      If you want the constraint name as such, it would be better if you do what Chandu
      suggested.
      Eg:
      If your table was like
      create table table1 (col1 integer not null)
      then you can use
      [code=sql]
      Alter table table1 modify col1 integer null
      [/code]
      to remove the constraint.

      Hope it helped
      Cheers

      Comment

      • chandu031
        Recognized Expert New Member
        • Mar 2007
        • 77

        #4
        Hi,

        If you just want to drop the not null constraint then there is a better way of doing it.

        Just use the ALTER TABLE command:

        [code=sql]

        ALTER TABLE <table_name> MODIFY <column> <datatype> NULL

        [/code]

        Of course this does not work the other way round i.e from NULL to NOT NULL
        if there are already NULLs in your table. It will work if the field doesn't have NULLs

        Comment

        Working...