Oracle Problems with Composite Keys - sql

bit confusing question here:
I am trying to create a composite FK from a composite PK. I'll show you my tables in question that I'm having problems with;
CREATE TABLE Weapons (
Weapon_ID VARCHAR(10) NOT NULL,
Weapon_Name VARCHAR(30),
Range_In_Meters INT,
Maximum_Number_Of_Uses INT,
Damage_Factor INT,
Cost INT,
primary key(Weapon_ID));
CREATE TABLE WeaponInventory (
Inventory_ID VARCHAR(30) NOT NULL,
WeaponInventory_ID Varchar(10) NOT NULL,
Weapon_ID VARCHAR(10) REFERENCES Weapons(Weapon_ID) NOT NULL,
primary key(Inventory_ID, WeaponInventory_ID));
CREATE TABLE Avatars (
Avatar_ID VARCHAR(10) NOT NULL,
Avatar_Name VARCHAR(30),
AvA_DOB DATE,
Age VARCHAR(30),
Gender VARCHAR(30),
Strength_Indicated INT,
Hoard INT,
Avatar_Level VARCHAR(30),
Skill VARCHAR(30),
Original_Owner VARCHAR(30),
Family_ID VARCHAR(10) NOT NULL,
Species_ID VARCHAR(10) NOT NULL,
Inventory_ID VARCHAR(30) NOT NULL,
Weapon_ID VARCHAR(10) NOT NULL,
Player_ID VARCHAR(10) NOT NULL,
PRIMARY KEY (Avatar_ID),
FOREIGN KEY (Inventory_ID, Weapon_ID)
REFERENCES WeaponInventory (Inventory_ID, Weapon_ID));
In the table Avatars, I have been trying to create this Foreign key but I'm having no luck. I have errors such as:
ORA-02270: no matching unique or primary key for this column-list
and:
ORA-02270: no matching unique or primary key for this column-list
Any help would be greatly appreciated! I just keep getting confused and ending up in the same place! (:

I think you planned to reference column WeaponInventory_ID (instead of Weapon_ID) in table WeaponInventory:
FOREIGN KEY (Inventory_ID, Weapon_ID)
REFERENCES WeaponInventory (Inventory_ID, WeaponInventory_ID) -- see here
Note that Weapon_ID is not part of the PK in WeaponInventory:
CREATE TABLE WeaponInventory (
Inventory_ID VARCHAR(30) NOT NULL,
WeaponInventory_ID Varchar(10) NOT NULL,
Weapon_ID VARCHAR(10) REFERENCES Weapons(Weapon_ID) NOT NULL,
primary key(Inventory_ID, WeaponInventory_ID) -- Weapon_ID is not part of PK
);
Check this Fiddle.

Related

Getting missing parenthesis in my sql code [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
Using ORACLE SQL Developer
CREATE TABLE PAYMENT (
Payment_ID VARCHAR2(300) NOT NULL,
Account_ID VARCHAR(300) NOT NULL,
Discount_Code VARCHAR(50),
Gift_Card VARCHAR(50),
Cust_Rewards INTEGER,
Credit_Card VARCHAR(50),
Checking VARCHAR(50),
Bank_Account VARCHAR(80) NOT NULL,
Store_Card VARCHAR(50),
CONSTRAINT PK_PAYMENT_ID PRIMARY KEY REFERENCES PAYMENT(Payment_ID),
CONSTRAINT FK_PAYMENT_Account_ID FOREIGN KEY (Account_ID) REFERENCES ACCOUNT(Account_ID)
);
I get an error report that states "ORA-00906: missing left parenthesis:
How can I fix this??
EDIT:
Full code being ran:
CREATE TABLE CUSTOMER (
Customer_ID VARCHAR(300) NOT NULL,
Cust_FName VARCHAR(20) NOT NULL,
Cust_LName VARCHAR(20) NOT NULL,
Cust_Email VARCHAR(50),
Cust_Address VARCHAR(100),
CONSTRAINT PK_CUSTOMER PRIMARY KEY (Customer_ID)
);
/*Account table*/
CREATE TABLE ACCOUNT (
Account_ID VARCHAR(300) NOT NULL,
Customer_ID VARCHAR(300) NOT NULL,
Cust_Username VARCHAR(50) NOT NULL,
Cust_Password VARCHAR(50) NOT NULL,
Pet_Name VARCHAR(25),
Pet_DOB INTEGER,
Cust_Rewards INTEGER,
CONSTRAINT PK_ACCOUNT PRIMARY KEY (Account_ID,Customer_ID),
CONSTRAINT FK_ACCOUNT FOREIGN KEY (Customer_ID) REFERENCES CUSTOMER(Customer_ID)
);
CREATE TABLE PAYMENT (
Payment_ID VARCHAR2(300) NOT NULL,
Account_ID VARCHAR(300) NOT NULL,
Discount_Code VARCHAR(50),
Gift_Card VARCHAR(50),
Cust_Rewards INTEGER,
Credit_Card VARCHAR(50),
Checking VARCHAR(50),
Bank_Account VARCHAR(80) NOT NULL,
Store_Card VARCHAR(50),
CONSTRAINT PK_PAYMENT_ID PRIMARY KEY REFERENCES PAYMENT(Payment_ID),
CONSTRAINT FK_PAYMENT_Account_ID FOREIGN KEY (Account_ID) REFERENCES ACCOUNT(Account_ID)
);
Error starting at line : 32 in command-
CREATE TABLE PAYMENT (
Payment_ID VARCHAR2(300) NOT NULL,
Account_ID VARCHAR(300) NOT NULL,
Discount_Code VARCHAR(50),
Gift_Card VARCHAR(50),
Cust_Rewards INTEGER,
Credit_Card VARCHAR(50),
Checking VARCHAR(50),
Bank_Account VARCHAR(80) NOT NULL,
Store_Card VARCHAR(50),
CONSTRAINT PK_PAYMENT_ID PRIMARY KEY REFERENCES PAYMENT(Payment_ID),
CONSTRAINT FK_PAYMENT_Account_ID FOREIGN KEY (Account_ID) REFERENCES ACCOUNT(Account_ID)
)
Error report-
ORA-00906: missing left parentesis
00906. 0000 - "missing left parenthesis"
*Cause:
*Action:
A foreign key must reference the primary key of the parent table - the entire primary key. (SQL. How to reference a composite primary key Oracle?)
here is the fiddle http://sqlfiddle.com/#!4/eacfa48
CREATE TABLE PAYMENT (
Payment_ID VARCHAR2(300) NOT NULL ,
Account_ID VARCHAR(300) NOT NULL,
Customer_ID VARCHAR(300) NOT NULL,
Discount_Code VARCHAR(50),
Gift_Card VARCHAR(50),
Cust_Rewards INTEGER,
Credit_Card VARCHAR(50),
Checking VARCHAR(50),
Bank_Account VARCHAR(80) NOT NULL,
Store_Card VARCHAR(50),
CONSTRAINT PK_PAYMENT_ID PRIMARY KEY (Payment_ID),
CONSTRAINT FK_PAYMENT FOREIGN KEY (Account_ID, Customer_ID) REFERENCES ACCOUNT(Account_ID, Customer_ID)
);
one of the reasons I never use composite primary keys.

There is no unique constraint matching given keys for referenced table " exam_subjects"

CREATE TABLE student
(
student_id INT PRIMARY KEY,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
birth_day DATE NOT NULL,
sex VARCHAR(1) NOT NULL,
student_email_address VARCHAR(40) NOT NULL UNIQUE,
student_password VARCHAR(10) NOT NULL UNIQUE,
student_nick_name VARCHAR(10) NOT NULL,
student_qualification VARCHAR(10) NOT NULL,
student_documents VARCHAR(255) NOT NULL,
student_image VARCHAR(100) NOT NULL
);
CREATE TABLE student_feedback
(
sr_no BIGSERIAL PRIMARY KEY,
student_id INT NOT NULL,
feedback_type VARCHAR(10) NOT NULL,
feedback_text VARCHAR(200) NOT NULL,
FOREIGN KEY (student_id) REFERENCES student(student_id)
);
CREATE TABLE online_exam
(
exam_id INT PRIMARY KEY,
exam_title VARCHAR(20) UNIQUE NOT NULL,
exam_duration_min INT NOT NULL,
total_questions INT NOT NULL,
marks_per_right_answer INT NOT NULL,
marks_per_wrong_answer INT NOT NULL,
passing_marks INT NOT NULL,
exam_status VARCHAR(2)
);
CREATE TABLE exam_subjects
(
sub_id INT,
exam_id INT,
sub_name VARCHAR(20) NOT NULL,
sub_desc VARCHAR(20) NOT NULL,
UNIQUE(sub_id,exam_id),
PRIMARY KEY(sub_id,exam_id),
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE
);
CREATE TABLE sub_questions
(
sub_id1 INT,
ques_id INT,
ques_text VARCHAR(150) NOT NULL,
ques_attachments VARCHAR(255),
option_1 VARCHAR(20) NOT NULL,
option_2 VARCHAR(20) NOT NULL,
option_3 VARCHAR(20) NOT NULL,
option_4 VARCHAR(20) NOT NULL,
UNIQUE(sub_id1,ques_id),
PRIMARY KEY (sub_id1,ques_id),
FOREIGN KEY (sub_id1) REFERENCES exam_subjects(sub_id) ON DELETE CASCADE
);
CREATE TABLE ques_answers
(
ans_id INT,
ques_id INT,
ans VARCHAR(20) NOT NULL,
PRIMARY KEY (ans_id,ques_id),
FOREIGN KEY (ques_id) REFERENCES sub_questions(ques_id) ON DELETE CASCADE
);
CREATE TABLE exam_registration
(
student_id INT,
exam_id INT,
exam_date_time TIMESTAMP NOT NULL,
PRIMARY KEY (student_id,exam_id),
FOREIGN KEY (student_id) REFERENCES student(student_id) ON DELETE CASCADE,
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE
);
CREATE TABLE exam_result
(
student_id INT,
exam_id INT,
sub_id INT,
marks_scored INT NOT NULL,
correct_ques INT NOT NULL,
wrong_ques INT NOT NULL,
grade VARCHAR(10) NOT NULL,
PRIMARY KEY(student_id,exam_id,sub_id),
FOREIGN KEY (student_id) REFERENCES student(student_id) ON DELETE CASCADE,
FOREIGN KEY (exam_id) REFERENCES online_exam(exam_id) ON DELETE CASCADE,
FOREIGN KEY (sub_id) REFERENCES exam_subjects(sub_id) ON DELETE CASCADE
);
Output :
CREATE TABLE,
CREATE TABLE,
CREATE TABLE,
CREATE TABLE,
There is no unique constraint matching given keys for referenced table "exam_subjects", relation "sub_questions" do not exist,
CREATE TABLE,
There is no unique constraint matching given keys for referenced table "exam_subjects".
Why am I getting "there is no unique..." error in postgresql? How to solve it? Please help.
In your table exam_subjects the primary key is a compossed key (PRIMARY KEY(sub_id, exam_id))
Therefore, your table sub_questions must have both columns to create the constraint, otherwise you get the error about unique constraints (or you can create a unique field acting as primary key, and create a unique index on both columns sub_id and exam_id)

Problems referencing primary key with foreign key

This is code to create the beginnings of a book library database with Microsoft SQL Server Management Studio.
CREATE DATABASE BOOK_LIBRARY
CREATE TABLE LIBRARY_USER
(
usr_id int not null primary key,
f_name varchar(30) not null,
m_init char(1),
l_name varchar(30) not null,
balance decimal(6,2),
join_date date,
addrss_1 varchar(30) not null,
addrss_2 varchar(30),
city varchar(30) not null,
addrss_state char(2) not null,
zip_code varchar(10) not null,
email varchar(30)
);
CREATE TABLE LIBRARY_TRANSACTIONS
(
transaction_id int not null primary key,
maximum_borrow_duration int not null,
strt_date date not null,
actual_return_date date not null,
borrow_usr_id int not null,
foreign key (borrow_usr_id) references LIBRARY_USER(usr_id)
);
CREATE TABLE BOOKS
(
isbn varchar(17) not null primary key,
title varchar(30) not null,
number_of_copies int not null,
Author varchar(30) not null,
Number_of_pages int not null,
publish_year int not null,
book_type varchar(20)
);
CREATE TABLE DIGITAL_BOOKS
(
digital_id int not null primary key,
format varchar(30) not null,
size_mb int not null,
digital_isbn varchar(17) not null,
foreign key(digital_isbn) references BOOKS(isbn)
);
CREATE TABLE PHYSICAL_BOOKS
(
physical_id int not null primary key,
condition varchar(20) not null,
physical_isbn varchar(17) not null,
foreign key(physical_isbn) references BOOKS(isbn)
);
CREATE TABLE BOOK_COPY
(
digi_id int not null,
phys_id int not null,
primary key(digi_id, phys_id),
foreign key(digi_id) references DIGITAL_BOOKS(digital_id),
foreign key(phys_id) references PHYSICAL_BOOKS(physical_id)
);
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id) references BOOK_COPY(digi_id),
foreign key(phys_id) references BOOK_COPY(phys_id)
);
Despite me being able to look and see that digi_id and phys_id are in fact primary keys of the book_copy table, I keep getting this error:
Msg 1776, Level 16, State 0, Line 66
There are no primary or candidate keys in the referenced table 'BOOK_COPY' that match the referencing column list in the foreign key 'FK__CONTNS__digi_id__37A5467C'.
Msg 1750, Level 16, State 1, Line 66
Could not create constraint or index. See previous errors.
Am I missing something obvious? I'm just starting out with using this program, so any help is greatly appreciated.
Here:
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id) references BOOK_COPY(digi_id),
foreign key(phys_id) references BOOK_COPY(phys_id)
);
You are creating two foreign keys to parent table BOOK_COPY. But that table has a compound primary key (ie a multi-column primary key)
CREATE TABLE BOOK_COPY
(
digi_id int not null,
phys_id int not null,
primary key(digi_id, phys_id), --> here
...
)
As a consequence, you need a compound foreign key:
CREATE TABLE CONTNS
(
trans_id int not null primary key,
digi_id int not null,
phys_id int not null,
foreign key(digi_id, phys_id) references BOOK_COPY(digi_id, phys_id)
);

