- Node.js Tutorial
- NodeJS Home
- NodeJS Introduction
- NodeJS Setup
- NodeJS First App
- NodeJS REPL
- NodeJS Command Line
- NodeJS NPM
- NodeJS Callbacks
- NodeJS Events
- NodeJS Event-Loop
- NodeJS Event-Emitter
- NodeJS Global-Objects
- NodeJS Console
- NodeJS Process
- NodeJS Buffers
- NodeJS Streams
- Node.js File Handling
- Node.js File System
- Node.js Read/Write File
- Working with folders in Node.js
- HTTP and Networking
- Node.js HTTP Module
- Anatomy of an HTTP Transaction
- Node.js MongoDB
- MongoDB Get Started
- MongoDB Create Database
- MongoDB Create Collection
- MongoDB Insert
- MongoDB Find
- MongoDB Query
- MongoDB Sort
- MongoDB Delete
- MongoDB Update
- MongoDB Limit
- MongoDB Join
- Node.js MySQL
- MySQL Get Started
- MySQL Create Database
- MySQL Create Table
- MySQL Insert Into
- MySQL Select From
- MySQL Where
- MySQL Order By
- MySQL Delete
- MySQL Update
- MySQL Join
- Node.js Modules
- Node.js Modules
- Node.js Built-in Modules
- Node.js Utility Modules
- Node.js Web Module
- Node.js Advanced
- Node.js Debugger
- Node.js Scaling Application
- Node.js Packaging
- Node.js Express Framework
- Node.js RESTFul API
- Node.js Useful Resources
- Node.js Useful Resources
- Node.js Discussion
Node.js MySQL Create Table
In Node.js, creating a database table is the fundamental step in defining your application's data structure. Think of a table as a blueprint that dictates how your data—like users, products, or orders—will be stored and organized. Using the mysql2 package, you can send SQL commands from your JavaScript code directly to your MySQL server. This allows you to automate database setup and manage your schema programmatically.
mysql package is still around, modern developers prefer mysql2 because it is faster, supports prepared statements for better security, and includes built-in support for Promises.
Key Features of Node.js MySQL Table Creation
- Schema Definition: Use standard SQL
CREATE TABLEsyntax to define the architecture of your data storage. - Data Constraints: Precisely define column types (like
INTfor numbers orVARCHARfor text) and constraints likeNOT NULLorUNIQUEto ensure data integrity. - Primary Keys: Automatically generate unique identifiers for every row using
AUTO_INCREMENT, which is essential for relational database mapping. - Flexible Control Flow: Choose between traditional callbacks for simpler scripts or modern
async/awaitsyntax for cleaner, more readable application code.
Step 1 Prerequisites
Before writing your script, ensure you have a MySQL server running and a database already created (e.g., testdb). You will also need to initialize your Node.js project and install the driver:
npm install mysql2
Step 2 Create a Table with Callback
The callback pattern is the classic way of handling asynchronous operations in Node.js. It’s useful for quick scripts, though it can become difficult to manage in larger applications (a situation often called "callback hell"). In this example, we define a users table structure and execute it through the query() method.
Example Code
const mysql = require('mysql2');
// Create a connection to the database
const connection = mysql.createConnection({
host: 'localhost',
user: 'root', // Replace with your MySQL username
password: '', // Replace with your MySQL password
database: 'testdb' // The database must already exist
});
// Establish the connection
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err.stack);
return;
}
console.log('Connected to MySQL as id ' + connection.threadId);
// Define the SQL query
// We use AUTO_INCREMENT for the ID so MySQL handles the numbering for us
const createTableQuery = `
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`;
connection.query(createTableQuery, (err, results) => {
if (err) {
console.error('Error creating table:', err.message);
return;
}
console.log('Table created successfully:', results);
});
// Always close the connection to prevent memory leaks
connection.end();
});
Table 'users' already exists. In a production environment, this could crash your startup script.
Output:
Connected to MySQL as id 1
Table created successfully:
{ fieldCount: 0, affectedRows: 0, insertId: 0, serverStatus: 2, warningCount: 0, message: '', protocol41: true, changedRows: 0 }
Step 3 Create a Table with Promises
Modern Node.js development relies heavily on Promises and async/await. This makes your code look synchronous and much easier to follow, especially when you need to perform multiple database operations in a specific order.
IF NOT EXISTS in your SQL query. This prevents the "Table already exists" error and makes your script "idempotent"—meaning it can be run multiple times without causing errors.
Example Code with Promises
const mysql = require('mysql2/promise');
async function createTable() {
// Using the promise-based connection
const connection = await mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'testdb'
});
try {
// Adding 'IF NOT EXISTS' is a best practice for setup scripts
const createTableQuery = `
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`;
const [results] = await connection.execute(createTableQuery);
console.log('Table check/creation completed:', results);
} catch (err) {
console.error('Database Operation Failed:', err.message);
} finally {
// The finally block ensures the connection closes even if an error occurs
await connection.end();
console.log('Connection closed.');
}
}
createTable();
Output:
Table check/creation completed:
{ fieldCount: 0, affectedRows: 0, insertId: 0, serverStatus: 2, warningCount: 0, message: '', protocol41: true, changedRows: 0 }
Connection closed.
Step 4 Modifying Table Structure
Real-world applications evolve. You might start with a users table and later realize you need to store the user's age or a profile picture URL. Instead of deleting the table and losing your data, you use the ALTER TABLE command.
Example Code to Add a Column
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'testdb'
});
connection.connect((err) => {
if (err) throw err;
// Real-world scenario: Adding a column for user age
const alterTableQuery = 'ALTER TABLE users ADD COLUMN IF NOT EXISTS age INT';
connection.query(alterTableQuery, (err, results) => {
if (err) {
console.error('Error altering table:', err.message);
} else {
console.log('Table updated successfully.');
}
connection.end();
});
});
ALTER TABLE scripts manually. They use "Migration" tools like Knex.js or Sequelize to track changes to the database over time.
Step 5 Error Handling and Cleanup
Robust error handling is what separates a demo script from production-ready code. You must account for connection timeouts, incorrect credentials, and SQL syntax errors. Furthermore, managing the connection lifecycle is critical; leaving connections open can eventually crash your database server by exhausting the "max connections" limit.
Example Code with Proper Error Handling
const mysql = require('mysql2');
const dbConfig = {
host: 'localhost',
user: 'root',
password: '',
database: 'testdb'
};
const connection = mysql.createConnection(dbConfig);
connection.connect((err) => {
if (err) {
return console.error('FAILED TO CONNECT:', err.message);
}
const sql = `CREATE TABLE IF NOT EXISTS orders (
id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(100),
amount DECIMAL(10, 2)
)`;
connection.query(sql, (err, results) => {
if (err) {
console.error('QUERY ERROR:', err.message);
} else {
console.log('Orders table is ready.');
}
// Always attempt to close the connection, regardless of query success
connection.end((closeErr) => {
if (closeErr) console.error('Error while closing connection:', closeErr.message);
});
});
});
mysql.createPool) instead of single connections. Pools handle multiple users much more efficiently and automatically manage the opening and closing of connections for you.
Summary
Creating and managing MySQL tables in Node.js is a straightforward process once you understand the relationship between the JavaScript environment and SQL queries. By using the mysql2 package, you gain the flexibility of both callbacks and promises. Remember to always sanitize your data, handle errors gracefully using try/catch or callback checks, and use IF NOT EXISTS to keep your setup scripts reliable across different environments.