Add an autoincrement column to a populated table
Hi I have a large table say test. I want to add a new column and populate this column with row number. How do I do it?
I was thinking that I could create a new table with id as column and populate it with numbers from 1 to total row number of the test table. Then merge the two table on columns. How do I do this?
Edited by: user10151006 on May 26, 2009 9:03 AM
Why do you want to change the sequence on an update?
Can't you do something like this:
SQL> create table t as select * from all_objects;
Table created.
SQL> alter table t add (seq_col number);
Table altered.
SQL> create sequence t_seq;
Sequence created.
SQL> update t
2 set seq_col = t_seq.nextval;
53029 rows updated.
SQL> commit;
Commit complete.
SQL> create or replace
2 trigger t_trig
3 before insert
4 on t
5 for each row
6 declare
7 l_num number;
8 begin
9 select t_seq.nextval
10 into l_num
11 from dual;
12
13 :new.seq_col := l_num;
14 end;
15 /
Trigger created.
SQL> insert into t (owner, object_id, object_name, created, last_ddl_time) values ('ME',999,'BLAH',sysdate,sysdate);
SQL> select owner, object_name, seq_col from t where object_name = 'BLAH';
OWNER OBJECT_NAME SEQ_COL
ME BLAH 53033
Similar Messages
-
How can I add a new column in compress partition table.
I have a compress partition table when I add a new column in that table it give me an error "ORA-22856: CANNOT ADD COLUMNS TO OBJECT TABLES". I had cretaed a table in this clause. How can I add a new column in compress partition table.
CREATE TABLE Employee
Empno Number,
Tr_Date Date
COMPRESS PARTITION BY RANGE (Tr_Date)
PARTITION FIRST Values LESS THAN (To_Date('01-JUL-2006','DD-MON-YYYY')),
PARTITION JUNK Values LESS THAN (MAXVALUE));
Note :
When I create table with this clause it will allow me to add a column.
CREATE TABLE Employee
Empno Number,
Tr_Date Date
PARTITION BY RANGE (Tr_Date)
PARTITION FIRST Values LESS THAN (To_Date('01-JUL-2006','DD-MON-YYYY')),
PARTITION JUNK Values LESS THAN (MAXVALUE));
But for this I have to drop and recreate the table and I dont want this becaue my table is in online state i cannot take a risk. Please give me best solution.Hi Fahed,
I guess, you are using Oracle 9i Database Release 9.2.0.2 and the Table which you need to alter is in OLTP environment where data is usually inserted using regular inserts. As a result, these tables generally do not get much benefit from using table compression. Table compression works best on read-only tables that are loaded once but read many times. Tables used in data warehousing applications, for example, are great candidates for table compression.
Reference : http://www.oracle.com/technology/oramag/oracle/04-mar/o24tech_data.html
Topic : When to Use Table Compression
Bug
Reference : http://dba.ipbhost.com/lofiversion/index.php/t147.html
BUG:<2421054>
Affects: RDBMS (9-A0)
NB: FIXED
Abstract: ENH: Allow ALTER TABLE to ADD/DROP columns for tables using COMPRESS feature
Details:
This is an enhancement to allow "ALTER TABLE" to ADD/DROP
columns for tables using the COMPRESS feature.
In 9i errors are reported for ADD/DROP but the text may
be misleading:
eg:
ADD column fails with "ORA-22856: cannot add columns to object tables"
DROP column fails with "ORA-12996: cannot drop system-generated virtual column"
Note that a table which was previously marked as compress which has
now been altered to NOCOMPRESS also signals such errors as the
underlying table could still contain COMPRESS format datablocks.
As of 10i ADD/SET UNUSED is allowed provided the ADD has no default value.
Best Regards,
Muhammad Waseem Haroon
[email protected] -
How can I add a new column to an Attachments Table
How can I add a new column to an Attachments Table??
And I want to remove the usage column also!
Thanks!I tried to remove the usage column doing this:
OAAttachmentTableBean attachTable = (OAAttachmentTableBean)webBean.findChildRecursive("attachTable");
if (attachTable != null)
attachTable.findChildRecursive("UsageTypeColumn").setRendered(false);
but I'm getting null pointer exception on "UsageTypeColumn"...
:( -
How to add an unique column to an existing table?
How to add an unique column to an existing table?
I have a large table which has no unique constraint. and I want to add an unique column for it. How to do it?
Does adding a sequence is a good choice? How to do it?
Thank youHi,
alter table tablename
add constraint contraint_name unique (columnname);but before that you need to check in the table.column there is no duplicate record exist.
Does adding a sequence is a good choice?
Your talking about unique constraint then yes.
Regards,
Taj -
Add a new column in Agreement Items table control (ME31K/ME32K/ME33K)
Hi guys,
I have a big issue.
How can I add a new column at Agreement Items table control (ME31K/ME32K/ME33K) level.
Thanks a lot.I think i solved the problem . The single way to to that is to modify the SAP standard Screen.
-
Need to add 2 new columns to the existing table control of C223 transaction
Hi ABAP Gurus,
I have to do a screen enhancement for transaction C223.
Below is the requirement:
need to add 2 new columns to the existing table control of C223 transaction.
there is no customer exits, screen exit or user exit present for this transaction C223, i have found one enhancement spot for this transaction.
i dont have any idea how to do this in standard transaction C223, the table control in C223 saves the data to MKAL table and the table control uses the structure MKAL_EXPAND in the screen program.
i have created an append structure for the 2 fields to the standard table MKAL.
Can anyone please suggest me how this can be done in standard screen C223, will the enhancement spot can be used to do this....
please sugest...
Thanks & RegardsHi Santosh,
Thanks for the reply. I have looked into this Enhancement Spot CPFX_SCREEN_SET , inside this there is only one method INPUT_DISABLED having below parameters
IM_MKAL Importing Type MKAL Production Version
EX_MSGID Exporting Type SY-MSGID Messages, Message
EX_MSGTY Exporting Type SY-MSGTY Messages, Message
EX_MSGNO Exporting Type SY-MSGNO Messages, Message
EX_MSGV1 Exporting Type SY-MSGV1 Messages, Message
EX_MSGV2 Exporting Type SY-MSGV2 Messages, Message
EX_MSGV3 Exporting Type SY-MSGV3 Messages, Message
EX_MSGV4 Exporting Type SY-MSGV4 Messages, Message
EX_INPUT_DISABLE Exporting Type CHAR1 Display Only if X Was Set
the BADI definition present here is a SAP internal so we cant implement the BADI , but we can created a enhancement spot implementation for this. as per my understanding on this the enhancement spot is only for making the table control fields display / change . i dont think this can be used to add two new coloumns to C223 table control.
I am not sure thats why seeking your help/valuable sugestion on this.
Please provide your sugestion on this , so that i can come to conclusion on this issue.
Thanks & Regards
Siddhartha Mishra -
How to add a new column to a existing table?
Hi all,
I have requriment acc which i need toadd a new column to a exsiting table.the name of the columns is taken frm database.I.e.,
say if i have 3 names in DB name1,name2,name3
i need to add in the table these three names as columns and tat to as inputfield(tablecelleditor) .Please help me with the code, i'll give full points to everyone who gives me correct answer
Regards
Sharan and please this is Really Urgent!!! plzzzzzzzzzz
Edited by: Armin Reichert on Dec 30, 2007 7:47 PMHi,
You can add the following lines of code where-ever you want to add the table:
IWDTransparentContainer tr = (IWDTransparentContainer) viewObj.getElement("trans"); // Container where table will be added
IWDTable tab = (IWDTable) viewObj.createElement(IWDTable.class ,"Table ID");
tr.addChild(tab);
IWDNodeInfo nodeInfo = wdContext.nodeProducts().getNodeInfo();
tab.bindDataSource(nodeInfo); // datasource of your table.
// First Column
IWDTableColumn tc1 = (IWDTableColumn) viewObj.createElement(IWDTableColumn.class, "TC1");
IWDCaption cap1 = (IWDCaption) viewObj.createElement(IWDCaption.class,"cap1");
cap1.setText("Column Heading 1"); // name1 of your column
tc1.setHeader(cap1);
IWDInputField inp1 = (IWDInputField) viewObj.createElement(IWDInputField.class,"INP1");
tc1.setTableCellEditor(inp1);
IWDAttributeInfo attrInfo1 = wdContext.nodeProducts().getNodeInfo().getAttribute(IPrivateTEST.IProductsElement.PRODUCT_ID);
inp1.bindValue(attrInfo1);
tab.addColumn(tc1);
// Second Column
IWDTableColumn tc2 = (IWDTableColumn) viewObj.createElement(IWDTableColumn.class, "TC2");
IWDCaption cap2 = (IWDCaption) viewObj.createElement(IWDCaption.class,"cap2");
cap2.setText("Column Heading 2");
tc2.setHeader(cap2);
IWDInputField inp2 = (IWDInputField) viewObj.createElement(IWDInputField.class,"INP2");
tc2.setTableCellEditor(inp2);
IWDAttributeInfo attrInfo2 = wdContext.nodeProducts().getNodeInfo().getAttribute(IPrivateTEST.IProductsElement.NAME);
inp2.bindValue(attrInfo2);
tab.addColumn(tc2);
// Third Column
IWDTableColumn tc3 = (IWDTableColumn) viewObj.createElement(IWDTableColumn.class, "TC3");
IWDCaption cap3 = (IWDCaption) viewObj.createElement(IWDCaption.class,"cap3");
cap3.setText("Column Heading 3");
tc3.setHeader(cap3);
IWDInputField inp3 = (IWDInputField) viewObj.createElement(IWDInputField.class,"INP3");
tc3.setTableCellEditor(inp3);
IWDAttributeInfo attrInfo3 = wdContext.nodeProducts().getNodeInfo().getAttribute(IPrivateTEST.IProductsElement.DETAILS);
inp3.bindValue(attrInfo3);
tab.addColumn(tc3);
For any further doubts you can always come back to me.
thanks & regards,
Manoj -
How to add a new column to a existing table and add data to this column?
I have a table with about 10 millions row and I want to add a new column to this table, then fill this new column with prepared data.
How to do it ?
Thank youCan I use insert statement to add the data to new-added column?No.
07:19:37 oracle >create table test (x number);
Table created.
Elapsed: 00:00:03.05
07:19:53 oracle >
07:19:53 oracle >
07:19:53 oracle >insert into test values (1);
1 row created.
Elapsed: 00:00:00.00
07:20:01 oracle >insert into test values (2);
1 row created.
Elapsed: 00:00:00.00
07:20:10 oracle >insert into test values (3);
1 row created.
Elapsed: 00:00:00.00
07:20:12 oracle >
07:20:13 oracle >commit;
Commit complete.
Elapsed: 00:00:00.00
07:20:14 oracle >
07:20:19 oracle >select * from test;
X
1
2
3
Elapsed: 00:00:00.00
07:20:22 oracle >
07:20:36 oracle >alter table test add (y number);
Table altered.
Elapsed: 00:00:00.05
07:20:41 oracle >
07:20:41 oracle >
07:20:41 oracle >
07:20:41 oracle >select * from test;
X Y
1
2
3
Elapsed: 00:00:00.00
07:20:43 oracle >
07:20:44 oracle >
07:20:44 oracle >update test set y=1;
3 rows updated.
Elapsed: 00:00:00.02
07:20:52 oracle >commit;
Commit complete.
Elapsed: 00:00:00.00
07:20:56 oracle >select * from test;
X Y
1 1
2 1
3 1
Elapsed: 00:00:00.00
07:20:58 oracle >Anand -
HOWTO: Add a UNIQUE constraint to a populated column
Here is the way to add a unique constraint to a populated column, ensuring that the existing values are unique as well.
ALTER TABLE t1
ADD CONSTRAINT t1_uk UNIQUE (col1,col2)
EXCEPTIONS INTO my_exceptions
/If the alter table statement fails this will populate the MY_EXCEPTIONS table with the rows that contain duplicate values for (col1,col2). These are identified by ROWID. We can then chose to:
[list]
[*]delete rows with a bad key;
[*]amend the values of the key columns;
[*]re-apply the constraint with the NOVALIDATE option.
[list]
We should be wary of choosing the NOVALIDATE option. There's usually a good reason why the unique constraint is required and we should not circumvent it. Apart from anything else, Oracle allows us to build foreign keys referencing NOVALIDATE unique keys. This could result in child rows that have two parents, which is normal in biology, but very wrong in a database.
NOVALIDATE is useful in datawarehouses and suchlike, because little updating occurs and the data integrity issues are of less imporatance. I don't think it ought to be used in OLTP situations. The code example at the end of this posting illustrates why we should exercise caution with NOVALIDATE.
Once we have handled the duplicate values we can re-run the alter table statement and apply the constraint.
If you don't already have an EXCEPTIONS table (it can be called anything, it's the structure that counts) you may need to run (or get a DBA to run) a script called UTLEXCPT.SQL, which will be in the $ORACLE_HOME/rdbms/admin directory.
Cheers, APC
SQL> SELECT col1, col2, col3 FROM t1;
COL1 COL2 COL3
1 1 0
2 1 0
2 1 0
SQL> SELECT col1, col2, cola FROM t2;
COL1 COL2 COLA
1 1 I'm child #1
1 1 I'm child #2
2 1 I'm child #3
SQL> ALTER TABLE t1
2 ADD CONSTRAINT t1_uk UNIQUE (col1, col2);
ALTER TABLE t1 ADD CONSTRAINT t1_uk UNIQUE (col1, col2)
ERROR at line 1:
ORA-02299: cannot validate (TST2.T1_UK) - duplicate keys found We can't add a unique key, so we decide to use the NOVALIDATE clause.
SQL> CREATE INDEX t1_i ON t1(col1, col2);
Index created.
SQL> ALTER TABLE t1
2 ADD CONSTRAINT t1_uk UNIQUE (col1, col2) ENABLE NOVALIDATE;
Table altered.
SQL> INSERT INTO t1 VALUES (2, 1, 1);
insert into t1 values (2, 1, 1)
ERROR at line 1:
ORA-00001: unique constraint (TST2.T1_UK) violated Well, we can't add any more duplicate keys, so that's alright isn't it? Nope. We can add a foreign key to table T2 referencing T1 but we don't know which row in T1 is the parent of the rows in T2.
SQL> ALTER TABLE t2
2 ADD CONSTRAINT t2_t1_fk foreign key (col1, col2)
3 REFERENCES t1(col1, col2)
Table altered.
SQL> SELECT rowid FROM t1 WHERE col1 = 2;
ROWID
AAAVnMAANAAAB96AAB
AAAVnMAANAAAB96AAC
SQL> DELETE FROM t1 WHERE rowid = 'AAAVnMAANAAAB96AAB';
1 row deleted.
SQL> DELETE FROM t1 WHERE rowid = 'AAAVnMAANAAAB96AAC';
DELETE FROM t1 WHERE rowid = 'AAAVnMAANAAAB96AAC'
ERROR at line 1:
ORA-02292: integrity CONSTRAINT (TST2.T2_T1_FK) violated - child record found
SQL> ROLL
Rollback complete.
SQL> DELETE FROM t1 WHERE rowid = 'AAAVnMAANAAAB96AAC';
1 row deleted.
SQL> DELETE FROM t1 WHERE rowid = 'AAAVnMAANAAAB96AAB';
DELETE FROM t1 WHERE rowid = 'AAAVnMAANAAAB96AAB'
ERROR at line 1:
ORA-02292: integrity CONSTRAINT (TST2.T2_T1_FK) violated - child record found
SQL> ROLL
Rollback complete.
Why this matters: queries joining child and parent tables return more rows than we'd expect normally.
SQL> SELECT t2.cola
2 FROM t2
3 WHERE t2.col1 = 2
4 and t2.col2 = 1;
COLA
I'm child #3
SQL> SELECT t2.cola, t1.col4
2 FROM t1, t2
3 WHERE t2.col1 = 2
4 AND t2.col2 = 1
5 AND t1.col1 = 2
6 AND t2.col2 = 1;
COLA COL4
I'm child #3 I'm the daddy
I'm child #3 No, I'm the daddy!!
SQL> Caveat emptor
This posting is issued on behalf of the Rogue Moderators. It is posted with the best of intentions but is not guaranteed in any way, shape or form. In particular, the code is presented "as is" and you assume full responsibility for running it on your system. Furthermore, you must not download and install software from the internet unless you know what you are doing and have the permission of whoever owns your system. It was never this hard for the Lone Ranger.Hi,
You can not create unique key with duplicate values as you know. Now if you want to create unique key then you have to delete duplicate records from the table but this again if you can afford to delete because this data may be more importante to you and you can not delete any record.
or you can use ENABLE NOVALIDATE option of oracle explain by Tom on below link.
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:8806498660292
or if you dont want to use Tom's way then you go with primary key like
Drop old primary key if there is already on the column.
create new primary key with enable novalidate option. -
How to add a new column (Project Number) in the action items table under NPD Module?
There are two projects with same name and created by same person in NPD.
So when it is displayed in "Action Items" table, It looks similar.
To avoid this, I need one more column (Project Number) to be added in the "Action Items" table and " Strategic briefs and projects" table.
So, How to add a new column (Project Number) in the "Action Items" table and " Strategic briefs and projects" table under NPD Module?
Please do the needful.There is no out of the box configuration available to add columns to NPD action items. As always we welcome enhancement requests.
Thanks
Kelly -
How to add a new columns in table contorl in standard screen.
Hi All,
I am working on a enhancement for transaction VA01. I have to add few column for tab Additional Data B. In this tab column are displayed in table control. Now i have to add two more column in table control. Could you please help me to find out the exit and the way to populate the new columns.
Thanks
Piyush MathurHi Piyush,
That table control might have been created using an internal table i.e declared in the exit. Add two more fields which you want to that internal table. And come to the layout and create table control once again using that internal table.
Reward if helpful.
Regards,
Ramana -
Can't add a new column to LCD table? Option there, just wont add it.
I have a LCD form with a table in it. Im trying to simply add a new column to the table. The option is there but the column just wont appear after clicking add Column to Left. I selected a column, right clicked the space > Insert > Column to left. But wont intsert. The table object properties tab shows rows and colum numbers but they are grayed out.
See screenshots:
http://www.emermed.net/staging/forums...
http://www.emermed.net/staging/forums...
This is a dynamic table where a new row can be inserted using a button. Is that dynamic nature the issue? Id hate to undo all the dynamic flow and programming just to add a column then reapply everything.
Thanks!Hi,
You don't add columns in the Object > Table palette (Screenshot 2). I suspect that the problem is that there is not enough on the page (within the content area) to actually add the column. In Screenshot 1, if you look at the width of the highlighted column versus the space available to the right of the table, you will see that a column cannot be added due to space restrictions.
Reduce the width of the highlighted column (temporarily), then add a column. Once added, you can resize the columns to match the page width.
Hope that helps,
Niall -
How to check 2 tables(Table A and Table B) and figure out new columns present in Table A and add these new columns to Table b.
DDL-
Create table A
( A INT,
B INT,C VARCHAR(2)
Create table B
A INT,
B INT
Any advice on the best approach or method to achieve this.
I understand that I need to check the schema of the columns and then do a match between 2 tables and find new columns and then alter my target table
MudassarCan you try this..
CREATE TABLE A ( A INT, B INT, C VARCHAR(2) )
CREATE TABLE B ( A INT, B INT )
Declare @ColumnVar nvarchar(128),@DatatypeVar nvarchar(128)
SELECT @ColumnVar=x.COLUMN_NAME, @DatatypeVar=x.DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS AS x
WHERE TABLE_NAME = 'A'
AND NOT EXISTS ( SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'B'
AND COLUMN_NAME = x.COLUMN_NAME )
Declare @SQL VarChar(1000)
SELECT @SQL = 'ALTER TABLE B ADD ' + @ColumnVar + ' '+ @DatatypeVar
Exec (@SQL)
select * from B
Please Mark This As Answer if it helps to solve the issue
http://stackoverflow.com/questions/2614101/alter-table-my-table-add-column-int -
Add a new column in search table SERIAL_NUMBER that remains 1-10 everytime
Hi All,
I need to add a column SERIAL_NUMBER to search table and what i want is that it should display 1-10 on every page.I press Next and it should again show Sr No. 1-10.
For this i added a transient variable to the SearchVO and the CO code for Table region is as follows :-
public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
super.processFormRequest(pageContext, webBean);
SearchAMImpl am= (SearchAMImpl)pageContext.getApplicationModule(webBean);
SearchVoExImpl vo=(SearchVoExImpl)am.getSearchVoEx();
SearchVoExRowImpl searchRow=(SearchVoExRowImpl)vo.first();
searchRow.setSerial(new Integer(1));
while(vo.hasNext())
if(searchRow.getSerial().intValue()==11)
searchRow.setSerial(new Integer(1));
int i=searchRow.getSerial().intValue();
Integer p=new Integer(i+1);
searchRow=(SearchVoExRowImpl)vo.next();
searchRow.setSerial(p);
The problem i am facing with this is when i sort the result table using any column the Serial Number gets disappeared.
And also when i press Go button for searching it shows me the end results first.
Please help me out.hi,
yes i am displaying 10 rows at a time and also there is no order by clause in VO query.
The above method did not work again. I tried and again got the serial number which is not ordered.
You can see my query:- select decode(mod(rownum,10), 0, 10, mod(rownum,10)) Serial_Number,party_name,address1,address2 from hz_parties
As i mentioned in my question in the beginning that after sortin the sr_no disappers i am able to solve it by handling SORT_EVENT.
Now the only problem left is that it shows me the last results first. Can you please help me here?
Thanks -
Nsert/Update and Add Column at the same Table and at the "same" Time
Hello,
I want Insert/Update and Add Column at the same Table and at the "same" Time but in different sessions.
Example:
At first the "insert/update" statement:
Insert into TestTable (Testid,Value) values (1,5105);
After that the "add" statement:
Alter table TestTable add TestColumn number;
- sadly now I get the message: ORA-00054: resource busy and acquire with NOWAIT specified
"insert/update" statement:
Insert into TestTable (Testid,Value) values (2,1135);
After that the execute commit.
I don't know when the first session set the commit statement so I want that the DB the "Alter Table..." statement execute if it's possible.
If it's possible I want to save a repeat loop with the "Alter Table..." statemtent.
Thanks for ideasWell I want to walk in the rain without and umbrella and still stay dry, but it ain't gonna happen.
You can't run a DDL statement against a table with transactions pending. Session 2 has to wait until session commits or rollbacks (or until the session is killed). That's just the way it is.
This makes sense if you think about it. The data dictionary has to be consistent across all sessions. If session 2 was allowed to change the table structure whilst session 1 has a pending transaction then the database is in an inconsistent state. This is easier to see if you consider the reverse situation - the ALTER TABLE statement run by session 2 does a DROP COLUMN TESTID rather than adding a column: now what should happen to session 1's INSERT statement? You have retrospectively invalidated a statement that was perfectly legal when it was executed.
If it's possible I want to save a repeat loop with the "Alter Table..." statemtent.Fnord.
Cheers, APC
Maybe you are looking for
-
Can i use my G5 drive in the Mac Pro?
Hi. I have a 2005 G5 with a western digital serial ata drive with all my audio on it. I also have an airport card which id like to put in the mac pro. Can i put them in my new mac pro which i should get in the next few days? Im not sure if there is a
-
Can any one tell me how param is used in Jsp:setProperty ?
Hi Can anyone please explain me with example how param attribute is used in <jsp:setProperty>? I have gone through the java doc but it still not clear to me....
-
Hello I read that if no undo_tablespace is available, the user transactions will be executed using the SYSTEM rollback segment. In what circumstance will the DBA start the DataBase without an UNDO tablespace? What will be the consequences? Thank you.
-
Just this morning iI noticed that the menu bar and back/forwrd buttons are not displayed. Other weirdnesses with Safari. Help? If I hover the crusor over the top of the screen things return until I move the cursor. How to fix?
-
On (keyPress " Enter ") --PLZ check
Hi, For some reason, I can't get the published SWF to advance to the target frame called 'enter name' On frame 1, I have an invisible button with the following code attached: on (keyPress "<Enter>") { gotoAndPlay ("enter name"); Any help would be gre