EN
Node.js - MySQL Create table
0
points
In this article, we would like to show you how to create a table in Node.js.
const mysql = require('mysql');
const connection = mysql.createConnection({ // gets connection with database
host: 'localhost', // '127.0.0.1'
user: 'root',
password: 'password',
database: 'database_name',
});
connection.connect((error) => {
if (error) throw error;
const query = `CREATE TABLE users (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
role VARCHAR(15) NOT NULL,
PRIMARY KEY (id)
)`;
connection.query(query, (error, result) => { // sends queries
connection.end(); // closes connection
if (error) throw error;
console.log(result);
});
});
Database:
Note:
To create a multi-line string, create a
template literal
usingbackticks
``
.