Foreign Key Creation issue in Oracle - sql

When I try to create these two tables I get:
"SQL Error: ORA-00904: "COLLECTIBLENUM": invalid identifier"
I'm sure it's a noob error but I'm just not seeing it. Can someone please point out what I'm doing wrong? Thanks in advance.
CREATE TABLE Collectibles(
CollectibleNum Number(10) NOT NULL,
CONSTRAINT collectibles_pk PRIMARY KEY(CollectibleNum));
Create table DiecastItems(
DiecastName VARCHAR2(45) NOT NULL,
DiecastCopy NUMBER(2) NOT NULL,
DiecastScale VARCHAR2(25),
ColorScheme VARCHAR2(25),
DiecastYear NUMBER(4),
CONSTRAINT diecastItem_pk PRIMARY KEY(DiecastName, DiecastCopy),
CONSTRAINT diecastItem_Collectible_fk FOREIGN KEY(CollectibleNum) REFERENCES Collectibles(CollectibleNum));

When you add FK, you are linking a column as a child from the table you are creating, to its parent from the parent table. Hence, you need to provide the child column name, as well as the parent column name.
The general syntax is
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
);
Notice that the columns between FOREIGN KEY brackets, are from the table you are creating, while the columns betweeN REFERENCES PARENT_TABLE are from the parent table.
You do not have a column called CollectibleNum in yourDiecastItems. Hence, the following works fine by adding such a column:
CREATE TABLE collectibles
(
collectiblenum NUMBER(10) NOT NULL,
CONSTRAINT collectibles_pk PRIMARY KEY(collectiblenum)
);
CREATE TABLE diecastitems
(
diecastname VARCHAR2(45) NOT NULL,
diecastcopy NUMBER(2) NOT NULL,
diecastscale VARCHAR2(25),
colorscheme VARCHAR2(25),
diecastyear NUMBER(4),
collectiblenum NUMBER(10), --added column
CONSTRAINT diecastitem_pk PRIMARY KEY(diecastname, diecastcopy),
CONSTRAINT diecastitem_collectible_fk FOREIGN KEY(collectiblenum)
REFERENCES collectibles(collectiblenum)
);
FIDDLE

Related

Oracle PLSQL Cascade Delete doesn't work?

I have some tables with foreign keys which should be deleted. I Put the "on delete cascade" everywhere I needed it, but when I try to Drop the Table i get following error:
*Cause: An attempt was made to drop a table with unique or
primary keys referenced by foreign keys in another table.
This is the Table I want to drop:
DROP TABLE Author;
CREATE TABLE Author (
id NUMBER(4) NOT NULL,
first_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL,
date_of_birth DATE NOT NULL,
date_of_death DATE NULL,
CONSTRAINT Author_PK PRIMARY KEY (id)
);
And this is the Table that is in relation to the Author table:
CREATE TABLE Book (
id NUMBER(4) NOT NULL,
author NUMBER(4) NULL,
title VARCHAR2(30) NOT NULL,
ISBN VARCHAR2(13) NOT NULL,
book_language VARCHAR2(2) NOT NULL,
book_genre VARCHAR2(20) NOT NULL,
CONSTRAINT Book_PK PRIMARY KEY (id),
CONSTRAINT Book_Author FOREIGN KEY (author) REFERENCES Author(id) ON DELETE cascade
);
DROP is a DDL. It has nothing to do with DELETE (DML) (affected by the way you created the foreign key constraint).
Drop child table first; then drop its parent.
I resolved it now with help from #a_horse_with_no_name
If you have the same issue, just write
drop table table_name cascade constraints;

How to define a foreign key

