使用序列创建自动合成主键

时间:2015-05-18 21:39:41

标签: sql oracle11g primary-key

我有一个包含VARCHAR主键的表,该主键包含多个列。其中一列是VARCHAR,最多有100个不同的值。

我想生成一个新表,使用序列将PK的这个特定VARCHAR列更改为Integer,问题是我正在使用的序列为每一行生成不同的Id,它不会对不同的现有组进行分组的ID。

CREATE SEQUENCE my_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1

如何创建此序列,以便将键的值分组在不同的行上?

2 个答案:

答案 0 :(得分:1)

你应该在你的测试环境中这样做。首先,然后只有非常非常小心,当你100%肯定在其他系统上做。我们没有您系统的结构或数据,因此您需要根据您的需求提供这种通用解决方案我们不承担任何责任:)。

SQL> create table old_table (object_name varchar2(30), object_type varchar2(19), val_1 varchar2(20), val_2 varchar2(20), 
     primary key(object_name, object_type));

Table created.                                                                                                                                                         

SQL> desc old_table                                                                                                                                                    
 Name                                      Null?    Type                                                                                                               
 ----------------------------------------- -------- ----------------------------                                                                                       
 OBJECT_NAME                               NOT NULL VARCHAR2(30)                                                                                                       
 OBJECT_TYPE                               NOT NULL VARCHAR2(19)                                                                                                       
 VAL_1                                              VARCHAR2(20)                                                                                                       
 VAL_2                                              VARCHAR2(20)                                                                                                       

 -- you can see this table has your table with composit pk , consists of (object_name and object_type) -- like your table.

 SQL> l                                                                                                                                                                 
  1  insert into old_table                                                                                                                                             
  2  select object_name, object_type, status, timestamp                                                                                                                
  3* from all_objects                                                                                                                                                  
SQL> /                                                                                                                                                                 

7289 rows created.                             
-- we just created some test data --similar to your table data

SQL> l                                                                                                                                                                 
  1  select object_type,count(*)                                                                                                                                       
  2  from old_table                                                                                                                                                    
  3* group by object_type                                                                                                                                              
SQL> /                                                                                                                                                                 

OBJECT_TYPE           COUNT(*)                                                                                                                                         
------------------- ----------                                                                                                                                         
CONSUMER GROUP               2                                                                                                                                         
EDITION                      1                                                                                                                                         
SCHEDULE                     3                                                                                                                                         
SEQUENCE                    13                                                                                                                                         
OPERATOR                    45                                                                                                                                         
PROCEDURE                   31                                                                                                                                         
WINDOW                       9                                                                                                                                         
SCHEDULER GROUP              4                                                                                                                                         
DESTINATION                  2                                                                                                                                         
PACKAGE                    296                                                                                                                                         
PROGRAM                     11                                                                                                                                         
XML SCHEMA                  31                                                                                                                                         
TRIGGER                      2                                                                                                                                         
JOB CLASS                    2                                                                                                                                         
SYNONYM                   3974                                                                                                                                         
VIEW                      1579                                                                                                                                         
TABLE                       96                                                                                                                                         
FUNCTION                   163                                                                                                                                         
INDEXTYPE                    8                                                                                                                                         
INDEX                       21                                                                                                                                         
TYPE                       995                                                                                                                                         
EVALUATION CONTEXT           1                                                                                                                                         

22 rows selected.                                         

-- here you can see total rows are 7289 but unique object_types( part of composit pk ) is only 22 distinct values..

SQL> CREATE TABLE NEW_TABLE1(ID NUMBER PRIMARY KEY, OBJECT_TYPE VARCHAR2(19));                                                                                         

Table created.                                                                                                                                                         

SQL> DESC NEW_TABLE1                                                                                                                                                   
 Name                                      Null?    Type                                                                                                               
 ----------------------------------------- -------- ----------------------------                                                                                       
 ID                                        NOT NULL NUMBER                                                                                                             
 OBJECT_TYPE                                        VARCHAR2(19)                             

 SQL> 

 -- here we created a new table with --id number-- as pk, and your obejct_type as value


CREATE SEQUENCE MY_SEQ MINVALUE 1 START WITH 1 INCREMENT BY 1; 

SQL> INSERT INTO NEW_TABLE1                                                                                                                                            
  2  SELECT MY_SEQ.NEXTVAL, OBJECT_TYPE FROM (SELECT DISTINCT OBJECT_TYPE FROM OLD_TABLE);                                                                             

22 rows created.                                                               

