I have to create tables and insert the data using INSERT SQL statements and SQL LOADER utility. I am using Oracle 10g.
the following are the create table statements:
CREATE TABLE Employee_C (
EID char(3),
Name varchar2(20),
Salary number(7,2),
MID char(3),
PRIMARY KEY (EID),
FOREIGN KEY (MID) REFERENCES Employee_C (EID)
);
CREATE TABLE Conference_C (
ConfID char(6),
Title varchar2(20),
Location varchar2(20),
Sdate date,
PRIMARY KEY (ConfID)
);
CREATE TABLE Topic_C (
Tnum char(3),
Title varchar2(20),
PRIMARY KEY (Tnum)
);
CREATE TABLE Includes_C (
Tnum char(3),
ConfID char(6),
PRIMARY KEY (Tnum,ConfID),
FOREIGN KEY (Tnum) REFERENCES Topic_C,
FOREIGN KEY (ConfID) REFERENCES Conference_C
);
CREATE TABLE Deals_C (
EID char(3),
ConfID char(6),
PRIMARY KEY (EID,ConfID),
FOREIGN KEY (EID) REFERENCES Employee_C,
FOREIGN KEY (ConfID) REFERENCES Conference_C
);
I used INSERT SQL statements to insert data into Topic_C table, and used sql loader utility to insert data into other 4 tables.
So my problem is when I run sql loader, I get the following error:
Record 1: Rejected - Error on table INCLUDES_C.
ORA-02291: integrity constraint (NAVY10.SYS_C00317513) violated - parent key not found
The other tables are working perfectly fine!
I don't know where I am going wrong, please clarify
Thank You
As #OMGPonies mentioned above, when you're loading records into Includes_C one or both of the following is happening:
Includes_C.Tnum isn't in Topic_C
Includes_C.ConfID isn't in Conference_C
The best approach would be to load the parent tables first, then work down through the children. To do that, just load the tables in the order you've defined them. If that doesn't work it means you truly do have a Tnum and/or ConfID value that violates the foreign key.
Related
I have encountered some problem with SQL foreign key.
Here are my table and insert SQL.
create table passenger_card2
(
phone char(20) primary key,
name char(20)
);
create table card
(
card_num char(20) primary key,
balance number(10,2),
cvn char(20)
);
create table passenger_card1
(
sin integer primary key,
user_id char(20) not null unique,
phone char(20),
card_num char(20) unique,
foreign key(phone) references passenger_card2,
foreign key (card_num) references card
);
And here are my INSERT statements:
INSERT INTO PASSENGER_CARD2 VALUES ( '111222333' , 'Ace');
INSERT INTO CARD VALUES ( '1000' , '100.1' , '110');
INSERT INTO PASSENGER_CARD1 VALUES ('100', 'aaaa', '111222333', '1000');
However, I get an error when I tried to insert PASSENGER_CARD1 data:
SQL ORA-02291: integrity constraint violated - parent key not found
I am not sure why my foreign key is wrong?
I am not sure if this is going to be right but you should make the table 2 first before you create the first table. The database is confused because it wouldnt make sense telling them that theres gonna be a foreign key in the second table but the table isnt created. Run the code for the second table first and then run the code for the first table.
I am creating a table in a command SQL sections into a script already populated I have created several tables already but in this one I get a message saying
ORA-00902: invalid datatype
CREATE TABLE Weapons
(
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2 (10),
CONSTRAINT pk_Weapons PRIMARY_KEY(id),
CONSTRAINT fk_Weapons_company
FOREIGN_KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo
FOREIGN_KEY(ammo_id) REFERENCES Ammo(id)
);
In the CONSTRAINT, it should be FOREIGN KEY and not FOREIGN_KEY. Also it should be PRIMARY KEY, not PRIMARY_KEY.
There is no underscore required as per syntax. So the query will be:
CREATE TABLE Weapons (
id NUMBER(4),
name VARCHAR2(30),
damage NUMBER(4),
company_id VARCHAR2(10),
CONSTRAINT pk_Weapons PRIMARY KEY(id),
CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES Ammo(id)
);
About Foreign Keys: https://www.techonthenet.com/oracle/foreign_keys/foreign_keys.php
About Primary Keys: https://www.techonthenet.com/oracle/primary_keys.php
Here's a working example. I've created the AMMO table (whose description you didn't post, so I used only the ID column so that the foreign key constraint wouldn't fail). Pay attention to comments I wrote within the code.
SQL> create table ammo
2 ( id VARCHAR2(10),
3 CONSTRAINT pk_ammo PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Company
2 ( id VARCHAR(3),
3 name VARCHAR(30), --> switch from CHAR to VARCHAR2
4 CONSTRAINT pk_Company PRIMARY KEY(id) );
Table created.
SQL> CREATE TABLE Weapons
2 ( id NUMBER(4),
3 name VARCHAR2(30),
4 damage NUMBER(4),
5 company_id VARCHAR2(3), --> should match COMPANY.ID datatype
6 ammo_id VARCHAR2(10), --> should match AMMO.ID datatype
7 CONSTRAINT pk_Weapons PRIMARY KEY(id),
8 CONSTRAINT fk_Weapons_company FOREIGN KEY(company_id) REFERENCES Company(id),
9 CONSTRAINT fk_Weapons_ammo FOREIGN KEY(ammo_id) REFERENCES ammo(id) );
Table created.
SQL>
In referential integrity constraint, you should match datatypes of the foreign and primary key columns. There's no sense in having a VARCHAR2(10) in the detail table which points to a VARCHAR2(3) column in the master table; you won't be able to put anything longer than 3 characters into the detail table's column anyway (foreign key constraint won't let you).
These are the tables i am trying to create. i am not able to find the mistake here at all.
CREATE TABLE Pharmaceutical (
PharName VARCHAR(30),
PharTelephone VARCHAR(25),
PRIMARY KEY (PharName)
);
CREATE TABLE Doctor (
DID VARCHAR2(20),
DName VARCHAR2(50),
Specialty VARCHAR2(70),
YearOfExp NUMBER,
PRIMARY KEY (DID)
);
CREATE TABLE Patient (
PID VARCHAR2(20),
PName VARCHAR2(50),
PDOB DATE,
PAddress VARCHAR2(70),
PPostalCode VARCHAR2(12),
FamilyDoctor VARCHAR2(20),
PRIMARY KEY (PID),
FOREIGN KEY (FamilyDoctor) REFERENCES Doctor(DID)
);
CREATE TABLE Drug (
PharName VARCHAR2(30),
Tradename VARCHAR2(30),
Formula VARCHAR2(70),
PRIMARY KEY (PharName, Tradename),
FOREIGN KEY (PharName) REFERENCES Pharmaceutical(PharName)
);
CREATE TABLE Prescription (
DID VARCHAR2(20),
PID VARCHAR2(20),
PharName VARCHAR2(30),
Tradename VARCHAR2(30),
PrescriptionDT DATE,
Quantity NUMBER,
CONSTRAINT checkQuantity CHECK (Quantity>0 AND Quantity<31),
PRIMARY KEY (DID, PID, PharName, Tradename, PrescriptionDT),
FOREIGN KEY (DID) REFERENCES Doctor(DID),
FOREIGN KEY (PID) REFERENCES Patient(PID),
FOREIGN KEY (PharName, Tradename) REFERENCES Drug(PharName, Tradename),
);
i also use drop.sql, to drop the tables:
DROP TABLE Pharmaceutical PURGE;
DROP TABLE Doctor PURGE;
DROP TABLE Patient PURGE;
DROP TABLE Drug PURGE;
DROP TABLE Prescription PURGE;
thank you!
P.S. --> removed the 'add' from the add contraint part. this is the error i get:
SP2-0734: unknown command beginning "PRIMARY KEY ..." - rest of line ignored.
getting this error for the last table's primary key and all of it's foreign keys, so assumed that the problem is with the contraint but i may be wrong.
In the definition of table Prescription, the last line before the ending ); reads
FOREIGN KEY (PharName, Tradename) REFERENCES Drug(PharName, Tradename),
Get rid of the trailing comma.
Best of luck.
This is the table I am trying to create. However, I get the error
SQL Error: ORA-02270: no matching unique or primary key for this column-list
SQL:
create table Meets_In
(
cid char(20),
rno integer,
time char(20),
CONSTRAINT PRIM_KEY PRIMARY KEY(time),
constraint meets_fk1 foreign key(cid) references COURSES(CID),
constraint meets_fk2 foreign key(rno) references ROOMS(RNO)
);
These are the parent tables:
create table Courses
(
cid char(20),
cname char(20),
credits integer,
constraint CoursesKey Primary Key (cid, cname)
);
CREATE TABLE ROOMS
(
rno INTEGER,
address CHAR(20),
capacity INTEGER,
CONSTRAINT room_key PRIMARY KEY(rno)
);
I don't understand why I am getting this error.
Cause
The ORA-2270, as the error message suggests, happens when there is no matching unique or primary key for this column-list. This could be because
the parent lacks a constraint altogether
the parent table's constraint is a compound key and we haven't referenced all the columns in the foreign key statement.
Now in your COURSES table, CID is not a primary key. It is a combination of cid,cname. So for every cid, there can be multiple rows.
Now when you reference cid as foreign key for meets_in, it will not work as it violates the second point as I mentioned above.
Workaround
Add column cname in your meets_in table as well. Then use it like below.
create table Meets_In
(
cid char(20) not null,
cname char(20),
rno integer not null,
time1 char(20) not null,
CONSTRAINT PRIM_KEY PRIMARY KEY(time1),
constraint meets_fk1 foreign key(cid,cname) references COURSES (cid,cname), /*Added cid,cname */
constraint meets_fk2 foreign key(rno) references ROOMS (RNO)
);
Meets_In is acting as an associative table. Therefore its primary key should include the foreign keys into the tables it is associating.
Try a primary key consisting of: cid, cname, rno and time.
As others have noted, your primary key for courses is (cid, cname), so you also need to include both of these in your foreign key constraint meets_fk1. Or, if possible, ensure that cid only is the primary key on courses.
(I think time may be a reserved word, so perhaps consider renaming it.)
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!