DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
VGSources
Blog

How to Create a Database and Insert Into a Table in MySQL

A practical MySQL walkthrough covering CREATE DATABASE, USE, CREATE TABLE, INSERT INTO, verification queries, qualified names, upserts, privileges, and common errors.
WorldMySQL Length8 min Posted Quest giverVGSources Team

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In MySQL, create the schema first, select it with USE, create a table, then add rows with INSERT INTO. The complete sequence is CREATE DATABASE (or reuse an existing schema), USE, CREATE TABLE, INSERT INTO, and a verification query such as SELECT.

What you need before running the SQL

You need access to a running MySQL server and a user account with the privileges required for each operation. You can run the statements in the MySQL command-line client, a graphical SQL editor, or an application connection.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • The account needs CREATE privilege to create a database and to create a table.
  • The account needs INSERT privilege to add rows.
  • If you use ON DUPLICATE KEY UPDATE, the account also needs UPDATE privilege on the columns changed by that branch.

Connect to the server first. For a local server, a typical command-line connection is:

mysql -u your_user -p

The client prompts for the password. If the server is on another host or uses a non-default port, supply the connection options required by that server. Once connected, the SQL statements below are executed in the current session.

#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter | 4Pack Type C Female to A Male Cable Converter for Apple Duo 17 16 15 14 Plus,iWatch Watch 12 11 10,iPad 9 8 Air,Airpods 5 4
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

1. Create a database or reuse one that already exists

An unqualified table name belongs to the database selected for the session, so a database (also called a schema in MySQL) must exist before you create that table. To create a new schema without failing when it is already present, use:

CREATE DATABASE IF NOT EXISTS shop;

CREATE DATABASE creates the schema. CREATE SCHEMA is a synonym, so this is equivalent:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
CREATE SCHEMA IF NOT EXISTS shop;

The IF NOT EXISTS clause makes the statement idempotent: an existing shop schema produces a note instead of an error, and the existing schema is left unchanged. Without that clause, attempting to create a database whose name is already in use causes an error.

Creating the schema requires the CREATE privilege for the database. If MySQL reports an access-denied error, ask an administrator to grant the privilege or provide a schema you are allowed to use. Do not try to create a folder manually inside MySQL’s data directory. MySQL manages database directories and table files itself; direct file manipulation is unsupported and can leave the server’s metadata and files inconsistent.

2. Select the schema for your session

After the schema exists, make it the default for subsequent statements:

USE shop;

USE changes the default database for the current connection. The command-line client normally confirms the change with a message such as “Database changed.” Every later unqualified table reference, including CREATE TABLE customers and SELECT * FROM customers, points to shop until you select another schema or disconnect.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Sale
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

You can check which schema is active at any time:

SELECT DATABASE();

If this returns NULL, no default schema is selected. You can still work with fully qualified names such as shop.customers, but using USE shop is usually clearer for a sequence of related statements.

3. Define the table and its columns

With shop selected, create a customers table:

CREATE TABLE customers (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(255)
);

CREATE TABLE defines the table’s columns and constraints. In this example:

  • id INT stores an integer identifier.
  • AUTO_INCREMENT lets MySQL generate the next identifier when an insert omits id.
  • PRIMARY KEY identifies each row uniquely and prevents duplicate id values.
  • name VARCHAR(100) NOT NULL stores text up to 100 characters and requires a value.
  • email VARCHAR(255) stores text up to 255 characters. Because it has no NOT NULL constraint, an insert may omit it or provide NULL.

Creating a table requires the CREATE privilege for that table. If you want a setup script that can be run repeatedly, add IF NOT EXISTS:

CREATE TABLE IF NOT EXISTS customers (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(255)
);

This clause prevents an error when customers already exists, but it does not modify an existing table to match the definition in your script. Use an explicit schema change when you need to add or alter columns.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A table can also be created with a fully qualified name. This is useful when a script should not depend on which schema is selected:

CREATE TABLE shop.customers (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(255)
);

4. Insert a row with an explicit column list

Once the table exists, add a customer with INSERT INTO:

Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter | 85W Max PD, 2 USB 3.0 & 1 USBC Data Ports, SD/TF Card Reader, for Laptops/Mac and Other Type C Devices (Charger Not Included)
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
INSERT INTO customers (name, email)
VALUES ('Ada Lovelace', '[email protected]');

The explicit column list is important. It documents which value belongs to which column and avoids relying on the table’s physical column order. Since id is omitted, MySQL generates it from the AUTO_INCREMENT column. The quoted strings are literal text values; SQL string literals use single quotation marks.

You may include the generated key when you have a deliberate reason to assign it, but then the value must satisfy the primary-key constraint:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
INSERT INTO customers (id, name, email)
VALUES (1001, 'Grace Hopper', '[email protected]');

For a nullable column, use the SQL value NULL without quotation marks. Quoting it as 'NULL' stores the four-character word instead of a null value.

An insert requires the INSERT privilege on the target table. If a required column such as name is omitted, or a value violates a data type or constraint, MySQL rejects the statement rather than creating an invalid row.

5. Verify the table and the inserted data

After loading data, verify both the table’s existence and its contents:

SHOW TABLES;

SELECT *
FROM customers;

SHOW TABLES lists tables in the selected schema. The SELECT query retrieves the rows and lets you confirm the generated identifier and stored values. For a more focused check, select only the columns you need:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
SELECT id, name, email
FROM customers;

To inspect the table definition, use:

DESCRIBE customers;

This displays the column names, data types, nullability, key information, default values, and extra attributes such as auto_increment.

Complete first-run script

The following script creates the schema if necessary, selects it, creates the table if necessary, inserts one row, and verifies the result:

CREATE DATABASE IF NOT EXISTS shop;
USE shop;

