MySQL error while creating tables - mysql

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)

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)

#1072 - Key column 'Customer_ID' doesn't exist in table

This is the parent table
CREATE TABLE Customer(
Customer_ID INT(5) not null,
CustName VARCHAR(50) not null,
CustSurname VARCHAR(50) not null,
CustEmail VARCHAR(100) unique not null,
CustMobileNo INT(12) not null,
HomeAddress VARCHAR(255) not null,
Password VARCHAR(10) not null,
constraint c_cuid_pk PRIMARY KEY (Customer_ID))
ENGINE = InnoDB;
When I try to create the property table it shows me
"#1072 - Key column 'Customer_ID' doesn't exist in table"
this is the child table
CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;
In table Property, you must define a column named Customer_ID (well it could be named anything!), then you define the FOREIGN KEY on it.
Your code currently tries to link IndificualCustomer(Customer_ID) to Property(Customer_ID), but Property(Customer_ID) does not exist.
Declaring the constraint just links the columns, but it does not create them.
CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
Customer_ID INT(5) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;
In the first place, you should add a column: Customer_ID to your Property table to create your foreign key constraint:
Property.Customer_ID → IndividualCustomer.Customer_ID
You can not make a constraint on something that does not exist.
Or change the +++ +++ part of your table creation query to a column that exists.
constraint p_cuid_fk FOREIGN KEY (+++Customer_ID+++) --to be changed accordingly
references IndividualCustomer(Customer_ID)
Example: adding the Customer_ID column to your table declaration would give you the following code
CREATE TABLE Property(
Property_ID INT(5) not null,
Customer_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
constraint p_phn_pk PRIMARY KEY (Property_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references IndividualCustomer(Customer_ID))
ENGINE = InnoDB;
CREATE TABLE Property(
Property_ID INT(5) not null,
PhouseNumber INT(4) not null,
PstreetName VARCHAR(50) not null,
PpostCode VARCHAR(7) not null,
Pcity VARCHAR(20) not null,
Customer_ID INT(5) not null,
PRIMARY KEY (Property_ID),
KEY p_cuid_fk (Customer_ID),
constraint p_cuid_fk FOREIGN KEY (Customer_ID)
references Customer(Customer_ID))
ENGINE = InnoDB;
You have to add Customer_ID in the table PROPERTY.

mysql unable to add foreign key: error 1215 (hy000)

I am getting error 1215 (hy000): cannot add foreign key constraint then I run the following .sql file. The code below is also not creating the documents table, probably because it fails to create its foreign keys. Can anyone show me how to fix the code below so it will create the documents table including its foreign keys without throwing errors:
CREATE DATABASE IF NOT EXISTS petclinic;
GRANT ALL PRIVILEGES ON petclinic.* TO pc#localhost IDENTIFIED BY 'pc';
USE petclinic;
CREATE TABLE IF NOT EXISTS types (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(80),
INDEX(name)
) engine=InnoDB;
CREATE TABLE IF NOT EXISTS documenttypes (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(80),
INDEX(name)
) engine=InnoDB;
CREATE TABLE IF NOT EXISTS owners (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(30),
last_name VARCHAR(30),
address VARCHAR(255),
city VARCHAR(80),
telephone VARCHAR(20),
INDEX(last_name)
) engine=InnoDB;
CREATE TABLE IF NOT EXISTS pets (
id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30),
birth_date DATE,
type_id INT(4) UNSIGNED NOT NULL,
owner_id INT(4) UNSIGNED NOT NULL,
INDEX(name),
FOREIGN KEY (owner_id) REFERENCES owners(id),
FOREIGN KEY (type_id) REFERENCES types(id)
) engine=InnoDB;
CREATE TABLE IF NOT EXISTS documents (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
client_id int(4) NOT NULL,
type_id INT(4),
name varchar(200) NOT NULL,
description text NOT NULL,
filename varchar(200) NOT NULL,
content mediumblob NOT NULL,
content_type varchar(255) NOT NULL,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (client_id) REFERENCES owners(id),
FOREIGN KEY (type_id) REFERENCES documenttypes(id)
) engine=InnoDB;
I tried the advice at this link, but none of those suggestions work with this code. Can someone show me something that does work with this code?
The foreign key columns have to be the exact same data type as the original columns.
Try this:
CREATE TABLE IF NOT EXISTS documents (
id int(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
client_id int(4) UNSIGNED NOT NULL,
type_id INT(4) UNSIGNED NOT NULL,
name varchar(200) NOT NULL,
description text NOT NULL,
filename varchar(200) NOT NULL,
content mediumblob NOT NULL,
content_type varchar(255) NOT NULL,
created timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (client_id) REFERENCES owners(id),
FOREIGN KEY (type_id) REFERENCES documenttypes(id)
) engine=InnoDB;
Added UNSIGNED to client_id and UNSIGNED NOT NULL to type_id (the NOT NULL part in is not mandatory though)
sqlfiddle demo

Cannot Add foreign key constraint Error 1215

I am using Workbench 6.0 and having a frustrating issue I am new to SQl and would like to keep this as simple as possible at this point, I believe my tables are ordered properly to add contraints except for my TICKET table, no idea how to find what foreign key is having problems.
CREATE TABLE IF NOT EXISTS ACTION_TYPE (
ActionCode int primary key,
Description char(50) not null
)ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS OFFICER (
PersonnelNo int primary key,
OfficerLName char(50) not null,
OfficerFName char(50) not null
)ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS DRIVER (
DriverLicenseNo int primary key,
DriverLastName char(50) not null,
DriverFirstName char(50) not null,
DriverAddress char(50) not null,
DriverCity char(50) not null,
DriverProv char(50) not null,
DriverPostalCode varchar(6) not null,
DriverGender char(1) not null,
DriverBirthDate Date not null
)ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS REGISTERED_OWNER (
RegOwnerID int primary key auto_increment,
RegOwnerLName char(50) not null,
RegOwnerFName char(50) not null,
RegOwnerAddress char(50) not null,
RegOwnerCity char(50) not null,
RegOwnerProv char(50) not null,
RegOwnerPostalCode varchar(6) not null
)ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS VEHICLE_TYPE (
VehicleType int primary key,
VehicleDescription char(50) not null
)ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS VEHICLE (
VehicleLicense int primary key,
ProvinceIssued char(2) not null,
VehicleYear int not null,
VehicleMake char(10) not null,
VehicleType int not null,
index (VehicleType),
foreign key (VehicleType)
references VEHICLE_TYPE (VehicleType)
)ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS TICKET ( <-----this table calling the error
TicketNo int primary key auto_increment,
TicketDateTime datetime not null,
TicketLocationCity char(50) not null,
TicketLocationProv char(50) not null,
TicketLocationRoad char(50) not null,
PersonnelNo int not null,
VehicleLicense int not null,
ActionCode int not null,
RegOwnerID int not null,
DriversLicenseNo int not null,
index (PersonnelNo),
foreign key (PersonnelNo)
references OFFICER (PersonnelNo),
index (VehicleLicense),
foreign key (VehicleLicense)
references VEHICLE (VehicleLicense),
index (ActionCode),
foreign key (ActionCode)
references ACTION_TYPE (ActionCode),
index (RegOwnerID),
foreign key (RegOwnerID)
references REGISTERED_OWNER (RegOwnerID),
index (DriversLicenseNo),
foreign key (DriversLicenseNo)
references DRIVER (DriversLicenseNo)
)ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS VIOLATION_TYPE (
ViolationCode int primary key auto_increment,
ViolationDesc char(50) not null,
ViolationCurrFineAmt int not null
)ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS VIOLATION (
ViolationNo int primary key auto_increment,
TicketNo int not null,
ViolationCode int not null,
AppliedFineAmount int not null,
index (TicketNo),
foreign key (TicketNo)
references TICKET (TicketNo),
index (ViolationCode),
foreign key (ViolationCode)
references VIOLATION_TYPE (ViolationCode)
)ENGINE=InnoDB;
Spelling mistake on DriverLicenseNo in TICKET table ane
DriversLicenseNo in DRIVER table

Creating a table with foreign keys shows an error

I'm trying to create a table and am getting an error telling me there's something wrong around line 9. This is the code.
CREATE TABLE shirts_link (
adult VARCHAR(1) NOT NULL,
kids VARCHAR(1) NOT NULL,
babies VARCHAR(1) NOT NULL,
shirt_id INT(4) NOT NULL,
size_id INT(4) NOT NULL,
price_id INT(4) NOT NULL,
PRIMARY KEY (shirt_id,size_id,price_id),
FOREIGN KEY (shirt_id) REFERENCES shirts(id),
FOREIGN KEY (size_id) REFERENCES shirt_sizes(id),
FOREIGN KEY (price_id) REFERENCES shirt_prices(id)
)ENGINE=INNODB;
here's the other tables I'm trying to link to..
CREATE TABLE shirts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
shirt_name VARCHAR(20) NOT NULL,
men VARCHAR(10) NULL,
women VARCHAR(10) NULL,
boys VARCHAR(10) NULL,
girls VARCHAR(10) NULL,
babies VARCHAR(10) NULL,
)ENGINE=INNODB;
CREATE TABLE shirt_sizes (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
size_name VARCHAR(10) NOT NULL
)ENGINE=INNODB;
CREATE TABLE shirt_prices (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
price_cat VARCHAR(10) NOT NULL,
price NUMERIC(6,2) NOT NULL
)ENGINE=INNODB;
the problem is that you are wrapping table names and column names with single quotes. it shouldn't be or use backtick instead
FOREIGN KEY (shirt_id) REFERENCES shirts(id),
FOREIGN KEY (size_id) REFERENCES shirt_sizes(id),
FOREIGN KEY (price_id) REFERENCES shirt_prices(id)
or
FOREIGN KEY (`shirt_id`) REFERENCES `shirts`(`id`),
FOREIGN KEY (`size_id`) REFERENCES `shirt_sizes`(`id`),
FOREIGN KEY (`price_id`) REFERENCES `shirt_prices`(`id`)
but in this case, they are optional since non of they are MySQL reserved Keywords.
MySQL Reserved Keywords List
UPDATE 1
The data type of the keys must be the same with each other, declare these columns as UNSIGNED
shirt_id INT(4) UNSIGNED NOT NULL,
size_id INT(4) UNSIGNED NOT NULL,
price_id INT(4) UNSIGNED NOT NULL,
SQLFiddle Demo
CREATE TABLE shirts_link (
adult VARCHAR(1) NOT NULL,
kids VARCHAR(1) NOT NULL,
babies VARCHAR(1) NOT NULL,
shirt_id INT(4) NOT NULL,
size_id INT(4) NOT NULL,
price_id INT(4) NOT NULL,
PRIMARY KEY (shirt_id,size_id,price_id),
FOREIGN KEY (shirt_id) REFERENCES shirts(id),
FOREIGN KEY (size_id) REFERENCES shirt_sizes(id),
FOREIGN KEY (price_id) REFERENCES shirt_prices(id)
)ENGINE=INNODB;
it worked for me on sqlfiddle
Here is my code:
CREATE TABLE shirts(
id INT NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirts_link (
adult VARCHAR(1) NOT NULL,
kids VARCHAR(1) NOT NULL,
babies VARCHAR(1) NOT NULL,
shirt_id INT(4) NOT NULL,
size_id INT(4) NOT NULL,
price_id INT(4) NOT NULL,
PRIMARY KEY (shirt_id,size_id,price_id),
FOREIGN KEY (shirt_id) REFERENCES shirts(id)
);