ErrorNo 150 mySQL - mysql

I have this problem when I try to create two foreign key in the same table
how I can fix it & thank you
create table employee(
employeeNumber INT (11) primary key,
);
create table projects(
projectNumber INT(11) primary key ,
);
create table workat(
wemployeeNumber INT(11),
wprojectNUmber INT (11),
primary key (wemployeeNumber,wprojectNUmber),
FOREIGN key workat(wemployeeNumber) references employee(employeeNumber),
FOREIGN key workat(wprojectNUmber) references projects(projectNumber)
);

Your FOREIGN KEY syntax looks off to me, and I don't know why you wrap the definition in workat. Try this version:
CREATE TABLE workat (
wemployeeNumber INT(11),
wprojectNUmber INT(11),
PRIMARY KEY (wemployeeNumber, wprojectNumber),
FOREIGN KEY (wemployeeNumber) REFERENCES employee (employeeNumber),
-- projects, not project
FOREIGN KEY (wprojectNUmber) REFERENCES projects (projectNumber)
);
Here is a demo showing that your code compiles after making the above changes:
Demo

Related

Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'projects_ibfk_1' in the referenced table 'employees'

CREATE DATABASE employeeDB;
USE employeeDB;
CREATE TABLE employees(
employeeid NUMERIC(9),
firstname VARCHAR(10),
lastname VARCHAR(20),
deptCode CHAR(5),
salary NUMERIC(9, 2),
PRIMARY KEY (employeeid)
);
CREATE TABLE projects(
projectid CHAR(8),
deptcode CHAR(5),
description VARCHAR(200),
startdate DATE,
stopdate DATE,
revenue NUMERIC(12, 2),
PRIMARY KEY (projectid),
FOREIGN KEY (deptcode) REFERENCES employees(deptCode)
);
CREATE TABLE departments(
code CHAR(5),
name VARCHAR(5),
managerid NUMERIC(9),
subdeptof CHAR(5),
PRIMARY KEY (code),
FOREIGN KEY (managerid) REFERENCES employees(employeeid),
FOREIGN KEY (subdeptof) REFERENCES projects(deptcode)
);
ALTER TABLE employees ADD FOREIGN KEY (deptCode) REFERENCES projects(deptcode);
Something wrong at the line CREATE TABLE projects(...). When I run the code in MySQL it give the Error Code 1822. What is the problem ? Any expert can help ?
You cant create foreign key with a non-primary key, and if you really want to create foreign key to non-primary key (column), the column must be indexed with a unique constraint on it.
So either create unique constraint on deptCode column, or reference by already existing primary key, or change the primary key.

Im getting an error code 1822 when i making the TAG table

create database priceTag;
use priceTag;
CREATE TABLE `ProductNumber` (
`Sku` INT auto_increment not null,
`Model` VARCHAR(100),
PRIMARY KEY (`Sku`)
);
ALTER TABLE ProductNumber AUTO_INCREMENT=60000;
CREATE TABLE `Manufacture` (
`Manufacture` VARCHAR(100),
`Model` VARCHAR(100),
`Category` VARCHAR(100),
PRIMARY KEY (`Model`)
);
CREATE TABLE `OpenBox` (
`Condtion` VARCHAR(100),
`LP` INT auto_increment not null,
`MissingItems` VARCHAR(100),
`Model_` VARCHAR(100),
FOREIGN KEY (`Model_`) REFERENCES `Manufacture`(`Model`),
PRIMARY KEY (`LP`)
);
ALTER TABLE OpenBox AUTO_INCREMENT=200000000;
CREATE TABLE `TAG` (
`SKU*` INT,
`Model*` VARCHAR(100),
`PRICE*` DECIMAL(10,2),
`LP*` INT,
`condtion*` VARCHAR(100),
FOREIGN KEY (`SKU*`) REFERENCES `ProductNumber`(`Sku`),
FOREIGN KEY (`Model*`) REFERENCES `Manufacture`(`Model`),
FOREIGN KEY (`LP*`) REFERENCES `OpenBox`(`LP`),
FOREIGN KEY (`condtion*`) REFERENCES `OpenBox`(`condtion`)
);
CREATE TABLE `Inventory` (
`INV` int,
`Sku!` int,
`Model!` VARCHAR(100),
FOREIGN KEY (`Sku!`) REFERENCES `ProductNumber`(`Sku`),
FOREIGN KEY (`Model!`) REFERENCES `Manufacture`(`Model`)
);
The column which you refer on in FOREIGN KEY (`condtion*`) REFERENCES `OpenBox`(`condtion`) (i.e. OpenBox.condtion) is not indexed.
Add needed unique index creation then create TAG table.
DEMO
I tested your code, and then ran SHOW ENGINE INNODB STATUS to get more detailed information about the error.
LATEST FOREIGN KEY ERROR
2021-12-02 09:47:16 0x700007565000 Error in foreign key constraint of table test2/tag:
FOREIGN KEY (condtion*) REFERENCES OpenBox(condtion)
):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
It's complaining about this foreign key:
FOREIGN KEY (`condtion*`) REFERENCES `OpenBox`(`condtion`)
The OpenBox.condtion column is not a primary key or unique key. Foreign keys must reference a key of the parent table.
You already have another foreign key in your TAGS table referencing the OpenBox table. Are you intending that the condtion column of the respective row be copied to the TAGS table? That's not how foreign keys are intended to be used.

