1.const mysql = require('mysql2'); const connection = mysql.createConnection({ host: localhost, user: root, ...
Question:
1.const mysql = require('mysql2');
const connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "Reset2015"
});
connection.connect((err)=>{
if (err){
return console.log('error' + err.message);
}
console.log('Connected!!!');
});
connection.end((err)=>{
if (err){
return console.log('error' + err.message);
}
console.log('Closing the database connection');
});
2.Change the name of the database connection object to a shorter name, such as db, for convenience.
3. Creating the Database: To manage a database from within a .js file, you need first to make a string that holds the SQL command and then call the query() method on the database connection object passing to it as a parameter the string that holds the SQL command and a callback function.
a. Declare the string variable sql to hold the SQL command for creating a new database called StudTracking.
b. Pass sql and a call back function to the query() function
c. Save connect1.js and execute it from the command prompt.
d. Check that the new database has been created on the MySQL server. e. If you execute connect1.js again you will receive the following error that indicates that the database already exists. Error: Can't create database 'StudTrack'; database exists
f. To avoid this error, add the clause IF NOT EXISTS to the sql command creating the database. Check the MySQL Cheat sheet for the correct syntax.
4. Creating the Database Table: The code for creating the tables in the database will be written after the code for creating the Database developed in the previous step and before the call to the end().
a. Creating the Student Table: assign the string variable sql to hold the SQL command for creating the student table. Note that for the S-code, it is assumed that the student code will be composed of at most 5 characters provided by the user.
b. Call the query() function again passing to it the SQL command stored in sql and a callback function as before. Note that you might receive the following errors: i. Error: No database selected. (Hint: you need to specify the database used. One solution is to precede the table name with the database name or check the online manual for another solution) ii. Error: Table 'students' already exists. (Hint: the solution is similar to that used for the database above. Check the online manual for syntax) c. Run your script twice to ensure that you handled all errors
d. Create the Course table applying similar steps as before.
e. Create StudentCourse table applying similar steps as before.
f. After correctly executing your code, check the database and the tables on the MySQL server to ensure that you have created all tables correctly including constraints for let sql = "Fill in SQL COMMAND TO CREATEs A DATABASE"; sql = "Fill in SQL COMMAND TO CREATE Student TABLE"; the primary keys and foreign keys. If there are any errors, delete the table that has an error and update the SQL command accordingly. Then run your code again.