my two parents table are person and artwork. I created their table as follows:
create table person (
person_id number(20) primary key,
name varchar2(20),
address varchar(50),
contact_number number (10)
);
and other
create table artwork (
artwork_id number primary key,
barcode char (20),
title varchar2(20),
description varchar2(50));
When I try to make the child table vote it is giving this error.
create table vote
(
vote_id number(7) NOT NULL,
artwork_id number(20),
person_id number(20),
PRIMARY KEY (vote_id),
FOREIGN KEY (artwork_id) REFERENCES artwork(artwork_id),
FOREIGN KEY ( person_id) REFERENCES Person(person_id)
);
SP2-0734: unknown command beginning "FOREIGN KE..." - rest of line ignored.
You forgot to mention constraint before foreign key:
create table vote
(
vote_id number(7) NOT NULL,
artwork_id number(20),
person_id number(20),
PRIMARY KEY (vote_id),
CONSTRAINT fk_artwork_id FOREIGN KEY (artwork_id) REFERENCES artwork(artwork_id),
CONSTRAINT fk_person_id FOREIGN KEY ( person_id) REFERENCES Person(person_id)
);
Syntax
The syntax for creating a foreign key using a CREATE TABLE statement
is:
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT fk_column
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n)
);
Demo
http://sqlfiddle.com/#!4/3d141
Explanation
By default SQLPlus doesn't like blank lines. However we can easily configure our SQLPlus environment to ignore them:
SQL> set sqlblanklines on
We can also put the setting in our glogin.sql file (assuming we're allowed to edit it, which isn't always the case).
remove that blank line before the definition of your constraints when creating the vote table:
create table vote
(
vote_id number(7) NOT NULL,
artwork_id number(20),
person_id number(20),
PRIMARY KEY (vote_id),
FOREIGN KEY (artwork_id) REFERENCES artwork(artwork_id),
FOREIGN KEY ( person_id) REFERENCES Person(person_id)
);
Any blank rows will stop SQL*Plus from accepting input lines.
If you want it to avoid this run:
set sqlblanklines on
before any other instruction.

Oracle SQL Database error: "no matching unique or primary key for this column-list"

I'm trying to set up a database in Oracle sql developer, I've got these 3 tables.
I need the table "GuyAddress" to have 3 primary keys, which are all foreign keys as well. This is where I'm running into an error which I can't get my head around.
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
"number" NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, "number")
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
This is the error, hopefully someone can spot the mistake I made because I can't...
Error starting at line : 18 in command -
CREATE TABLE "GuyAddress"
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
)
Error report -
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
Thanks!
You don't need separate foreign keys for each column in the referenced table's primary key - you can have multiple columns in a foreign key, e.g.:
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
address_number NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, address_number)
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address FOREIGN KEY(Address_zipcode, Address_number) REFERENCES Address(zipcode,address_number),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
Note that I've updated your address.number column to be address.address_number, as it's not recommended that you use a column based on a keyword. Using doublequotes to get around this (and also enforce case sensitivity) is not recommended either; you'll have to remember to use them every time you reference that column!
As an aside, I assume that your address table has other columns? Because as things stand, the address table is pointless and could be skipped!

(SQL) integrity constraint violated - parent key not found

