MYSQL Err #1072 doesn't exist in table when specifying FK - mysql

I am new to mysql, so sorry if this is a trivial problem. Problem is when I create the second table I get the error:
key iplogger_redirect_key doesn't exist.
Here's my code:
DROP DATABASE iploggerdb;
CREATE DATABASE iploggerdb;
USE iploggerdb;
CREATE TABLE iplogger_info_table(
iplogger_redirect_key CHAR(8) PRIMARY KEY,
access_key CHAR(8) NOT NULL,
creation_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
creator_ip VARCHAR(45),
original_url VARCHAR(2000)
);
CREATE TABLE logs_table(
id INT(11) AUTO_INCREMENT,
iplogger_redirect_key FOREIGN KEY (iplogger_redirect_key) REFERENCES iplogger_info_table(iplogger_redirect_key),
logged_ip VARCHAR(45),
logged_dns_server VARCHAR(45),
logged_ip_country_city VARCHAR(200),
logged_hostname VARCHAR(200),
logged_user_agent VARCHAR(150),
logged_referrer VARCHAR(2000),
logged_ip_timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

You need to correct FOREIGN KEY part:
CREATE TABLE logs_table(
id INT(11) AUTO_INCREMENT PRIMARY KEY,
iplogger_redirect_key CHAR(8),
CONSTRAINT fk_name FOREIGN KEY (iplogger_redirect_key)
REFERENCES iplogger_info_table(iplogger_redirect_key),
...
)
DBFiddle Demo

Related

I am trying to create a table in MySQL but I get the following error:

"Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'users_ibfk_1' in the referenced table 'users'"
Here's what I have so far:
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books`(
`title` VARCHAR(50) NOT NULL,
`year` INT,
`edition` VARCHAR(10),
`authorLast` VARCHAR(10),
`authorFirst` VARCHAR(10),
`major` VARCHAR(4),
`courseNo` INT,
`profLast` VARCHAR(10),
`profFirst` VARCHAR(10),
`quality` VARCHAR(10) NOT NULL,
`price` FLOAT NOT NULL,
`user` VARCHAR(10) NOT NULL UNIQUE,
`ISBN` BIGINT NOT NULL,
PRIMARY KEY (`ISBN`)
);
-- Users Table
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users`(
`nameFirst` VARCHAR(10) NOT NULL,
`nameLast` VARCHAR(10),
`user` VARCHAR(10) NOT NULL UNIQUE,
`email` VARCHAR(20) NOT NULL,
`phoneNo` INT,
`major` VARCHAR(10),
`major2` VARCHAR(10),
PRIMARY KEY (`email`),
FOREIGN KEY (`user`) REFERENCES `users` (`user`) ON DELETE CASCADE ON UPDATE CASCADE
);
I've tried making the attributes unique because I read somewhere that that could be the problem but that doesn't seem to be my issue. I'm coming back to MySQL for the first time in a while so this can be a really simple error I'm just not seeing but I would appreciate the help! Thanks.
Your foreign key in the users table is referring same user column in same table

Cannot add foreign key restraint error in MySQL?

I keep getting the same error in MySQL and i'm not sure what I missing here.
I am very new to working with MySQL, so I am not sure if I need to add a constraint maybe? If I do not need a constraint, is there another way to go about defining the relationships? Below is my syntax:
DROP TABLE IF EXISTS `art`;
CREATE TABLE `art` (
`art_id` INT NOT NULL,
`art_name` VARCHAR(45) NULL,
`artist_id` INT(11) NULL,
`location_id` INT(11) NULL,
`employee_id` INT(11) NULL,
`art_received` DATE NULL,
primary key (`art_id`),
FOREIGN KEY (`artist_id`) REFERENCES artist(`artist_id`),
FOREIGN KEY (`location_id`) REFERENCES location(`location_id`),
FOREIGN KEY (`employee_id`) REFERENCES employee(`employee_id`)
);
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`employee_id` int(11),
`employee_userid` varchar(45),
`employee_fname` varchar(45),
`employee_lname` varchar(45),
`employee_phone` varchar (10),
primary key (`employee_id`)
);
DROP TABLE IF EXISTS `artist`;
CREATE TABLE `artist` (
`artist_id` int(11),
`artist_first` varchar(45),
`artist_last` varchar(45),
`artwork_title` varchar(45),
primary key (`artist_id`)
);
DROP TABLE IF EXISTS `location`;
CREATE TABLE `location` (
`location_id` int(11),
`location_name` varchar(45),
`start_date` date,
`end_date` date,
primary key (`location_id`)
);
simply switch the order of which table gets created first. when you created
table art the other three table still have not be created so it doesn't know what artist(artist_id),location(location_id),employee(employee_id)
are.
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
`employee_id` int(11),
`employee_userid` varchar(45),
`employee_fname` varchar(45),
`employee_lname` varchar(45),
`employee_phone` varchar (10),
primary key (`employee_id`)
);
DROP TABLE IF EXISTS artist;
CREATE TABLE `artist` (
`artist_id` int(11),
`artist_first` varchar(45),
`artist_last` varchar(45),
`artwork_title` varchar(45),
primary key (`artist_id`)
);
DROP TABLE IF EXISTS `location`;
CREATE TABLE `location` (
`location_id` int(11),
`location_name` varchar(45),
`start_date` date,
`end_date` date,
primary key (`location_id`)
);
DROP TABLE IF EXISTS `art`;
CREATE TABLE `art` (
`art_id` INT NOT NULL,
`art_name` VARCHAR(45) NULL,
`artist_id` int(11) NULL,
`location_id` int(11) NULL,
`employee_id` int(11) NULL,
`art_received` DATE NULL,
primary key (`art_id`),
FOREIGN KEY (`artist_id`) REFERENCES artist(`artist_id`),
FOREIGN KEY (`location_id`) REFERENCES location(`location_id`),
FOREIGN KEY (`employee_id`) REFERENCES employee(`employee_id`)
);
And welcome to StackOverflow. if you find this answer or any other answer helpful please mark it as the solution. That way would help the community and if fellow programmers run into the same problem as you did in the future they can find the solution easily.

