Solved

Does anyone have PL/SQL code to add new field in an Oracle  table with specific order?

Posted on 2014-12-29
27
221 Views
Last Modified: 2015-01-09
I need to add fields to tables and I do not want them added to the end of the table order.  I know that I can create temp table, add the field, and then repopulate from the temp table.  This is tedious.

I would like a procedure that would take in a schema, table_name, new_field_name, new_field_type, and order_number.  Then, the new field would be added to the table and the table would maintain constraints/grants/triggers etc.


Does anyone know if this exists already?
0
Comment
Question by:kfeiner
  • 8
  • 6
  • 3
  • +3
27 Comments
 
LVL 13

Accepted Solution

by:
Alexander Eßer [Alex140181] earned 333 total points
ID: 40522551
Then, the new field would be added to the table and the table would maintain constraints/grants/triggers etc.
"ALTER TABLE ADD ... " would not necessarily render those objects invalid. Apart from that: what exactly are you aiming at?! I am afraid there is no way adding a column "in between" ...
If you're having problems taking a table "offline" for reordering purposes (via temp table), please have a loom at DBMS_REDEFINITION" and/or EBR (Edition based redefinition)...
0
 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 167 total points
ID: 40522684
0
 

Author Comment

by:kfeiner
ID: 40522705
Im not concerned with taking the table offline.  I am concerned with the tedious nature of creating temp tables, grabbing grants, dropping temp_tables etc... everytime I want to add a field.

Will DBMS_REDEFINITION  allow me to add a field to an existing table in an order of my choosing?
0
 
LVL 13

Assisted Solution

by:Alexander Eßer [Alex140181]
Alexander Eßer [Alex140181] earned 333 total points
ID: 40522727
Will DBMS_REDEFINITION  allow me to add a field to an existing table in an order of my choosing?
Short answer: yes
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40522731
I recommend, you take a look at editionable views (when using EBR) ;-)
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40522781
Personally I would forget about columns in specific positions.  There is really no need for doing it today.  Maybe back in the infancy of databases there might be some performance or storage arguments but those days are long gone.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 40523190
It can be done as said above or even by proc if you google it out. But getting curios as to why you need fields only in a given order as it does not matter for anything right ? any particular reasons behind.

thanks
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 40523409
i bet the columns are in alphabetical order ... ? no ?
some front-ends like toad can display a table with the column names ordered.

it doesn't matter what the order of columns is physically for the database.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40523891
Yes, column order within a table really doesn't matter anymore, but if you need some kind of sorting (apart from "built-in" ordering in some IDEs), you may query "user_tab_columns" or "dba_tab_columns" and filter/order by "column_id" or "column_name" or or or....
0
 

Author Comment

by:kfeiner
ID: 40523897
The reason for the order is so that a set of fields are listed last in the table.  We have a database standard of listing the timestamp, last_updated, user etc at the end of the tables.  Its more consistent when running adhoc queries.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40523903
Then why not just use a view "instead"?!
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:kfeiner
ID: 40524231
Dont get hung up on the "WHY" I want a generic utility to add fields in a specific order.   Im not going to add views for 15 different tables just for ordering.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40524436
>>Its more consistent when running adhoc queries.

Column order has nothing to do with consistency of queries.

>> I want a generic utility to add fields in a specific order.  

This was already provided:  DBMS_REDEFINITION.
0
 

Author Comment

by:kfeiner
ID: 40524444
The consistency Im speaking of is that of the table structure with the last 4 fields being similar across tables.   I have seen this practice in many shops.  

I will look further into DBMS_REDEFINITION, as I have yet to see if this will suit my need any easier than  creating temp tables, grabbing grants, dropping temp_tables etc... everytime I want to add a field.
0
 

Author Comment

by:kfeiner
ID: 40524450
I will look further into DBMS_REDEFINITION, as I have yet to see if this will suit my need any easier than  creating temp tables, grabbing grants, dropping temp_tables etc... everytime I want to add a field.
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40524565
This was already provided:  DBMS_REDEFINITION.
YES, it has been provided! By ??!! Most of the time I swallow the pill when it comes to points distribution, but this one here is absolutely ridiculous! Giving away the whole 500 pts for just 2 blind links???!!! That can't be true!!
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40524679
Alex,

If you have an issue with how a question was closed please click the 'Request Attention' link and explain the issues and a Moderator will take a look.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40525735
2 blind links???
What really matters is the content and accuracy of the links.
For example, the second link points to a fine EE article by Greg Clough detailing the best method to make fundamental changes to Oracle tables.
;)
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40526595
@MikeOM_DBA:

http://www.experts-exchange.com/Database/Oracle/Q_28587953.html#a40522684

Just 2 BLIND links. Period. No explanation at all, just links. Period.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 40528285
Perhaps the links are self-explanatory?
;)
0
 
LVL 13

Expert Comment

by:Alexander Eßer [Alex140181]
ID: 40528394
No further questions, your honor ;-)
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 40533087
>>  I have seen this practice in many shops.  
there is a lot of shops where the design is beautiful and where the performance is appalling !

do you really want a production plant having to go down for just adding a column to a table ?
any good ceo will wipe a request for having columns in a specific order off the table for oracle db's.
the costs to keep those 4 columns last in the table will never be beneficial
0
 

Author Comment

by:kfeiner
ID: 40533535
You must understand that I do not create the standards, but I must follow them.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now