Error ORA-00904 in SQL developer. Seemly no fix online that works that I can find while creating a table

I have seen many posts regarding the seemingly infamous ORA-00904 yet nothing seems to be helping. I am creating 2 tables (for now, more to come later) which are as follows;
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
);
and
CREATE TABLE Customer(
CustomerID INT NOT NULL PRIMARY KEY,
ProjectID INT NOT NULL,
FOREIGN KEY(ProjectID) REFERENCES Project(ProjectID), /*This is yet another table with a foreign key which needs to be sorted*/
CustomerName Char(255) NOT NULL,
PhoneNumber INT NOT NULL,
Region CHAR(255) NOT NULL
);
but every time I run it, I get error;
Error starting at line : 4 in command -
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY(customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Error report -
ORA-00904: "CUSTOMERID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
ANY HELP WOULD BE GREATLY APPRECIATED!
Oracle by default converts all names to UpperCase if you don't enquote them. (Edit: At the time of this answer the Foreign Key Constraint in the starting post did contain a quoted column name).
So, your constraint tries to find a column named "customerID" whereas the real name of your column is "CUSTOMERID".
Either enquote your column:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
"customerID" INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY ("customerID") REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Don't use quotes on your FOREIGN KEY constraint:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
Or use quotes and write it uppercase in your foreign key constraint:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY ("CUSTOMERID") REFERENCES customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
As already mentioned, keeping the style consistent is recommended to prevent future problems, so Option 2 would be the easiest solution. Otherwise you need to enquote all columns to be consistent.
/Edit:
I just tried it - this definitely works with Oracle 12C:
CREATE TABLE Customer (
CustomerID INT,
PRIMARY KEY (CustomerID)
)
Followed by:
CREATE TABLE Employee(
EmployeeID int NOT NULL,
PRIMARY KEY(EmployeeID),
customerID INT NOT NULL,
CONSTRAINT employee_customer_fk FOREIGN KEY(customerID) REFERENCES Customer(CustomerID),
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
)
The referenced table needs to created before the foreign key reference. One method is to create the tables first and then add the foreign key references:
CREATE TABLE Employee (
EmployeeID int NOT NULL,
PRIMARY KEY (EmployeeID),
customerID INT NOT NULL,
LastName CHAR(20) NOT NULL,
MiddleInitial CHAR(1),
FirstName CHAR(20) NOT NULL,
Region CHAR(20) NOT NULL,
DateOfHire VARCHAR(20) NOT NULL,
Skill VARCHAR(50) NOT NULL
);
CREATE TABLE Customer (
CustomerID INT NOT NULL PRIMARY KEY,
ProjectID INT NOT NULL,
-- FOREIGN KEY(ProjectID) REFERENCES Project(ProjectID), /*This is yet another table with a foreign key which needs to be sorted*/
CustomerName Char(255) NOT NULL,
PhoneNumber INT NOT NULL,
Region CHAR(255) NOT NULL
);
alter table Employee add CONSTRAINT employee_customer_fk FOREIGN KEY (customerID) REFERENCES Customer (CustomerID)
Here is a db<>fiddle.
Note that I commented out the foreign key constraint to Project.
Also, in most data models you can just order the table creates -- there are no cycles in the foreign key "linkages". However, because you have not defined Project, I am suggesting this more general approach.

Informix - Cant find syntax error

This may be a stupid mistake but I'm new to Informix and I can't seem to figure out why none of my CREATE TABLE statements won't run. I keep getting a syntax error on all of my CREATE TABLE statements.
CREATE TABLE customer(
store_num INTEGER NOT NULL,
store_name VARCHAR(20) NOT NULL,
addr VARCHAR(20),
addr2 VARCHAR(20),
city VARCHAR(15),
state VARCHAR(2),
zip-code VARCHAR(5),
contact_name VARCHAR(30),
phone VARCHAR(18),
CONSTRAINT cust_pk PRIMARY KEY(store_num)
);
create table orders(
order_num INTEGER NOT NULL,
order_date DATE NOT NULL,
store_num INTEGER NOT NULL,
fac_code CHAR(3),
ship_instr CHAR(10),
promo CHAR(1) NOT NULL,
CONSTRAINT orders_pk PRIMARY KEY(order_num)
);
create table factory(
fac_code CHAR(3) NOT NULL,
fac_name CHAR(15) NOT NULL,
CONSTRAINT fac_pk PRIMARY KEY(fac_code)
);
create table stock(
stock_num INTEGER NOT NULL,
fac_code CHAR(3) NOT NULL,
description CHAR(15) NOT NULL,
reg_price DECIMAL(8,2) NOT NULL,
promo_price DECIMAL(8,2),
price_updated DATE,
unit CHAR(4) NOT NULL,
CONSTRAINT stock_pk PRIMARY KEY(stock_num)
);
create table items(
order_num INTEGER NOT NULL,
stock_num INTEGER NOT NULL,
quantity SMALLINT NOT NULL,
price DECIMAL(8,2) NOT NULL,
CONSTRAINT items_pk PRIMARY KEY(order_num, stock_num)
);
create table state(
state_code CHAR(2) NOT NULL,
state_name CHAR(15) NOT NULL,
CONSTRAINT state_pk PRIMARY KEY(state_code)
);
Any help will be appreciated.
For reasons I don't understand, Informix requires the constraint name after the constraint, whereas standard SQL requires the constraint name before the constraint.
Thus, in addition to changing zip-code to zip_code (as pointed out by daniel.shih in an answer), you need:
CREATE TABLE customer(
store_num INTEGER NOT NULL,
store_name VARCHAR(20) NOT NULL,
addr VARCHAR(20),
addr2 VARCHAR(20),
city VARCHAR(15),
state VARCHAR(2),
zip_code VARCHAR(5),
contact_name VARCHAR(30),
phone VARCHAR(18),
PRIMARY KEY(store_num) CONSTRAINT cust_pk
);
You can try customer's field zip_code instead of zip-code