Issues with already defined PK's: no matching unique or primary key for this column-list - sql

I am aware this error is telling me I shouldn't have a FK of something that isn't a PK, but I think that's not my case and I haven't found a way around it, so here's my code:
I have this ENTITY table
CREATE TABLE ENTITY(
entity_id VARCHAR(20) PRIMARY KEY,
name VARCHAR(30)
);
No problem there, the problem comes when I try to create another table with entity_id as a FK, as such:
CREATE TABLE EXPORTER(
exporter_id VARCHAR(20) PRIMARY KEY,
name VARCHAR(30),
FOREIGN KEY (exporter_id) REFERENCES ENTITY(ENTITY_ID)
);
Any help would be appreciated.

try this:
CREATE TABLE ENTITY(
entity_id VARCHAR(20) PRIMARY KEY,
name VARCHAR(30)
);
CREATE TABLE EXPORTER(
exporter_id VARCHAR(20) PRIMARY KEY,
name VARCHAR(30),
CONSTRAINT FK_exporter_id
FOREIGN KEY (exporter_id)
REFERENCES ENTITY(entity_id)
);

Related

SQL Constraint names

If we have query for creating table like this..
create table if not exists food
(
id int not null auto_increment,
user_id int,
name varchar(30),
constraint pk_food primary key(id,name),
foreign key(user_id) references userss(id)
);
What does pk_food mean in this example? I know this is a constraint name, but for what we should be give a name for constraint, if its working without?
create table if not exists food
(
id int not null auto_increment,
user_id int,
name varchar(30),
primary key (id, name),
foreign key (user_id) references userss(id)
);
I mean.. how to use these names and for what we need it?
You gives constraints names for basically two reasons:
You can better understand the error message when the constraint is violated.
You can more easily find the constraint if you want to delete it.

ERROR :ORA-02264: name already used by an existing constraint

create table ward
(
wnum int primary key,
wname varchar(30),
phoneno int,
wloc varchar(50),
chnursename varchar(20) constraint ward_fk references charge_nurse
);
create table charge_nurse
(
chnurse varchar(20) constraint charge_nurse_pk primary key,
stnum int constraint charge_nurse_fk references staff
);
create table staff
(
stname varchar(20),
stnum int constraint staff_pk primary key,
addr varchar(20),
phoneno int,
stposition varchar(30),
specality varchar(30) unique,
shift varchar(10),
noofhoursperweek int
);
create table generalsupplies
(
itnum int constraint generalsupplies_pk primary key,
itname varchar(20) unique,
quantityinstock int,
reorder varchar(10),
despt varchar(10),
costperunit int
);
create table pharmasupplies
(
dnum int constraint pharmasupplies_pk primary key,
dname varchar(30) unique,
despt varchar(20),
dosage_Mg int,
quantityinstock int,
reorder varchar(10),
costperunit int
);
While creating the below table I am facing problem:
ORA-02264: name already used by an existing constraint
create table centralsupplies
(
wardnum int constraint centralsupplies_fk references ward,
itemnum int constraint centralsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_fk references staff,
regnum int unique,
dateord date,
daterec date
);
How do I solve this problem?
You use 3 times the same constraint name centralsupplies_fk in your centralsupplies table.
3 constraints = 3 constraint names
Your create table statement has four foreign keys all called centralsupplies_fk. That is not allowed: constraint names must be unique within a schema. You must give each one a different name.
It is usual practice to include the referenced table in the key name. So
create table centralsupplies
(
wardnum int constraint centralsupplies_ward_fk references ward,
itemnum int constraint centralsupplies_generalsupplies_fk references generalsupplies,
drugnum int constraint centralsupplies_pharmasupplies_fk references pharmasupplies,
quantity_required varchar(20),
staffname varchar(10) references staff(stname),
staffnum int constraint centralsupplies_staff_fk references staff,
regnum int unique,
dateord date,
daterec date
)
Also you have another foreign key constraint on STAFFNAME which you have not named. You do not need to name constraints, the system will generate a unique one for you, but it's generally a good idea to name them, not least because it is easier to diagnose relational integrity error messages with meaningfully named constraints.
However, in this case the correct solution is to drop the STAFFNAME column. You already have a foreign on the STAFF table, and you should join to that table whenever you need to display a value for STAFFNAME. Besides you do not have a unique constraint on staff.stname column, so the foreign key statement will fail: foreign keys can only reference primary or unique keys.

Problem in creating new table ORA-00906: missing left parenthesis

