TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: MySQL Insert Statement to Add Rows
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
MySQL Tutorial

MySQL Insert Statement to Add Rows

Last updated: Oct 01, 2023 12:31 pm
By Meenakshi Agarwal
Share
8 Min Read
MySQL Insert to Add Single Multiple Rows
MySQL Insert to Add Single Multiple Rows
SHARE

This tutorial explains the MySQL INSERT command to insert single and multiple rows in a table. Here, you’ll find some unique ways with different variations for adding records with fully working examples.

Contents
SyntaxMySQL INSERT Statement ExamplesSimple Insert Query ExampleInsert with Default ValuesFill Date Values using InsertInsert Multiple Rows in EMPL Table

1. INSERT Statement Syntax
2. INSERT Single Row
3. INSERT Default Values
3. INSERT Date Columns
4. INSERT Multiple Rows

Let’s now read and understand each of the sections one by one.

MySQL INSERT statement

As stated initially, the INSERT command is a built-in MySQL statement that inserts the specified records into a given table. So, let’s first check the details and see how to use the INSERT command. But first, create a sample table which we’ll use in our examples.

-- Creating a sample table we will use in our examples
CREATE TABLE EMPL
    (
        e_id INT AUTO_INCREMENT PRIMARY KEY,
        e_first_name VARCHAR(20) NOT NULL,
        e_middle_name VARCHAR(20),
        e_last_name VARCHAR(20),
        e_address VARCHAR(40),
        e_pin VARCHAR(10),
        e_salary INT NOT NULL DEFAULT 5000,
        e_birth_date DATE,
        e_join_date DATE
    );

Syntax

Below is the signature of this command:

-- Adding a single record using INSERT command
INSERT INTO TABLE
   (field1_name, field2_name, ...)
VALUES
   (value1, value2, ...);

Below are some facts about the above statement.

  • You begin this MySQL query with the INSERT INTO clause. Then, you provide the table name appended with a comma-separated list of all its fields inside parentheses.
  • After that, the keyword VALUES appears with a list of comma-delimited values (one value per field) in parentheses.

While supplying the VALUES, you need to make sure that each field has a corresponding value. Moreover, the order of field values should also align.

By the way, it is common that you may want to insert multiple rows in a table with one INSERT statement. It has a little modified syntax:

-- Adding multiple records with single INSERT command
INSERT INTO TABLE
   (field1_name, field2_name, ...)
VALUES 
   (value11, value12, ...), -- row 1
   (value21, value22, ...), -- row 2
    ...
   (valueN1, valueN2, ...); -- row N

MySQL INSERT Statement Examples

We already have created a sample table for practicing with the INSET command. Let’s now utilize it in our examples:

Simple Insert Query Example

The following MySQL query inserts a new row to the EMPL table:

-- Inserting a single row in EMPL table
INSERT INTO 
   EMPL(e_first_name, e_salary)
VALUES
   ('Amit', 20000);

In our first example, we only mentioned the values for two fields: e_first_name and e_salary. For the rest of the columns, MySQL would assume the default values.

You can confirm the same after running this query.

SELECT * FROM EMPL;

The result should come like this:

e_id, e_first_name, e_middle_name, e_last_name, e_address, e_pin, e_salary, e_birth_date, e_join_date
1   , Amit        , NULL         , NULL       , NULL     , NULL , 20000   , NULL        , NULL

All the fields except e_id, e_first_name, and e_salary have initialized with NULL as the default value. It is because MySQL inserted NULL for remaining columns which didn’t appear in the INSERT command.

Also, note that we only inserted one field which e_first_name. The other one, e_id  is of auto-increment type. Hence, MySQL auto-generated a value for the same.

Insert with Default Values

We can also supply the default values explicitly. You have seen the implicit way (by ignoring the columns in the INSERT) in the previous example.

It is simple as we only have to place the DEFAULT keyword in the INSERT INTO clause for every corresponding field name.

