How to create an Intermediary table for a M:M database - mysql

So I have a many-to-many relational database and right now I am at the stage of just getting it set up.
I am using mariadb and have read that when dealing with a M:M then it is a good idea to have a intermediary table to store the relations. I am trying to do that but I am so dumb that I can't figure out the right syntax for creating a column?
CREATE TABLE doctor_hospitals (
doctor_id INT FOREIGN KEY (doctor_id) REFERENCES doctors(id),
hospital_id INT FOREIGN KEY (hospital_id) REFERENCES hospitals(id)
) ENGINE = InnoDB;
above yields the error:
Query 1 ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FOREIGN KEY (doctor_id) REFERENCES doctors(id),
hospital_id INT FOREIGN KEY ' at line 2
while:
CREATE TABLE doctor_hospitals (
FOREIGN KEY (doctor_id) REFERENCES doctors(id),
FOREIGN KEY (hospital_id) REFERENCES hospitals(id)
) ENGINE = InnoDB;
yields the error:
Query 1 ERROR: A table must have at least 1 column
this sorta makes sense. Here is what I am trying to create:
CREATE TABLE doctors (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
userID VARCHAR(100) NOT NULL,
first_names VARCHAR(100) NOT NULL,
last_names VARCHAR(100) NOT NULL,
medical_number INT NOT NULL,
email VARCHAR(20) NOT NULL,
country VARCHAR(20) NOT NULL,
province VARCHAR(20) NOT NULL,
city VARCHAR(20)) ENGINE = InnoDB;
CREATE TABLE hospitals (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(150) NOT NULL,
address1 VARCHAR(75),
address2 VARCHAR(75),
phone_number INT,
country VARCHAR(15),
province VARCHAR(15),
city VARCHAR(20),
zip_code VARCHAR(15)) ENGINE = InnoDB;
CREATE TABLE doctor_hospitals (
doctor INT FOREIGN KEY (doctor_id) REFERENCES doctors(id),
hospital INT FOREIGN KEY (hospital_id) REFERENCES hospitals(id)
) ENGINE = InnoDB;
thank you for any help!

FKs are constraints. You need to define the columns first. Also need to define the indexes for the FKs.
CREATE TABLE doctor_hospitals (
doctor_id INT UNSIGNED NOT NULL,
hospital_id INT UNSIGNED NOT NULL,
PRIMARY KEY (doctor_id,hospital_id),
KEY IX_doctor_hospitals_hospital_id(hospital_id),
CONSTRAINT FK_doctor_hospitals_hospitals FOREIGN KEY (hospital_id) REFERENCES hospitals(id),
CONSTRAINT FK_doctor_hospitals_doctors FOREIGN KEY (doctor_id) REFERENCES doctors(id)
) ENGINE = InnoDB;

Related

Failed to add the foreign key constraint in MySQL: error 3780

I am getting the error:
Error Code: 3780. Referencing column 'category' and referenced column 'category_id' in foreign key constraint 'product_ibfk_1' are incompatible.
drop table if exists Provider;
drop table if exists Category;
drop table if exists Product;
create table Provider
(
privider_id serial not null primary key,
login_password varchar(20) not null
constraint passrule3 check(login_password sounds like '[A-Za-z0-9]{6,20}'),
fathersname varchar(20) not null,
name_of_contact_face varchar(10) not null,
surname varchar(15),
e_mail varchar(25) unique
constraint emailrule2 check(e_mail sounds like '[A-Za-z0-9]{10,10})\#gmail.com\s?')
);
create table Category
(
title varchar(20),
category_id serial not null primary key
);
create table Product
(
barecode serial not null primary key,
provider_id bigint not null,
manufacturer varchar(25) not null,
category_id bigint not null,
dimensions varchar(10) not null,
amount int not null,
date_of_registration datetime not null,
#constraint 'provider_for_product'
foreign key (provider_id) references Provider (provider_id) on delete restrict on update cascade,
foreign key (category_id) references Category (category_id) on delete restrict on update cascade
);
The datatypes of the two columns referenced in a foreign key constraint need to match
In MySQL, SERIAL is an alias for BIGINT UNSIGNED AUTO_INCREMENT.
To make a foreign key that references this column, it must be BIGINT UNSIGNED, not a signed BIGINT.
You might like to view a checklist of foreign key mistakes I contributed to: https://stackoverflow.com/a/4673775/20860
I also cover foreign key mistakes in more detail in a chapter of my book, SQL Antipatterns Volume 1: Avoiding the Pitfalls of Database Programming.