How to change sql code to mysql

i have this script running on sql, but i need to optimize it to mysql using the workbench, but it shows me an error in the foreign key and i don't know how to change it.
create table EspecialidadesMedicas(
IdEspecialidad int(4) primary key,
DescripcionEspecialidad varchar(30));
create table Doctores(
IdDoctor int(5) PRIMARY KEY,
NombreDoctor varchar(30),
Salario int(12.2),
Especialidad int(4),citascitas
Especialidad FOREIGN KEY references EspecialidadesMedicas(IdEspecialidad));
The following is working syntax:
create table EspecialidadesMedicas (
IdEspecialidad int(4) primary key,
DescripcionEspecialidad varchar(30)
);
create table Doctores(
IdDoctor int(5) PRIMARY KEY,
NombreDoctor varchar(30),
Salario int(12),
Especialidad int(4),
citascitas varchar(30),
constraint fk_Especialidad FOREIGN KEY (Especialidad) references EspecialidadesMedicas(IdEspecialidad)
);

PHP6 chapter 3 - #1215 - Cannot add foreign key constraint

I try to learn from book Professional PHP6 and in chapter 3 I need to create tables:
CREATE TABLE `entity` (
`entityid` SERIAL PRIMARY KEY NOT NULL,
`name1` varchar(100) NOT NULL,
`name2` varchar(100) NOT NULL,
`type` char(1) NOT NULL
);
and
CREATE TABLE `entityaddress` (
`addressid` SERIAL PRIMARY KEY NOT NULL,
`entityid` int,
`saddress1` varchar(255),
`saddress2` varchar(255),
`scity` varchar(255),
`cstate` char(2),
`spostalcode` varchar(10),
`stype` varchar(50),
CONSTRAINT `fk_entityaddress_entityid`
FOREIGN KEY (`entityid`) REFERENCES `entity`(`entityid`)
);
result is error: #1215 - Cannot add foreign key constraint
I check in original code for that book and there is sql file, which give me same error.
...is there anything wrong, or is something with my db in xampp?
I try to create only tables and then create relation in designers, but I got program error...
I set InnoDB engine.
Thanks for any suggestion.
The type of entityid has to be the same in both tables. SERIAL is an alias for BIGINT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE. So change
`entityid` int,
to
`entityid` BIGINT UNSIGNED NOT NULL,

SQL error: #1005 - Can't create table

Here is my code:
create table if not exists Pelajar (
no_pelajar char(7) not null,
nama varchar(30) not null,
alamat varchar(50) not null,
no_telepon varchar(10),
grade integer not null,
primary key (no_pelajar)
);
create table if not exists Kelas (
kode_kelas char(5) not null,
no_pengajar char(9) not null,
no_pelajaran char(4) not null,
ruang integer not null,
waktu char(5) not null,
primary key (kode_kelas)
);
create table if not exists Pelajaran (
no_pelajaran char(4) not null,
grade integer not null,
subjek varchar(30) not null,
primary key (no_pelajaran)
);
create table if not exists Pengajar (
no_pengajar char(9) not null,
nama varchar(30) not null,
alamat varchar(50) not null,
no_telepon varchar(10) not null,
primary key (no_pengajar)
);
create table if not exists Pembayaran (
no_pembayaran char(11) not null,
status_pembayaran varchar(11) not null,
tgl_pembayaran char(8) not null,
primary key (no_pembayaran)
);
create table if not exists KartuAnggota (
no_pelajar char(7) not null,
kode_kelas char(5) not null
);
create table if not exists SlipPembayaran (
no_pembayaran char(11) not null,
no_pelajar char(7) not null
);
alter table SlipPembayaran
add foreign key (no_pembayaran)
references Pembayaran(no_pembayaran);
alter table SlipPembayaran
add foreign key (no_pelajar)
references Pelajar(no_pelajar);
alter table KartuAnggota
add foreign key (kode_kelas)
references Kelas(kode_kelas);
alter table KartuAnggota
add foreign key (no_pelajar)
references Pelajar(no_pelajar);
alter table Kelas
add foreign key (no_pelajaran)
references Pelajaran(no_pelajaran);
alter table Kelas
add foreign key (no_pengajar)
references Pengajar(no_pengajar);
=================================================================================
after running the code, i got the message:
SQL query:
ALTER TABLE KartuAnggota ADD FOREIGN KEY ( kode_kelas ) REFERENCES Kelas( kode_kelas ) ;
MySQL said:
#1005 - Can't create table 'bimbel.#sql-2f2c_1c6' (errno: 150)
can you explain that?
If you re-create a table that was dropped, it must have a definition that conforms to the foreign key constraints referencing it. It must have the right column names and types, and it must have indexes on the referenced keys, as stated earlier. If these are not satisfied, MySQL returns error number 1005 and refers to error 150 in the error message.