简体   繁体   中英

How do I invalidate a table in Oracle 11g on purpose?

I'm writing a small query to find invalid tables in Oracle :

select * from user_tables where status != 'VALID'

For testing, I thought it would be good to create a table and invalidate it on purpose. Is there a way to do this?

Invalidating a view is easy, just drop one of the underlying tables.

Any hint welcome.

You won't see status INVALID in user_tables , however, you would see that in [USER|ALL|DBA]_OBJECTS view.

One simple way is to create the table using an object type , and force the object type attribute to invalidate .

For example,

SQL> CREATE OR REPLACE TYPE mytype AS OBJECT(col1 VARCHAR2(10))
  2  /

Type created.

SQL>
SQL> CREATE TABLE t(col1 NUMBER,col2 mytype)
  2  /

Table created.

SQL>
SQL> SELECT object_name, object_type, status FROM user_objects WHERE object_name='T'
  2  /

OBJECT_NAME OBJECT_TYPE             STATUS
----------- ----------------------- -----------
T           TABLE                   VALID

SQL>

So, the table is now in VALID status . Let's make it INVALID :

SQL> ALTER TYPE mytype ADD ATTRIBUTE col2 NUMBER INVALIDATE
  2  /

Type altered.

SQL>
SQL> SELECT object_name, object_type, status FROM user_objects WHERE object_name='T'
  2  /

OBJECT_NAME OBJECT_TYPE             STATUS
----------- ----------------------- -----------
T           TABLE                   INVALID

SQL>

Its an old question but I want to point on something for future readers, the opposit of valid in the status user_tables is UNUSABLE so if you want to make a table UNUSABLE ,A DROP TABLE operation failed from oracle doc

If a previous DROP TABLE operation failed, indicates whether the table is unusable (UNUSABLE) or valid (VALID)

As for the type INVALID in user_objects its mainly related to views/package/procedure however lalit kumar gave a good example where a table is invalid.

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