many to many relationship in MySQL

I have this entity relationship:
I want to save information about the current team and the last teams, also I save the different positions in each team.
I create this tables:
table of player:
create table player(
codPlayer varchar(12) primary key,
name varchar(30) unique not null,
codCurrentTeam varchar(12),
position varchar(20),
foreign key (codCurrentTeam) references team(codTeam)
);
table of team:
create table team(
codTeam varchar(12) primary key,
name varchar(40) unique not null
);
table of lastTeam:
create table lastTeam(
startDate date,
finishDate date,
codTeam varchar(12),
codPlayer varchar(12),
constraint pkLastTeam primary key (codTeam,codPlayer),
foreign key (codTeam) references team(codTeam),
foreign Key (codPlayer) references jugador (codPlayer)
);
table of positions:
create table position(
codPlayer varchar(12),
codLastTeam varchar(24),
position varchar(20),
primary key (codPlayer, codLastTeam),
foreign key (codPlayer) references jugador(codPlayer),
foreign key (codLastTeam) references lastTeam(pkLastTeam)
);
I can create all tables except position, mysql returns the next error:
Error Code: 1215. Cannot add foreign key constraint
perhaps my diagram have some mistakes, but I don't know how to implement the design
the issue is you dont have
create table position "pkLastTeam" column in lastTeam t
codPlayer varchar(12),
codLastTeam varchar(24),
position varchar(20),
primary key (codPlayer, codLastTeam),
foreign key (codPlayer) references jugador(codPlayer),
foreign key (codLastTeam) references lastTeam(pkLastTeam)
);
I think I can solve the issue adding a new atributte codPlayer1 and changing pkLastTeam for codTeam, codPlayer1.
the table would look like this:
create table position(
codPlayer varchar(12),
codTeam varchar(12),
codPlayer1 varchar(12),
position varchar(20),
primary key (codPlayer, codTeam,codPlayer1),
foreign key (codPlayer) references player(codPlayer),
foreign key (codPlayer,codPlayer1) references lastTeam(codTeam,codPlayer)
);

how to make a primary key a foreign key in another table?

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),
)

Error 1215 MySQL Cannot add foreign key constraint

I am creating a WebApp for my stage tech crew to proper manage our hours worked. When I try to create a table 'Worked' I get a foreign key constraint error.
USE MySLS;
DROP TABLE IF EXISTS People;
DROP TABLE IF EXISTS Events;
DROP TABLE IF EXISTS Worked;
CREATE TABLE People
(pid int PRIMARY KEY,
pname varchar(255)
);
CREATE TABLE Events
(eid int PRIMARY KEY,
ename varchar(255),
edate DATE
);
CREATE TABLE Worked
(
pid int,
eid int,
hours decimal(3,1),
FOREIGN KEY (pid) REFERENCES People,
FOREIGN KEY (eid) REFERENCES Events
);
Data types are the same, I see no reason why this shouldn't work. Any thoughts?
shouldn't it go like this :
FOREIGN KEY(pid) REFERENCES People(pid)
FOREIGN KEY (eid) REFERENCES Events(eid)