ORACLE Get_DLL for Constraint using index - oracle

I am trying to export some Constraint DDLs from Oracle like this :
EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'STORAGE',false);
EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'TABLESPACE',false);
EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SEGMENT_ATTRIBUTES',false);
EXECUTE DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',true);
DBMS_METADATA.GET_DDL
( 'CONSTRAINT',MYCONSTRAINT, ME);
I get
ALTER TABLE "ME"."MYTABLE" ADD CONSTRAINT "MYCONSTRAINT" PRIMARY KEY
("COL1") ENABLE;
I want
ALTER TABLE "ME"."MYTABLE" ADD CONSTRAINT "MYCONSTRAINT" PRIMARY KEY ("COL1") USING INDEX MYINDEX);
I couldn't find any option in documentation for Oracle 11.2 :
http://docs.oracle.com/cd/E11882_01/appdev.112/e25788/d_metada.htm#BGBJBFGE
Any idea how to achieve this ?

This might help, or get you very close:
SELECT REPLACE(dbms_metadata.get_ddl('CONSTRAINT', constraint_name, USER)
,' USING INDEX '
,' USING INDEX "' || index_name || '" ' ) ddl
FROM user_constraints
WHERE index_name IS NOT NULL
AND constraint_name NOT LIKE 'BIN$%';

Related

Right way to create index organized table

I am trying to create a index organized table in oracle 11. I create the index organized table and insert the row from another table.
create table salIOT (
mypk ,
cid ,
date,
CONSTRAINT sal_pk PRIMARY KEY (mypk)
) ORGANIZATION INDEX
AS Select * from another table;
But the leaf blocks are empty when I query
SQL> Select owner, index_name, table_name, leaf_blocks from all_indexes where table_name like 'SALIOT';
Am I missing something here?
You still need to gather statistics on the table, something like:
exec dbms_stats.gather_table_stats('ABC', 'IOTTableName')
(assuming 'ABC' is your username; change as needed) - then re-run your SELECT against ALL_INDEXES and you will see how many leaf blocks you have.

Alter all table columns with out white space in between names

Oracle - Alter all table column names with trim of white space in between names
For suppose column names before alter :
Home number
Mobile number
Local number
After alter column names shall be :
Homenumber
Mobilenumber
Localnumber
I've tried this way: but unable to crack:
UPDATE SA_VW_PHONENUMBER TN SET TN.Column_Name = TRIM (TN.Column_Name);
Fully automatic way
Use this cursor based DDL hacking - statement concat.
BEGIN
FOR alters IN
(
SELECT
'ALTER TABLE "'||table_name||'" RENAME COLUMN "'||column_name||
'" TO "'||replace(cols.column_name,' ','')||'"' sql_stmt
FROM all_tab_cols cols
WHERE REGEXP_LIKE(column_name,'[[:space:]]')
AND owner = user --Add real schema name here
ORDER BY 1
) LOOP
DBMS_OUTPUT.PUT_LINE ( alters.sql_stmt ||';') ;
EXECUTE IMMEDIATE alters.sql_stmt;
END LOOP;
END;
/
If you want to use the safe way
As I know you cannot perform a DDL as a dynamic SQL, so you cannot pass variables to the ALTER TABLE command, but here is what you can do instead of that.
Selecting the occurences:
SELECT table_name,column_name,replace(cols.column_name,' ','') as replace_name
FROM all_tab_cols
WHERE REGEXP_LIKE(column_name,'[[:space:]]');
Use the ALTER TABLE DDL command:
alter table T_TABLE rename column "COLUMN SPACE" TO "COLUMNNOSPACE";
Try the REPLACE function
UPDATE SA_VW_PHONENUMBER TN SET TN.Column_Name = REPLACE(TN.Column_Name,' ','')

Oracle: ORA-01408: such column list already indexed

I have created a table:
CREATE TABLE SCHEMA_NAME.TABLE_NAME
(
ID VARCHAR2(100) PRIMARY KEY NOT NULL,
A_ID VARCHAR2(100) NOT NULL,
B_ID VARCHAR2(100),
C_ID INTEGER,
CONSTRAINT FK_C_NOTE FOREIGN KEY (C_ID) REFERENCES C(ID),
CONSTRAINT FK_A_NOTE FOREIGN KEY (A_ID) REFERENCES A(ID),
CONSTRAINT FK_B_NOTE FOREIGN KEY (B_ID) REFERENCES B(ID)
);
And I have tried to apply the following indexes:
CREATE INDEX IDX_FK_A_TABLE_NAME on SCHEMA_NAME.TABLE_NAME(A_ID) tablespace TS_SCHEMA_NAME_DATA;
CREATE INDEX IDX_FK_C_TABLE_NAME on SCHEMA_NAME.TABLE_NAME(C_ID) tablespace TS_SCHEMA_NAME_DATA; --Fails
CREATE INDEX IDX_FK_B_TABLE_NAME on SCHEMA_NAME.TABLE_NAME (B_ID) tablespace TS_SCHEMA_NAME_DATA; --Fails
But when I try to create the last two indexes I get:
ORA-01408: such column list already indexed
Why is this? Are these indexes created automatically?
If I try:
SELECT INDEX_NAME FROM ALL_INDEXES WHERE TABLE_NAME = 'NOTE';
I get:
INDEX_NAME
----------
IDX_FK_A_TABLE_NAME
SYS_C0044692561
Double check you indexed columns by querying ALL_IND_COLUMN (or DBA_IND_COLUMNS if you have acess). It will show you what table columns are indexed by which index.
SELECT c.index_owner, c.index_name, c.table_owner, c.table_name, c.column_name, c.column_position
FROM all_ind_columns c
WHERE c.table_owner = 'SCHEMA_NAME'
AND c.table_name = 'NOTE'
ORDER BY c.index_owner, c.index_name, c.column_position;
On a site note, When you are creating your indexes you're not prefixing them with a schema owner (CREATE INDEX SCHEMA.IDX_FK_A_TABLE_NAME ...). That means they're being created in your current schema. On the other hand, it's probably not related to the error you are facing.