Check out the following MySQL example, which illustrates how to insert default values:

-- Inserting default values in EMPL table
INSERT INTO 
   EMPL (e_first_name, e_salary)
VALUES
   ('Vijay', DEFAULT);

Here, we’ve specified two columns and provided their specified values. For instance – DEFAULT for the e_salary field.

It is because of the e_salary column, which has a default value of 5000, as mentioned in the CREATE command.

e_salary INT NOT NULL DEFAULT 5000

Let’s now fetch the records from the EMPL table:

SELECT * FROM EMPL;

After querying the contents of the EMPL table, the following result comes:

e_id, e_first_name, e_middle_name, e_last_name, e_address, e_pin, e_salary, e_birth_date, e_join_date
1   , Vijay       , NULL         , NULL       , NULL     , NULL , 5000    , NULL        , NULL

Fill Date Values using Insert

We can fill the date type fields by inserting the values in the following style:

'YYYY-MM-DD' or 'YYYY/MM/DD'

This convention implies:

  • YYYY -> Four-digit year, for example, 2019.
  • MM -> Two-digit month, for example, 01-12.
  • DD -> Two-digit day, for example, 01-28 or 01-30 or 01-31.

The below example inserts a new record to the EMPL table with birth and join_date values:

-- Inserting date type values in EMPL table
INSERT INTO
   EMPL (e_first_name, e_birth_date, e_join_date)
VALUES
   ('Sojay', '2000-02-11','2019-07-15');

The following output captures the current state of the EMPL table post the insert:

After querying the contents of the EMPL table, the following result comes:

e_id, e_first_name, e_middle_name, e_last_name, e_address, e_pin, e_salary, e_birth_date, e_join_date
1   , Sojay       , NULL         , NULL       , NULL     , NULL , 5000    , 2000-02-11  , 2019-07-15

Insert Multiple Rows in EMPL Table

So far, in our examples, we have applied the MySQL INSERT statement to add a single record. However, it is possible to push multiple rows using the INSERT command.

Check the following statement that inserts four rows into the EMPL table:

-- Inserting multiple rows in EMPL table
INSERT INTO EMPL
    (
        e_first_name, e_middle_name, e_last_name, e_address, e_pin, e_salary, e_birth_date, e_join_date
    )
VALUES
    ('Amit', 'Kumar', "Singh", "Sec-62, Noida", "201301", DEFAULT, '2000-02-11','2019-08-28'),
    ('Jyoti', 'Rani', "Saini", "Sec-21, Noida", "201301", 10000, '1998-03-01','2019-07-30'),
    ('Vimal', 'Kumar', "Sood", "Sec-11, Noida", "201301", DEFAULT, '1997-08-09','2019-03-14'),
    ('Neelam', 'Jai', "Singh", "Sec-23, Noida", "201301", 50000, '2001-01-12','2019-04-21');

SELECT * FROM EMPL;

The result is as follows:

1	Amit	Kumar	Singh	Sec-62, Noida	201301	5000	2000-02-11	2019-08-28
2	Jyoti	Rani	Saini	Sec-21, Noida	201301	10000	1998-03-01	2019-07-30
3	Vimal	Kumar	Sood	Sec-11, Noida	201301	5000	1997-08-09	2019-03-14
4	Neelam	Jai	Singh	Sec-23, Noida	201301	50000	2001-01-12	2019-04-21

We hope that after wrapping up this tutorial, you should feel comfortable using the MySQL INSERT statement. However, you may practice more with examples to gain confidence.

Also, to learn SQL from scratch to depth, read our step-by-step MySQL tutorial.

You Might Also Like

MySQL vs MongoDB Comparison

Concatenate Strings in an SQL Query With Examples

The Difference between UPSERT & Insert

SQL Programming Test in 2024

SQL Table Creation: The Missing Manual

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Python f-string for string formatting Python F-Strings: What They Are and How to Use
Next Article Python String Find with Examples Your Ultimate Guide to Python String Find()

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x