I am trying to create a writers table that contains the author ID, last name, first name, and ISBN and title of the book each author wrote. While using the same data types as the author and books table, but not copying the data from these tables. And include the author ID as the primary key, the title as not null, and the ISBN as the foreign key referencing the ISBN in the books table. However I receive the "unique constraint (WT_PK) violated" error:
CREATE TABLE writers
(authorid VARCHAR2(4),
lname VARCHAR2(10),
fname VARCHAR2(10),
isbn VARCHAR2(10),
title VARCHAR2(30) CONSTRAINT title_nn NOT NULL,
CONSTRAINT wt_pk PRIMARY KEY(authorid),
CONSTRAINT wt_fk FOREIGN KEY(isbn) REFERENCES books(isbn));
INSERT INTO writers
SELECT authorid, fname, lname, isbn, title
FROM author
JOIN bookauthor USING(authorid)
JOIN books USING(isbn);
It seems that you need to use a composite key for your WRITERS table. Example (tested with Oracle 12c and 11g, dbfiddle here):
Add some constraints to the BOOKAUTHOR table, and check its contents:
"Original" DDL code (with minor modifications) -> INSERT fails
Suggested DDL code (and testing):
SELECTing from WRITERS2:
Not sure why you would need the WRITERS table, though - as you can generate its data by running a query.
Maybe try this:
Just make sure this line references your author table primary key (now it is authorid)
Also remove unique index if don't need it. And in your example you probably don't need it, becouse one author can have multiple book titles. You should clarify what you trying to accomplish.