How add primary key constraint to already existing table with data

I want apply primary key constraint to already existing table with data
is there any command or way to do

Alternatively, assuming you want to ensure uniqueness in your primary key column you can do this:
alter table <table name> add constraint <cons name> primary key (col1,col2) exceptions into <exception_table>
If the altter table statement fails this will populate the EXCEPTIONS table with the rows that contain duplicate values for (col1,col2).
You will need to run (or get a DBA to run) a script called UTLEXCPT.SQL (which will be in the $ORACLE_HOME/rdbms/admin directory) if you don't already have an EXCEPTIONS table.
Cheers, APC

Similar Messages

  • Primary key constraint for index-organized tables or sorted hash cluster

    We had a few tables dropped without using cascade constraints. Now when we try to recreate the table we get an error message stating that "name already used by an existing constraint". We cannot delete the constraint because it gives us an error "ORA-25188: cannot drop/disable/defer the primary key constraint for index-organized tables or sorted hash cluster" Is there some sort of way around this? What can be done to correct this problem?

    What version of Oracle are you on?
    And have you searched for the constraint to see what it's currently attached to?
    select * from all_constraints where constraint_name = :NAME;

  • Create a materized view without primary key constraint on the base table?

    Hi
    I tried to create a materized view but I got this error:
    SQL> CREATE MATERIALIZED VIEW TABLE1_MV REFRESH FAST
    START WITH
    to_date('04-25-2009 03:00:13','MM-dd-yyyy hh24:mi:ss')
    NEXT
    sysdate + 1
    AS
    select * from TABLE1@remote_db
    SQL> /
    CREATE MATERIALIZED VIEW TABLE1_MV REFRESH FAST
    ERROR at line 1:
    ORA-12014: table 'TABLE1' does not contain a primary key constraint.
    TABLE1 in remote_db doesn't have a primary key constraint. Is there anyway that I can create a materized view on a base table which doesn't have a primary key constraint?
    Thanks
    Liz

    Hi,
    Thanks for your helpful info. I created a materialized view in the source db with rowid:
    SQL> CREATE MATERIALIZED VIEW log on TABLE1 with rowid;
    Materialized view log created.
    Then I created a MV on the target DB:
    CREATE MATERIALIZED VIEW my_schema.TABLE1_MV
    REFRESH FAST
    with rowid
    START WITH
    to_date('04-25-2009 03:00:13','MM-dd-yyyy hh24:mi:ss')
    NEXT
    sysdate + 1
    AS
    select * from TABLE1@remote_db
    SQL> /
    CREATE MATERIALIZED VIEW my_schema.TABLE1_MV
    ERROR at line 1:
    ORA-12018: following error encountered during code generation for
    "my_schema"."TABLE1_MV"
    ORA-00942: table or view does not exist
    TABLE1 exists in remote_db:
    SQL> select count(*) from TABLE1@remote_db;
    COUNT(*)
    9034459
    Any clue what is wrong?
    Thanks
    Liz

  • Can we have more than one primary key constraint to a Oracle Table?

    Hi,
    Can we have more than one primary keys to a single table in oracle? ( Not the composite key)
    Please somebody answer..
    Regards,
    Alaka

    811935 wrote:
    Can we have more than one primary keys to a single table in oracle? ( Not the composite key)
    In principle a table can have multiple keys if you need them. It is a very strong convention that just one of those keys is designated to be "primary" but that's just a convention and it doesn't stop you implementing other keys as well.
    Oracle provides two uniqueness constraints for creating keys: the PRIMARY KEY constraint and the UNIQUE constraint. The PRIMARY KEY constraint can only be used once per table whereas the UNIQUE constraint can be used multiple times. Other than that the PRIMARY KEY and UNIQUE constraints serve the same function (always assuming the column(s) they are applied to are NOT NULL).

  • How many primary key fields  allowed for one table?

    hi,
    when i creating a table , how many primary key fields are allowed for one table.
    please any one give ans
    thanks

    Just checked it,  Its 255, not 155.  You can have as many key fields as you want, but you can not go over the 255 byte length for the total of all key fields.  You will get a warning for over 120, as it says that there is limited functionality with a key over 120 in length.
    Again, this is a total length of all key fields.
    Regards,
    Rich Heilman

  • Rename the existing table with date suffix

    Hi,
    I'm trying to rename a table with date suffix at the end of the table name, and drop the date suffix table which is greater than 7 days. for that I have the below sql, I have not included the drop syntax in this.
    I'm not able to rename with the date suffix in the below sql, syntax error at '+'  
    DECLARE
    @TPartitionDate date
    IF EXISTS (SELECT * FROM sysobjects WHERE Name = 'IIS_4')
    BEGIN
    SELECT
    @TPartitionDate = MAX(PartitionDate)
    FROM PartitionLog (NOLOCK)
    EXEC sp_rename 'IIS_4','IIS_4_'+ @TPartitionDate
    END

    create table Test(sno int)
    DECLARE
    @TPartitionDate date = getdate()
    declare @a varchar(200)
    IF EXISTS (SELECT * FROM sysobjects WHERE Name = 'Test')
    BEGIN
    select @a='TEST_'+ cast(@TPartitionDate as varchar(10))
    EXEC sp_rename 'TEST',@a
    END
    drop table [test_2015-04-23]
    Hope it Helps!!

  • Partitioning an Existing Table with data

    Hi All,
    I am few tables with data, I need to Partition the table without affecting existing table values is it possible?.
    if yes then Please suggest me some ideas to archive that..
    Thanks & Regards
    Sami

    Hi All,
    I Need to partition for existing table with 1 million records
    1. First partition should be created for 6 months
    2.Second partition should created for 1 year.
    3.So as of now we have 6 months Data in production + another 6 months data in First partition + another 1 year data in Second partition.
    4.More than 2 year’s data’s should be moved other partition or archived.
    kindly provide your valuabe suggestion.
    Thanks & Regards
    Sami

  • How to set  a field primary key in a pre existing table

    Hi,
    Suppose i have a table and no field in it is a primary key like this
    create table t1
    (a number not null,
    b number);
    Later i want to make a as primary key then how to do that?
    I tried with this statement but it gave error
    alter table t1
    modify a primary key;

    I see, you want autogenerated constraint name (I hate that feature). What version are you on? It works on my 10.2.0.4.0:
    SQL> create table t1
      2  (a number not null,
      3  b number);
    Table created.
    SQL> alter table t1
      2  modify a primary key;
    Table altered.
    SQL> SY.

  • How to change the partition type on a table with data

    Hi
    I have a table, which is partitioned by list on date column. The table contains large volume of data.
    My requirement is to make the list partition into range on the same date column.
    I am working on Oracle10g
    Kindly advice

    partitioned by list on date How many distinct DATE values do you have ? Each DATE value would be a Partition in List Partitioning.
    Why do you need to convert this to RANGE Partitioning ? How many DATE values do you expect in the table in the future ?
    You can use DBMS_REDIFINITION to copy a LIST Partitioned Table to a RANGE Partitioned Table and then switch the tables at the end.
    See examples (where the source table is not partitioned)
    http://www.oracle-base.com/articles/misc/partitioning-an-existing-table.php
    http://uhesse.com/2010/02/15/partitioning-a-table-online-with-dbms_redefinition/
    But before converting I would justify why a LIST Partitioned Table be converted to RANGE Partitioned. Do you have Date-Range queries ?
    Hemant K Chitale
    Edited by: Hemant K Chitale on Mar 15, 2013 2:11 PM

  • Creating Partitions in Existing tables with Data

    Hello everyone,
    I want to know what the best way of creating partitions, in tables having data, is .. ???
    thanks
    Rossy Rocs

    There are two ways :
    1a Create a new Partitioned Table
    1b Copy data from the existing table to the new table
    1c Create Indexes
    1d Rename old table
    1e Rename new table
    OR
    2 Use DBMS_REDEFINITION
    See
    http://www.oracle-base.com/articles/misc/PartitioningAnExistingTable.php
    Hemant K Chitale

  • Generate a Number for a already existing table with records

    Hi All,
    Is it possible to generate number for a column in a table. The table already has 50,000 records. I have a empty column in which I want to generate number sequentially for reference purpose......
    Whether it can be done ?????
    I was thinking og Merge/ rownum I didnt get any possibls solution out of it .........any suggestions....
    Thanks
    Ananda

    I have a empty column in which I want to generate number sequentially for reference purposeThe following table content :
    Oracle
    DB2
    MSSQLNow, you have to put a kind of ID, but what row will get the 1st ? What row will get the 2nd ? etc.
    If you don't care, then sequence as suggested is your friend. Then, you could use also sequence for further insert.
    Nicolas.

  • Partition an Existing Table with Data

    We have a table that is approx. 23Gb with 90 million rows. We need to partition it. What is the fastest or most efficient way to accomplish this? Oracle 10.2.0.4.

    user1175547 wrote:
    We have a table that is approx. 23Gb with 90 million rows. We need to partition it. What is the fastest or most efficient way to accomplish this? Oracle 10.2.0.4.you can use DBMS_REDEFINITION, also exchange partition is also a good solution :)
    http://www.oracle-base.com/articles/misc/PartitioningAnExistingTableUsingExchangePartition.php

  • IDOC to MSsql,  error in mapping(Violation of PRIMARY KEY constraint)

    Hi All,
    I'm working with MATMAS IDOC  to MSSql. My SQL structure is of 8 tables(multiple statements) with primary key. In mapping i have used UPDATE_INSERT in action field for all the tables but still im getting "Violation of PRIMARY KEY constraint" for the last table structure. first 7 tables are single occurances but the 8th structure data is coming multple times from IDOC(E1MARMM).
    my 8th table structure is :
    STATEMENT8                                IDOC(MAPPED)
    TABLE8
    ACTION                                      UPDATE_INSERT
    TABLE                                           TABLENAME
    ACCESS     0 to Unbounded            MAPPED with E1MARMM
    Item_CD                                used oneasmany +splitbyvalue with MATNR
    Plant_ID                                  used oneasmany +splitbyvalue with WERKS  
    EAN_CAT                                 used oneasmany +splitbyvalue with NUMTP
    EAN                                             used oneasmany +splitbyvalue with EAN11
    Numerator_For_Conversion_To_BaseUOM
    Display_UOM
    Denominator_for_conversion_To_baseUOM
    KEY           0 to Unbounded                 MAPPED with E1MARMM
    Item_CD                                used oneasmany +splitbyvalue with MATNR
    Plant_ID                                  used oneasmany +splitbyvalue with WERKS  
    EAN_CAT                                 used oneasmany +splitbyvalue with NUMTP
    EAN                                             used oneasmany +splitbyvalue with EAN11
    Display_UOM
    in test tab its fine and fetching number of times according to MARMM segments  but in END to END testing its triggering an error stating that
    ""Message processing failed. Cause: com.sap.engine.interfaces.messaging.api.exception.MessagingException: Error processing request in sax parser: Error when executing statement for table/stored proc. 'MM_EAN' (structure 'STATEMENT8'): com.microsoft.sqlserver.jdbc.SQLServerException: Violation of PRIMARY KEY constraint 'PK_MM_EAN'. Cannot insert duplicate key in object 'dbo.MM_EAN'.  ""
    Plz help me regarding this..

    Hi team,
    How resolve the below error 
    Violation of PRIMARY KEY constraint 'PK_test'. Cannot insert duplicate key in object 'dbo.test'. The duplicate key value is (12610). (Source: MSSQLServer, Error number: 2627) ?
    Thanks,
    Ram
    RAM
    There can be two reasons
    1. The insert script used is having multiple instances of the records with Key as 12610 returned from the source query. If this is the issue add a logic to include only the unique set of id values for records by avoiding duplicates. There are several approaches
    for this like using ROW_NUMBER with PARTITION BY, using a join with derived table etc
    2. The record with Key 12610 already exist in your destination table and your script is again trying to insert another instances of record with same key. This can be avoided by adding a NOT EXISTS condition with a subquery which will check and return only
    those records which doesnt already exist in the source
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • Modify a primary key constraint

    Hi,
    I'm starting with oracle, and I have two doubts that can't handle after searching on the internet.
    1º It's possible to make an ALTER TABLE of a CONSTRAINT DEFAULT?. Maybe you think that the question should be in another way like this: Does oracle database take the default condition as a constraint?. As long as I know, doesn't do that.
    2º It's possible, with an ALTER TABLE, to MODIFY a CONSTRAINT of a PRIMARY KEY, to add another more field?
    I've been searching on the internet, but I didn't find a clear answer. I'm using oracle 10g express edition.
    Thank you very much.
    P.D.:Sorry for my english it's not very good.

    #2 - You can not 'add a column to the primary-key constraint of a table' with a single ALTER, but you can drop and re-create the primary key constraint (including the additional column).
    13:25:26> create table t1 (a number primary key, b number, c number );
    Table created.
    Elapsed: 00:00:00.09
    13:25:46> alter table t1 drop primary key;
    Table altered.
    Elapsed: 00:00:00.06
    13:25:47> alter table t1 add primary key (a,b);
    Table altered.
    Elapsed: 00:00:00.09But you will not be able to drop the primary key of an index-organized table.
    13:25:47> create table t2 (a number primary key, b number, c number ) organization index;
    Table created.
    Elapsed: 00:00:00.03
    13:25:59> alter table t2 drop primary key;
    alter table t2 drop primary key
    ERROR at line 1:
    ORA-25188: cannot drop/disable/defer the primary key constraint for
    index-organized tables or sorted hash cluster
    Elapsed: 00:00:00.00
    13:25:59> #1 - I'm not sure I understand your question , but you can alter a table's column to have a default. This is not a 'constraint', but a default. It only has effect when you perform an insert which does not include that column.
    13:31:23> alter table t1 modify (c default 3.14);
    Table altered.
    Elapsed: 00:00:00.04
    13:31:24> insert into t1 (a,b,c) values (1,2,3);
    1 row created.
    Elapsed: 00:00:00.01
    13:31:24> insert into t1 (a,b,c) values (4,5,NULL);
    1 row created.
    Elapsed: 00:00:00.00
    13:31:24> insert into t1 (a,b) values(6,7);
    1 row created.
    Elapsed: 00:00:00.01
    13:31:24> select * from t1;
             A          B          C
             1          2          3
             4          5
             6          7       3.14
    3 rows selected.
    Elapsed: 00:00:00.18

  • Adding Constraints to an existing table

    Hi,
    I've consulted the forum, documentation, the internet and text books but cannot find the correct SQL command to add constraints to an existing table
    Obviously I would
    ALTER TABLE Turkey ADD CONSTRAINT Whitemeat
    But what comes next is puzzling, the values of column Whitemeat can be any value other than Dinner, Gravy, Pumpkin, or Sauce
    Any assistance would be great
    Thank You

    Hi,
    user8998591 wrote:
    Sorry,
    This is what I'm trying to run
    ALTER TABLE TURKEY add
    CONSTRAINT TURKEY_WHITEMEAT_CK CHECK WHITEMEAT NOT IN ('Dinner', 'Gravy', 'Pumpkin', 'Sauce');And this is the error message I am getting
    Error report:
    SQL Error: ORA-00906: missing left parenthesis
    00906. 00000 -  "missing left parenthesis"
    *Cause:   
    *Action:Thank you for your assistanceSee the [SQL Language manual|http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_3001.htm#sthref5176] for the correct syntax.
    The condition after the keyword CHECK has to be in parentheses.
    To add a CHECK constraint to an existing table, I think you have to modify the column.
    ALTER TABLE turkey
    MODIFY  ( whitemeat     CONSTRAINT     turkey_whitemeat_ck
                     CHECK          ( whitemeat NOT IN ( 'Dinner'
                                                , 'Gravy'
                                          , 'Pumpkin'
                                          , 'Sauce'
         )                    )             );Edited by: Frank Kulash on Jan 22, 2010 4:37 PM

Maybe you are looking for

  • Updating large pdf file on multi devices but do I have to download the entire file every time?

    So I have a pdf file that is 500mb (already on acrobat.com cloud) and I want to read this across tablet, desktop, and phone. I will be updating this file bit by bit on every one of those devices. Does this mean that every time I want to see the updat

  • Download my old age of mythology game onto my macbook pro

    i don't know how to download my old age of mythology game onto my new macbook pro even though it says it uses the same operating system

  • Black background icons

    The screen on my MacBook appears to have a black background like a picture negitive. The original background was changed by my son, and now I cannot change the screen back to a visible display?

  • Webdynpro CATS Approval.

    Dear Expert, I have request a overtime request thru the portal and the standard workflow TS31000007 is trigger and in the requester inbox, i am able to see the item in the UWL However when i try to click on the cats approval item, the page give me a

  • How to Configure the system to use an firm's accepted warning banner in 10g

    Hello. I need to configure the system (oracle 10.2) to use an firm's accepted warning banner. I was able to do it in version 11 by putting entries in sqlnet.ora file. It then displayed when connectting as user (sys, system). I tried the same with 10.