创建表中的SQL错误:缺少括号

时间:2019-03-13 02:10:17

标签: sql oracle

我正在尝试创建3个关联表

/*create customer_dml ---this works fine*/

DROP TABLE Customer_DML;
CREATE TABLE Customer_DML
(
 customerId CHAR(10) CONSTRAINT customer_pk PRIMARY KEY, 
 ssn CHAR(9) CONSTRAINT customer_uq_ssn UNIQUE,
 firstName VARCHAR2(25) CONSTRAINT customer_nn_fname NOT NULL,
 lastName VARCHAR2(25) CONSTRAINT customer_nn_lname NOT NULL,
 phoneNumber CHAR(10),
 address VARCHAR (100)
);

/*create account_dml- this gets missing right parenthesis error*/

DROP TABLE Account_DML;

CREATE TABLE Account_DML
(
  AccountNumber CHAR(10) CONSTRAINT account_pk PRIMARY KEY,
  CustomerId CHAR(10) 
  CONSTRAINT account_nn_customerId NOT NULL,
  CONSTRAINT account_fk_customerid REFERENCES Customer_DML(customerId)
  ON DELETE CASCADE,
  Type CHAR(1) CONSTRAINT account_ck_type CHECK (Type IN ('C','S')),
  balance NUMBER(8,2) CONSTRAINT account_nn_balance NOT NULL,
  AcctOpenDate DATE CONSTRAINT account_nn_acctopendate NOT NULL,
);

/*create CreditCard_dml --this gets right parenthesis error*/

 CREATE TABLE CreditCard 
(
  cardNumber CHAR (16) CONSTRAINT creditCard_pk PRIMARY KEY,
  customerId CHAR (10) CONSTRAINT creditCard_fk_customerId 
    REFERENCES Customer_DML (customerId)
    ON DELETE CASCADE, 
  limit NUMBER(8,2) creditCard_nn_limit NOT NULL, 
  expirationDate DATE CONSTRAINT creditCard_nn_expirationDate NOT NULL
);

我在做错什么导致以下错误消息?

  

错误报告-ORA-00907:缺少右括号
  00907. 00000-“缺少右括号”
  *原因:
  *动作:

有没有一种方法可以获取更具体的错误代码,以便我可以找出问题出在哪里?

1 个答案:

答案 0 :(得分:0)

调试DDL的最佳方法是一次删除一行,直到它起作用为止,然后开始一次又一次添加这些行,直到您可以隔离出有问题的确切行为止。

在DDL中,您有多个语法问题,都与constraint语法有关。

声明约束时,先给它命名,然后该约束所属的列。您省略了第二部分。

CONSTRAINT account_nn_customerId NOT NULL应该是CONSTRAINT account_nn_customerId CustomerID NOT NULL

CONSTRAINT account_fk_customerid REFERENCES Customer_DML(customerId) ON DELETE CASCADE,应该是CONSTRAINT account_fk_customerid foreign key (CustomerId) REFERENCES Customer_DML(customerId) ON DELETE CASCADE,

这两个示例更正应帮助您解决其他语法错误。