I did look up for solutions for this problem but i still get the same error..
I'm trying to insert values into PART and MANUFACTURER tables. Initially, i inserted values into MANUFACTURER without knowing the fact i need to deal with the parent table i.e. PART. So, i did the PART then the MANUFACTURER but still not working :(.
These are the tables:
PART(PNum, PName, PUnitPrice, ComponentOf)
primary key (PNum)
foreign key (ComponentOf) references PART(PNum)
MANUFACTURER(MName, MAddress, MPhone)
primary key (MName)
candidate key (MPhone)
candidate key (MAddress)
PART-MANUFACTURED(MDate, PNum, MName, Quantity)
primary key (MName, PNum, MDate)
foreign key (PNum) references PART(PNum)
foreign key (MName) references MANUFACTURER(MName)
CUSTOMER(CNum, CName, CType)
primary key (CNum)
domain constraint ctype in ('INDIVIDUAL', 'INSTITUTION')
ORDERS(CNum, PNum, OrderDate, OrderQuantity)
primary key (CNum, PNum, OrderDate)
foreign key (CNum) references CUSTOMER(CNum)
foreign key (PNum) references PART(PNum)
Create statements:
CREATE TABLE PART(PNum VARCHAR(25) NOT NULL, PName VARCHAR(75) NOT NULL, PUnitPrice NUMBER(7,2) NOT NULL, ComponentOf VARCHAR(25), PRIMARY KEY(PNum), FOREIGN KEY(ComponentOf) REFERENCES PART(PNum));
Table created.
SQL> CREATE TABLE MANUFACTURER(MName VARCHAR(50) NOT NULL, MAddress VARCHAR(100) NOT NULL, MPhone VARCHAR(25) NOT NULL, PRIMARY KEY(MName), CONSTRAINT UK_MADDRESS Unique(MAddress), CONSTRAINT UK_MPHONE UNIQUE(MPhone));
Table created.
SQL> CREATE TABLE PARTMANUFACTURED(MDate DATE NOT NULL, PNum VARCHAR(25) NOT NULL, MName VARCHAR(50) NOT NULL, QUANTITY NUMBER(10) NOT NULL, PRIMARY KEY(MName, PNum, MDate), FOREIGN KEY(PNum) REFERENCES PART(PNum), FOREIGN KEY(MName) REFERENCES MANUFACTURER(MName));
Table created.
SQL> CREATE TABLE CUSTOMER(CNum VARCHAR(25) NOT NULL, CName VARCHAR(75) NOT NULL, CType VARCHAR(20) NOT NULL, PRIMARY KEY(CNum), CHECK(Ctype in('INDIVIDUAL','INSTITUTION')));
Table created.
SQL> CREATE TABLE ORDERS(CNum VARCHAR(25) NOT NULL, PNum VARCHAR(25) NOT NULL, OrderDate DATE NOT NULL, OrderQuantity NUMBER(7,2) NOT NULL, PRIMARY KEY(CNum, PNum, OrderDate), FOREIGN KEY(CNum) REFERENCES CUSTOMER(CNum), FOREIGN KEY(PNum) REFERENCES PART(PNum));
Isn't the PNum already the primary or parent key? and PART table is the parent table? since, other tables have the PNum as foreign key.. i really don't get it..
anyone knows and can help me with it, is greatly appreciated. thanks :)
The error with your insert statement INSERT INTO PART VALUES('S001', 'System-economy', 1100, 'Null') is that you are trying to insert a string 'NULL' rather than an actual NULL for the column ComponentOf in the PART table.
The problem with the string 'NULL' is that you have a FOREIGN KEY constraint on ComponentOf that references the column PNum, which means that all the values in the column ComponentOf must also be in PNum. However, there is no value 'NULL' in PNum so that's why it threw the error. An actual NULL works since it means that it is not referencing anything.
The value inserted for ComponentOf has to match an existing PNum in the PARTS table. Your key is their to ensure you don't have any "orphaned" components.
If you try to insert 'Null' (a string value as mentioned in the comments) then it can't find the "parent". However, null is allowed since it means that particular part is not a component of any other part, i.e. it doesn't have a "parent".

Error creating database with multiple primary key columns and referencing foreign key

I've been having an issue when creating a database. Each table has a primary key with many foreign keys used also. The issue I have is that I keep getting the error
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
I don't know what is causing this and is flagging the PROJECT_RECORDS table as the issue. I have used the same method in the PROJECT_TABLES table.
SQL/Oracle
CREATE TABLE PROJECT_DB
(DB_ID number (3) NOT NULL primary key,
DB_NAME varchar2 (25) NOT NULL,
DB_DESCRIPTION varchar2 (75) NOT NULL,
DB_DATE date NOT NULL);
CREATE TABLE PROJECT_DATATYPE
(DATATYPE_NAME varchar2 (20) NOT NULL PRIMARY KEY,
DATATYPE_DATATYPE varchar2(50) NOT NULL);
CREATE TABLE PROJECT_TABLES (
PROJECT_ID number(3) not null references PROJECT_DB(DB_ID) on delete cascade,
PROJECT_FIELDNAME varchar2(25) not null,
PROJECT_DATATYPE varchar2(50) not null references PROJECT_DATATYPE(DATATYPE_NAME),
PROJECT_LENGTH number(3),
PROJECT_REQUIRED varchar2(8),
PROJECT_LISTCOLUMNID number (3) not null,
primary key(PROJECT_ID, PROJECT_LISTCOLUMNID));
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null references PROJECT_TABLES(PROJECT_LISTCOLUMNID)on delete cascade,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID));
commit;
The reasoning for adding multiple primary keys to the PROJECT_TABLES table is that the listcolumnid isn't unique.
If listcolumnid is not unique, you cannot put a foreign key constraint on it. A foreign key always refrences exatcly one parent row. So you probably should use both columns in your foreign key (assuming there is really a 1:n relation):
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_ID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID),
foreign key fk_project_projectrecords (RECORDS_ID, RECORDS_LISTCOLUMNID) references PROJECT_TABLES(PROJECT_ID, PROJECT_LISTCOLUMNID)on delete cascade,
);
(The example is using your prefix naming convention, I would change RECORDS_ID to PROJECT_ID, same for RECORDS_LISTCOLUMNID)
The problem is with your syntax. You don't do this:
, fieldname datatype references (something)
You do this:
, primary key(somefield)
, foreign key (somefield) references sometable(somefield)