SQL> SELECT * FROM NEW_TABLE1;                                                                                                                                         

        ID OBJECT_TYPE                                                                                                                                                 
---------- -------------------                                                                                                                                         
         1 CONSUMER GROUP                                                                                                                                              
         2 EDITION                                                                                                                                                     
         3 SCHEDULE                                                                                                                                                    
         4 SEQUENCE                                                                                                                                                    
         5 OPERATOR                                                                                                                                                    
         6 PROCEDURE                                                                                                                                                   
         7 WINDOW                                                                                                                                                      
         8 SCHEDULER GROUP                                                                                                                                             
         9 DESTINATION                                                                                                                                                 
        10 PACKAGE                                                                                                                                                     
        11 PROGRAM                                                                                                                                                     
        12 XML SCHEMA                                                                                                                                                  
        13 TRIGGER                                                                                                                                                     
        14 JOB CLASS                                                                                                                                                   
        15 SYNONYM                                                                                                                                                     
        16 VIEW                                                                                                                                                        
        17 TABLE                                                                                                                                                       
        18 FUNCTION                                                                                                                                                    
        19 INDEXTYPE                                                                                                                                                   
        20 INDEX                                                                                                                                                       
        21 TYPE                                                                                                                                                        
        22 EVALUATION CONTEXT                                                                                                                                          

        22 rows selected.                               

    -- now we add new column to old/existing table

alter table old_table add (new_number_pk number);   

-- update the new column with id/number data

 SQL> update old_table set new_number_pk = ( select id from new_table1 where object_type = old_table.object_type);

  7289 rows updated.

SQL> select * from old_table where rownum <  20 order by object_type ;


OBJECT_NAME                    OBJECT_TYPE         VAL_1                VAL_2                NEW_NUMBER_PK  
------------------------------ ------------------- -------------------- -------------------- -------------  
V$FLASHBACK_DATABASE_STAT      SYNONYM             VALID                2011-08-28:22:11:07             15  
V$PARAMETER                    SYNONYM             VALID                2011-08-28:22:11:07             15  
V$RESTORE_POINT                SYNONYM             VALID                2011-08-28:22:11:07             15  
V$ROLLNAME                     SYNONYM             VALID                2011-08-28:22:11:07             15  
V$ROLLSTAT                     SYNONYM             VALID                2011-08-28:22:11:07             15  
V$UNDOSTAT                     SYNONYM             VALID                2011-08-28:22:11:07             15  
V$SGA                          SYNONYM             VALID                2011-08-28:22:11:07             15  
V$CLUSTER_INTERCONNECTS        SYNONYM             VALID                2011-08-28:22:11:07             15  
V$CONFIGURED_INTERCONNECTS     SYNONYM             VALID                2011-08-28:22:11:07             15  
V$ROWCACHE_SUBORDINATE         SYNONYM             VALID                2011-08-28:22:11:07             15  
V$PARAMETER2                   SYNONYM             VALID                2011-08-28:22:11:07             15  
V$OBSOLETE_PARAMETER           SYNONYM             VALID                2011-08-28:22:11:07             15  
V$SYSTEM_PARAMETER             SYNONYM             VALID                2011-08-28:22:11:07             15  
V$SYSTEM_PARAMETER2            SYNONYM             VALID                2011-08-28:22:11:07             15  
V$SPPARAMETER                  SYNONYM             VALID                2011-08-28:22:11:07             15  
V$PARAMETER_VALID_VALUES       SYNONYM             VALID                2011-08-28:22:11:07             15  
V$ROWCACHE                     SYNONYM             VALID                2011-08-28:22:11:07             15  
V$ROWCACHE_PARENT              SYNONYM             VALID                2011-08-28:22:11:07             15  
V_$RESTORE_POINT               VIEW                VALID                2011-08-28:22:11:07             16  

19 rows selected.                                                                                       
-- drop old primary key
SQL> alter table old_table drop primary key;

Table altered.   

-- mark object_type column unused in old_table
SQL> alter table old_table set unused  column object_type;

Table altered.

-- crate new pk with new column on old_table
SQL> alter table old_table                                           
  2  add constraint pk_old_table primary key(object_name, new_number_pk);

Table altered.


-- drop unused column
SQL> alter table old_table drop unused columns;  

Table altered. 

答案 1 :(得分:0)

步骤1 - 创建一个包含所有旧PK列和新序列驱动列的新表。

第2步插入新表中,从旧表中选择不同的列

步骤3将新的PK列添加到原始表中。

步骤4使用新表中的新PK来更新原始表,其中列匹配