Add a primary key to table.

Hello everybody,
I want to add a primary key to a table but it doesn't work.
select count(*), count(id) from tableA;
COUNT(*) COUNT(ID)
830447 830447
You see: The number of ids is equal to the number of rows. The ID is not marked as unique but it should be unique. I try to set id as the primary key:
alter table tableA add constraint "pk_constraint" PRIMARY KEY (id);
But I a get an error message: ORA-02437: cannot validate (pk_constraint) - primary key violated.
What is the problem?
I use Oracle 11g.

user9206958 wrote:
Hello everybody,
I want to add a primary key to a table but it doesn't work.
select count(*), count(id) from tableA;
COUNT(*) COUNT(ID)
830447 830447
You see: The number of ids is equal to the number of rows. The ID is not marked as unique but it should be unique. I try to set id as the primary key:
alter table tableA add constraint "pk_constraint" PRIMARY KEY (id);
But I a get an error message: ORA-02437: cannot validate (pk_constraint) - primary key violated.
What is the problem?
I use Oracle 11g.Revisit your query... 11g can help much in this sql.... select count(*), count(id) from tableA;... to get what your desired output should be.
Edited by: Z.K. on Mar 30, 2010 1:15 PM

Similar Messages

  • Add another Primary key column problem

    Hi all
    I do not Add primary key in existing table .how it possiable
    say I have Existing Table
    EMPNO NUMBER(4), primary key
    ENAME VARCHAR2(10 BYTE),
    JOB VARCHAR2(9 BYTE),
    EMPSLNO VARCHAR2(10 BYTE)
    Now Add another Primary key column(EMPSLNO)
    my coding this
    ALTER TABLE EMP_TEST ADD (
    CONSTRAINT PK_EMP_TEST PRIMARY KEY (EMPSLNO))
    but error message
    Error on line 0
    ALTER TABLE EMP_TEST ADD (
    CONSTRAINT PK_EMP_TEST PRIMARY KEY (EMPSLNO))
    ORA-02260: table can have only one primary key

    ..Or, too much few information, wild guess, (composite index)
    Do this :
    ALTER TABLE <your_table> ADD (
         CONSTRAINT PK_mycode PRIMARY KEY (head_code,item_code )
              USING INDEX TABLESPACE tbls_index;

  • ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    Hi,
    I am trying to delete data from a table by dropping a partition. I have identified all the child tables by running the following command.
    select 'select count(*) from '||table_name||' where employee_id = 100;'
    from dba_constraints
    where constraint_type='R'
    and r_constraint_name in
    (select constraint_name from dba_constraints
    where constraint_type in ('P','U') and table_name='EMPLOYEE);
    'SELECTCOUNT(*)FROM'||TABLE_NAME||'WHEREEMPLOYEE_ID_ID=100;'
    select count(*) from PT_ORDERS where employee_id = 100;
    select count(*) from PT_DEP where employee_id = 100;
    select count(*) from PT_SKILLSET where employee_id = 100;
    I dropped the partition for employee_id 100 in all of the child tables. The select count(*) returns 0 rows for each of the above.
    When I try to run the below command on the EMPLOYEE table, I get 'ORA-02266: unique/primary keys in table referenced by enabled foreign keys'.
    alter table EMPLOYEE drop partition EMP_ID_100;
    I cant see why I am unable to drop this partition now as there is now child data in any of the referenced tables. Any suggestions or help on this would be greatly appreciated.
    Thanks.
    Rgs,
    Rob

    You should disable foreign key constraints first and drop partition. Deletion of rows or dropping partitions in childs don't work in this case
    as you have the global dependency:
    <PRE>
    SQL> create table scott.t (x int primary key, y int)
    2 partition by list (y) (
    3 partition p_1 values(1), partition p_2 values(2))
    4 /
    Table created.
    SQL> create table scott.t_c (x int references scott.t(x), y int)
    2 partition by list (y) (
    3 partition p_1 values(1), partition p_2 values(2))
    4 /
    Table created.
    SQL> insert into scott.t values(1,1)
    2 /
    1 row created.
    SQL> insert into scott.t values(2,2)
    2 /
    1 row created.
    SQL> insert into scott.t_c values(1,1)
    2 /
    1 row created.
    SQL> insert into scott.t_c values(2,2)
    2 /
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> alter table scott.t_c drop partition p_2;
    Table altered.
    SQL> alter table scott.t drop partition p_2;
    alter table scott.t drop partition p_2
    ERROR at line 1:
    ORA-02266: unique/primary keys in table referenced by enabled foreign keys
    SQL> select constraint_name from dba_constraints
    2 where owner = 'SCOTT' and constraint_type = 'P'
    3 and table_name = 'T';
    CONSTRAINT_NAME
    SYS_C0011058
    SQL> select constraint_name from dba_constraints
    2 where owner = 'SCOTT' and constraint_type = 'R'
    3 and r_constraint_name = 'SYS_C0011058';
    CONSTRAINT_NAME
    SYS_C0011059
    SQL> alter table scott.t_c disable constraint SYS_C0011059;
    Table altered.
    SQL> alter table scott.t drop partition p_2;
    Table altered.
    SQL> alter table scott.t_c enable novalidate constraint SYS_C0011059;
    Table altered.
    </PRE>
    I guess you should consider such option as Referencial partitioning (with some restrictions).
    Best wishes,
    Dmitry.

  • Populate choice component  with   2 primary key  in table

    There are 2 primary key in table. When I create my uix with jheadstart, the UIXs don't populate choice component. How can I create automatically populate choice component, with 2 PK in table?

    A choice component can only have one value attribute. So if you have a composite primary key in the table you use to populate the choice, it will not work correctly, because based on this single attribute the choice cannot render the correct row as selected.
    Steven Davelaar,
    JHeadstart Team.

  • How many primary keys for one table?can we add more primary keys

    In general Table have one  primary key.is it possible to more than one primary key in a table?(ie is it possible to add more than one pirmary key in a table). Help me out.

    pls do check this link
    [http://help.sap.com/saphelp_nw04/helpdata/en/cf/21ea0b446011d189700000e8322d00/frameset.htm|http://help.sap.com/saphelp_nw04/helpdata/en/cf/21ea0b446011d189700000e8322d00/frameset.htm]
    All the key fields of a table must be stored together at the beginning of the table. A non-key field may not occur between two key fields.
    A maximum of 16 key fields per table is permitted. The maximum length of the table key is 255.
    If the key length is greater than 120, there are restrictions when transporting table entries. The key can only be specified up to a maximum of 120 places in a transport. If the key is larger than 120, table entries must be transported generically.

  • Alter different users table, i need to add constraint: primary key

    Hi All,
    my oracle database : 10G
    I have a user named rahul and its trying to alter a table of a different user 'biapps_12'
    here is the alter table script that I am firing using user 'rahul' when connected to database 'MDMINT5' : alter table BIAPPS_12.W_PAYMENT_METHOD_D_O add constraint pk_W_PAYMENT_METHOD_D_O PRIMARY KEY (INTEGRATION_ID);
    Oracle error: [ORA-01031: insufficient privileges]
    PS: both these users are on the same database 'MDMINT5'
    2) I have an exclusive privilege which is [alter any table] but still I am getting this error.
    Please help me out.
    Regards
    Rahul

    If the user RAHUL genuinely has ALTER ANY TABLE then that user should be able to run the command you posted.
    Unless the privilege was granted through a role and the statement is being run as part of a PL/SQL block (through EXECUTE IMMEDIATE or similar contrivance). Anonymous blocks count as PL/SQL, and we cannot use privileges granted through roles in PL/SQL.
    Cheers, APC

  • How to add primary key for table with existing data?

    The table is occupied data already. There was no primary key before, so for every column, there are some duplicate values.
    I want to add a new column, which should be of the datatype integer, and can automatically increment, starting from 001. I tried this in Oracle SQL Developer, but it says "ORA-01758: table must be empty to add mandatory (NOT NULL) column". How can I do it? Thanks!

    Hello, don't call the column ID, that could be an ID relating to anything. Perhaps be a little more specific like emp_id .
    Then to update it:
    UPDATE Employee emp
      SET emp_id = (
      SELECT row_num
        FROM (
      SELECT ROWID,
                 ROW_NUMBER() OVER (ORDER BY ROWID) row_num
        FROM Employee emp2) emp2
      WHERE emp.ROWID = emp2.ROWID);That will assign a new EMP_ID for each, beginning at 1.
    Then:
    ALTER TABLE Employee MODIFY emp_id NOT NULL;
    ALTER TABLE Employee ADD CONSTRAINT EMP_PK PRIMARY KEY (EMP_ID);Then create a sequence for future population of the Employee table:
    CREATE SEQUENCE emp_seq START WITH <maximum value of emp_id + 1>;

  • How to create tabular form whithout primary key in table

    Hi All,
    I have requirement to create a tabular form but the problem is, the table which I am using in the application don't have any Primary key, I am using only one table can't change any thing in table.(i.e can't add any row in table,no change in data model)
    How can I create tabular form when primary key is not available.
    looking forward for all of your quick responce.
    Thanks in advance.
    Dikshit Kumar Nidhi

    You can create a view like
    select a.rowid id, a.*
    from table a
    and create a tabular form on this view. You can use
    the new column ID as Primary key.Did you try to actually do this? I did at
    http://htmldb.oracle.com/pls/otn/f?p=24317:159
    And when I change something and click Submit, I get an error
    Error in mru internal routine: ORA-20001: Error in MRU: row= 1, ORA-01733: virtual column not allowed here, update "VIKASA"."NO_PK_VW" set "ID" = :b1, "I" = :b2, "J" = :b3 where "ID" = :p_pk_col
    Thanks

  • Help with setting primary key in table

    Hi,
    I have created a table in Oracle and populated it with information, but I need a primary key (like an autonumber in Access) to identify each record. How can I do this for an existing table?
    Thanks so much for any help you can provide!

    Hello,
    I am assuming you have created the sequence and trigger i posted in my earlier post, here run this and if this answers your question mark post answered and reward points for helpful posts.
    INSERT INTO customer1 (FIRST_NAME,
                           LAST_NAME,
                           TITLE,
                           ADDRESS1,
                           ADDRESS2,
                           CITY,
                           STATE,
                           ZIP,
                           PHONE,
                           EXTENSION,
                           FAX,
                           WORK_PHONE,
                           WORK_EXTENSION,
                           MOBILE_PHONE,
                           EMERGENCY_PHONE,
                           PHONE_NOTES,
                           EMAIL)
       SELECT   FIRST_NAME,
                LAST_NAME,
                TITLE,
                ADDRESS1,
                ADDRESS2,
                CITY,
                STATE,
                ZIP,
                PHONE,
                EXTENSION,
                FAX,
                WORK_PHONE,
                WORK_EXTENSION,
                MOBILE_PHONE,
                EMERGENCY_PHONE,
                PHONE_NOTES,
                EMAIL
         FROM   CUSTOMER;
         COMMIT;Regards

  • Issues while changing primary key in table

    Hi
    I have one table. In that two fields are primary key. I want to change the second PK as FK only. But when i am changing this field as FK. Its showing one error 'Primary Key Change not permitted for value Table ZCAUSECATMASTER' . How to avoid this error.
    Please help me.

    Hi.....
    Remove that primary key for the second field and assign your foregin key..table to that filed.....
    So when you give entries in that second filed it will be validated with its foregin key table......its nothing but check table...
    what all values in the foregin table only can given.....
    regards
    raja

  • Inner join-select -primary key in table issue

    Hi ,
            Iam using FEBKO(header) and FEBEP(item) in inner join  select .But the datas fetching by this selct in not correct.The analysis is the is no common primary fields in the both table.
    Question 1-> Can i use inner join without common primary key in the both tables, weather it possible to make a select without common primary key in both table. Please kindly let me know.
    Question 2-> What is the other possible way to give the selct for both table(better performance)
    Regards,
    Veera

    Hi,
    When you use INNER JOIN in this case, link your tables based on KUKEY and ESNUM fields, bcoz there can be many items under a single header. So this will work for you, even from the performance point of view.
    Hope this is helpful to you. If you need further information, revert back.
    Reward all the helpful answers.
    Regards
    Nagaraj T

  • Database Copy can't copy primary key for tables

    When I use Tools->Database Copy Wizard to copy my existing schema (for example: azteca_KSMMS) to a brand new schema (azteca), I find the primary key for some of the tables can't be copied, also the views seem not valid under the new schema (azteca). Please give me your comments about that. Thanks for your help.
    Kevin

    For example I have a table called workorder, it has a primary key with name (CW_PKEY_519, on WORKORDERID column) from the source schema (azteca_KSMMS). After I database copy to new schema azteca, the primary key lost. The data of workorder and other indexes have been copied to azteca schema.
    Kevin

  • Primary key for table

    I have a table Gr_order which was populated ages ago and now it need to be populated
    with some more look up data.
    "Orderid" is primary key for the table but question is how do I insert "orderid" in insert statement in procedure. I could not find any sequence name. There are 1230
    records which need to be inserted.
    Desc Gr_order
    Orderid  Number Primary key
    Desc     Varchar2
    INSERT INTO gr_order (orderid,desc) 
    VALUES (???, upper(each_pom.desc);Thanks
    sandy

    You find the code that inserts into the table and see if it uses the sequence ;-) . A sequence is not "attached" to a particular table - it is up to the inserting statement to decide if a sequence is to be used.
    You might want to check if the table has a trigger - some developers like to emulate MS-sql server behaviour by having a trigger on the table that selects from a sequence. If it has such a trigger, then you just do your inserts without specifying order_id. But if the table does not have such a trigger, then you cannot know if the table was originally populated using a sequence or not (unless you can find the insert statements somewhere.)

  • Please give the query to find out primary key in table in Sql plus

    Dear friends,
    Please give me the query to find out the primary key in Sql plus.

    hi
    SQL> DESC user_constraints
    Name                                                                                                  
    OWNER                                                                                                 
    CONSTRAINT_NAME                                                                                       
    CONSTRAINT_TYPE                                                                                       
    TABLE_NAME                                                                                            
    SEARCH_CONDITION                                                                                      
    R_OWNER                                                                                               
    R_CONSTRAINT_NAME                                                                                     
    DELETE_RULE                                                                                           
    STATUS                                                                                                
    DEFERRABLE                                                                                            
    DEFERRED                                                                                              
    VALIDATED                                                                                             
    GENERATED                                                                                             
    BAD                                                                                                   
    RELY                                                                                                  
    LAST_CHANGE                                                                                           
    INDEX_OWNER                                                                                           
    INDEX_NAME                                                                                            
    INVALID                                                                                               
    VIEW_RELATED                                                                                          
    SQL> SELECT constraint_name,table_name,r_constraint_name,status
      2  FROM user_constraints WHERE constraint_type='P';
    CONSTRAINT_NAME                TABLE_NAME                     R_CONSTRAINT_NAME              STATUS
    SYS_C003141                    CUSTOMERS                                                     ENABLED
    PK_DEPT                        DEPT                                                          ENABLED
    SYS_C003139                    SALESREPS                                                     ENABLEDKhurram

  • Identifying Primary Keys for table(s) which have no natural PK

    Hello,
    At my organization, a person used the loader to bring in 4 tables which have no natural PK's. The user is using BI Studio to demo to upper management. He wants to be able to get down to a single row returned, but the table(s) don't appear to have anything unique, and the data pulled from the warehouse has no timestamps, and only dates. Since there can be multiple occurrences of case names, supervisor names, worker names, case numbers, dates, etc. I have no idea how to get anything for sure down to a unique single row returned and this table has no other table in relation to it:
    Name Null Type
    APPROVAL_PERIOD VARCHAR2(50)
    PAGE_NUMBER VARCHAR2(50)
    SERVICING_AGENCY VARCHAR2(50)
    ELIGIBILITY_OVERRIDES_1 VARCHAR2(50)
    SERVICE_AUTH_OVERRIDES_1 VARCHAR2(50)
    SUPERVISOR VARCHAR2(50)
    ELIGIBILITY_OVERRIDES_2 VARCHAR2(50)
    SERVICE_AUTH_OVERRIDES_2 VARCHAR2(50)
    WORKER VARCHAR2(50)
    ELIGIBILITY_OVERRIDES_3 VARCHAR2(50)
    SERVICE_AUTH_OVERRIDES_3 VARCHAR2(50)
    TYPE_OF_OVERRIDE VARCHAR2(50)
    CASE_NUMBER VARCHAR2(50)
    CASE_NAME VARCHAR2(50)
    CC_PROGRAM VARCHAR2(50)
    PERIOD_BEGIN DATE
    OVERRIDE_REASON VARCHAR2(50)
    OVERRIDE_VERSION VARCHAR2(2)
    APPROVED_DATE DATE
    If anyone has some advice on how to proceed with this data I would appreciate it.

    Data model normalization is usually different for data warehouses.
    Data often is denormalized purposely.
    To get "unique" rows you have to aggregate data one or another way.
    Work with OLAP/BI is oriented rather on aggregations of facts by dimensions, versus looking to individual rows in fact table.
    Aggregations are usually done by BI Studio automatically when user slices and dices, a fact column will be either summarized, or max'ed or aggregated otherwise.
    Getting down "to the row" may make sense when user wants to go (drill through) to original data that came from OLTP database.
    Usually it is required for investigations or like that.
    Normally OLAP/BI applications do no go that deep and manipulate aggregated data on more high level.
    He wants to be able to get down to a single row returnedWhat is a reason for that? What will change if he will see that same worker had multiple interactions for a case for one day?
    Worker  Case       Date
    Joe Doe SC12345 2013-02-07
    Joe Doe SC12345 2013-02-07
    Joe Doe SC12345 2013-02-07
    Joe Doe SC12345 2013-02-07instead of normally aggregated result
    Worker  Case       Date           Count
    Joe Doe SC12345 2013-02-07  4Edited by: Mark Malakanov (user11181920) on Feb 7, 2013 12:22 PM
    If user still want to drill through, a drill through query needs to be specified and send to OLTP database where this data was originated from.
    For above it should be like:
    select w.WorkerName, c.Case#, i.Timestamp, ... other details
    from Workers@OLTP w
    join Cases@OLTP c on c.worker_id=w.worker_id
    join Interactions@OLTP i on i.case#=c.case#
    where w.WorkerName='Joe Doe' and c.Case#='SC12345'
    and TRUNC(i.Timestamp)=TO_DATE('2013-02-07');Edited by: Mark Malakanov (user11181920) on Feb 7, 2013 12:22 PM
    Edited by: Mark Malakanov (user11181920) on Feb 7, 2013 12:35 PM

Maybe you are looking for

  • Error while connecting to Cloudscape Database

    Hi All, I am working on Sun's reference implementation App Server verson 1.3.1. It comes shipped with cloudscape database. When I write try to connect to this App Server through JSP that calls methods through Enterprise Java Beans I get this Error Me

  • Fix page error in Explorer

    Help! Explorer strikes again. Every page of my classroom website loads properly except one. Here's the website: http://teacher.ocps.net/marylouise.wells Here's the error message I get on the problem page: "undefined" is null or not an object iWebSite

  • How can I have images open in their original applications from Links panel?

    Before I switched to my new Mac, I was able to open files in their original applications when I clicked the Edit Original button at the bottom of the Links panel. But now everything-TIFF, EPS and PDF-opens in Preview because that's the default applic

  • JDeveloper & ADF with EJB

    Hi I created a Session EJB in my ADF project but when I try to run a flow that calls the EJB I get the following error: Target URL -- http://127.0.0.1:7101/BrowseEditApp-ViewController-context-root/faces/adf.task-flow?adf.tfId=ejb-claims&adf.tfDoc=/W

  • Problems with Zen:M 3

    Hi, I have the model stated in the topic and im having problems. I have lost the original CD that came with it. I installed MediaSource5 and it cannot see the device. Windows Device Manager can see it under portable devices and it says the device is