-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lidiia starshynova w2 databases #14
base: main
Are you sure you want to change the base?
Lidiia starshynova w2 databases #14
Conversation
I tried to implement a more complicated solution for this tasks. But I got some errors that I was not able to solve. This topic is very interesting for me and I would like you to help me with solving my errors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Lidiia,
It's very good that you're interested in exploring some advanced MySQL syntax and topics! I left some suggestions about real working scenarios related to DB. Feel free to check them.
There's only one point that you need to rework. I have made it explicit.
I saw you left a comment saying you encountered some errors. Don't hesitate to ping me on Slack to share your screenshots and code.
Have a nice weekend!
Week2/Assignment/Relationships.js
Outdated
const create_table_author_paper_query = `CREATE TABLE IF NOT EXISTS author_paper( | ||
author_id INT, | ||
paper_id INT, | ||
UNIQUE KEY (author_id, paper_id), | ||
FOREIGN KEY (author_id) REFERENCES authors(author_id) ON DELETE CASCADE, | ||
FOREIGN KEY (paper_id) REFERENCES research_papers(paper_id) ON DELETE CASCADE | ||
)`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to work: Defining UNIQUE
for the combination of author_id
and paper_id
is fantastic, but it's strongly recommended to have PK for each table. Why not use it as PK? There is a type of PK called composite key
. PK will automatically set the combination to be unique.
const create_temp_table_query = `CREATE TEMPORARY TABLE temp_author_mentor (author_id INT, mentor_id INT)`; | ||
|
||
const insert_mentor_id_into_temp_table_query = `INSERT INTO temp_author_mentor (author_id, mentor_id) | ||
SELECT author_id, mentor_id | ||
FROM ( | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'John Doe') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Alice Green') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Jane Smith') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Olivia Brown') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Alice Green') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Olivia Brown') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Michael Brown') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Jane Smith') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Robert Black') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'John Doe') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Sophia Johnson') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Noah Lee') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Daniel Martinez') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Jane Smith') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Olivia Brown') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Alice Green') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Isabella Clark') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'John Doe') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Mia Hall') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Sophia Johnson') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Noah Lee') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Sophia Johnson') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Amelia Young') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Sophia Johnson') AS mentor_id | ||
UNION ALL | ||
SELECT | ||
(SELECT author_id FROM authors WHERE author_name = 'Sophia Johnson') AS author_id, | ||
(SELECT author_id FROM authors WHERE author_name = 'Mia Hall') AS mentor_id | ||
) AS temp_values`; | ||
|
||
const update_mentor_id_query = ` | ||
UPDATE authors AS a1 | ||
JOIN temp_author_mentor AS tam ON a1.author_id = tam.author_id | ||
SET a1.mentor_id = tam.mentor_id`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good that you want to explore more SQL syntax in this assignment.
In real working situation it's better to avoid over-complicated code (for example UNION ALL
and TEMPORARY TABLE
). It's hard to read and maintain for long term. I don't see people use TEMPORARY TABLE
for backend. I think it's more relevant when you're doing some complex data analysis and want to cache some intermediate data.
const insert_author_paper_query = `INSERT INTO author_paper (author_id, paper_id) VALUES | ||
((SELECT author_id FROM authors WHERE author_name = 'John Doe'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Deep Learning Advances')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'John Doe'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Quantum Computing Innovations')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Jane Smith'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Blockchain for Supply Chains')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Jane Smith'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Neural Networks in Medicine')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Alice Green'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Climate Change Modeling')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Alice Green'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Advances in Robotics')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Michael Brown'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Ethical AI')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Michael Brown'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Renewable Energy Innovations')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Emily White'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Deep Learning Advances')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Emily White'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Genetic Algorithms in Biology')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Robert Black'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Neural Networks in Medicine')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Robert Black'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Human-Computer Interaction Design')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Sophia Johnson'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Space Exploration and AI')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Sophia Johnson'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Cybersecurity in IoT')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Daniel Martinez'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Machine Learning in Healthcare')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Daniel Martinez'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Deep Learning Advances')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Olivia Brown'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Advances in Cryptography')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'James Wilson'), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Bioinformatics Tools')), | ||
|
||
((SELECT author_id FROM authors WHERE author_name = 'Isabella Clark '), | ||
(SELECT paper_id FROM research_papers WHERE paper_title = 'Self-Driving Cars'))`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not recommended to write this kind of code in real working situation. It's hard to read and maintain.
Usually in real work, all mock/test data will be written into a JSON file and then populated into database. Unless you have complex queries, you will use ORM a lot when you're writing backend code, and ORM can help you hide a lot of this complexities.
From my personal experience, in most cases, the most complicated SQL query in real work contains a lot of JOINs or subqueries. Usually when you found yourself are using too many JOINs and subqueries for a single service (queries across multiple services owned by multiple teams, of course, are forgivable), it implies the database schema design might have some problems 😅
university VARCHAR(255), | ||
date_of_birth DATE, | ||
h_index INT(100), | ||
gender VARCHAR(100) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: gender usually is a set of options: female, male, or undefined. For a set with the finite number of options which hardly changes (gender's definition is pretty stable), we usually use type ENUM
.
We can give names for different options in a ENUM, but the value is actually stored as an integer behind the scene. For example, we define ENUM ('M', 'F', 'X')
. M
is stored as 0
underlying.
JOIN authors ON author_paper.author_id = authors.author_id | ||
WHERE gender = 'Female'`; | ||
|
||
const average_h_index_query = `SELECT university AS 'University', ROUND(AVG(h_index), 2) AS 'Average h-index' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice try to use ROUND()
👍
await connection.query(use_database_query); | ||
const [result_all_paper_count_author_query] = await connection.query(all_paper_count_author_query); | ||
console.log(result_all_paper_count_author_query); | ||
const [result_research_paper_publishing_woman_query] = await connection.query(research_paper_publishing_woman_query); | ||
console.log(result_research_paper_publishing_woman_query); | ||
const [result_average_h_index_query] = await connection.query(average_h_index_query); | ||
console.log(result_average_h_index_query); | ||
const [result_sum_papers_university_query] = await connection.query(sum_papers_university_query); | ||
console.log(result_sum_papers_university_query); | ||
const [result_min_max_h_index_university_query] = await connection.query(min_max_h_index_university_query); | ||
console.log(result_min_max_h_index_university_query); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing tabs at the beginning. Not a big problem, especially if you use auto formatter
No description provided.