How to Add Data to MySQL DatabaseMySQL 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:
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. 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:
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:
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:
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:
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.
Next TopicHow to Comment in MySQL
|