SQL error: #1005 - Can't create table - mysql

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.

Related

Error number: 3780 Referencing column '%s' and referenced column '%s' in foreign key constraint '%s' are incompatible

DROP DATABASE IF EXISTS ProviderPatients;
CREATE DATABASE ProviderPatients;
USE ProviderPatients;
CREATE TABLE IF NOT EXISTS Date_Dim
(
Date_ID integer not null,
Date_ date,
Full_Date_Des varchar(25) not null,
Day_Of_Week int(11) not null,
Calender_Year int(11) not null,
Weekday_Indicator int(11) not null,
PRIMARY KEY (Date_ID)
);
CREATE TABLE IF NOT EXISTS Insurer_DIM
(
Insurer_ID int(11) not null,
Insurer_Name varchar(25) not null,
Line_Of_Buissness varchar(25) not null,
PRIMARY KEY (Insurer_ID)
);
CREATE TABLE IF NOT EXISTS Member_DIM
(
Member_ID int(11) not null,
Member_Name varchar(25) not null,
Age int(11) not null,
Ethnicity varchar(25) not null,
Health_Condition varchar(25) not null,
PRIMARY KEY (Member_ID)
);
CREATE TABLE IF NOT EXISTS Geography_Dim
(
Geography_ID varchar(25) not null,
Country varchar(25) not null,
State varchar(10) not null,
State_Code int(11) not null,
County_Code int(11) not null,
PRIMARY KEY (Geography_ID)
);
CREATE TABLE Provider_Dim
(
Provider_ID int(11) not null,
Provider_Name VARCHAR(45) NOT NULL,
Gender Varchar(25) Not Null,
NPI Varchar(25) Not Null,
Credential Varchar(25) Not Null,
PRIMARY KEY(Provider_ID)
);
CREATE TABLE Eval_Fact_Table
(
Date_ID int(11) not null,
Member_ID int(11) not null,
Provider_ID int(11) not null,
Insurer_ID int(11) not null,
Geography_ID int(11) not null,
Num_Visits int(11) not null,
Eval_Costint int(11) not null,
Eval_Start date not null,
Eval_End date not null,
FOREIGN KEY (Date_ID)
REFERENCES Date_Dim (Date_Id) ON DELETE RESTRICT,
FOREIGN KEY (Member_ID)
REFERENCES Member_Dim (Member_ID) ON DELETE RESTRICT,
FOREIGN KEY (Geography_ID)
REFERENCES Geography_Dim (Geography_ID) ON DELETE RESTRICT,
FOREIGN KEY (Provider_ID)
REFERENCES Proveider_Dim (Provider_ID) ON DELETE RESTRICT,
FOREIGN KEY (Insurer_ID)
REFERENCES Insurer_Dim (Insurer_ID) ON DELETE RESTRICT
);
Error number: 3780; Symbol: ER_FK_INCOMPATIBLE_COLUMNS; SQLSTATE: HY000
Message:Error Code: 3780. Referencing column 'Geography_ID' and referenced column 'Geography_ID' in foreign key constraint 'eval_fact_table_ibfk_3' are incompatible.
Error Referencing column 'Geography_ID' and referenced column 'Geography_ID' in foreign key constraint 'eval_fact_table_ibfk_3' are incompatible.
is quite clear, columns are incompatible:
CREATE TABLE IF NOT EXISTS Geography_Dim (
Geography_ID varchar(25) not null,
CREATE TABLE Eval_Fact_Table(
... truncated
Geography_ID int(11) not null,
Make them of same type or remove foreign key constraint.
You can read more about foreign key constraints in documentation, most interesting part is
Corresponding columns in the foreign key and the referenced key must
have similar data types.
That is not true in your case : varchar(25) vs. int(11)
I tried all other ways but I found this useful as it worked for me. It is applicable also to the new MySQL version(v8.0)
CREATE TABLE Customers(
Customers_ID INT(10) PRIMARY KEY AUTO_INCREMENT,
Customers_name varchar(40) not null,
Customers_phone INT(10) not null,
Customers_email varchar(40) not null,
date_became_customer DATE not null,
login varchar(40) not null,
password varchar(40) not null,
other_details varchar(40) not null,
Customer_Types_code int(10) not null
REFERENCES Customer_types(Customer_Types_code));
Just change Geograpy_ID on Geography_Dim table to Geography_ID int(11) or change Geograpy_ID on Eval_Fact_Table to Geography_ID varchar(25) to solve the problem.
Error number: 3780 is due to incompatible foreign keys, the datatype of foreign key in each table must be same .
You could also not edit the primary key column in one table if it is a foreign key in another table.
For editing first drop the foreign key constraint in child table and then edit the primary key in the parent table and then again add constraints to child table.
HERE: Geography_ID varchar(25) vs Geography_ID int(11) are incompatible
First drop or alter this
FOREIGN KEY (Geography_ID) REFERENCES Geography_Dim (Geography_ID)
Geography_ID int(11) NOT NULL, and then add new column:
FOREIGN KEY (Geography_ID) REFERENCES Geography_Dim (Geography_ID)
with Geography_ID varchar(25)

MARIADB: errno: 150 "Foreign key constraint is incorrectly formed

Im trying to create 3 different tables in mariadb. I am using the exact same code I used in my localhost and it worked.
The tables name are: location, artist and murals.
I first create the location table, then I create the artist table and finally I try create the murals table because this is where my foreign keys will be, but I keep getting the following error:
ERROR 1005 (HY000): Can't create table KOMA.murals (errno: 150 "Foreign key constraint is incorrectly formed")
Creating Location Table
Create TABLE location(
l_id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
country varchar(255),
city varchar(255) NOT NULL,
address varchar(255),
a_number int(10),
zipcode int(5)
);
Creating artist Table
Create TABLE artist(
a_id int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(255) NOT NULL );
Creating murals Table
CREATE table murals (
m_id INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
title varchar(255) NOT NULL,
imageurl varchar(255) NOT NULL,
about varchar(255) NOT NULL,
year INT(4),
a_id INT(11),
l_id INT(11),
FOREIGN KEY (a_id) REFERENCES artist,
FOREIGN KEY (l_id) REFERENCES location
);
I would like to be able to create the last table with my foreign keys
When defining a foreign key, you need to point to the "parent" table and the referenced column; MySQL doesn't assume that the column has the same name.
CREATE TABLE `murals` (
`m_id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`imageurl` VARCHAR(255) NOT NULL,
`about` VARCHAR(255) NOT NULL,
`year` INT(4),
`a_id` INT(11),
`l_id` INT(11),
FOREIGN KEY (`a_id`) REFERENCES `artist`(`a_id`),
FOREIGN KEY (`l_id`) REFERENCES `location`(`l_id`)
);

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

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

Error with creating table

This is my statement, I'm getting an error on Customer_T. The error states:
"01:22:05 DROP TABLE customer_t Error Code: 1051. Unknown table 'energyefficient.customer_t' 0.016 sec"
CREATE TABLE Customer_t
(CustomerID INT NOT NULL,
Name VARCHAR(45) NOT NULL,
Address VARCHAR(256) ,
Email VARCHAR(100) ,
Phone VARCHAR(16) ,
CONSTRAINT PK_CustomerID PRIMARY KEY (CustomerID));
CREATE TABLE Order_t
(OrderID INT NOT NULL,
OrderDate DATE NULL,
CustomerID INT NOT NULL,
CONSTRAINT PK_OrderID PRIMARY KEY (OrderID),
CONSTRAINT FK_CustomerID FOREIGN KEY (CustomerID) REFERENCES customer_t(CustomerID));
CREATE TABLE Equipment
(EquipmentID INT NOT NULL,
EquipmentType VARBINARY(12) ,
YearOfManufacture INT ,
Cost DECIMAL(9,2) ,
Maker VARCHAR(45) ,
Model VARCHAR(45) ,
CustomerID INT ,
CONSTRAINT EquipmentID_PK PRIMARY KEY (EquipmentID),
CONSTRAINT CustomerID_FK FOREIGN KEY (CustomerID) REFERENCES Customer_t(CustomerID));
CREATE TABLE Order_Line_t
(OrderLineID INT NOT NULL,
OrderID INT NOT NULL,
EquipmentID INT NOT NULL,
OrderLineCost DECIMAL(9,2) ,
CONSTRAINT OrderLineID_PK PRIMARY KEY (OrderLineID),
CONSTRAINT OrderID_FK1 FOREIGN KEY (OrderID) REFERENCES Order_t(OrderID),
CONSTRAINT EquipmentID_FK2 FOREIGN KEY (EquipmentID) REFERENCES Equipment_t(EquipmentID));
CREATE TABLE MaintenanceSchedule_t
(MaintenanceID INT NOT NULL,
MaintenanceType VARCHAR(45) NOT NULL,
Schedule_Date DATE NOT NULL,
EquipmentID INT NOT NULL,
ServiceID INT ,
CONSTRAINT MaintenanceID_PK PRIMARY KEY (MaintenanceID),
CONSTRAINT EquipmentID_FK3 FOREIGN KEY (EquipmentID) REFERENCES Equipment(EquipmentID));
CREATE TABLE Service
(ServiceID INT NOT NULL,
EstimatedCost DECIMAL(9,2) NOT NULL,
Status VARCHAR(16) NOT NULL,
ServiceDate DATE ,
EquipmentID INT NOT NULL,
EmployeeID INT NOT NULL,
ActualCost DECIMAL(9,2) ,
ServiceType VARCHAR(45) NOT NULL,
Notes VARCHAR(2000) ,
CONSTRAINT ServiceID_PK PRIMARY KEY (ServiceID),
CONSTRAINT EquipmentID_FK FOREIGN KEY (EquipmentID) REFERENCES Equipment(EquipmentID));
CREATE TABLE Employee_t
(EmployeeID INT NOT NULL,
AnnualSalary DECIMAL(9,2) ,
Name VARCHAR(45) NOT NULL,
DOB DATE ,
POSITION VARCHAR(45) ,
CONSTRAINT EmployeeID_PK PRIMARY KEY (EmployeeID));
try using this:
set foreign_key_checks=0;
drop table energyefficient.customer_t;
set foreign_key_checks=1;
Some time it needs to set foreign key checks to 0;
Thanks
Possibility 1:
Your table creation script does not have any error.
As per the error message shown:
"01:22:05 DROP TABLE customer_t
Error Code: 1051. Unknown table 'energyefficient.customer_t' 0.016 sec"
You are trying to drop the table customer_t in a database named energyefficient.
Possible reason is that you have created the table in some other database and trying to drop from somewhere else.
Possibility 2:
You can modify the drop table to include if exists clause, so that the error is suppressed and ignored silently but with a warning, if such table does not exist.
Example:
DROP TABLE IF EXISTS customer_t;
In case if you are dropping the parent table first, change the global variable foreign_key_checks to false and run the drops.
set foreign_key_checks = 0;
DROP TABLE IF EXISTS customer_t;
-- other drop statements here.
set foreign_key_checks = 1; -- reset to default

MySQL error while creating tables

I have an error in my database
Error: Invalid structure on line 18. Refer to our Manual (PHPMYADMIN)
I use (WAMPSERVER 2 32bits)
-PHPMYADMIN
- MYSQL 5.5.6
- PHP 5
Although i need to use InnoDB (ENGINE=InnoDB)
thanks to help me. Take a look at the structure... the variable name meaning is not important.
Here's my code:
DROP TABLE IF EXISTS Adresse;
DROP TABLE IF EXISTS Telephone;
DROP TABLE IF EXISTS Personne;
DROP TABLE IF EXISTS TelPers;
DROP TABLE IF EXISTS Specialiste;
DROP TABLE IF EXISTS Patient;
DROP TABLE IF EXISTS ListePatient;
DROP TABLE IF EXISTS Produit;
DROP TABLE IF EXISTS Medicament;
DROP TABLE IF EXISTS Materiel;
DROP TABLE IF EXISTS Panier;
CREATE TABLE Adresse(
idAdresse INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
num INT(5) NOT NULL,
rue VARCHAR(30) NOT NULL,
ville VARCHAR(15) NOT NULL,
postal VARCHAR(6) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Telephone(
idTel INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
typeTel VARCHAR(15) NOT NULL,
ind INT(3) NOT NULL,
quartier INT(3) NOT NULL,
num INT(4) NOT NULL,
)ENGINE=InnoDB;
CREATE TABLE Personne(
idPersonne INT(100) PRIMARY KEY NOT NULL AUTO_INCREMENT,
nom VARCHAR(15) NOT NULL,
prenom VARCHAR(15) NOT NULL,
idTel INT(100) NOT NULL,
idAdresse INT(100) NOT NULL,
FOREIGN KEY(idAdresse) REFERENCES Adresse(idAdresse),
FOREIGN KEY(idTel) REFERENCES Telephone(idTel)
)ENGINE=InnoDB;
CREATE TABLE TelPers(
idPersonne INT(100) PRIMARY KEY NOT NULL,
idTel INT(100) PRIMARY KEY NOT NULL,
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne),
FOREIGN KEY(idTel) REFERENCES Telephone(idTel)
)ENGINE=InnoDB;
CREATE TABLE Specialiste(
login VARCHAR(10) PRIMARY KEY NOT NULL PRIMARY KEY,
password VARCHAR(10) NOT NULL,
profession VARCHAR(20) NOT NULL,
idListeP INT(5),
idPanier INT(5),
idPersonne INT(100),
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne)
)ENGINE=InnoDB;
CREATE TABLE Patient(
idPatient INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
sexe CHAR NOT NULL,
anniv DATE,
assurance INT(3) NOT NULL,
idPersonne INT(100),
FOREIGN KEY(idPersonne) REFERENCES Personne(idPersonne)
)ENGINE=InnoDB;
CREATE TABLE ListePatient(
idListeP INT(5) NOT NULL PRIMARY KEY,
idPatient INT(10)NOT NULL PRIMARY KEY,
FOREIGN KEY(idListeP) REFERENCES Specialiste(idListeP),
FOREIGN KEY(idPatient) REFERENCES Patient(idPatient)
)ENGINE=InnoDB;
CREATE TABLE Produit(
idProduit INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
nom VARCHAR(50) NOT NULL,
descr VARCHAR(255) NOT NULL,
prix DECIMAL(5,2) NOT NULL,
qte INT(100) NOT NULL
)ENGINE=InnoDB;
CREATE TABLE Medicament(
idMedic INT(100)NOT NULL PRIMARY KEY AUTO_INCREMENT,
marque VARCHAR(10) NOT NULL,
typeMed VARCHAR(10) NOT NULL,
idProduit INT(100) NOT NULL,
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
CREATE TABLE Materiel(
idMateriel INT(100) NOT NULL PRIMARY KEY AUTO_INCREMENT,
rabais INT(99) NOT NULL,
idProduit INT(100) NOT NULL,
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
CREATE TABLE Panier(
idPanier INT(5) NOT NULL PRIMARY KEY,
idProduit INT(100) NOT NULL PRIMARY KEY,
FOREIGN KEY(idPanier) REFERENCES Specialiste(idPanier),
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
THANKS FOR YOUR HELP! :D
In Telephone declaration there is a comma (,) before closing )
In TelPers there are two primary keys
In Specialiste there is PRIMARY KEY twice in login column
In ListePatien there are two primary keys
In Panier there are two primary keys
To define a PRIMARY key on two columns use
CREATE TABLE Panier(
idPanier INT(5) NOT NULL,
idProduit INT(100) NOT NULL,
PRIMARY KEY (`idPanier`,`idProduit`),
FOREIGN KEY(idPanier) REFERENCES Specialiste(idPanier),
FOREIGN KEY(idProduit) REFERENCES Produit(idProduit)
)ENGINE=InnoDB;
(same on other tables)