-- Create table
create table aa
(
  f1      VARCHAR2(20) not null,
  f2      VARCHAR2(18) not null,
  f3      NUMBER(10) not null,
)
-- Create/Recreate primary, unique and foreign key constraints 
alter table aa
  add constraint PK_P_ZJRB primary key (f1)
  using index 
  tablespace USERS
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 6528K
    next 128K
    minextents 1
    maxextents unlimited
    pctincrease 0
  );

解决方案 »

  1.   

    1.创建表时添加primary constraint(如上所示); 或者创建表后,创建unique index,如:
    CREATE TABLE your_table_name (f1 ..., f2 ..., f3 ...)
    CREATE UNIQUE INDEX idx_your_table_001 on your_table_name ( f1 )2.如果你的表中已经存在数据,而现在想使f1具备唯一值,则:
    2.1: 执行如下SQL以判断表中是否有重复数据:
    select f1, rowcount 
      from (select f1, count(*) rowcount from your_table_name group by f1)
     where rowcount>1 
     order by rowcount desc
    2.2:删除或者修改重复的数据,如果是删除的话可以通过一个PL/SQL块快速删除掉重复数据。
    2.3:创建唯一索引:CREATE UNIQUE INDEX idx_your_table_002 on your_table_name ( f1 )
      

  2.   

    -- Create table
    create table aa
    (
      f1      VARCHAR2(20) not null,
      f2      VARCHAR2(18) not null,
      f3      NUMBER(10) not null,
    )
    -- Create unique constraints 
    alter table aa
       add constraint ukf1 unique (f1);
      

  3.   

    like this:
    1、create table tab_name(f1 int unique,f2 int,f3 int);
    2、insert into tab_name(f2,f3) values (1,2);
    3、up
      

  4.   

    create table tablename 
    (
        F1 类型 primary key,
        F2 类型,
        F3 类型
    )insert into tablename values(1,'ok','');不改变表的记录, 修改表使F1, F2均只有唯一值,  <--- 如果已经有重复值呢,怎么处理?