在SQL中创建数据库表,奇怪的错误

时间:2016-02-01 21:55:55

标签: sql oracle database-design

这是Database.Its的代码,它给出了一个错误,即每个TABLE创建的东西都已经取了这个名字,其中一条错误消息就在帖子的末尾。

COMMIT;

CREATE TABLE Team (
  Team_ID INTEGER GENERATED ALWAYS AS IDENTITY,
  Team_Name NVARCHAR2(40) NOT NULL,
  Team_Manager VARCHAR(20),
  CONSTRAINT kkeyconst PRIMARY KEY(Team_ID)
 );

 COMMIT;

 insert into Team(TEAM_NAME, TEAM_MANAGER) VALUES ('Red Sox', 'Jon');
 insert into Team(TEAM_NAME, TEAM_MANAGER) VALUES('White Sox', 'Tony');

CREATE TABLE Driver (
  Driver_id INTEGER NOT NULL,
  Teams_ID INTEGER,
  Driver_age INTEGER,
  Driver_Name NVARCHAR2(20) NOT NULL,
  CONSTRAINT DriverAge CHECK (Driver_age BETWEEN '19' AND '65'),
  CONSTRAINT driverpk PRIMARY KEY(Driver_ID),
  CONSTRAINT Teams_PK FOREIGN KEY (Teams_ID) REFERENCES Team(Team_ID)
  );

 COMMIT;

  insert into Driver(DRIVER_AGE,DRIVER_NAME) VALUES ('21', 'Jon');
  insert into Driver(DRIVER_AGE, DRIVER_NAME) VALUES ('20', 'Tony');

  CREATE TABLE Participation (
    TeamName_ID INTEGER,
    Driver_ID INTEGER,
    PointsEarned INTEGER,
    CONSTRAINT TeamName_FK FOREIGN KEY (TeamName_ID) REFERENCES     Team(Team_ID),
    CONSTRAINT Driver_FK FOREIGN KEY(Driver_ID) REFERENCES Driver(Driver_ID)
   );

  COMMIT;
  insert into Participation(PARTICIPATION_POINTS_EARNED) VALUES (150);


CREATE TABLE Finish (
  Racer_ID INTEGER,
  Finish_Position INTEGER NOT NULL,
  Fishish_Result VARCHAR(50)  NOT NULL,
  CONSTRAINT Racer_FK FOREIGN KEY (Racer_ID) REFERENCES Driver(Driver_id)
);

COMMIT;
  insert into Finish(Finish_POSITION, Finish_RESULT) VALUES ('1', 'Winner');
 insert into Finish(Finish_POSITION, Finish_RESULT) VALUES ('3', 'Third   Place');

CREATE TABLE RaceComponent (
  RC_ID INTEGER NOT NULL,
  Driver1_ID INTEGER,
  RC_Type VARCHAR(25),
  CONSTRAINT Rcpk  PRIMARY KEY(RC_ID),
  CONSTRAINT Driver1_FK FOREIGN KEY (Driver1_ID) REFERENCES Driver(Driver_ID)
  );

 COMMIT;
  insert into RaceComponent(RC_TYPE) VALUES ('Hot Wheels');
  insert into RaceComponent(RC_TYPE) VALUES ('Tonka');



CREATE TABLE Race (
  Race_Id INTEGER,
  RC_ID INTEGER,
  Race_Title VARCHAR(30) NOT NULL,
  Race_Location VARCHAR(50) NOT NULL,
  Race_Date DATE,
  CONSTRAINT RACPEK PRIMARY KEY(RACE_ID),
  CONSTRAINT RC_FK FOREIGN KEY (RC_ID) REFERENCES RaceComponent(RC_ID)
  );

  COMMIT;
  insert into Race(RACE_TITLE, RACE_LOCATION, RACE_DATE) VALUES ('Tonys    race',   'Moncton', DATE '2016-04-25');
  insert into Race(RACE_TITLE, RACE_LOCATION, RACE_DATE) VALUES ('Mikes Racing',     'San-Francisco', DATE '2015-04-25');

ERROR:

Commit complete.


Error starting at line : 3 in command -
CREATE TABLE Team (
  Team_ID INTEGER GENERATED ALWAYS AS IDENTITY,
  Team_Name NVARCHAR2(40) NOT NULL,
  Team_Manager VARCHAR(20),
  CONSTRAINT kkeyconst PRIMARY KEY(Team_ID)
)
Error report -
SQL Error: ORA-00955: name is already used by an existing object
00955. 00000 -  "name is already used by an existing object"
*Cause:    
*Action:

Commit complete.

我认为可能是因为我调用的外键与其主键值相同,但我不能100%确定导致它的原因。

1 个答案:

答案 0 :(得分:0)

如果表是在早期运行脚本期间创建的,则会出现该错误。在尝试创建表之前检查表是否已存在总是很好的。这是StackOverflow的一小段代码,用于检查表是否已存在。

IF (EXISTS (SELECT * 
                 FROM INFORMATION_SCHEMA.TABLES 
                 WHERE TABLE_SCHEMA = 'TheSchema' 
                 AND  TABLE_NAME = 'TheTable'))
BEGIN
    --Do Stuff
END