I am translating a recursive relationship in a schema to sql and i am trying to reference two columns in a table to one column in another table
the Schema is as following :
Table: Request
(
mem_id1,
mem id2,
approved
)
desired foreign keys:
request.mem_id1 will reference member.mem_id
request.mem_id2 will reference member.mem_id
I have tried the following:
CREATE TABLE members (
mem_id INTEGER,
f_name VARCHAR(10),
l_name VARCHAR(10),
address VARCHAR(40),
name VARCHAR(20),
domain VARCHAR(15),
PRIMARY KEY(mem_id)
)
CREATE TABLE member_phone (
mem_id INTEGER,
phone_no INTEGER,
PRIMARY KEY (mem_id,phone_no),
FOREIGN KEY (mem_id) REFERENCES members (mem_id)
)
CREATE TABLE request (
mem_id1 INTEGER,
mem_id2 INTEGER,
approved BIT(1),
PRIMARY KEY (mem_id1,mem_id2),
FOREIGN KEY (mem_id1,mem_id2) REFERENCES members (mem_id)
)
I believe you want two separate foreign keys:
CREATE TABLE request (
mem_id1 INTEGER,
mem_id2 INTEGER,
approved BIT(1),
PRIMARY KEY (mem_id1, mem_id2),
FOREIGN KEY (mem_id1) REFERENCES members (mem_id),
FOREIGN KEY (mem_id2) REFERENCES members (mem_id)
)
Related
create database MALL;
use MALL;
create table customer (
customer_id int not null,
name varchar (20),
lastname varchar(20),
registration_date date,
primary key (customer_id)
);
create table inventory (
item_id int not null,
item_name varchar(20),
cost int,
primary key (item_id)
);
create table purchase (
purchase_date date,
purchase_count int,
customer_id int,
item_name varchar(20),
foreign key (customer_id) references customer(customer_id),
foreign key (item_name) references inventory(item_name)
);
I get this error
ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing
index for constraint 'purchase_ibfk_2' in the referenced table
'inventory'
Here:
create table purchase (
...
foreign key (item_name) references inventory(item_name)
)
The column referenced by the foreign key needs a unique index: inventory(item_name) does not have that. I would simply recommend referencing the primary key of inventory rather than some other column:
create table purchase (
purchase_date date,
purchase_count int,
customer_id int,
item_id int,
foreign key (customer_id) references customer(customer_id),
foreign key (item_id) references inventory(item_id)
);
I'm trying to make a many-to-many relationship between these two tables.
CREATE TABLE series (
title VARCHAR(30),
language VARCHAR(30),
year INT,
PRIMARY KEY (title)
);
CREATE TABLE actors (
name VARCHAR(30),
age INT,
country VARCHAR(30),
serie VARCHAR(30),
PRIMARY KEY (name , age , country , serie),
FOREIGN KEY (serie)
REFERENCES series (title)
);
I tried to create a separate table to form a many-to-many relation. Does this look correct?
CREATE TABLE series_actors_combined (
title VARCHAR(30),
name VARCHAR(30),
age INT,
country VARCHAR(30),
serie VARCHAR(30),
FOREIGN KEY (title)
REFERENCES series (title),
FOREIGN KEY (name)
REFERENCES actors (name),
FOREIGN KEY (age)
REFERENCES actors (age),
FOREIGN KEY (country)
REFERENCES actors (country),
FOREIGN KEY (serie)
REFERENCES actors (serie)
);
Your tables do not look right.
As a starter, you have a foreign key in the actors table that references the series, which basically defeats the purpose of the bridge table.
Also, the foreign keys from the bridge table to actors are not well-formed: you have one key per column, while you should have a unique, multi-column key to reference the actor. I would recomend having auto incremented primary keys rather than using these combinations of columns. This gives more flexibility to your design (in real life, two different series might have the same title), and makes it much easier to create foreign keys in the bridge table.
Consider:
create table series(
id int primary key auto_increment,
title varchar(30) unique,
language varchar(30),
year int
);
create table actors(
id int primary key auto_increment,
name varchar(30),
dob date, -- better than age
country varchar(30)
);
create table series_actors(
series_id int references series(id),
actor_id int references actors(id),
primary key (series_id, actor_id)
)
CREATE TABLE student
(
s_id INTEGER ,
s_name VARCHAR2(80) ,
s_email VARCHAR (80) ,
FOREIGN KEY (s_id) REFERENCES classes (s_id)
);
CREATE TABLE teacher
(
t_id INTEGER ,
t_name VARCHAR2(80) ,
t_email VARCHAR2 (80) ,
FOREIGN KEY (t_id) REFERENCES classes (t_id)
);
CREATE TABLE courses
(
c_id INTEGER ,
c_title VARCHAR2 (25) ,
c_describetion VARCHAR2 (25) ,
FOREIGN KEY (c_id) REFERENCES classes (c_id)
);
CREATE TABLE classes
(
cl_name VARCHAR2 (30) ,
cl_number INTEGER ,
t_id INTEGER ,
s_id INTEGER ,
c_id INTEGER ,
PRIMARY KEY (cl_number , t_id , s_id , c_id)
);
when i need to create these tables (student , teacher , courses) i have this Error :
Error report -
ORA-02270: no matching unique or primary key for this column-list
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
You are referencing the table "classes" from the other three. It's classes the one that should reference those. Put the FK in the columns of classes referencing the "id" field of the corresponding table. Also, I think you will need to define them as PK/UNIQUE.
You are creating the foreign keys the other way around. You want classes to have foreign keys that reference the other tables. You also need to define primary keys in the 3 master tables, so they can be referenced as foreign key.
Also, there is no VARCHAR2 datatype in MySQL (this is an Oracle-specific datatype): use `VARCHAR instead.
Consider:
CREATE TABLE student (
s_id INTEGER PRIMARY KEY,
s_name VARCHAR(80),
s_email VARCHAR(80)
);
CREATE TABLE teacher (
t_id INTEGER PRIMARY KEY,
t_name VARCHAR(80),
t_email VARCHAR(80)
);
CREATE TABLE courses (
c_id INTEGER PRIMARY KEY,
c_title VARCHAR(25),
c_description VARCHAR(25)
);
CREATE TABLE classes (
cl_name VARCHAR(30),
cl_number INTEGER,
t_id INTEGER,
s_id INTEGER,
c_id INTEGER,
PRIMARY KEY (cl_number , t_id , s_id , c_id),
FOREIGN KEY (s_id) REFERENCES student (s_id),
FOREIGN KEY (t_id) REFERENCES teacher (t_id),
FOREIGN KEY (c_id) REFERENCES courses (c_id)
);
Demo on DB Fiddle
If you are running Oracle (like the error message in your question indicates) rather than MySQL (which you tagged your question) with, then you want VARCHAR2 instead of VARCHAR (which is deprecated). The rest of the script remains the same:
CREATE TABLE student (
s_id INTEGER PRIMARY KEY,
s_name VARCHAR2(80),
s_email VARCHAR2(80)
);
CREATE TABLE teacher (
t_id INTEGER PRIMARY KEY,
t_name VARCHAR2(80),
t_email VARCHAR2(80)
);
CREATE TABLE courses (
c_id INTEGER PRIMARY KEY,
c_title VARCHAR2(25),
c_description VARCHAR2(25)
);
CREATE TABLE classes (
cl_name VARCHAR2(30),
cl_number INTEGER,
t_id INTEGER,
s_id INTEGER,
c_id INTEGER,
PRIMARY KEY (cl_number , t_id , s_id , c_id),
FOREIGN KEY (s_id) REFERENCES student (s_id),
FOREIGN KEY (t_id) REFERENCES teacher (t_id),
FOREIGN KEY (c_id) REFERENCES courses (c_id)
);
Demo on DB Fiddle
These are my tables. I have a problem with the last one (Inscription) it doesn't accept CodeProjet as a foreign key. The error says table (Projet) doesn't have a primary key called CodeProjet but it does! I have used every trick that I know and nothing. I altered the table to add constraint, etc. Still nothing. I always get the same error. Here are the tables:
create database Gestion_Stages_Employe
create table Employe
(
NumEmploye int primary key,
NomEmploye varchar(15),
PrenomEmploye varchar(15),
SexeEmploye varchar(10),
DNaissEmploye date,
FonctionEmploye varchar(20)
)
create table TypeProjet
(
TypeProjet varchar(20) primary key,
libelleProjet varchar(20),
DureeProjet date,
)
create table Projet
(
CodeProjet int,
TypeProjet varchar(20),
DateDebut Date,
DateFin Date,
Constraint Pk_CodeProj primary key (CodeProjet,TypeProjet),
Constraint FK_TypeProj foreign key (TypeProjet) references TypeProjet(TypeProjet),
)
create table Inscription
(
NumEmploye int foreign key references Employe(NumEmploye),
CodeProjet int foreign key references Projet(CodeProjet),
dateiscription Date,
primary key (NumEmploye,CodeProjet),
)
As was mentioned in the comment, the Primary Key on the Projet table is a "composite key" (multiple columns are required to enforce uniqueness).
As a result, for your foreign key to work, you need to either remove "TypeProjet" from the primary key of Project (assuming CodeProjet is sufficient to uniquely identify a Projet), or you need to add TypeProjet to the Inscription table and define your foreign key with both columns, for example:
create table Inscription (
NumEmploye int foreign key references Employe(NumEmploye),
CodeProjet int,
dateiscription Date,
TypeProjet varchar(20),
Constraint fk_Inscription_project Foreign key (CodeProjet,TypeProjet) references Projet(CodeProjet,TypeProjet),
primary key (NumEmploye,CodeProjet),
)
i am using sequel pro on MACOSX and i've defined the table containing the foreign key and it still gives me this error Can't create table (errno: 150)
CREATE TABLE members (
mem_id INTEGER,
f_name VARCHAR(10),
l_name VARCHAR(10),
address VARCHAR(40),
name VARCHAR(20),
domain VARCHAR(15),
PRIMARY KEY(mem_id)
)
CREATE TABLE member_phone (
mem_id INTEGER,
phone_no INTEGER,
PRIMARY KEY (mem_id,phone_no),
FOREIGN KEY (mem_id) REFERENCES members
)
You need to specify which reference column is the target:
CREATE TABLE members (
mem_id INTEGER,
f_name VARCHAR(10),
l_name VARCHAR(10),
address VARCHAR(40),
name VARCHAR(20),
domain VARCHAR(15),
PRIMARY KEY(mem_id)
)
CREATE TABLE member_phone (
mem_id INTEGER,
phone_no INTEGER,
PRIMARY KEY (mem_id,phone_no),
FOREIGN KEY (mem_id) REFERENCES members (mem_id)
)
Your foreign key in member_phone needs to reference the column within the members table:
FOREIGN KEY (mem_id) REFERENCES members(mem_id)