Data insertion is very important in understanding MySQL databases. If the right data is not inserted in the right columns, we may get a lot of errors. Let us quickly create a small employee table by running the following command.
create table employee(name varchar(20), city varchar(10), salary int);
Once we are done with creating the table we can start inserting some data into it.
Follow the complete MySQL Series here.
Inserting Single Record
To insert one record into the database, we will use the following simple command. insert into table (‘Rishabh’, ‘Mumbai’, 5000); This will insert a record in the employee table that we created. To see if the record was entered properly, we need to run another command called select * from the employee. This is a query command and we will be studying more about query commands later.
You can see that the record we entered has been inserted properly.
Let’s say we wanted to insert a record without entering the city name. We could use the following command for that purpose. insert into employee(name, salary) values(‘Ketan’,8500); You can see in the above example that we specified which columns were to be filled with what data. This is very handy when sometimes you may not want to fill all columns in a table. Also, you can see that the second row’s city column shows Null. This is due to the column’s default attribute which set’s it to null. We will learn more about it soon.
Inserting Multiple Records
If you wish to insert multiple records you can do that as well. To insert multiple records we can use the following command. insert into employee(name,city,salary) values (“Sahil”,”Delhi”,6000), (“Aniket”,”Paris”,3300) , (“Subhash”,”Jaipur”,4400);
It should give you the following output when querying the table. Lastly, if you wish to remove all records from the table just run the truncate employee command. This will delete all records from your employee table.
We inserted some records into a MySQL table. We also inserted multiple records from the same command. Also, we learned how to delete all records from a table. This was a fairly simple topic. If you have any doubts regarding this topic, do let me know in the comment section below.