简体   繁体   中英

How do I remove the default value from a column in oracle?

表中的列的默认值是 sysdate,我想更改它以使其没有默认值,我该怎么做?

ALTER TABLE YourTable MODIFY YourColumn DEFAULT NULL;

Joe's answer is correct in the sense that a column with DEFAULT NULL is functionally equivalent to having never defined a default value for that column in the first place: if a column has no default value, inserting a new row without specifying a value for that column results in NULL .

However , Oracle internally represents the two cases distinctly, as can be seen by looking at the ALL_TAB_COLUMNS system view . (This applies to Oracle 10.x, 11.x, and 12.x, and probably to older versions as well.)

  1. The case where a column has been created, or ALTER ed, with DEFAULT NULL :

     create table foo (bar varchar2(3) default null); select default_length, data_default from all_tab_columns where table_name='FOO'; => default_length data_default -------------- ------------ 4 NULL select dbms_metadata.get_ddl('TABLE','FOO') from dual; => CREATE TABLE "FOO" ( "BAR" VARCHAR(3) DEFAULT NULL … )
  2. No default ever specified:

     create table foo (bar varchar2(3)); select default_length, data_default from all_tab_columns where table_name='FOO'; => default_length data_default -------------- ------------ (null) (null) select dbms_metadata.get_ddl('TABLE','FOO') from dual; => CREATE TABLE "FOO" ( "BAR" VARCHAR(3) … )

As shown above, there is an important case where this otherwise-meaningless distinction makes a difference in Oracle's output: when using DBMS_METADATA.GET_DDL() to extract the table definition.

If you are using GET_DDL() to introspect your database, then you will get slightly different DDL output for functionally-identical tables.

This is really quite annoying when using GET_DDL() for version control and comparison among multiple instances of a database, and there is no way to avoid it, other than to manually modify the output of GET_DDL() , or to completely recreate the table with no default value.

The only way to do what you want is to recreate the table.

It is pretty easy to do in Toad, just right click on the table and select "Rebuild Table". Toad will create script which will rename the table and recreate a new table. The script will recreate indexes, constraints, foreign keys, comments, etc... and populate the table with data.

Just modify the script to remove "default null" after the column in question.

I don't know if this works for older versions of Oracle, but a colleague of mine actually discovered that you can reset it properly to the state it was initially (ie as if it had never been set) with:

ALTER TABLE YourTable MODIFY YourColumn DEFAULT (null);

NOTE: It doesn't solve the issue if you rely on get_ddl to compare the table, as pointed out by tejoe in the comments, but at least select data_default from all_tab_columns will return (null) and not NULL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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