mysql error 1215(hy000) cannot add foreign key constraint

I keep getting this error, I used InnoDB for all tables, menuID, and customerID, are primary keys in their respective tables, and the datatypes appear to be the same.
ERROR 1215 (HY000): Cannot add foreign key constraint
Sorry if I am missing something simple, I am new to mySQL.
create table customers(
customerID int not null auto_increment primary key,
LastName varchar(255) not null,
FirstName varchar(255) not null,
email varchar(255) not null,
password varchar(255),
phone varchar(255),
creditCard varchar(255),
address varchar(255),
time timestamp)
Engine=InnoDB;
create table menu(
menuID int not null auto_increment primary key,
typeID int,
itemName varchar(255),
price varchar(255))
Engine=InnoDB;
create table orders(
orderID int not null,
customerID int not null,
menuID int not null,
PRIMARY KEY (orderID, customerID, menuID),
FOREIGN KEY (customerID) REFERENCES customers(customerID) on delete set null on update cascade,
foreign key (menuID) references menu(menuID) on delete set null on update cascade )
Engine=InnoDB;
You can't use ON DELETE SET NULL for a foreign key column that you declared NOT NULL.
See this answer for a long checklist of things to check as possible causes of foreign key errors: MySQL Creating tables with Foreign Keys giving errno: 150

Syntax Error in MySQL - Same syntax worked in earlier table

I'm working on building a small database and am running into a syntax error on these two lines in my last table.
They're foreign keys and those lines didn't produce any errors in the tables where they originally appeared.
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
DONUT_ID VARCHAR(10) NOT NULL UNIQUE,
These two lines are the PK and FK in the table, and I think this is where I'm running into the problem because I used the same syntax as I did in earlier tables that aren't generating any errors.
Here is the full code for four tables:
CREATE TABLE CUSTOMER (
CUST_ID INT NOT NULL AUTO_INCREMENT UNIQUE,
CUST_LNAME VARCHAR(25) NOT NULL,
CUST_FNAME VARCHAR(25) NOT NULL,
CUST_INITIAL CHAR(1),
CUST_STREET_NO VARCHAR(6),
CUST_STREET_NAME VARCHAR(25),
CUST_APT_NO VARCHAR(10),
CUST_CITY VARCHAR(25),
CUST_STATE CHAR(2),
CUST_ZIP_CODE CHAR(5),
CUST_HOME_AC CHAR(3),
CUST_HOME_PHONE CHAR(8),
PRIMARY KEY (CUST_ID)
)ENGINE = InnoDB;
CREATE TABLE INVOICE (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
CUST_ID INTEGER NOT NULL,
INV_DATE DATE NOT NULL,
SPECIAL_HANDLING VARCHAR(35),
PRIMARY KEY (INVOICE_ID),
FOREIGN KEY (CUST_ID) REFERENCES CUSTOMER(CUST_ID) ON UPDATE CASCADE
)ENGINE = InnoDB;
CREATE TABLE PRODUCT (
DONUT_ID VARCHAR(10) NOT NULL UNIQUE,
DONUT_NAME VARCHAR(25) NOT NULL,
DONUT_DESC VARCHAR(35) NOT NULL,
DONUT_PRICE DECIMAL(13,2) NOT NULL,
PRIMARY KEY (DONUT_ID)
)ENGINE = InnoDB;
CREATE TABLE INVOICE LINE ITEM (
INVOICE_ID INTEGER NOT NULL AUTO_INCREMENT UNIQUE,
DONUT_ID VARCHAR(10) NOT NULL UNIQUE,
DONUT_QTY INTEGER NOT NULL,
PRIMARY KEY (INVOICE_ID, DONUT_ID),
FOREIGN KEY (INVOICE_ID) REFERENCES INVOICE(INVOICE_ID) ON UPDATE CASCADE,
FOREIGN KEY (DONUT_ID) REFERENCES PRODUCT(DONUT_ID) ON UPDATE CASCADE
)ENGINE = InnoDB;
The fourth CREATE TABLE statement will fail because of the spaces in the table name:
CREATE TABLE INVOICE LINE ITEM ...
You should use a different name, without spaces. For example you could replace the spaces with underscores and use snake-case:
CREATE TABLE INVOICE_LINE_ITEM ...

Cannot create interconnecting table

