Database Create table Primary key not found

Mattps

I have two tables i'm trying to create with foreign keys. The statements are below

Book_Copy Table

CREATE TABLE book_copy (
    bid          NUMBER(15) NOT NULL,
    isbn         VARCHAR(15) NOT NULL,
    firstavaib   VARCHAR(9) NOT NULL,
    outservice   VARCHAR(9) NULL,
    CONSTRAINT primary_key PRIMARY KEY ( bid,isbn ),
    FOREIGN KEY ( isbn )
        REFERENCES book_catalog ( isbn )
);

History table

CREATE TABLE history (
    bid          NUMBER(15) NOT NULL,
    mid          NUMBER(10) NOT NULL,
    FOREIGN KEY ( mid )
        REFERENCES member ( mid ),
    datetaken    VARCHAR(9) NOT NULL,
    datereturn   VARCHAR(9) NULL,
    FOREIGN KEY ( bid )
        REFERENCES book_copy ( bid ),
    CONSTRAINT primary_key PRIMARY KEY ( bid, datetaken )
);

Now when I run it the first one says table created but i get the following for the second.

CREATE TABLE history (
    bid          NUMBER(15) NOT NULL,
    mid          NUMBER(10) NOT NULL,
    FOREIGN KEY ( mid )
        REFERENCES member ( mid ),
    datetaken    VARCHAR(9) NOT NULL,
    datereturn   VARCHAR(9) NULL,
    FOREIGN KEY ( bid )
        REFERENCES book_copy ( bid ),
   CONSTRAINT primary_key PRIMARY KEY ( datetaken )

) * ERROR at line 1:

ORA-02270: no matching unique or primary key for this column-list

gvenzl

There are several errors in the statements above and also not all the relevant information was made available.

Your table book_copy has a foreign key reference to the table book_catalog via the isbn column:

CREATE TABLE book_copy (
    ...
    FOREIGN KEY ( isbn )
        REFERENCES book_catalog ( isbn )

To proceed, I took the liberty to add a simple one:

CREATE TABLE book_catalog (
    isbn VARCHAR(15) NOT NULL,
    CONSTRAINT pk_ct PRIMARY KEY (isbn));

And similar, they history table also references another table member via the mid column:

CREATE TABLE history (
    ...
    FOREIGN KEY ( mid )
        REFERENCES member ( mid ),

I also create that one in order to proceed:

CREATE TABLE MEMBER (
    mid NUMBER(10) NOT NULL,
    CONSTRAINT pk_mem PRIMARY KEY (mid));

The error that you get above means what it says: There is no such primary key or unique key on the other table that you try to reference, hence a value cannot be uniquely identified and hence the integrity of the data not verified. So the database stops you from doing so in the first place.

The culprit is that you specify the PRIMARY KEY of your table book_copy to be bid,isbn while your foreign key reference in the history table only references the bid:

CREATE TABLE history (
    ...
    FOREIGN KEY ( bid )
        REFERENCES book_copy ( bid ),

So you ask the database to check the integrity on something that is not uniquely identifiable. In order to solve this you have to expand your foreign key of the history table to include the isbn column as well:

So the full DDL looks like this:

CREATE TABLE book_catalog (
    isbn VARCHAR(15) NOT NULL,
    CONSTRAINT book_catalog_pk PRIMARY KEY (isbn));

CREATE TABLE MEMBER (
    mid NUMBER(10) NOT NULL,
    CONSTRAINT member_pk PRIMARY KEY (mid));

CREATE TABLE book_copy (
    bid          NUMBER(15) NOT NULL,
    isbn         VARCHAR(15) NOT NULL,
    firstavaib   VARCHAR(9) NOT NULL,
    outservice   VARCHAR(9) NULL,
    CONSTRAINT book_copy_pk PRIMARY KEY ( bid,isbn ),
    FOREIGN KEY ( isbn )
        REFERENCES book_catalog ( isbn )
);

CREATE TABLE history (
    bid          NUMBER(15) NOT NULL,
    mid          NUMBER(10) NOT NULL,
    isbn         VARCHAR2(15) NOT NULL,
    FOREIGN KEY ( mid )
        REFERENCES member ( mid ),
    datetaken    VARCHAR(9) NOT NULL,
    datereturn   VARCHAR(9) NULL,
    FOREIGN KEY ( bid, isbn )
        REFERENCES book_copy ( bid, isbn ),
    CONSTRAINT history_pk PRIMARY KEY ( bid, datetaken )
);

There is another problem in your DDL above. In Oracle, constraint names are globally unique! That means that whatever you put after CONSTRAINT has to be unique. In the case above you use the same name primary_key for both your tables which will fail with an ORA-02264: name already used by an existing constraint error on creating the history table. You will see that I have given the primary key constraints on the tables more meaningful names.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Create table with primary key using jOOQ

How create composite foreign key to table with composite primary key

How to use RODBC to save dataframe to table with primary key generated at database

Create a table with primary key and 2 foreign keys

Could not create constraint or index or foreign key on primary key of existing table

Unable to create or change a table without a primary key - Laravel DigitalOcean Managed Database

How to create a table with Interval as primary key in SQLAlchemy?

CREATE TABLE LIKE with different primary key for partitioning

Create a table with a varchar as primary key

How to get list of database table primary key columns in EF Core

create a foreign key on a primary key of another table

How to create a primary key in a CREATE TABLE...SELECT?

database design primary key of multiple tables link to one table

How to update remote tables with local database table with same primary key?

Error when creating a table in SQLite database with composite primary key at AUTOINCREMENT

How to create a table with _id as composite primary key?

create primary key on existing table without primary key in SQL Server

Create an index, or add to the primary key and create a new table?

Do gaps in the Identity (primary key clustered) table affects performance of database?

Create a table who's name must be a primary key in another table

Is there a way in MySQL to implicitly create a primary key for a table?

How to create a table with composite primary key in sqlite?

Cassandra create duplicate table with different primary key

Creating a table in database defining a column as primary key

Is it possible to create a table in the Room without a primary key?

If I create a table without a primary key then add a primary key, is the primary key a clustered index?

How to create a table with two columns that are related to the primary key of another table?

Join Table to create composite Primary Key

Create table from select with primary key?