CREATE TABLE IF NOT EXISTS customers (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(255)
);

INSERT INTO customers (name, email)
VALUES ('Ada Lovelace', '[email protected]');

SHOW TABLES;
SELECT id, name, email
FROM customers;

Running this script a second time will not recreate the schema or table, but the INSERT statement will attempt to add another customer row. Idempotent creation and duplicate-safe data loading are separate concerns; choose an insert strategy that matches your intended behavior.

Choose the right insert form

INSERT ... VALUES is the clearest form when you are supplying literal values. MySQL also supports other forms for different data sources:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Form Use it when Example
INSERT ... VALUES You are adding one or more literal rows. INSERT INTO customers (name, email) VALUES ('Ada Lovelace', '[email protected]');
INSERT ... SET You prefer assignments written as column-value pairs. INSERT INTO customers SET name = 'Ada Lovelace', email = '[email protected]';
INSERT ... SELECT You are inserting the result of a query, such as copying rows from another table. INSERT INTO customers (name, email) SELECT name, email FROM archived_customers;
INSERT ... TABLE You are copying a table’s rows directly; this form is available in MySQL 8.0.19 and later. INSERT INTO customers TABLE archived_customers;

For multi-row literal inserts, provide one parenthesized value list per row:

INSERT INTO customers (name, email)
VALUES
  ('Ada Lovelace', '[email protected]'),
  ('Grace Hopper', '[email protected]'),
  ('Katherine Johnson', '[email protected]');

Keep the column list aligned with every value list. An explicit list remains safer than relying on all columns appearing in their declaration order, especially after a later schema change.

Best Value
Sale
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display | 85W Max PD, 1 Powered USB-C 5Gbps & 2 Powered USB-A 3.0 5Gbps Data Ports, for MacBook Pro, MacBook Air, Dell and More (Black)
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Handle duplicate keys deliberately

A plain insert fails when a value conflicts with a unique or primary-key constraint. If your application intentionally wants an upsert—insert a new row or update selected columns when a duplicate key is found—use ON DUPLICATE KEY UPDATE:

INSERT INTO customers (name, email)
VALUES ('Ada Lovelace', '[email protected]')
ON DUPLICATE KEY UPDATE
  email = '[email protected]';

The update branch requires UPDATE privilege on the affected columns in addition to INSERT. Do not add this clause merely to suppress errors: it changes existing data when a duplicate key occurs. If duplicates should be rejected and investigated, use the plain INSERT and handle its error instead.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use fully qualified names when several schemas are involved

The same operations can be written without changing the session’s default database:

CREATE TABLE IF NOT EXISTS shop.customers (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(255)
);

INSERT INTO shop.customers (name, email)
VALUES ('Ada Lovelace', '[email protected]');

SELECT id, name, email
FROM shop.customers;

Qualified names make the destination explicit and can prevent accidental writes to the wrong schema. They are especially useful in administrative scripts that touch more than one database. The account still needs the appropriate privileges on the named schema and table.

Troubleshoot common failures

Message or symptom Likely cause What to check
No database selected You ran an unqualified CREATE TABLE, INSERT, or SELECT without selecting a schema. Run USE shop;, or qualify the table as shop.customers.
Access denied while creating the database or table The account lacks the required CREATE privilege. Ask an administrator to grant the privilege or use a schema assigned to your account.
INSERT command denied The account lacks INSERT privilege on the target table. Confirm the connected user and the exact schema/table name, then request the required grant.
Table ... doesn't exist The table was not created, the wrong schema is selected, or the name is misspelled. Run SELECT DATABASE();, SHOW TABLES;, and DESCRIBE customers;.
Table already exists during setup The table is already present and the statement omitted IF NOT EXISTS. Inspect the existing definition before deciding whether to reuse it or apply a schema change.
Duplicate entry The insert conflicts with a primary-key or unique constraint. Correct the key, intentionally use an upsert, or treat the conflict as an application error.
Column ... cannot be null or a missing-default error A required column was omitted or received NULL. Provide a valid value for every required column, or revise the table definition intentionally.

Operational practices that prevent data problems

  • Use an explicit column list in every insert, including multi-row and INSERT ... SELECT statements.
  • Review an existing table before reusing its name; CREATE TABLE IF NOT EXISTS does not reconcile different definitions.
  • Keep schema creation and application data loading as separate decisions. A script that safely creates a table can still insert duplicate business records every time it runs.
  • Use a least-privilege account. Grant only the database and table permissions the task needs; an upsert requires the additional update permission described above.
  • Let MySQL manage its data directory and table files. Never copy, rename, or create database folders there as a substitute for SQL statements.
  • Verify the active schema before destructive or high-volume operations, particularly when a connection is reused by scripts.

The correct order in one view

  1. Connect to MySQL with an account that has the required privileges.
  2. Create the schema with CREATE DATABASE IF NOT EXISTS shop;, or choose an existing schema.
  3. Select it with USE shop;, unless every table reference is fully qualified.
  4. Define the table and its constraints with CREATE TABLE.
  5. Add data with INSERT INTO table (columns) VALUES (...);.
  6. Confirm the structure and rows with SHOW TABLES, DESCRIBE, and SELECT.

The essential dependency is simple: the schema must exist before an unqualified table can be created, and the table must exist before INSERT INTO can add rows. Keeping those operations in that order makes setup scripts predictable and makes permission or naming errors much easier to locate.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More quests from Patch Notes

  1. How to Set Up a RedM (RDR2) Server in 2026: License Key, txAdmin and server.cfg, Step by StepBlog11min
  2. Best RedM (RDR2) Server Hosting in 2026: Comparing Four Hosts on Slots, Memory and PriceBlog10min
  3. How to Host a Mindustry Server in 2026: server-release.jar, Port 6567 and the Commands That MatterBlog7min
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.