ORA-01758: The table must be empty to add required (not null) Columns

Source: Internet
Author: User

Add a new column to a table with records and specify not null to report a ORA-01758. What should I do?

1. Specify the default value for the newly added column.

2. Clear the records in the table and add new columns.

The experiment is as follows:

  1. SQL> drop table ord;
  2. Table dropped
  3. SQL> create table ORD
  4. 2 (
  5. 3 ORD_NO NUMBER (2) notNull,
  6. 4 ORD_DATE DATE,
  7. 5 CUST_ID NUMBER (4)
  8. 6 );
  9. Table created
  10. SQL> insert into ORD values (1, sysdate, 100 );
  11. 1 row inserted
  12. SQL> COMMIT;
  13. Commit complete
  14. -- The table contains records, adds new columns, and specifies notNullTimes ORA-01758
  15. SQL> ALTER TABLE ORD ADD price NUMBER (8, 2) NOT NULL;
  16. Alter table ord add price NUMBER (8, 2) NOT NULL
  17. ORA-01758: The table must be empty to add required (not null) Columns
  18. -- Specify the default value for the new column, which can be added.
  19. SQL> ALTER TABLE ORD ADD price NUMBER (8, 2) DEFAULT 0 NOT NULL;
  20. Table altered
  21. -- Clear the records in the table without specifying the default value. This method is rarely used.
  22. SQL> TRUNCATE TABLE ORD;
  23. Table truncated
  24. SQL> alter table ORD drop column PRICE;
  25. Table altered
  26. SQL> ALTER TABLE ORD ADD price NUMBER (8, 2) NOT NULL;
  27. Table altered
  28. SQL>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.