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

Posted

技术标签:

【中文标题】在 SQL 中创建数据库表,奇怪的错误【英文标题】:Making a Database table in SQL, weird error 【发布时间】:2016-02-01 21:55:54 【问题描述】:

这是数据库的代码。它给我一个错误,对于创建的每个 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');

错误:

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% 确定是什么原因造成的。

【问题讨论】:

您可能有另一个表,其主键名为kkeyconst。我不熟悉用于检查该名称是否已在其他地方存在的 Oracle 语法。 注意:Team_Manager 定义为VARCHAR(20),应改为VARCHAR2(20) 【参考方案1】:

如果该表是在您的脚本的早期运行期间创建的,您将收到该错误。在尝试创建表之前检查表是否已经存在总是好的。这是来自 *** 的一个很好的 sn-p 代码,用于检查表是否已经存在。

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

【讨论】:

哦,我完全忘记了这一点。谢谢。

以上是关于在 SQL 中创建数据库表,奇怪的错误的主要内容,如果未能解决你的问题,请参考以下文章

在mysql中创建了一个表,当想插入数据的时候,一直提示一个错误,望得到解决。

在 MySQL 5.6.27 中创建触发器时出现奇怪的错误 [重复]

数据库中创建一个学生表

SQL中,想在一个指定的数据库中创建一个表怎么写?

使用 SELECT ... INTO 在 SQL Server 中创建表

sqlserver中创建一个表对象应该包括哪两个步骤?