I am trying to alter the column size of a view with the same command that we use for table like :
alter table
STUDENT
modify (
ROLL_NO VARCHAR2(80)
);
But its throwing error
SQL Error: ORA-00942: table or view does not exist
So how we can alter the column size of a view?
A view is simply saved query and "inherits" column type from underlying base table. So if you need to change metadata you should alter view definition:
ALTER VIEW view_students
AS
SELECT CAST(roll_no AS VARCHAR2(80)) AS roll_no,
...
FROM tab_students;
If you want to change data type to store longer strings, then you need to locate base table and alter it instead:
ALTER VIEW tab_students
MODIFY (ROLL_NO VARCHAR2(80));
Here is the procedure that I followed :
1- First find the base table for that view by running the following query
SELECT * FROM DBA_DEPENDENCIES
WHERE OWNER = '<scheman_name>'
AND NAME = '<view_name>'
AND TYPE = 'VIEW';
2- Above query will you a table where you will find the base table under the column name 'REFERENCED_NAME'.
3- Now Change the column size of that base table.
NOTE: The view can be made up of 1 or more than 1 tables, so you need to change the column size of all those base tables.
Related
I need to create table again, so I did the following:
RENAME DOCUMENT TO DOCUMENT_TO_DROP;
ALTER INDEX DOCUMENT_I RENAME TO DOCUMENT_I_XXX;
ALTER INDEX DOCUMENT_II RENAME TO DOCUMENT_II_XXX;
ALTER INDEX DOCUMENT_PK RENAME TO DOCUMENT_PK_XXX ;
Then:
CREATE TABLE "A"."DOCUMENT"
( "SID" NUMBER(15,0), .....
However this end with error:
ORA-00955: name is already used by an existing object.
but
SELECT *
FROM dba_objects
WHERE object_name = upper('DOCUMENT');
-> no rows!
after this i go back to original state
RENAME DOCUMENT_TO_DROP TO DOCUMENT;
-> no error all good.
How is this possible ?
You will see this if your table definition includes a nested table. From comments it seems that it does (but showing the full create statement would still be helpful).
As a simple extension of what you have shown, if I define a table type and include that in the table definition:
create table document (
sid number(15,0),
nt_col nt_type
)
nested table nt_col store as nt_col_nested_tab;
then I get the same error you do:
rename document to document_to_drop;
create table document (
sid number(15,0),
nt_col nt_type
)
nested table nt_col store as nt_col_nested_tab;
ORA-00955: name is already used by an existing object
Depending on how it was defined (when it's a table of objects or of a scalar data type), you might see the nested table name in dba_tables, but it might only appear in dba_nested_tables:
select table_name from user_tables
TABLE_NAME
DOCUMENT_TO_DROP
select table_name from user_nested_tables
TABLE_NAME
NT_COL_NESTED_TAB
So the problem is that you are trying to create a second nested table with the same name. You can rename that as well:
rename nt_col_nested_tab to nt_col_nested_tab_to_drop;
and then the recreation will work:
create table document (
sid number(15,0),
nt_col nt_type
)
nested table nt_col store as nt_col_nested_tab;
and you will see the old and new parent and nested tables in the data dictionary:
select table_name from user_tables
TABLE_NAME
DOCUMENT_TO_DROP
DOCUMENT
select table_name from user_nested_tables
TABLE_NAME
NT_COL_NESTED_TAB_TO_DROP
NT_COL_NESTED_TAB
fiddle
If you change your mind and want to revert the name of the original table you will also probably want to revert the name of the nested table as well.
You might also want to drop the table and use flashback table ... to before drop (docs) to recover it; but test that and make sure flashback is enabled and suitably configured before you do anything drastic...
Can't tell, it isn't obvious at first sight.
Though, I noticed that you're enclosing names into double quotes. My suggestion is NOT to do that.
This is pretty much useless:
SELECT *
FROM dba_objects
WHERE object_name = upper('DOCUMENT'); --> DOCUMENT already is in upper case
Should've been
where upper(object_name) = 'DOCUMENT'
What happens if you run such a query?
I tried to do the same, to see what causes the error:
SQL> create table document
2 (id number constraint pk_doc primary key,
3 id_2 number
4 );
Table created.
SQL> create index document_i on document(id_2);
Index created.
SQL> rename document to document_to_drop;
Table renamed.
SQL> alter index document_i rename to document_i_xxx;
Index altered.
At first I though it might be because of a constraint, but - nope, error code is different:
SQL> create table document
2 (id number constraint pk_doc primary key,
3 id_2 number
4 );
(id number constraint pk_doc primary key,
*
ERROR at line 2:
ORA-02264: name already used by an existing constraint
Is it a table? Seems so, it raises ORA-00955:
SQL> create table document_to_drop
2 (id number constraint pk_do2c primary key,
3 id_2 number
4 );
create table document_to_drop
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
SQL>
Perhaps it is some kind of a bug in Oracle. Which version do you use?
Recently, I had 2 database links with exactly the same name; when I tried to drop any of them, I got "ORA-03113: end-of-file on communication channel" error. DBA bounced the database and it "solved" the problem. Can you do that? I'm not saying that it'll actually help (besides, your problem is different than mine) but - if nothing else helps, try it.
I would like to update a view in HSQL without writing the same statement again and again.
I have a table CONTACTS with ID, First_NAME and LAST_NAME. I also have a VIEW for this table, which I created with
CREATE VIEW IDGREATERTHREE AS SELECT * FROM CONTACTS WHERE ID > 3;
How can I update my VIEW after I added a new column to my table. I want to update my table without anything like this:
ALTER VIEW IDGREATERTHREE AS SELECT * FROM CONTACTS WHERE ID > 3;
I would like to find a way to refresh my invalid view in a similar way like in Oracle:
ALTER VIEW IDGREATERTHREE COMPILE;
I am also looking for a way to select just the invalid views. WithSELECT * FROM INFORMATION_SCHEMA.VIEWS I am not able to see any difference between an invalid and a non-invalid view.
A solution for this would be to write an ON DDL trigger.
In this ON DDL trigger , you check if your modifying your table.
If this is the case, then you use Dynamic SQL to recreate your view. This is doable with plsql (you tagged with oracle). There is ample documentation on creating triggers and dynamic SQL on the Internet.
HSQLDB cannot have invalid views. When you create a view, the SELECT * FROM CONTACTS is expanded to the actual column names. When you add a column to the table the view is recompiled with the original column names and the new column is not included.
I created view myview with two columns ID and Name. But I want add extra column for this.
I using the query as :
ALTER VIEW myview ADD COLUMNS (AGE int);
But I am getting error as:
required (...)+ loop did not match anything at input 'columns' in add
partition statement.
Any help me in this?
You will have to get the new column from the table from which the view was created.
alter view myview as select col_1 ,col_2 ,Age from your_table
How can I alter the type of a column in an existing table in MonetDB? According to the documentation the code should be something like
ALTER TABLE <tablename> ALTER COLUMN <columnname> SET ...
but then I am basically lost because I do not know which standard the SQL used by MonetDB follows here and I get a syntax error. If this statement is not possible I would be grateful for a workaround that is not too slow for large (order of 10^9 records) tables.
Note: I ran into this problem while doing some bulk data imports from csv files into a table in my database. One of the columns is of type INT but the values in the file at some point exceed the INT limit of 2^31-1 (yes, the table is big) and so the transaction aborts. After I found out the reason for this failure, I wanted to change it to BIGINT but all versions of SQL code I tried failed.
This is currently not supported. However, there is a workaround:
Example table for this example, say we want to change the type of column b from integer to double.
create table a(b integer);
insert into a values(42);
Create a temporary column alter table a add column b2 double;
Set data in temporary column to original data update a set b2=b;
Remove the original column alter table a drop column b;
Re-create the original column with the new type alter table a add column b double;
Move data from temporary column to new column update a set b=b2;
Drop the temporary column alter table a drop column b2;
Profit
Note that this will change the ordering of columns if there are more than one. However, this is only a cosmetic issue.
I have database and some text fields are CLOB type, I need to change most of these into VARCHAR2.
Have tried to do that using SQL Developer tool, by clicking edit on table, but get error like this one:
The following SQL statement failed:
ALTER TABLE TBL_PEOPLE MODIFY (PERSON VARCHAR2(150) )
Want to ask, how can this change be done
You can't, directly. The code you tried will have got an `ORA-22859, presumably. (It's helpful to show the actual errors you get, of course).
You'll need to add a new varchar2 column; copy the data across - or a substring of it if it might be larger than the new column you're creating; drop the clob column. You can rename the columns so it looks fairly transparent.
As in this SQL Fiddle:
alter table tbl_people rename column person to clob_person;
alter table tbl_people add (person varchar2(150));
update tbl_people set person = clob_person;
alter table tbl_people drop column clob_person;
Obviously don't drop the old column until you're sure the data has copied without errors. Also take into account any constraints, indexes, etc. that might exist in the old column; they will need to be recreated. And anything that references the old column will have been invalidated - generally procedures will recompile themselves on next use.
So be careful, test it first, and plan some down time.
ALTER TABLE tablename ADD (FIELD_LIST_TEMP VARCHAR2);
UPDATE tablename SET FIELD_LIST_TEMP = FIELD_LIST;
ALTER TABLE tablename DROP COLUMN FIELD_LIST;
ALTER TABLE tablename RENAME COLUMN FIELD_LIST_TEMP TO FIELD_LIST;
Here FIELD_LIST existing column which is defined it as CLOB. With above query it will change from CLOB to VARCHAR2.