Here is my instruction :
it works on my friend computer !
CREATE TABLE typeRdv1(
id_type int PRIMARY KEY,
des VARCHAR(20)
);
CREATE TABLE rdv1(
id_rdv int PRIMARY KEY,
cin_pat VARCHAR(20),
date_rdv VARCHAR NOT NULL,
type_rdv VARCHAR(20),
state VARCHAR(20),
CONSTRAINT `FK_cin_rdv` FOREIGN KEY (cin_pat)
REFERENCES patients(cin_pat) ON DELETE NO ACTION,
CONSTRAINT `FK_id_type_rdv` FOREIGN KEY (type_rdv)
REFERENCES typeRdv1(id_type) ON DELETE NO ACTION
);
A ORA-00906: missing left parenthesis
Any help !
Please make sure all the "varchar" fields has a length defined. In your second table, it says: date_rdv VARCHAR NOT NULL, which I believe causing the issue, so try to change it into date_rdv VARCHAR(50) NOT NULL and try again.
...
CREATE TABLE rdv1(
id_rdv int PRIMARY KEY,
cin_pat VARCHAR(20),
date_rdv VARCHAR(50) NOT NULL, --length added to varchar
type_rdv VARCHAR(20),
state VARCHAR(20),
...
EDIT:
The reason you get "invalid character" error is that you cannot use "on delete no action" while creating a table with a constraint. Instead you should alter your table after to create your constraints with "on delete cascade" option. Probably like below ( you may need to drop your constraints first):
CREATE TABLE patients(
cin_pat int PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE typeRdv1(
id_type int PRIMARY KEY,
des VARCHAR(20)
);
CREATE TABLE rdv1(
id_rdv int PRIMARY KEY,
cin_pat VARCHAR(20),
date_rdv VARCHAR(50) NOT NULL,
type_rdv VARCHAR(20),
state VARCHAR(20)
);
alter table rdv1 add (
CONSTRAINT FK_cin_rdv FOREIGN KEY (cin_pat)
REFERENCES patients (cin_pat) ON DELETE cascade,
CONSTRAINT FK_id_type_rdv FOREIGN KEY (type_rdv)
REFERENCES typeRdv1 (id_type) ON DELETE cascade
);
Check this for more details: https://www.haidongji.com/2006/07/24/defining-no-action-foreign-key-constraints-in-oracle/comment-page-1/

SQl Server - unable to refer composite primary key

I am new to SQL server,I have the table with composite primary keys as follows:
CREATE TABLE Systems
(
Layer VARCHAR(25),
System_Name VARCHAR(25) ,
Sub_System_Name VARCHAR(25),
Q1_Scope VARCHAR(25),
Q2_Scope VARCHAR(25),
Q3_Scope VARCHAR(25),
Q4_Scope VARCHAR(25),
U20 VARCHAR(25),
Extracts_Requested VARCHAR(25),
Extracts_Received VARCHAR(25),
Control VARCHAR(25),
Extracts_Reviewed VARCHAR(25),
Control_Frequency VARCHAR(25),
System_Layer_Exists VARCHAR(25),
Notes VARCHAR(25),
Alias VARCHAR(25),
Extract_Requested_Date VARCHAR(25),
PRIMARY KEY (Layer,System_Name,Sub_System_Name)
);
Now I want to refer the columns like Layer,System_Name,Sub_System_Name in to another table say:
CREATE TABLE Owns_System
(
Name VARCHAR(25) FOREIGN KEY REFERENCES Employees(Name),
Layer VARCHAR(25) FOREIGN KEY REFERENCES Systems(Layer),
System_Name VARCHAR(25) FOREIGN KEY REFERENCES Systems(System_Name),
Sub_System_Name VARCHAR(25) FOREIGN KEY REFERENCES Systems(Sub_System_Name),
Role VARCHAR(25)
);
It shows me the error that the table Systems
"There are no primary/candidates keys in the table Systems".
Can anyone please suggest me what I am doing wrong here.
You don't want to declare 3 separate foreign key constraints - you need to use a single composite foreign key:
CREATE TABLE Owns_System
(
Name VARCHAR(25) FOREIGN KEY REFERENCES Employees(Name),
Layer VARCHAR(25),
System_Name VARCHAR(25),
Sub_System_Name VARCHAR(25),
Role VARCHAR(25),
constraint FK_Blah FOREIGN KEY (Layer,System_Name,Sub_System_Name)
REFERENCES Systems(Layer,System_Name,Sub_System_Name)
);
A FOREIGN KEY is a field (or collection of fields) in one table that
refers to the PRIMARY KEY in another table
So Before referring a column (PKey) in one table (Say TableA) as Foreign key in another Table (TableB), You need to make sure that the Column PKey is the Primary Key / Unique Key in TableA.
The Table Design Might be something like this
CREATE TABLE TableA
(
PKey INT PRIMARY KEY,
FullName VARCHAR(50),
Email VARCHAR(255) NOT NULL UNIQUE
)
CREATE TABLE TableB
(
Id INT PRIMARY KEY,
FKey INT FOREIGN KEY References TableA(PKey),
Email VARCHAR(255) FOREIGN KEY References TABLEA(Email)
)
Please Refer this W3 Schools link for more in detail on Foreign key

column "parent_id" referenced in foreign key constraint does not exist when creating SQL table

I am new in SQL and trying to understand Foreign key syntax. I know this was asked in multiple questions but each question I found did not seem to teach me what I am doing wrong here.
This is my SQL code:
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
CREATE TABLE Adult
(
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
CREATE TABLE Shop
(
id int primary key
);
CREATE TABLE Drink
(
name varchar(30) primary key
);
CREATE TABLE AlcoholicDrink
(
FOREIGN KEY (name) REFERENCES Drink(name)
);
CREATE TABLE NonAlcoholicDrink
(
FOREIGN KEY (name) REFERENCES Drink(name)
);
And this is the error I am getting:
ERROR: column "parent_id" referenced in foreign key constraint does not exist
SQL state: 42703
You need to add fields in your tables to make the reference. Something like this :
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
minor_id serial primary key,
parent_id int,
other_fields text etc.
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
This is simply the reason why it's not working.
Like this
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
parent_id int ,
FOREIGN KEY (parent_id) REFERENCES Customer(id)
);
To add an answer without just a code snippet:
You've got the right idea with FOREIGN KEY (parent_id) REFERENCES Customer(id). This bit adds a constraint or a "rule" to your table. This constraint ensures that parent_id holds a real id in your Customer table.
The error message told us that column "parent_id" referenced in foreign key constraint does not exist. The confusion comes, understandably, from mistaking the constraint declaration for a column declaration.
As others have pointed out, we need to both 10 declare the foreign key constraint and 2) declare the column.
CREATE TABLE Customer
(
id int primary key,
name varchar(30),
age int,
gender bool
);
CREATE TABLE Minor
(
parent_id int, -- Declare the column
FOREIGN KEY (parent_id) REFERENCES Customer(id) -- Declare the constraint
);