MySQL Error Code 1072- Can't create a Foreign Key - mysql

I created this table:
create table users
(
id_user int not null auto_increment,
username varchar (255) not null,
email varchar (255) not null,
CONSTRAINT id_user PRIMARY KEY (id_user)
);
And now I am trying to create this one:
create table games
(
id_game int not null auto_increment,
game_name varchar (255) not null,
preco decimal (6,2),
autor varchar (255) not null,
CONSTRAINT id_game PRIMARY KEY (id_game),
CONSTRAINT autor FOREIGN KEY (username) REFERENCES users (username)
);
The idea is to make the column username from the table users a foreign key in the table games as "autor". However, I am getting this error:
Error 1072. Key Column "username" doesn't exist in table.

Related

#1005 - Can't create table `musicplayer`.`Albums` (errno: 150 "Foreign key constraint is incorrectly formed")

I'm trying to define some tables with SQL, but when I execute it I have an error. Below there is my code and the error which I receive.
CREATE TABLE Artists
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
description VARCHAR(255)
);
CREATE TABLE Albums
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
releasedate DATE,
artistid INT,
genreid INT,
picture VARCHAR(255),
CONSTRAINT `fk_albums_artists`
FOREIGN KEY (artistid) REFERENCES Artists(id),
CONSTRAINT `fk_albums_genres`
FOREIGN KEY (genreid) REFERENCES Genres(id)
);
CREATE TABLE Tracks
(
id INT(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL,
playtime INT NOT NULL,
albumid INT,
CONSTRAINT `fk_tracks_album`
FOREIGN KEY (albumid) REFERENCES Albums(id)
);
CREATE table Genres
(
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
name VARCHAR(255) NOT NULL
);
Error in my SQL query:
MySQL said: Documentation
1005 - Can't create table musicplayer.Albums (errno: 150 "Foreign
key
constraint is incorrectly formed") (Details…)
Declare the tables in order, so the tables are defined before they are referenced:
CREATE TABLE Artists (
id int AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null,
description varchar(255)
);
CREATE table Genres(
id int AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null
);
CREATE TABLE Albums (
id int AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null,
releasedate date,
artistid int,
genreid int,
picture varchar(255),
CONSTRAINT `fk_albums_artists` FOREIGN KEY (artistid) REFERENCES Artists(id),
CONSTRAINT `fk_albums_genres` FOREIGN KEY (genreid) REFERENCES Genres(id)
);
CREATE TABLE Tracks(
id int(11) AUTO_INCREMENT PRIMARY KEY NOT null,
name varchar(255) not null,
playtime int not null,
albumid int,
CONSTRAINT `fk_tracks_album` FOREIGN KEY (albumid) REFERENCES Albums(id)
);
Here is a db<>fiddle.
You can still have tables that reference each other. However, you will need to declare such foreign key constraints using ALTER TABLE rather than in the CREATE TABLE statement. However, you do not need to do this for these table definitions.

MySQL Error "Can't create table" Foreign Key Constraint

I'm trying to create a database using MySQL and having difficulties with my code. I created the base table with one attribute that I plan on using as a foreign key in another table but an error message comes up saying I cannot create the table. I only want help with creating that table as I know how to insert data.
create database aerogames_table;
CREATE TABLE
O_DETAILS
(
B_Number INT NOT NULL,
B_Name VARCHAR(60) NOT NULL,
Order_ID INT NOT NULL,
Order_CName VARCHAR(50) NOT NULL
)
engine=innodb;
CREATE TABLE
P_DETAILS
(
PRO_ID INT NOT NULL PRIMARY KEY,
Order_ID INT NOT NULL,
CONSTRAINT fk_OrdID_this FOREIGN KEY (Order_ID) REFERENCES O_DETAILS(Order_ID),
PRO_Seller VARCHAR (50) NOT NULL,
PRO_Name VARCHAR (50) NOT NULL,
PRO_Year INT NOT NULL,
PRO_Price INT NOT NULL
)
engine=innodb;
1005 - Can't create table 'aerogames_table.p_details' (errno: 150)
Due to this https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html
[CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
Foreign key need to reference to index column (no need to be primary key) so you need to create index on O_DETAILS.Order_ID like this
CREATE TABLE
O_DETAILS
(
B_Number INT NOT NULL,
B_Name VARCHAR(60) NOT NULL,
Order_ID INT NOT NULL,
Order_CName VARCHAR(50) NOT NULL,
KEY (Order_ID)
)
engine=innodb;
CREATE TABLE
P_DETAILS
(
PRO_ID INT NOT NULL PRIMARY KEY,
Order_ID INT NOT NULL,
CONSTRAINT fk_OrdID_this FOREIGN KEY (Order_ID) REFERENCES O_DETAILS(Order_ID),
PRO_Seller VARCHAR (50) NOT NULL,
PRO_Name VARCHAR (50) NOT NULL,
PRO_Year INT NOT NULL,
PRO_Price INT NOT NULL
)
engine=innodb;
Certainly if you make O_DETAILS.Order_ID to be primary key, it will also create for you and this works

Error 1072 MySQL

I keep on getting error 1072 when trying to add a foreign key attribute and linking my attribute to another table. I've tried different ways of writing this but I keep on getting the message that "department_id doesn't exist in the table". If you could give me a hand that would be awesome! thank you so so much!
Here is the my code:
Alter table employee
add constraint department_fk
foreign key ( department_id)
references department(department_id);
And here is the rest of my tables:
Create table Department (
department_id integer auto_increment primary key not null,
department_name varchar (50) not null,
Office_number varchar(50) not null,
phone char (20) not null
);
Create table employee (
employee_id integer auto_increment primary key not null,
first_name varchar (25) not null,
last_name varchar (25) not null,
phone char(20) not null,
email varchar (100) not null,
salary decimal (10,2)
);
Create table project (
project_id integer auto_increment primary key not null,
max_hours time not null,
start_date datetime not null,
end_date datetime not null,
Project_lead integer,
Constraint project_fk1
foreign key (employee_id)
references employee(employee_id),
Department_id integer,
Constraint project_fk2
foreign key (department_id)
references department (department_id)
);
Create table assignment (
employee_id integer not null,
project_id integer not null,
hours_worked time not null,
primary key (employee_id, project_id),
constraint assignment_fk1
foreign key(employee_id)
references employee(employee_id),
constraint assignment_fk2
foreign key (project_id)
references project(project_id)
);
Alter table project
drop foreign key department_id;
Thank you in advance!
You need a Foreign key column in your employee table:
Create table employee (
employee_id integer auto_increment primary key not null,
first_name varchar (25) not null,
last_name varchar (25) not null,
phone char(20) not null,
email varchar (100) not null,
dept_id integer,
salary decimal (10,2)
);
Alter table employee
add constraint department_fk
foreign key ( dept_id)
references department(department_id);
Trust the message ;) - the column "department_id" doesn't exist in your employee table!

errno 150 mySQL foreign key

This SQL is giving me Errno 150 when i'm trying to create the second table with the foreign key on UserID
Can anyone please advice me what am i doing wrong?
CREATE DATABASE IF NOT EXISTS OTA;
USE OTA;
CREATE TABLE IF NOT EXISTS Users
(
UserID int AUTO_INCREMENT NOT NULL PRIMARY KEY,
UserName varchar(255) NOT NULL,
Email varchar(255) UNIQUE ,
PW varchar(255),
PN varchar(255),
Admin BIT
);
CREATE TABLE IF NOT EXISTS Notes
(
UID int AUTO_INCREMENT NOT NULL PRIMARY KEY,
Note varchar (255) NOT NULL,
c_Date Date NOT NULL,
c_text varchar (255) NOT NULL,
FOREIGN KEY (UID) REFERENCES Persons(UserID)
);
Sorry for being a duplicate question but i can't find my answer between the related ones.
The problem is that you are trying to reference Persons(UserID) when your first table is called Users. Try this for your key:
FOREIGN KEY (UID) REFERENCES Users(UserID)
However, you should have a separate column for the note ID.
Try declaring the primary key after:
CREATE TABLE Orders
(O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id))
Also, you auto-incremented both UID and UserID. This is fine if they are the same length, but if you try to reference a foreign key value that doesn't exist, you will get an error.

