创建表时SQL Developer出错

时间:2014-11-18 03:19:08

标签: sql oracle oracle-sqldeveloper

我尝试使用SQL Developer创建3个表并出错。不能解释它为什么会发生。

Create table Album
(
  "alb_code" integer primary key,
  "alb_name" char(50),
  "publ" char(50),
  "publ_date" date not null
);
Create table Song
(
  "song_code" integer primary key,
  "song_name" char(50),
  "auth" char(50),
  "year" integer,
  "style" char(30)
);
Create table Song_from_Album
(
  "in_alb_code" integer primary key,
  "time" float,
  "no" integer,
  FOREIGN KEY REFERENCES Song("song_code"),
  FOREIGN KEY REFERENCES Album("alb_code")
);

我收到错误:

Error starting at line : 1 in command -
Create table Album
(
  "alb_code" integer primary key,
  "alb_name" char(50),
  "publ" char(50),
  "publ_date" date not null
)
Error at Command Line : 1 Column : 14
Error report -
SQL Error: ORA-00955: имя уже задействовано для существующего объекта
00955. 00000 -  "name is already used by an existing object"
*Cause:    
*Action:

Error starting at line : 8 in command -
Create table Song
(
  "song_code" integer primary key,
  "song_name" char(50),
  "auth" char(50),
  "year" integer,
  "style" char(30)
)
Error at Command Line : 8 Column : 14
Error report -
SQL Error: ORA-00955: имя уже задействовано для существующего объекта
00955. 00000 -  "name is already used by an existing object"
*Cause:    
*Action:

Error starting at line : 16 in command -
Create table Song_from_Album
(
  "in_alb_code" integer primary key,
  "time" float,
  "no" integer,
  FOREIGN KEY REFERENCES Song("song_code"),
  FOREIGN KEY REFERENCES Album("alb_code")
)
Error at Command Line : 21 Column : 15
Error report -
SQL Error: ORA-00906: отсутствует левая скобка
00906. 00000 -  "missing left parenthesis"
*Cause:    
*Action:

1 个答案:

答案 0 :(得分:0)

前两个表正在创建,但第三个表有错误。

删除表并重新创建第三表语法

错误“缺少左括号”是使用外键约束声明

改为使用

  CONSTRAINT fk_no FOREIGN KEY ("no") REFERENCES Song("song_code"),
  CONSTRAINT fk_alb_code FOREIGN KEY ("in_alb_code") REFERENCES Album("alb_code")

通过此更改,Song_Album表声明将如下所示

Create table Song_from_Album
(
  "in_alb_code" integer primary key,
  "time" float,
  "no" integer,
  CONSTRAINT fk_no FOREIGN KEY ("no") REFERENCES Song("song_code"),
  CONSTRAINT fk_alb_code FOREIGN KEY ("in_alb_code") REFERENCES Album("alb_code")
);