-
Notifications
You must be signed in to change notification settings - Fork 3
Ilias_Khugaev-w2-Databases #13
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| import mysql from 'mysql2/promise'; | ||
|
|
||
| const connection = await mysql.createConnection({ | ||
| host: 'localhost', | ||
| user: 'hyfuser', | ||
| password: 'hyfpassword' | ||
| }); | ||
|
|
||
|
|
||
| export const aggreg = async() => { | ||
| connection.query("SELECT rp.paper_id,rp.paper_title, COUNT(ap.author_id) AS number_of_authors FROM research_papers rp LEFT JOIN author_papers ap ON rp.paper_id = ap.paper_id GROUP BY rp.paper_id, rp.paper_title"); | ||
| connection.query("SELECT COUNT(ap.paper_id) AS total_papers_by_female_authors FROM authors a JOIN author_papers ap ON a.author_id = ap.author_id a.gender = 'female';"); | ||
| connection.query("SELECT AVG(h_index) AS avg_h_index FROM authors GROUP BY university"); | ||
| connection.query("SELECT a.university, COUNT(ap.paper_id) AS total_papers FROM authors a JOIN author_papers ap ON a.author_id = ap.author_id GROUP BY a.university"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, if several authors from the same university write the same paper, this paper will be counted multiple times. |
||
| connection.query("SELECT university, MIN(h_index), MAX(h_index) FROM authors GROUP BY university"); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import mysql from 'mysql2/promise'; | ||
|
|
||
| const connection = await mysql.createConnection({ | ||
| host: 'localhost', | ||
| user: 'hyfuser', | ||
| password: 'hyfpassword' | ||
| }); | ||
|
|
||
| export const joins = async() => { | ||
| connection.query("SELECT author_name, mentor FROM authors"); | ||
| connection.query("SELECT authors.author_name, research_papers.paper_title FROM authors LEFT JOIN author_papers ON authors.author_id = author_papers. author_id LEFT JOIN research_papers ON research_papers.paper_id = author_papers.paper_id"); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import mysql from 'mysql2/promise'; | ||
| import {relationships} from "./Relationships.js"; | ||
| import {aggreg} from "./Aggregate-Functions.js"; | ||
|
|
||
| const connection = await mysql.createConnection({ | ||
| host: 'localhost', | ||
| user: 'hyfuser', | ||
| password: 'hyfpassword', | ||
| database: 'week2' | ||
| }); | ||
|
|
||
| try { | ||
| await connection.query('DROP DATABASE IF EXISTS week2'); | ||
| await connection.query('CREATE DATABASE IF NOT EXISTS week2'); | ||
| await connection.query('USE week2'); | ||
|
|
||
| await connection.query( | ||
| `CREATE TABLE IF NOT EXISTS authors ( | ||
| author_id INT AUTO_INCREMENT PRIMARY KEY, | ||
| author_name VARCHAR(50) NOT NULL, | ||
| university VARCHAR(50), | ||
| date_of_birth DATE NOT NULL, | ||
| h_index INT NOT NULL, | ||
| mentor INT, | ||
| gender ENUM('male', 'female', 'other'), | ||
| FOREIGN KEY (mentor) REFERENCES authors(author_id) | ||
| )` | ||
| ); | ||
|
|
||
| await connection.query( | ||
| `CREATE TABLE IF NOT EXISTS research_papers ( | ||
| paper_id INT AUTO_INCREMENT PRIMARY KEY, | ||
| paper_title VARCHAR(100) NOT NULL, | ||
| conference TEXT | ||
| )` | ||
| ); | ||
|
|
||
| await connection.query( | ||
| `CREATE TABLE IF NOT EXISTS author_papers ( | ||
| author_id INT, | ||
| paper_id INT, | ||
| PRIMARY KEY (author_id, paper_id), | ||
| FOREIGN KEY (author_id) REFERENCES authors(author_id), | ||
| FOREIGN KEY (paper_id) REFERENCES research_papers(paper_id) | ||
| )` | ||
| ); | ||
|
|
||
| relationships(); | ||
| console.log('Tables created successfully!'); | ||
| } catch (error) { | ||
| console.error('Error creating tables:', error); | ||
| } finally { | ||
| await connection.end(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import mysql from 'mysql2/promise'; | ||
|
|
||
| const connection = await mysql.createConnection({ | ||
| host: 'localhost', | ||
| user: 'hyfuser', | ||
| password: 'hyfpassword', | ||
| database: 'week2' | ||
| }); | ||
|
|
||
| export const relationships = async () => { | ||
| try { | ||
| await connection.query('USE week2'); | ||
| await connection.query(` | ||
| INSERT INTO authors (author_name, university, date_of_birth, h_index, gender) VALUES | ||
| ('Alice Smith', 'MIT', '1980-05-14', 32, 'female'), | ||
| ('Bob Johnson', 'Stanford', '1975-03-22', 40, 'male'), | ||
| ('Carol White', 'Harvard', '1982-07-11', 28, 'female'), | ||
| ('David Brown', 'Cambridge', '1990-01-09', 18, 'male'), | ||
| ('Eve Davis', 'Oxford', '1987-11-29', 35, 'female'), | ||
| ('Frank Miller', 'ETH Zurich', '1978-08-30', 30, 'male'), | ||
| ('Grace Wilson', 'Caltech', '1985-09-20', 25, 'female'), | ||
| ('Henry Moore', 'Princeton', '1991-04-17', 22, 'male'), | ||
| ('Ivy Taylor', 'Yale', '1983-02-13', 27, 'female'), | ||
| ('Jack Anderson', 'Columbia', '1986-06-06', 29, 'male'), | ||
| ('Karen Thomas', 'UCLA', '1979-12-19', 31, 'female'), | ||
| ('Leo Jackson', 'Toronto', '1988-10-23', 19, 'male'), | ||
| ('Mona Lewis', 'TUM', '1993-03-03', 21, 'female'), | ||
| ('Nina Harris', 'Sorbonne', '1990-09-09', 24, 'female'), | ||
| ('Oscar Martin', 'EPFL', '1984-04-04', 33, 'male'); | ||
| `); | ||
|
|
||
| await connection.query(` | ||
| INSERT INTO research_papers (paper_title, conference) VALUES | ||
| ('Quantum AI', 'NeurIPS'), | ||
| ('Deep Learning Optimization', 'ICML'), | ||
| ('Neural Graphs', 'CVPR'), | ||
| ('Bioinformatics Trends', 'RECOMB'), | ||
| ('Genetic Algorithms', 'GECCO'), | ||
| ('Quantum Circuits', 'QIP'), | ||
| ('AI in Medicine', 'MedConf'), | ||
| ('Secure ML', 'IEEE S&P'), | ||
| ('Blockchain Systems', 'CryptoCon'), | ||
| ('Data Visualization', 'VIS'), | ||
| ('NLP Advancements', 'ACL'), | ||
| ('Ethics of AI', 'AAAI'), | ||
| ('Robotics Control', 'ICRA'), | ||
| ('Swarm Intelligence', 'ANTS'), | ||
| ('3D Vision', 'SIGGRAPH'), | ||
| ('Cloud Security', 'USENIX'), | ||
| ('Edge Computing', 'EdgeConf'), | ||
| ('IoT Protocols', 'IoTConf'), | ||
| ('Image Compression', 'ICIP'), | ||
| ('Virtual Reality', 'VRConf'), | ||
| ('BioNLP', 'EMNLP'), | ||
| ('AI+Law', 'LAWConf'), | ||
| ('Graph Theory AI', 'NeurIPS'), | ||
| ('Medical Robotics', 'MedConf'), | ||
| ('AI for Education', 'EDM'), | ||
| ('Data Ethics', 'FAT*'), | ||
| ('ML at Scale', 'BigDataConf'), | ||
| ('Vision & Language', 'CVPR'), | ||
| ('Quantum ML', 'QMLConf'), | ||
| ('Adversarial Attacks', 'BlackHat'); | ||
| `); | ||
|
|
||
| await connection.query(` | ||
| INSERT INTO author_papers (author_id, paper_id) VALUES | ||
| (6, 8), | ||
| (6, 9), | ||
| (7, 10), | ||
| (8, 11), | ||
| (9, 12), | ||
| (10, 13), | ||
| (10, 14), | ||
| (11, 15), | ||
| (12, 16), | ||
| (12, 17), | ||
| (13, 18), | ||
| (14, 19), | ||
| (14, 20), | ||
| (15, 21), | ||
| (15, 22), | ||
| (5, 23), | ||
| (7, 24), | ||
| (8, 25), | ||
| (9, 26), | ||
| (11, 27), | ||
| (13, 28), | ||
| (2, 29), | ||
| (4, 30); | ||
| `); | ||
|
|
||
| console.log('Data inserted successfully!'); | ||
| } catch (err) { | ||
| console.error('Error inserting data:', err); | ||
| } finally { | ||
| await connection.end(); | ||
| } | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "name": "assignment", | ||
| "version": "1.0.0", | ||
| "main": "authors.js", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "echo \"Error: no test specified\" && exit 1" | ||
| }, | ||
| "author": "", | ||
| "license": "ISC", | ||
| "description": "", | ||
| "dependencies": { | ||
| "assignment": "file:", | ||
| "mysql2": "^3.14.1" | ||
| } | ||
| } |
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.
Please notice this query has syntax error:
"SELECT COUNT(ap.paper_id) AS total_papers_by_female_authors FROM authors a JOIN author_papers ap ON a.author_id = ap.author_id a.gender = 'female';"You're missing
where.And notice that you will count the same paper for multiple times if several female authors write this paper. If you don't want to count duplicated paper, you need to use
DISTINCT.