nodejs
  1. nodejs-mysql-create-table

Node.js MySQL Create Table

In Node.js, we can use the MySQL module to connect to a MySQL database and create tables within it. In this tutorial, we'll discuss how to create tables in MySQL using Node.js.

Syntax

The syntax for creating a table in MySQL using Node.js is as follows:

const mysql = require('mysql');
const connection = mysql.createConnection({
    host: 'localhost',
    user: 'username',
    password: 'password',
    database: 'database_name'
});

const createTableSql = `CREATE TABLE table_name (
    column1_name column1_type constraints,
    column2_name column2_type constraints,
    ...
    PRIMARY KEY (primary_key_column_name)
);`;

connection.query(createTableSql, (err, results) => {
    if (err) throw err;
    console.log('Table created successfully');
});

Here, we first include the MySQL module and create a connection to our database using the createConnection() method. We then define a SQL query to create our table, as a string variable (in this case createTableSql).

We use the query() method of the connection object to execute the SQL query. If the table creation is successful, we will get a Table created successfully message in the console.

Example

Let's say we want to create a table called employees with the following columns and data types:

  • id (integer)
  • name (string)
  • age (integer)
  • email (string)

Here's how we can implement it:

const mysql = require('mysql');

const connection = mysql.createConnection({
    host: 'localhost',
    user: 'username',
    password: 'password',
    database: 'database_name'
});

const createTableSql = `CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255),
    age INT,
    email VARCHAR(255)
);`;

connection.query(createTableSql, (err, results) => {
    if (err) throw err;
    console.log('Table created successfully');
});

connection.end();

This will create a table called employees with four columns, as specified in the SQL query.

Output

When we run the example code above, we will get a Table created successfully message in the console if the table creation is successful.

Explanation

In the example above, we created a mysql connection object and defined a SQL query to create a table named employees with the provided columns and data types. We then used the query() method of the connection object to execute the SQL query and create the table.

If the table creation is successful, we will get a Table created successfully message in the console.

Use

We can use Node.js and MySQL to create tables in a MySQL database. Creating tables is an important step towards developing a relational database.

Important Points

  • When creating tables, make sure to define the correct data types and constraints for each column.
  • Be sure to use semicolons to terminate the SQL statements.
  • Always sanitize user inputs to prevent SQL injection attacks.

Summary

In this tutorial, we discussed how to use Node.js to create a table in MySQL. We covered the syntax, example, output, explanation, use, and important points of creating tables in MySQL using Node.js. With this knowledge, you can now create tables in MySQL with Node.js and use them to store data.

Published on: