0% found this document useful (0 votes)
47 views2 pages

Rashi Project

This document connects to a MySQL database and sets up the database by: 1) Creating two tables, inserting sample data, and joining the tables in a query. 2) Removing a row from each table using another query. 3) Dropping one of the tables to conclude setting up the database. All queries are executed asynchronously using promises to handle errors.

Uploaded by

Attulaya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views2 pages

Rashi Project

This document connects to a MySQL database and sets up the database by: 1) Creating two tables, inserting sample data, and joining the tables in a query. 2) Removing a row from each table using another query. 3) Dropping one of the tables to conclude setting up the database. All queries are executed asynchronously using promises to handle errors.

Uploaded by

Attulaya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

const mysql = require('mysql2');

// Replace these values with your actual MySQL server configuration


const connectionConfig = {
host: 'your_mysql_host',
user: 'your_mysql_username',
password: 'your_mysql_password',
database: 'your_mysql_database',
};

// Create a connection pool


const pool = mysql.createPool(connectionConfig);

// Function to execute a SQL query


function executeQuery(query) {
return new Promise((resolve, reject) => {
pool.query(query, (err, results) => {
if (err) {
reject(err);
} else {
resolve(results);
}
});
});
}

// Create the first table


const createTable1 = `CREATE TABLE table1 (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255),
age INT
);`;

// Create the second table


const createTable2 = `CREATE TABLE table2 (
id INT AUTO_INCREMENT PRIMARY KEY,
city VARCHAR(255),
country VARCHAR(255)
);`;

// Insert data into table1


const insertDataIntoTable1 = `INSERT INTO table1 (name, age) VALUES
('John', 30),
('Alice', 25),
('Bob', 28);`;

// Insert data into table2


const insertDataIntoTable2 = `INSERT INTO table2 (city, country) VALUES
('New York', 'USA'),
('London', 'UK'),
('Paris', 'France');`;

// SQL query to join the two tables


const joinTablesQuery = `SELECT * FROM table1
INNER JOIN table2 ON table1.id = table2.id;`;

// SQL query to remove one row from each table


const removeRowQuery = `DELETE FROM table1 WHERE name = 'John';
DELETE FROM table2 WHERE city = 'Paris';`;

const dropTable1Query = 'DROP TABLE table1;';

// Execute all the queries in sequence


async function setupDatabase() {
try {
await executeQuery(createTable1);
await executeQuery(createTable2);
await executeQuery(insertDataIntoTable1);
await executeQuery(insertDataIntoTable2);

// Join the tables


const joinedData = await executeQuery(joinTablesQuery);
console.log('Joined Data:', joinedData);

// Remove a row from each table


await executeQuery(removeRowQuery);

await executeQuery(dropTable1Query);

// Close the pool (optional, usually done when the application is shutting down)
pool.end((error) => {
if (error) {
console.error('Error closing the connection pool:', error);
} else {
console.log('Connection pool closed.');
}
});
} catch (err) {
console.error('Error performing database setup:', err);
}
}

setupDatabase();

You might also like