i'm trying to create a table with columns that reference toward other tables.
How do i make the foreign keys?
Scheme:
Query: (not working):
CREATE TABLE gebruikers_trainingen (
gebruiker_id INT UNSIGNED NOT NULL,
training_id INT UNSIGNED NOT NULL,
gebruiker_naam VARCHAR(255) NOT NULL,
training_naam VARCHAR(255),
CONSTRAINT fk_idGebruiker FOREIGN KEY (gebruiker_id)
REFERENCES gebruikers(id),
CONSTRAINT fk_idTraining FOREIGN KEY (training_id)
REFERENCES trainingen(id),
CONSTRAINT fk_naamGebruiker FOREIGN KEY (gebruiker_naam)
REFERENCES gebruikers(voornaam),
CONSTRAINT fk_naamTraining FOREIGN KEY (training_naam)
REFERENCES trainingen(naam)
) ENGINE = INNODB;
Getting:
Error Code: 1005 Can't create table 'konecranes.gebruikers_trainingen'
(errno: 150)
EDIT:
Other tables' queries.
CREATE TABLE gebruikers (
id int unsigned NOT NULL,
voornaam varchar(255) NOT NULL,
achternaam varchar(255) NOT NULL,
account_level int unsigned NOT NULL,
PRIMARY KEY (id, voornaam)
) ENGINE = InnoDB;
CREATE TABLE trainingen (
id int unsigned NOT NULL,
naam varchar(255) NOT NULL,
PRIMARY KEY (id, naam)
) ENGINE = InnoDB;
You should add indexes on your foreign keys:
CREATE TABLE gebruikers_trainingen (
gebruiker_id INT UNSIGNED NOT NULL,
training_id INT UNSIGNED NOT NULL,
gebruiker_naam VARCHAR(255) NOT NULL,
training_naam VARCHAR(255) NOT NULL,
INDEX (gebruiker_id, gebruiker_naam),
INDEX (training_id, training_naam),
CONSTRAINT fk_idGebruiker FOREIGN KEY (gebruiker_id, gebruiker_naam)
REFERENCES gebruikers(id, voornaam),
CONSTRAINT fk_idTraining FOREIGN KEY (training_id, training_naam)
REFERENCES trainingen(id, naam)
) ENGINE = INNODB;
Has this table existed before in a different guise?
Mysql 1005 error when creating table using InnoDB engine
Hth Oli
Merging the constraints as follows did work it out. Also thanks too Justin Lurman for helping me out, had to add Indexes aswell.
CONSTRAINT fk_gebruikers FOREIGN KEY (gebruiker_id, gebruiker_naam) REFERENCES gebruikers(id, voornaam),
CONSTRAINT fk_trainingen FOREIGN KEY (training_id, training_naam) REFERENCES trainingen(id, naam)

How do I create a table with a two primary keys of two foreign keys?

create table Machine
(
Machine_ID int primary key,
Machine_Name varchar(30)
Machine_Title varchar(30)
)
create table Part
(
Part_ID int primary key,
Part_Name varchar(30),
Part_Description varchar(30)
)
//How do I make this table below?
create table Machine-Part
(
Machine_ID int foreign key references (Machine.Machine_ID),
Part_ID int foreign key references (Part.Part_ID)
Factory_Note varchar(30);
)
Mysql complains there is a problem with syntax?
Desired Result: have Table 'Machine-Part' use 'Machine_ID' & 'Part_ID' as both primary keys (which are both foreign keys).
If you declare the constraint separately (table level), it makes more sense
create table Machine-Part
(
Machine_ID int NOT NULL ,
Part_ID int NOT NULL ,
Factory_Note varchar(30) NULL,
PRIMARY KEY (Machine_ID, Part_ID),
UNIQUE INDEX (Part_ID, Machine_ID),
foreign key (Machine_ID) references (Machine.Machine_ID),
foreign key (Part_ID) references (Part.Part_ID)
)
Link tables almost always need a reverse index too
Something like this -
CREATE TABLE Machine(
Machine_ID INT PRIMARY KEY,
Machine_Name VARCHAR(30),
Machine_Title VARCHAR(30)
)
ENGINE = INNODB;
CREATE TABLE Part(
Part_ID INT PRIMARY KEY,
Part_Name VARCHAR(30),
Part_Description VARCHAR(30)
)
ENGINE = INNODB;
create table `Machine-Part`(
Machine_ID int,
Part_ID int,
Factory_Note varchar(30),
CONSTRAINT fk_Machine_ID FOREIGN KEY (Machine_ID) REFERENCES Machine(Machine_ID),
CONSTRAINT fk_Part_ID FOREIGN KEY (Part_ID) REFERENCES Part(Part_ID)
)
ENGINE = INNODB;
You can find all information on these pages:
FOREIGN KEY Constraints
CREATE TABLE Syntax