Javatpoint Logo
Javatpoint Logo

How to Add Data to MySQL Database

MySQL is one of the most prominent open-source relational database management systems that keep data in the form of tables made up of rows and columns. Developers around the globe use databases to store enormous amounts of data. It is a fundamental skill to insert data into a MySQL database for anybody who regularly works with databases.

In this article, we will comprehend how to add data to the MySQL database. You will gain the details you require to add data effortlessly in a MySQL database, but first, let us learn the necessity of adding data to a database.

Some points that tell the need to add data to MySQL database:

  • In a database, data is stored in a structured manner, which allows you to retrieve and use the data whenever you need it easily. Businesses or organizations that deal with a lot of data need a database to store and manage the data. Businesses or organizations ensure their long-term persistence by adding data to a MySQL database.
  • Adding data to MySQL is essential as it ensures data integrity. It permits us to retrieve data, filter data, etc. It allows organizations to extract remarkable insights and make more profitable judgments based on data stored in the database.
  • Stored data can be easily accessed and manipulated using SQL or any other programming language. It allows various operations to meet specific business requirements such as data retrieval, deletion, or update.
  • Adding data to a MySQL database provides data consistency and accuracy by using defined data types, constraints, and validation rules. You can describe the format of the data that you are adding to the database to make sure that only valid data is stored. It reduces the risk that arises from manual data handling.
  • MySQL databases can efficiently handle large amounts of data without sacrificing performance, which makes them suitable for applications with high data needs.
  • Storing data in a MySQL database facilitates regular backup and data recovery. Database backup lets you create copies of your data so that data can be restored if it is lost because of software issues, hardware failures, etc. If you have a backup, you can retrieve the data in case of data loss. Data restoration helps maintain business stability.

Steps to add data to MySQL database:

Set up your MySQL environment:

First, you must set up your MySQL environment before adding data to the MySQL database. For that, search MySQL on Google and download the latest version suitable for your operating system from its official website. After downloading MySQL, you can install MySQL Server on your system and make sure it runs properly. Once it is installed, open the MySQL command line client that comes as part of the MySQL installation to interact with the database.

Connect to the Database:

After installing the MySQL server on your computer, you have to connect to the MySQL database using a graphical user interface (GUI) tool, such as the MySQL command line client. You can also use various programming languages, such as Java, SQL, PHP, or JavaScript, to connect to the database as per your requirement.

To connect to MySQL database using MySQL command line client. Use the given command if you are connecting as a root user:

Now, it will ask you to write the password. You must write the password and press Enter. As you can see below, you are connected to the MySQL server.

How to Add Data to MySQL Database

Create a database:

After connecting to the MySQL database, the next step you need to construct a database. For that, you should open the MySQL command line client to create a new database. You can use the given statement to create a database:

As you can see below, after giving the above command in the MySQL command line client, it shows a message saying 'Query OK', meaning that the database has been created successfully.

Create a table in the database:

Now, you can make the table and add columns by specifying the column names, data types, and required constraints.

Example 1: Let us create a table called 'Employee', which consists of fields like empId, empName, empAge, empAddress, and empDept. To create a table, use the commands given below:

A table will look like it is shown below:

empId empName empAge empAddress empDept

Add data to a table in the database:

The next step is to add data to a table. We will use the given statements to insert data:

The table after inserting data will look like it is shown below:

empId empName empAge empAddress empDept
1 Oliver 21 Victoria IT
2 Ava 23 Victoria IT
3 Henry 20 Victoria Finance
4 William 21 Queensland Marketing
5 Olivia 21 Queensland Finance
6 Sophia 23 Queensland HR
7 Evelyn 24 Victoria HR
8 Layla 23 Victoria Marketing
9 Georgia 20 Queensland Finance
10 Luna 22 Victoria IT

Example 2: Let us create another table called 'electrical_appliances', which consists of fields like id, name, quantity, mfg_date, and price. To create a table, use the given commands:

A table will look like it is shown below:

id name quantity mfg_date price

Add data to a table in the database:

Now, we will add data in the 'electrical_applicances' table. We will use the given statements to insert data:

The 'electrical_applicances' table after inserting data will look like it is shown below:

id name quantity mfg_date price
1 Washing Machine 2 2023-02-10 28400
2 AC 2 2023-03-20 90000
3 Clothes Iron 5 2022-12-18 15000
4 Oven 1 2023-01-06 56000
5 Refrigerator 3 2023-01-05 96000
6 Television 1 2023-02-25 42000
7 Blender 4 2022-11-27 40000
8 Toaster 6 2022-11-18 36000
9 Clothes Dryer 3 2023-04-12 45000
10 Kettle 9 2023-05-05 54000

Conclusion:

In this article, you have understood how to add data in MySQL. You have learned that you can use the INSERT INTO statement to add data to a table in a database.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA