1

表已以这种方式在系统中创建

CREATE TABLE INSTANCES
(
    DM  INTEGER  NOT NULL,
    INSTANCEID  VARCHAR2(512)  NOT NULL,
    INSTANCENAME  VARCHAR2(64)  NOT NULL  UNIQUE,
    HOSTNAME  VARCHAR2(32)  NOT NULL,
    CONSTRAINT  PK_INSTANCES  PRIMARY KEY  (INSTANCEID, HOSTNAME)
);

新的 crete table 语句如下:

CREATE TABLE INSTANCES
(
    DM  INTEGER  NOT NULL,
    INSTANCEID  VARCHAR2(512)  NOT NULL  UNIQUE,
    INSTANCENAME  VARCHAR2(64)  NOT NULL  UNIQUE,
    HOSTNAME  VARCHAR2(32)  NOT NULL,
    CONSTRAINT  PK_INSTANCES  PRIMARY KEY  (INSTANCEID, HOSTNAME)
);

不同之处在于 INSTANCEID 具有 UNIQUE。我如何改变桌子?我使用了以下语句,但它对我不起作用。

ALTER TABLE INSTANCES ADD CONSTRAINT ab UNIQUE ( INSTANCEID);

它报错:ALTER TABLE INSTANCES ADD CONSTRAINT ab UNIQUE (INSTANCEID) 错误报告:SQL 错误:ORA-02261: such unique or primary key already exists in the table 02261. 00000 - "such unique or primary key already exists in the table " *原因:不言而喻。*行动:移除额外的钥匙。

请帮我按照上面的要求更改表格。谢谢!

这是 SELECT con.constraint_name, col.column_name, con.constraint_type FROM user_cons_columns col JOIN user_constraints con ON (col.constraint_name = con.constraint_name) WHERE col.table_name = 'INSTANCES' 的输出

"CONSTRAINT_NAME","COLUMN_NAME","CONSTRAINT_TYPE" 
"SYS_C0016531","DM","C" 
"SYS_C0016532","INSTANCEID","C" 
"SYS_C0016533","INSTANCENAME","C" 
"SYS_C0016534","HOSTNAME","C" 
"PK_INSTANCES","HOSTNAME","P" 
"PK_INSTANCES","INSTANCEID","P" 
"SYS_C0016536","INSTANCENAME","U"
4

1 回答 1

1

您已经声明 INSTANCEID 应该是 UNIQUE,因此已经创建了一个约束。

CREATE TABLE INSTANCES
(
    DM  INTEGER  NOT NULL,
    INSTANCEID  VARCHAR2(512)  NOT NULL  UNIQUE, -- UNIQUE constraint
    INSTANCENAME  VARCHAR2(64)  NOT NULL  UNIQUE,
    HOSTNAME  VARCHAR2(32)  NOT NULL,
    CONSTRAINT  PK_INSTANCES  PRIMARY KEY  (INSTANCEID, HOSTNAME)
);

编辑:好的,阅读您的评论后,试试这个:

SELECT con.constraint_name, col.column_name, con.constraint_type
  FROM user_cons_columns col
    JOIN user_constraints con ON (col.constraint_name = con.constraint_name)
WHERE col.table_name = 'INSTANCES'
  AND con.constraint_type = 'U'
;

它将列出 INSTANCE 表的唯一约束和关联列。请检查 INSTANCEID 列上是否存在唯一约束(以及该约束是否没有其他关联列)。

SQLFiddle 示例:http ://sqlfiddle.com/#!4/43b43/6

编辑#2:创建命名约束,所有选项:

-- CREATE TABLE - "In Line" Constraints

CREATE TABLE ports (
  ID NUMBER CONSTRAINT PORT_ID_PK PRIMARY KEY,
  NAME VARCHAR2(20)
);

CREATE TABLE ports (
  ID NUMBER,
  NAME VARCHAR2(20) CONSTRAINT NAME_NN NOT NULL
);

CREATE TABLE ports (
  ID NUMBER,
  NAME VARCHAR2(20) CONSTRAINT NAME_UQ UNIQUE
);

CREATE TABLE ports (
  ID NUMBER,
  STATUS NUMBER CONSTRAINT PROPER_STATUS_CK
    CHECK (STATUS IN (4, 5))
);

CREATE TABLE ships (
  SHIP_ID NUMBER,
  NAME VARCHAR2(20),
  HOME_PORT_ID NUMBER CONSTRAINT SHIP_PORT_FK
    REFERENCES PORTS (ID)
);


-- CREATE TABLE - "Out of Line" Constraints

CREATE TABLE ports (
  ID NUMBER,
  NAME VARCHAR2(20),
  CONSTRAINT PORT_ID_PK PRIMARY KEY (ID)
);

-- NOT NULL constraints can not be created "Out of Line"!

CREATE TABLE ports (
  ID NUMBER,
  NAME VARCHAR2(20),
  CONSTRAINT NAME_UQ UNIQUE (NAME)
);

CREATE TABLE ports (
  ID NUMBER,
  STATUS NUMBER,
  CONSTRAINT PROPER_STATUS_CK
    CHECK (STATUS IN (4, 5))
);

CREATE TABLE ships (
  SHIP_ID NUMBER,
  NAME VARCHAR2(20),
  HOME_PORT_ID NUMBER,
  CONSTRAINT SHIP_PORT_FK FOREIGN KEY
    (HOME_PORT_ID) REFERENCES PORTS (ID)
);

-- ALTER TABLE - "In Line" Constraints

ALTER TABLE PORTS MODIFY ID
  CONSTRAINT PORT_ID_PK PRIMARY KEY;

ALTER TABLE PORTS MODIFY NAME
  CONSTRAINT NAME_NN NOT NULL;

ALTER TABLE PORTS MODIFY NAME
  CONSTRAINT NAME_UQ UNIQUE;

ALTER TABLE SHIPS MODIFY HOME_PORT_ID
  CONSTRAINT SHIP_PORT_FK REFERENCES PORTS (ID);

-- ALTER TABLE - "Out of Line" Constraints

ALTER TABLE PORTS ADD CONSTRAINT
  PORT_ID_PK PRIMARY KEY (ID);

-- NOT NULL constraints can not be created "Out of Line"!

ALTER TABLE PORTS ADD CONSTRAINT
  NAME_UQ UNIQUE (NAME);

ALTER TABLE PORTS ADD
  CONSTRAINT PROPER_STATUS_CK
  CHECK (STATUS IN (4, 5));

ALTER TABLE SHIPS ADD CONSTRAINT SHIP_PORT_FK
  FOREIGN KEY (HOME_PORT_ID)
  REFERENCES PORTS (ID);

NOT NULL 约束不能创建 out 行。

于 2013-10-04T11:20:56.320 回答