0

I met an error in an SQL command when trying to create a table in SQL. Below is my command:

CREATE TABLE Registration
(
registrationID varchar2(5) NOT NULL CONSTRAINT registrationID PRIMARY KEY,
competitionID varchar2(5) NOT NULL CONSTRAINT competitionID REFERENCES Competition(competitionID),
competitorID varchar2(5) NOT NULL CONSTRAINT competitorID REFERENCES Competitor(competitorID),
categoryType varchar2(6) NOT NULL,
entryFeeStatus char(1) NOT NULL,
creditCardNumber number(16),
datePaid date

);

My competitionID is primary key of Competition table. My competitorID is primary key of Competitor table.

The error shown is:

Error report -
SQL Error: ORA-02264: name already used by an existing constraint
02264. 00000 -  "name already used by an existing constraint"
*Cause:    The specified constraint name has to be unique.
*Action:   Specify a unique constraint name for the constraint.

May I know what I should change in my statement? Thank you.


Below are the Competition and Competitor tables that I created:

CREATE TABLE Competition
(
competitionID varchar2(5) NOT NULL CONSTRAINT competitionID PRIMARY KEY,
timePlanned date NOT NULL,
country varchar2(50) NOT NULL,
city varchar2(50),
address varchar2(50),
entryFee number(4) NOT NULL
);

CREATE TABLE Competitor
(
competitorID varchar2(5) NOT NULL CONSTRAINT competitorID PRIMARY KEY,
firstName varchar2(9) NOT NULL,
lastName varchar2(9) NOT NULL,
dateOfBirth date NOT NULL,
nationality varchar2(12),
gender varchar2(1) NOT NULL,
lifetimeRanking number(6),
totalPrizeMoney number(6)
);
7
  • 2
    foreign key constraint should come after all the field definitions Commented Nov 23, 2015 at 10:00
  • Are you using Oracle? Remove the MySQL tag and add Oracle instead. Commented Nov 23, 2015 at 10:01
  • We also need to see the Competition and Competitor table definitions. Commented Nov 23, 2015 at 10:02
  • @jarlh I am using sqldeveloper. I assume it is mssql. I will update my post with Competitor and Competition as well. Commented Nov 23, 2015 at 10:03
  • FK needs matching data types. Length 5 <> 9 in your case. Commented Nov 23, 2015 at 10:10

2 Answers 2

1

You mixed up the two different ways to specify foreign keys. And had an extra comma.

CREATE TABLE Registration
(
registrationID varchar2(5) NOT NULL CONSTRAINT registrationID PRIMARY KEY,
competitionID varchar2(5) NOT NULL CONSTRAINT competitionIDfk  REFERENCES Competition(competitionID),
competitorID varchar2(5) NOT NULL CONSTRAINT competitorIDfk  REFERENCES Competitor(competitorID)
)

Foreign keys can either be specified per column:

columnname datatype CONSTRAINT constraintname REFERENCES tablename [ (column) ]

Or per table:

CONSTRAINT constraintname FOREIGN KEY (column-list) REFERENCES tablename [ (column-list) ]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @jarlh for the help. I have edited my code as per advised. But I had receive a new error saying invalid datatype. I double checked by code but I couldn't find any solution to it
Make sure both sides varchar2 columns have same length.
1

BOOLEAN is not valid SQL type. Use NUMBER(1) with 0 and 1, CHAR(1) or VARCHAR2(1) with 'Y' and 'N' values or other surrogate representation.

4 Comments

Thank you @Husqvik. I have made the relevant changes to my command. However, a new error appears, and it says Error report - SQL Error: ORA-02264: name already used by an existing constraint 02264. 00000 - "name already used by an existing constraint" *Cause: The specified constraint name has to be unique. *Action: Specify a unique constraint name for the constraint.
I changed to constraint key to competitionIDfk and competitorIDfk and everything is working now. Thank you!
Just some additional info - boolean is a valid ANSI SQL data type.
Since 1999, so it was added very late but thanks for the note.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.