Node.js MySQL Create Table
Creating a database table asynchronously in Node.js using mysql2 library.
const mysql = require('mysql2/promise');
async function createTable() {
const connection = await mysql.createConnection({
host: 'localhost', user: 'root', password: 'password', database: 'company_db'
});
const sql = `
CREATE TABLE IF NOT EXISTS system_logs (
log_id INT AUTO_INCREMENT PRIMARY KEY,
level VARCHAR(20) NOT NULL,
message TEXT NOT NULL,
logged_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`;
await connection.execute(sql);
console.log('Table system_logs created successfully in Node.js!');
await connection.end();
}
createTable();Table system_logs created successfully in Node.js!