JOIN FOUR TABLES in mysql. Error in syntax - mysql

I need to join four tables in mysql.
My database structure:
DROP DATABASE IF EXISTS db_applicant;
CREATE DATABASE db_applicant
DEFAULT CHARACTER SET 'utf8'
DEFAULT COLLATE 'utf8_unicode_ci';
USE db_applicant;
--
-- TABLE: PROFESSION
--
CREATE TABLE PROFESSION (
PROFESSION_ID INT NOT NULL AUTO_INCREMENT,
PROFESSION_NAME VARCHAR(50) NOT NULL,
PRIMARY KEY (PROFESSION_ID)
);
--
-- TABLE: SUBJECT
--
CREATE TABLE SUBJECT (
SUBJECT_ID INT NOT NULL AUTO_INCREMENT,
SUBJECT_NAME VARCHAR(50) NOT NULL,
PRIMARY KEY (SUBJECT_ID)
);
--
-- TABLE: APPLICANT
--
CREATE TABLE APPLICANT (
APPLICANT_ID INT NOT NULL AUTO_INCREMENT,
PROFESSION_ID INT NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
FIRST_NAME VARCHAR(30) NOT NULL,
ENTRANCE_YEAR INT NOT NULL,
PRIMARY KEY (APPLICANT_ID),
FOREIGN KEY (PROFESSION_ID) REFERENCES PROFESSION (PROFESSION_ID)
);
--
-- TABLE: APPLICANT_RESULT
--
CREATE TABLE APPLICANT_RESULT (
APPLICANT_RESULT_ID INT NOT NULL AUTO_INCREMENT,
APPLICANT_ID INT NOT NULL,
SUBJECT_ID INT NOT NULL,
MARK INT,
PRIMARY KEY (APPLICANT_RESULT_ID),
FOREIGN KEY (SUBJECT_ID)
REFERENCES SUBJECT (SUBJECT_ID),
FOREIGN KEY (APPLICANT_ID)
REFERENCES APPLICANT (APPLICANT_ID)
);
--
-- TABLE: SPECIALITY_SUBJECT
--
CREATE TABLE SPECIALITY_SUBJECT (
SP_SB_ID INT NOT NULL AUTO_INCREMENT,
PROFESSION_ID INT NOT NULL,
SUBJECT_ID INT NOT NULL,
PRIMARY KEY (SP_SB_ID),
FOREIGN KEY (PROFESSION_ID)
REFERENCES PROFESSION (PROFESSION_ID),
FOREIGN KEY (PROFESSION_ID)
REFERENCES PROFESSION (PROFESSION_ID),
FOREIGN KEY (SUBJECT_ID)
REFERENCES SUBJECT (SUBJECT_ID)
);
I need that output would be something like:
first_name (this column from table applicant), last_name (this column from table applicant), entrance_year (this column from table applicant), profession_name (this column from table profession), subject_name (this column from table subject), mark (this column from table applicant_result).
You can see, that i have related fields. But i need strong INNER QUERY.
For that, i create new table with structure:
CREATE TABLE APP(
ALL_ID INT NOT NULL AUTO_INCREMENT,
APPLICANT_ID INT NOT NULL,
SUBJECT_ID INT NOT NULL,
PROFESSION_ID INT NOT NULL,
APPLICANT_RESULT_ID INT NOT NULL,
PRIMARY KEY (ALL_ID),
FOREIGN KEY (SUBJECT_ID)
REFERENCES SUBJECT (SUBJECT_ID),
FOREIGN KEY (APPLICANT_ID)
REFERENCES APPLICANT (APPLICANT_ID),
FOREIGN KEY (PROFESSION_ID)
REFERENCES PROFESSION (PROFESSION_ID),
FOREIGN KEY (APPLICANT_RESULT_ID)
REFERENCES APPLICANT_RESULT (APPLICANT_RESULT_ID)
);
And my inner:
SELECT ap.ALL_ID, a.FIRST_NAME, a.LAST_NAME,
a.ENTRANCE_YEAR, p.PROFESSION_NAME s.SUBJECT_NAME, ar.MARK
FROM app ap
JOIN (applicant a, profession p, subject s, applicant_result ar)
ON ap.APPLICANT_ID = a.APPLICANT_ID
AND ap.SUBJECT_ID = s.SUBJECT_ID
AND ap.PROFESSION_ID = p.PROFESSION_ID
AND ap.APPLICANT_RESULT_ID = ar.APPLICANT_RESULT_ID;
But i have error:
[2015-09-19 10:08:52] [42000][1064] You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near '.SUBJECT_NAME, ar.MARK FROM app ap
JOIN (applicant a, profession p, subject s, a' at line 1

Don't you think, a comma ',' is missing here in select statement before s.SUBJECT_NAME?
SELECT ap.ALL_ID, a.FIRST_NAME, a.LAST_NAME,
a.ENTRANCE_YEAR, p.PROFESSION_NAME s.SUBJECT_NAME, ar.MARK

Related

How to copy column values to a new SQL table, remove duplicates and create mappings?

I have a table Excerpt
CREATE TABLE IF NOT EXISTS excerpt(
excerptID INT UNSIGNED NOT NULL AUTO_INCREMENT,
author VARCHAR(45) NOT NULL,
title VARCHAR(255) NOT NULL,
text VARCHAR(2500) NOT NULL,
comments VARCHAR(2500) NOT NULL,
PRIMARY KEY (excerptID)
) ENGINE=INNODB CHARACTER SET utf8mb4;
and want to remove its column Author while distributing its (often duplicate) values and information about these values into two new tables Author
CREATE TABLE IF NOT EXISTS author(
authorID INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(45) NOT NULL ,
PRIMARY KEY (authorID)
) ENGINE=INNODB CHARACTER SET utf8mb4;
and AuthorMap:
CREATE TABLE IF NOT EXISTS authormap (
excerptID INT UNSIGNED NOT NULL,
authorID INT UNSIGNED NOT NULL,
PRIMARY KEY (excerptID, authorID),
CONSTRAINT authorMapFK FOREIGN KEY (excerptID) REFERENCES excerpt (excerptID)
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT authorFK FOREIGN KEY (authorID) REFERENCES author (authorID)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=INNODB;
All duplicates should be removed and mappings between excerptID and authorID placed into AuthorMap, i.e eventually all unique author names should be in the table Author, their IDs created and all ExcerptID should be copied to AuthorMap alongside with the newly created AuthorIDs.
How should I do it?
You can create the table authors as:
create table authors (
author_id int auto_increment primary key,
name varchar(255) -- or whatever
);
insert into authors (name)
select distinct author
from excerpt;
Then create the author map table as:
create table excerptAuthors (
excerptAuthorsId int auto_icnrement primary key,
excerptid int,
authorid int
);
And finally:
insert into excerptAuthors (excerptId, authorId)
select e.excerptId, a.authorId
from excerpts e join
authors a
on e.author = a.name;

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

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;

Why can't I add foreign key constraints

this is my code:
CREATE DATABASE exams;
SHOW DATABASES;
CREATE TABLE IF NOT EXISTS students(
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(40),
last_name VARCHAR(40)NOT NULL,
email VARCHAR(60) NOT NULL,
password CHAR(40) NOT NULL,
reg_date DATETIME NOT NULL,
PRIMARY KEY (student_id),
UNIQUE(email));
SHOW table status
INSERT INTO exams_3121(student_id, first_name, middle_name, last_name, email, password, reg_date)
CREATE TABLE entries
(
entrie_id int NOT NULL,
student_id int NOT NULL,
subject_id int,
PRIMARY KEY (entrie_id),
FOREIGN KEY (student_id) REFERENCES student(student_id),
FOREIGN KEY (subject_id)REFERENCES subject(subject_id)
)
CREATE DATABASE subjects;
CREATE TABLE IF NOT EXISTS subjects(
subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
subject_name VARCHAR(20) NOT NULL,
level_entery VARCHAR(40)NOT NULL,
exam_board VARCHAR(60) NOT NULL,
PRIMARY KEY (subject_id));
CREATE TABLE entries
(
entrie_id int NOT NULL,
entrie_id int NOT NULL,
subject_id int,
PRIMARY KEY (entrie_id),
FOREIGN KEY (student_id) REFERENCES student(student_id),
FOREIGN KEY (subject_id)REFERENCES subject(subject_id)
)
When I use this code it says cannot add foreign key constraint
and I don't know what to do. Please and thanks in advance.
There are two problems.
First, you got the names of the table you're referencing wrong. The name of the tables are students and subjects, but you wrote student and subject.
Second, the entries table has two entrie_id columns. One of them should be student_id.
CREATE TABLE entries
(
entrie_id int NOT NULL,
student_id int NOT NULL,
subject_id int,
PRIMARY KEY (entrie_id),
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (subject_id) REFERENCES subjects(subject_id)
)
Also, if you're creating multiple databases and putting your tables in them, you'll need to refer to tables with their database prefixes if they're different from the one you selected as default with the USE command. As you've written it, you're not actually using the databases you created with CREATE DATABASE.

Error creating mysql table with Foreign Key

I have researched thoroughly before asking this question including on this site.
I have a students table:
CREATE TABLE IF NOT EXISTS students(
student_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
first_name VARCHAR(20) NOT NULL,
middle_name VARCHAR(20),
last_name VARCHAR(40) NOT NULL,
email VARCHAR(60) NOT NULL,
password CHAR(40) NOT NULL,
reg_date DATETIME NOT NULL,
PRIMARY KEY (student_id),
UNIQUE (email));
I also have a subjects table:
CREATE TABLE IF NOT EXISTS subjects(
subject_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
subject_name VARCHAR(40) NOT NULL,
level_of_entry VARCHAR(20) NOT NULL,
exam_board VARCHAR(20) NOT NULL,
PRIMARY KEY (subject_id));
I am now creating a table to link the above tables:
CREATE TABLE IF NOT EXISTS entries(
exam_date DATETIME NOT NULL,
FOREIGN KEY (student_id) REFERENCES students (student_id),
FOREIGN KEY (subject_id) REFERENCES subjects (subject_id)
);
My problem is that when I try to declare the foreign keys in the third table called entries, I get an error stating that the subject_id foreign key is not in the table referenced.
ERROR 1072 (42000) : Key column 'student_id' doesn't exist in table, even though it is clearly contained inside the the students table and the same applies to 'subject_id' and the subject table.
I am certain that my syntax for declaring the foreign keys is correct so I am unsure how to fix the problem.
All help is appreciated. Thank you.
You forgot to create these two columns before applying your foreign key constraints :
CREATE TABLE IF NOT EXISTS entries(
exam_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
student_id INT UNSIGNED NOT NULL,
subject_id INT UNSIGNED NOT NULL,
exam_date DATETIME NOT NULL,
PRIMARY KEY (exam_id),
FOREIGN KEY (student_id) REFERENCES students (student_id),
FOREIGN KEY (subject_id) REFERENCES subjects (subject_id)
);
EDIT :
I advise you to add in every table a unique ID column (here : exam_id).

"Key column doesn't exist in table" when trying to define FOREIGN KEY

I'm trying to setup a MySQL-Script that creates 5 tables. In three tables, there is a FOREIGN KEY, and for all three of them the same error appears:
Error Code: 1072. Key column ... doesn't exist in table
whereas ... are Gebaeude, Dept_Name and Mat_Nr
Here's the script
use cs261_24;
drop table if exists Professor;
drop table if exists Departement;
drop table if exists Gebaeude;
drop table if exists Student;
drop table if exists Pruefung;
CREATE TABLE Gebaeude (
Gebaeude VARCHAR(20) NOT NULL PRIMARY KEY,
Hauswart VARCHAR(20) NOT NULL,
Adresse VARCHAR(20) NOT NULL
)engine = innodb;
CREATE TABLE Professor (
P_ID INTEGER PRIMARY KEY CHECK (P_ID > 0),
P_Name VARCHAR(20) NOT NULL,
Dept_Name VARCHAR(20) NOT NULL,
Raum INTEGER UNIQUE CHECK (Raum > 0),
Tel INTEGER(10) UNIQUE CHECK (Tel > 210000000),
FOREIGN KEY (Gebaeude) REFERENCES Gebaeude (Gebaeude)
)engine = innodb;
CREATE TABLE Departement (
Dept_Name VARCHAR(20) NOT NULL PRIMARY KEY,
Vorsteher VARCHAR(20) NOT NULL
)engine = innodb;
CREATE TABLE Student (
Mat_Nr INTEGER(8) PRIMARY KEY CHECK (Mat_Nr > 0),
S_Name VARCHAR(20) NOT NULL,
Semester INTEGER CHECK(Semester > 0),
FOREIGN KEY (Dept_Name) REFERENCES Departement (Dept_Name)
)engine = innodb;
CREATE TABLE Pruefung (
Pr_ID INTEGER PRIMARY KEY CHECK(Pr_ID > 0),
Fach VARCHAR(20) NOT NULL,
Pruefer VARCHAR(20) NOT NULL,
Note FLOAT CHECK (Note >= 1 AND Note <= 6),
FOREIGN KEY (Mat_Nr) REFERENCES Student (Mat_Nr)
)engine = innodb;
Why? I work with MySQL Workbench, and I clearly see the created tables, plus the specific columns are marked as primary keys!
You are doing it wrong, take a look to this example
http://www.sqlfiddle.com/#!2/a86cf
your FK line should be more like this:
FOREIGN KEY (field_that_will_be_Fk) REFERENCES Table_to_reference (field_to_reference)
I.E=
CREATE TABLE Gebaeude (
Gebaeude VARCHAR(20) NOT NULL PRIMARY KEY,
Hauswart VARCHAR(20) NOT NULL,
Adresse VARCHAR(20) NOT NULL
)engine = innodb;
CREATE TABLE Professor (
Gebaeude_FK varchar(20) NOT NULL,
P_ID INTEGER PRIMARY KEY CHECK (P_ID > 0),
P_Name VARCHAR(20) NOT NULL,
Dept_Name VARCHAR(20) NOT NULL,
Raum INTEGER UNIQUE CHECK (Raum > 0),
Tel INTEGER(10) UNIQUE CHECK (Tel > 210000000),
FOREIGN KEY (Gebaeude_FK) REFERENCES Gebaeude (Gebaeude)
)engine = innodb;
You have to create the column in each of the tables before you can constrain them as a foreign key.
See this link for reference: Link