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

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?
kfeinerAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Alexander Eßer [Alex140181]Connect With a Mentor Software DeveloperCommented:
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
 
kfeinerAuthor Commented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Alexander Eßer [Alex140181]Connect With a Mentor Software DeveloperCommented:
Will DBMS_REDEFINITION  allow me to add a field to an existing table in an order of my choosing?
Short answer: yes
0
 
Alexander Eßer [Alex140181]Software DeveloperCommented:
I recommend, you take a look at editionable views (when using EBR) ;-)
0
 
slightwv (䄆 Netminder) Commented:
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
 
Naveen KumarProduction Manager / Application Support ManagerCommented:
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
 
Geert GOracle dbaCommented:
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
 
Alexander Eßer [Alex140181]Software DeveloperCommented:
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
 
kfeinerAuthor Commented:
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
 
Alexander Eßer [Alex140181]Software DeveloperCommented:
Then why not just use a view "instead"?!
0
 
kfeinerAuthor Commented:
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
 
slightwv (䄆 Netminder) Commented:
>>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
 
kfeinerAuthor Commented:
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
 
kfeinerAuthor Commented:
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
 
Alexander Eßer [Alex140181]Software DeveloperCommented:
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
 
slightwv (䄆 Netminder) Commented:
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
 
MikeOM_DBACommented:
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
 
Alexander Eßer [Alex140181]Software DeveloperCommented:
@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
 
MikeOM_DBACommented:
Perhaps the links are self-explanatory?
;)
0
 
Alexander Eßer [Alex140181]Software DeveloperCommented:
No further questions, your honor ;-)
0
 
Geert GOracle dbaCommented:
>>  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
 
kfeinerAuthor Commented:
You must understand that I do not create the standards, but I must follow them.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.