Sling Academy
Home/MySQL/How to insert a new record into a table in MySQL 8

How to insert a new record into a table in MySQL 8

Last updated: January 26, 2024

Introduction

MySQL is a widely-used, open-source relational database management system (RDBMS) that is a critical component of web-based applications. MySQL 8 is the latest major release that comes with many new features and enhancements, making it a powerful tool for modern app development. Knowing how to manage data, particularly how to insert records into a MySQL table, is essential for developers. This tutorial explains how to insert a new record into a MySQL table.

Prerequisites

  • A running instance of MySQL 8.
  • A user account with privilege to insert data into databases.
  • Basic understanding of SQL syntax and CRUD operations.
  • Knowledge of how to connect to a MySQL database.

Getting Started

Before inserting a record, ensure you have a table created to hold the data. If you need to create a table, you can use the following SQL statement:

CREATE TABLE employees (
  id INT AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  department VARCHAR(255),
  start_date DATE,
  PRIMARY KEY (id)
);

Basic Insert Syntax

The basic SQL syntax for inserting a new record into a table is as follows:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Let’s insert a single record into the ’employees’ table:

INSERT INTO employees (name, department, start_date)
VALUES ('John Doe', 'Engineering', '2023-04-01');

This SQL statement adds a new record to the ’employees’ table. The ‘id’ column will auto-increment.

Inserting Multiple Records

To insert multiple records in one statement:

INSERT INTO employees (name, department, start_date)
VALUES 
  ('Jane Smith', 'Marketing', '2023-04-03'),
  ('Alice Jones', 'HR', '2023-04-04');

This adds two new rows to the ’employees’ table.

NULL Values and Default Values

If a column is configured to accept NULL values and you want to leave it empty, you can do so by specifying NULL in the VALUES clause.

INSERT INTO employees (name, department, start_date)
VALUES ('Carl Kent', NULL, '2023-04-05');

Assuming the ‘department’ column allows NULLs, this SQL statement will accept a NULL value without an error.

Using Expressions and Functions

You can use expressions or built-in MySQL functions in the VALUES clause:

INSERT INTO employees (name, department, start_date)
VALUES ('Lana Ray', 'Support', CURDATE());

This will insert the current date for the ‘start_date’ value.

Inserting Data from Another Table

You might need to insert records into one table from another table:

INSERT INTO new_employees (name, department)
SELECT name, department FROM employees WHERE start_date > '2023-01-01';

This SQL statement takes records from ’employees’ and inserts them into ‘new_employees’ for those hired after January 1, 2023.

Error Handling

Error handling is crucial when inserting data:

INSERT INTO employees(name, department, start_date)
VALUES ('Jake Weaver', 'Development', '2023-04-10')
ON DUPLICATE KEY UPDATE start_date = '2023-04-10';

If there’s a duplicate entry for a unique key, MySQL will update the ‘start_date’ instead of throwing an error.

Best Practices

  • Always specify the column names for clarity and to avoid errors if the table structure changes.
  • Use transactions for critical operations to maintain data integrity and facilitate rollback in case of errors.
  • Regularly review and optimize your insert operations for better performance, especially when working with large data sets.

Conclusion

Inserting data into a MySQL table is a fundamental operation in the management of a database. This article presented different scenarios for inserting records into a MySQL table, dealing with NULL values, inserting multiple records at once, using expressions and functions, handling errors, and observing best practices. With this knowledge, you’re well-equipped to manage data insertion tasks in MySQL 8.

Don’t forget that exercising good security practices, such as sanitizing inputs and using prepared statements, is essential to protect your database from SQL injection attacks. Practice these examples and experiment with varied data types and table structures to familiarize yourself further with MySQL 8’s features.

Next Article: How to mass insert data into a table in MySQL 8

Previous Article: MySQL 8: Can a table store 100 million rows

Series: MySQL Tutorials: From Basic to Advanced

MySQL

You May Also Like

  • MySQL: How to reset the AUTO_INCREMENT value of a table
  • MySQL: How to add a calculated column to SELECT query
  • MySQL: Eliminate orphan rows in one-to-many relationships
  • MySQL: Using R-Tree Indexes for Spatial Data Types
  • How to Create Custom Collations in MySQL
  • Using Hash Indexes in MySQL: A Practical Guide
  • Understanding Full-Text Indexes in MySQL
  • Partial Indexes in MySQL: A Practical Guide
  • MySQL: How to Remove FOREIGN KEY Constraints
  • Using ENUM in MySQL 8: A Practical Guide (with Examples)
  • MySQL: Creating a Fixed-Size Table by Using Triggers
  • One-to-Many Relationship in MySQL 8: A Practical Guide
  • Using Regular Expressions in MySQL 8: The Complete Guide
  • Using Loops in MySQL: A Practical Guide (with Examples)
  • How to Execute an SQL File in VS Code
  • Making use of the JSON_REMOVE() function in MySQL 8
  • MySQL 8: How to count rows in related tables
  • Replication in MySQL 8: A Comprehensive Guide
  • MySQL 8: FIRST_VALUE(), LAST_VALUE(), and NTH_VALUE() functions – Explained with examples