sql Error 1064 (42000) Syntax Error

CREATE TABLE IF NOT EXISTS message(
id INT NOT NULL auto_increment,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY ('id')
FOREIGN KEY ('userid') REFERENCES users('id'));
I was just wondering if someone could help me in identifying a syntax error as I can not create a table.
Try to put , after the primary key declaration.
Update: I guess it should be
CREATE TABLE IF NOT EXISTS message (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));
I'm assuming this is for MS SQL Server? If you get MS SQL Server Studio, you can script stuff which gives you an idea:
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[message]') AND type in (N'U'))
CREATE TABLE message(
id INT IDENTITY NOT NULL,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (userid) REFERENCES users(id))
GO
You query should be as below
CREATE TABLE IF NOT EXISTS message (
id INT auto_increment PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));
Provided you have id as a primary key in users table.
CREATE TABLE users (id INT PRIMARY KEY)
Your query should look like this:
CREATE TABLE IF NOT EXISTS message(
id INT NOT NULL auto_increment,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
PRIMARY KEY ('id'),
FOREIGN KEY ('userid') REFERENCES users('id')
) Engine=InnoDB;
Note the , after PRIMARY KEY ('id').
Small trick
You don't have to specify foreign keys in table definitions. It's practical when you do it like this (because dump may export tables in order that foreign keys will fail on creating/inserting):
CREATE TABLE 1; -- With references to table 2
CREATE TABLE 2;
INSERT INTO 1;
INSERT INTO 2;
ALTER TABLE 1 ADD FOREIGN KEY (user_id) REFERENCES 2 2(id);
Try to change the name of your table, May be message is an in-built keyword in MySQL.
Update to this: I guess it should be
CREATE TABLE IF NOT EXISTS myMessage (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
userid INT NOT NULL,
date Date NOT NULL,
text varchar(255) NOT NULL,
FOREIGN KEY (userid) REFERENCES users(id));