Altering data type and default on DB2 LUW 10.5 - alter-table

I am trying to do 2 alters to a column in DB2 in the same alter command, and it doesn't seem to like my syntax. Is there a way to do this? If so, how?
create table tbl(col varchar(10))
The following works, using 2 alter statements:
alter table tbl
alter column col set data type varchar(128)
alter table tbl
alter column col set default current user
The following attempts fail:
alter table tbl
alter column col set data type varchar(128) set default current user
An unexpected token "set data type varchar(128)" was found following
"TBL alter column col". Expected tokens may include: ""..
SQLCODE=-104, SQLSTATE=42601, DRIVER=3.67.28
alter table tbl
alter column col set data type varchar(128) with default current user
An unexpected token "data type varchar(128)" was found following
"alter column col set". Expected tokens may include: ""..
SQLCODE=-104, SQLSTATE=42601, DRIVER=3.67.28
alter table tbl
alter column col set data type varchar(128)
alter column col set default current user
"COL" is a duplicate name.. SQLCODE=-612, SQLSTATE=42711,
DRIVER=3.67.28

You cannot alter the same column twice in the same statement -- that's what SQLCODE -612 is telling you. As per http://www-01.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.messages.sql.doc/doc/msql00612n.html
a column name can only be referenced in one ADD, DROP COLUMN, or ALTER COLUMN clause in a single ALTER TABLE statement.
The other attempts are syntactically incorrect.

alter table tbl
alter COLUMN col set DATA TYPE VARCHAR(128)
ALTER column B SET DEFAULT current user;

Related

ORA-12899 value too large error while trying to drop a NUMBER column

I am trying to drop a column from a table in Oracle using a statement like this:
ALTER TABLE "MY_TABLE" DROP COLUMN "ENABLED";
but I am getting this error:
ORA-12899: value too large for column "MY_TABLE"."ENABLED" (actual: 184, maximum: 22)
The column is defined as
"ENABLED" NUMBER(1,0) DEFAULT NULL NOT NULL ENABLE,
and it only has values of 0 or 1 in all the rows.
Can anyone tell me why I am getting this error and how to drop the column?
You can try setting it unused first, then dropping unused columns. For more, see Marking Columns Unused.
You maybe have some rows with incorrect encoding, you may want to try a FLIP/FLOP:
-Create a table NEW_MY_TABLE with the same structure as MY_TABLE, but without "ENABLED" Column.
-Make an Insert Into NEW_MY_TABLE (col1,col2...) Select (Col1,Col2...) from MY_TABLE
-Rename MY_TABLE to MY_TABLE_OLD
-Rename NEW_MY_TABLE to MY_TABLE
-Drop MY_TABLE_OLD

How to alter the data type of a column from varchar2 to number in Oracle database

When I ran the alter query, I get error saying that column to be modified must me empty.
Table : Monthly_Result (Id Number(38,0), dealer_ID varchar2, sales_revenue Number(38,2))
dealer_Id should be changed to Number(38,0)
Please help
As Alex mentioned in his comment you will need to add new column; update it and check values were converted correctly; then drop the old column when you're ready.
-- step 1
alter table monthly_result add tmp number(38, 0);
update monthly_result set tmp = to_number(dealer_id);
-- step 2
-- check values are set correctly in tmp
-- step 3
alter table monthly_result rename column dealer_id to dealer_id_old;
alter table monthly_result rename column tmp to dealer_id;
-- step 4
alter table monthly_result drop column dealer_id_old;
Please try this it would be help the update add column things you will loss your primary key and position of the table:
--COPY THE DATA TO TEMPORARY TABLE
CREATE TABLE _TMP AS SELECT * FROM ;
--TRUNCATE OLD DATA
TRUNCATE TABLE ;
--ALTER SPECIFIC COLUMN TYPE
alter table MODIFY number(38, 0);
--BRING THE DATA FROM WHICH COPY PREVIOUS TEMPORARY TABLE
INSERT INTO SELECT * FROM _TMP;
--CHECK YOUR DATA
SELECT * FROM ;
--DROP YOUR TEMPORARY TABLE
DROP TABLE _TMP;
I believe it will work for you.

How can I ALTER COLUMN to NULL when the column in NOT NULL in Oracle

How can I ALTER COLUMN to NULL when the column in NOT NULL in Oracle, with a sentence or script?
To alter the column (assuming the current type is varchar2(100)):
alter table mytable modify column1 varchar2(100) null
Interestingly, this fails if the column is already nullable.

date Not null , error ora_01758

why I am getting this error ?
In the table DDL I only have 2 columns , id (number) and name (varchar)
ALTER TABLE mytable ADD SUSPEND date NOT NULL
ORA-01758: table must be empty to add mandatory (NOT NULL) column
ORA-06512: at line 7
ORA-01758: table must be empty to add mandatory (NOT NULL) column ORA-06512: at line 7
And is your table empty? I think not.
There's probably a way around this involving adding the column as nullable, then populating every row with a non-NULL value, the altering the column to be not null.
Alternatively, since the problem is that these current rows will be given NULL as a default value, and the column is not allowed to be NULL, you can also get around it with a default value. From the Oracle docs:
However, a column with a NOT NULL constraint can be added to an existing table if you give a default value; otherwise, an exception is thrown when the ALTER TABLE statement is executed.
Here is a fiddle, how you could do it
Would a date in the future be acceptable as a temporary default? If so, this would work:
ALTER TABLE MYTABLE ADD (SUSPEND_DATE DATE DEFAULT(TO_DATE('21000101', 'YYYYMMDD'))
CONSTRAINT SUSPEND_DATE_NOT_NULL NOT NULL);
If table already contain the records then table won't allowes to add "Not null" column.
If you need same then set default value for the column or truncate the table then try.

How to set default value for column of new created table from select statement in 11g

I create a table in Oracle 11g with the default value for one of the columns. Syntax is:
create table xyz(emp number,ename varchar2(100),salary number default 0);
This created successfully. For some reasons I need to create another table with same old table structure and data. So I created a new table with name abc as
create table abc as select * from xyz.
Here "abc" created successfully with same structure and data as old table xyz. But for the column "salary" in old table "xyz" default value was set to "0". But in the newly created table "abc" the default value is not set.
This is all in Oracle 11g. Please tell me the reason why the default value was not set and how we can set this using select statement.
You can specify the constraints and defaults in a CREATE TABLE AS SELECT, but the syntax is as follows
create table t1 (id number default 1 not null);
insert into t1 (id) values (2);
create table t2 (id default 1 not null)
as select * from t1;
That is, it won't inherit the constraints from the source table/select. Only the data type (length/precision/scale) is determined by the select.
The reason is that CTAS (Create table as select) does not copy any metadata from the source to the target table, namely
no primary key
no foreign keys
no grants
no indexes
...
To achieve what you want, I'd either
use dbms_metadata.get_ddl to get the complete table structure, replace the table name with the new name, execute this statement, and do an INSERT afterward to copy the data
or keep using CTAS, extract the not null constraints for the source table from user_constraints and add them to the target table afterwards
You will need to alter table abc modify (salary default 0);
new table inherits only "not null" constraint and no other constraint.
Thus you can alter the table after creating it with "create table as" command
or you can define all constraint that you need by following the
create table t1 (id number default 1 not null);
insert into t1 (id) values (2);
create table t2 as select * from t1;
This will create table t2 with not null constraint.
But for some other constraint except "not null" you should use the following syntax
create table t1 (id number default 1 unique);
insert into t1 (id) values (2);
create table t2 (id default 1 unique)
as select * from t1;

Resources