<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/nodejs-mysql-create-table-d2unnfnaf" -->

---
title: NodeJS MySQL Create Table | daily.dev
description: A step-by-step walkthrough shows how to create a MySQL table from Node.js using the mysql2 driver, covering both callback and promise/async styles. It explains...
canonical: https://daily.dev/posts/nodejs-mysql-create-table-d2unnfnaf
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: NodeJS MySQL Create Table | daily.dev
og:description: A step-by-step walkthrough shows how to create a MySQL table from Node.js using the mysql2 driver, covering both callback and promise/async styles. It explains...
og:url: https://daily.dev/posts/nodejs-mysql-create-table-d2unnfnaf
og:image: https://api.daily.dev/og/posts/d2unNfnaf.png
og:image:alt: NodeJS MySQL Create Table
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# NodeJS MySQL Create Table

**[Planet MySQL](https://daily.dev/sources/planet-mysql)** · 12 min read · 0 upvotes · 0 comments

## Summary

A step-by-step walkthrough shows how to create a MySQL table from Node.js using the mysql2 driver, covering both callback and promise/async styles. It explains the CREATE TABLE SQL syntax, how to make table creation idempotent with IF NOT EXISTS or by catching the ER_TABLE_EXISTS_ERROR (1050) error code, common connection errors like ER_NO_DB_ERROR (1046) and ER_ACCESS_DENIED_ERROR (1045), and how to verify results with DESCRIBE and SHOW CREATE TABLE. Tested on Node v26.7.0 with mysql2 3.24.4 against MariaDB 10.11.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://codeforgeek.com/nodejs-mysql-create-table>

## Questions this post answers

### How do I make a Node.js CREATE TABLE script safe to run multiple times without crashing?

Add IF NOT EXISTS to the CREATE TABLE statement so MySQL silently skips creation if the table already exists, or catch the ER_TABLE_EXISTS_ERROR (errno 1050) in your query callback and log it instead of throwing. Checking err.errno or err.code is more reliable than parsing the error message text, since the numeric code stays stable across MySQL versions while messages can be localized.

_Track patterns like idempotent table creation and error-code handling on daily.dev as you harden Node database scripts._

### Why am I getting ER_NO_DB_ERROR 1046 when running a CREATE TABLE query in Node.js with mysql2?

This error occurs when the database field is omitted from the mysql2 createConnection options, since CREATE TABLE does not select a database on its own. Fixing it means adding the database name (for example testdb) to the connection config, or creating and selecting the target database before running any table-creation queries, even if the SQL itself is valid.

_daily.dev helps developers debugging MySQL connection errors find fixes for issues like ER_NO_DB_ERROR quickly._

### Should I use the mysql or mysql2 npm package for a new Node.js project connecting to MySQL?

Choose mysql2 for new code because it is a drop-in replacement for the mysql package that adds promise support and prepared statements without changing the query string syntax. The original mysql package (version 2.18.x) still works but is in maintenance mode, so it should only be kept for legacy projects already pinned to it.

_Developers choosing a MySQL driver for Node.js can compare options like mysql versus mysql2 on daily.dev._

## Similar posts on daily.dev

- [NodeJS MySQL Create Database](https://daily.dev/posts/nodejs-mysql-create-database-sccp02rys) · Planet MySQL · 0 upvotes · 0 comments
- [NodeJS MySQL Drop Table](https://daily.dev/posts/nodejs-mysql-drop-table-yzbbzf2bn) · Planet MySQL · 0 upvotes · 0 comments
- [NodeJS MySQL Create Connection](https://daily.dev/posts/nodejs-mysql-create-connection-9vbhox1al) · Planet MySQL · 0 upvotes · 0 comments
- [Node.js MySQL Insert Record](https://daily.dev/posts/node-js-mysql-insert-record-hsrhzgs3u) · Planet MySQL · 0 upvotes · 0 comments
- [NodeJS MySQL Select Record](https://daily.dev/posts/nodejs-mysql-select-record-sl7ytxmxc) · Planet MySQL · 0 upvotes · 0 comments

---

Tags: [#nodejs](https://daily.dev/tags/nodejs), [#backend](https://daily.dev/tags/backend), [#sql](https://daily.dev/tags/sql), [#mysql](https://daily.dev/tags/mysql)

[View this post on daily.dev](https://daily.dev/posts/nodejs-mysql-create-table-d2unnfnaf)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"NodeJS MySQL Create Table","url":"https://daily.dev/posts/nodejs-mysql-create-table-d2unnfnaf","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/nodejs-mysql-create-table-d2unnfnaf"},"datePublished":"2026-09-13T11:46:36.470Z","dateModified":"2026-09-13T11:46:58.332Z","description":"A step-by-step walkthrough shows how to create a MySQL table from Node.js using the mysql2 driver, covering both callback and promise/async styles. It explains...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/37613a68b40fb8385e640570c0d7f3fd?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/37613a68b40fb8385e640570c0d7f3fd?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Planet MySQL","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Planet MySQL","logo":"https://media.daily.dev/image/upload/s--UKAtmXTq--/f_auto,q_auto/v1780213421/logos/planet-mysql?_a=BAMAMiWQ0","url":"https://daily.dev/sources/planet-mysql"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/nodejs-mysql-create-table-d2unnfnaf","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"nodejs,backend,sql,mysql","timeRequired":"PT12M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Planet MySQL","item":"https://daily.dev/sources/planet-mysql"},{"@type":"ListItem","position":3,"name":"NodeJS MySQL Create Table"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/nodejs-mysql-create-table-d2unnfnaf#faq","mainEntity":[{"@type":"Question","name":"How do I make a Node.js CREATE TABLE script safe to run multiple times without crashing?","acceptedAnswer":{"@type":"Answer","text":"Add IF NOT EXISTS to the CREATE TABLE statement so MySQL silently skips creation if the table already exists, or catch the ER_TABLE_EXISTS_ERROR (errno 1050) in your query callback and log it instead of throwing. Checking err.errno or err.code is more reliable than parsing the error message text, since the numeric code stays stable across MySQL versions while messages can be localized. Track patterns like idempotent table creation and error-code handling on daily.dev as you harden Node database scripts."}},{"@type":"Question","name":"Why am I getting ER_NO_DB_ERROR 1046 when running a CREATE TABLE query in Node.js with mysql2?","acceptedAnswer":{"@type":"Answer","text":"This error occurs when the database field is omitted from the mysql2 createConnection options, since CREATE TABLE does not select a database on its own. Fixing it means adding the database name (for example testdb) to the connection config, or creating and selecting the target database before running any table-creation queries, even if the SQL itself is valid. daily.dev helps developers debugging MySQL connection errors find fixes for issues like ER_NO_DB_ERROR quickly."}},{"@type":"Question","name":"Should I use the mysql or mysql2 npm package for a new Node.js project connecting to MySQL?","acceptedAnswer":{"@type":"Answer","text":"Choose mysql2 for new code because it is a drop-in replacement for the mysql package that adds promise support and prepared statements without changing the query string syntax. The original mysql package (version 2.18.x) still works but is in maintenance mode, so it should only be kept for legacy projects already pinned to it. Developers choosing a MySQL driver for Node.js can compare options like mysql versus mysql2 on daily.dev."}}]}
```