pl/sql can't modify constraint defferable

I want to make constraint deferrable so I've wrote this code:
alter table life_cycle_phases modify constraint SOME_T_NAME_UNIQUE INITIALLY DEFERRED DEFERRABLE;
but orace returns mistake:
00933. 00000 - "SQL command not properly ended"
what am i doing wrong?
UPDATE:
ok, i see, once i've created not deferrable constraint, i can't check it's state, but i need to!
my problem is: i need to disable all constraints, add row in table,which was random chosen(in fact i don't know in which table the row will be inserted) then enable all constraints.enable novalidate doesn't work, it validates rows, deferrable also doesn't work, what should do?
In general, MODIFY constraint is to just change the state of it. For instance, enable it, disable it..
From docs.. You cannot change the state of a NOT DEFERRABLE constraint to INITIALLY DEFERRED.
Restrictions on Modifying Constraints Modifying constraints is
subject to the following restrictions:
•You cannot change the state of a NOT DEFERRABLE constraint to
INITIALLY DEFERRED.
•If you specify this clause for an index-organized table, then you
cannot specify any other clauses in the same statement.
•You cannot change the NOT NULL constraint on a foreign key column of
a reference-partitioned table, and you cannot change the state of a
partitioning referential constraint of a reference-partitioned table.
So, drop the constraint first and recreate it.
You may use the below to generate the DDL of the constraint.
--For referential integrity constraints.
DBMS_METADATA.GET_DDL('REF_CONSTRAINT',CONSTRAINT_NAME,OWNER)
--For other kinds of constraints.
DBMS_METADATA.GET_DDL('CONSTRAINT',CONSTRAINT_NAME,OWNER)
Or try this.. ( Courtesy : https://gist.github.com/sdeming/869717 ...I haven't tested!!)
select 'alter table ' || source_table || ' add constraint ' || constraint_name || ' foreign key (' || con_columns || ') references ' || target_table || ' (' || ind_columns || ') enable' data
from (select constraint_name, source_table, target_index, target_table, con_columns, wm_concat(column_name) ind_columns
from (select a.constraint_name, a.source_table, a.target_index, b.table_name target_table, a.con_columns, b.column_name, b.column_position
from (select a.constraint_name, a.source_table, a.target_index, wm_concat(a.column_name) con_columns
from (select a.constraint_name,
a.table_name source_table,
a.r_constraint_name target_index,
b.column_name,
b.position
from user_constraints a
inner join user_cons_columns b on (b.constraint_name = a.constraint_name)
where a.constraint_type = 'R'
and a.constraint_name = 'LIFE_CYCLE_PHASES_NAME_UNIQUE'
order by a.constraint_name, b.position) a
group by constraint_name, source_table, target_index) a
inner join user_ind_columns b on (b.index_name = a.target_index)
order by constraint_name, b.column_position)
group by constraint_name, source_table, target_index, target_table, con_columns);
And then DROP it,
alter table OWNER.life_cycle_phases drop constraint LIFE_CYCLE_PHASES_NAME_UNIQUE ;
Finally recreate the constraint only using the generated DDL.

Is there a way in oracle to disable/enable an unnamed constraint?

I want to disable NOT NULL constraints into a table to insert data for test but I can't find a way to disable unnamed constraints.
I found enough info to disable named constraints, but I couldn't find a example to disable unnamed NOT NULL constraint.
I would like to implement this without querying the data dictionary, but... I'm willing to do that if its the only way. But I would like to use a clean ALTER TABLE DDL.
You will need to query the data dictionary, something like this will disable all constraints on the table. Be aware though, that this will disable the constraint system wide, not just for your session.. Perhaps what you really want is to defer the constraint?
drop table testxx
drop table testxx succeeded.
create table testxx ( id number not null )
create table succeeded.
select status from user_constraints where table_name = 'TESTXX'
STATUS
--------
ENABLED
1 rows selected
begin
for cnames in ( select table_name,constraint_name from user_constraints where table_name = 'TESTXX' ) loop
execute immediate 'alter table ' || cnames.table_name || ' disable constraint ' || cnames.constraint_name;
end loop;
end;
anonymous block completed
select status from user_constraints where table_name = 'TESTXX'
STATUS
--------
DISABLED
1 rows selected
You can also just alter the column as follows
create table test_null (col_n number not null);
alter table test_null modify col_n number null;

Resources