4

我在 Oracle 中有以下内容作为 CREATE TABLE:

CREATE TABLE cs2_users (
    empnum     varchar2(12) PRIMARY KEY,
    toolsId    varchar2(20) 
        CONSTRAINT nn_cs2_users_toolsId NOT NULL
        CONSTRAINT fk_cs2_users_users FOREIGN KEY REFERENCES users.userid,
    admin      number(1,0) DEFAULT 0
        CONSTRAINT nn_cs2_users_admin NOT NULL
        CONSTRAINT ck_cs2_users_admin (admin IN (0,1)),
    givenname  varchar2(30) NOT NULL,
    middlename varchar2(30),
    sn         varchar2(30) NOT NULL,
    mail       varchar2(50) NOT NULL
);

但是它失败并出现此错误:

ERROR at line 5:
ORA-02253: constraint specification not allowed here`

当我使用 SQL*Plus 连接时,这是版本信息:

SQL*Plus: Release 11.2.0.3.0 Production on Tue Dec 18 16:38:27 2012

Copyright (c) 1982, 2011, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

帮助?

4

1 回答 1

5

如果要指定内联约束,则该定义与列的定义是分开的。如果要创建约束,则需要指定约束的类型CHECK。并且您的外键约束需要指定您引用的列。因此,例如,这将起作用

SQL> create table users( userid varchar2(20) primary key );

Table created.


SQL> CREATE TABLE cs2_users (
  2      empnum     varchar2(12) PRIMARY KEY,
  3      toolsId    varchar2(20) constraint nn_cs2_users_toolsId NOT NULL,
  4          CONSTRAINT fk_cs2_users_users FOREIGN KEY(toolsId) REFERENCES users(userid),
  5      admin      number(1,0) DEFAULT 0 constraint nn_cs2_users_admin NOT NULL,
  6          CONSTRAINT ck_cs2_users_admin CHECK(admin IN (0,1)),
  7      givenname  varchar2(30) NOT NULL,
  8      middlename varchar2(30),
  9      sn         varchar2(30) NOT NULL,
 10      mail       varchar2(50) NOT NULL
 11  );

Table created.

尽管从. toolsId_ 您似乎更有可能希望该列从表中引用该列,或者您希望将该列命名为,如果您想从. 但是我会质疑为什么你有一个单独的列,而不是简单地将这些信息放在表格中。userIduserstoolsIdtoolsIdtoolsuserIduserIduserscs2_usersusers

于 2012-12-18T21:50:11.433 回答