Suppose we need to change column data type in Oracle database table. Suppose also that the table has some data. Let's say 1000000 rows. I want to change datatype from NUMBER to VARCHAR2. Lets see.
Oracle doesn't allow me to modify column this way when there is data in the table. I can not also rename the column.
Solution:
ALTER TABLE: add new column (temp) with desired datatype.
UPDATE: (copy data to the new column using piecemeal batch size)
ALTER TABLE: drop the original column
ALTER TABLE: add new column with the name of the original and the desired datatype
UPDATE: (copy data to the new column from the temp column using piecemeal batch size)
ALTER TABLE: drop temp column
Now you say - which one is more powerful: MSSQL or ORACLE ? Although the same approach is considered as best practices for MSSQL also, but the fact is that MSSQL is still more flexible since you can rename the new column after dropping the original.
PostGreSQL is better than both of them put together. Other than in a few cases though Oracle is a much better RDBMS than MSSQL. Especially if you are dealing with huge amounts of data like storing mechanical drawing files or engineering schematics as whole files in the Database. MSSQL absolutely sucks for doing that. But MSSQL is a lot easier to use than Oracle is from a Dba point of view so I guess it depends on what you are looking for. For many things companies do ease of use for the Dba affects how good the RDBMS is for them so MSSQL is the way to go. Other companies want reliability and massive datasets with terrabytes of data which absolutely cannot go down if they want to maintain their five nines. Which is why most telecomm's use Oracle running on Sun Solaris boxes. Harder on the Dba's to work with but all the 911 calls go through and nobody dies because of a dropped call. It really depends on what you are doing as to which you would want to go with. Personally for what I do I'll take Oracle over MSSQL and PostGreSQL over both of them.
#re: How to change column datatype in Oracle table (ALTER TABLE).
What about trying to modify a column in Oracle that definitely has no data in the table, and it won't work. For instance, I've been trying to change the data_length of the column, and I don't get an error message, I get 'table altered' and yet even after a commit it shows up as the old data_length. Tried dropping the column, committing (queried to make sure it was gone), created a new column with the correct data_type and data_length, and committed, then it STILL used a different data_length. How is this possible?
#re: How to change column datatype in Oracle table (ALTER TABLE).
As it turns out, the default for Oracle is 22 for the datatype NUMBER.
So my changes were effective, but when I pull up the user_tab_columns query it shows the Oracle default for NUMBER instead of actual data_length.
I'm not sure why this is, but when I checked the actual code in SQL_STATION to create the table it definitely listed the data_type and data_length for the column I was concerned with correctly.
#re: How to change column datatype in Oracle table (ALTER TABLE).
OK, also, my query should have asked for data_precision, not data_length. Data_length within user_tab_columns asks for the total number of characters allowed by default for the data_type. Precision asks for exactly what you set the data_type at, and scale would of course be any digits after the decimal.
#re: How to change column datatype in Oracle table (ALTER TABLE).
Anatoly: regarding your reply to Jonathan, above, he was responding to your statement at the end of your post that MSSQL is more flexible because it allows you to rename the new column after dropping the original. He was pointing out that Oracle allows you to do this, too.
#re: How to change column datatype in Oracle table (ALTER TABLE).
Jonathan method will change Column_ID (order)
With text column, there re no proble when you extend column.
But with number col: You can create temp to hold old data and set null old column then modify old column and last copy data back.