Insert and Delete Command

Written by

Soumya Shaw

INSERT Command:

The general syntax for inserting a value into a table is mentioned as follows:

INSERT INTO table_name (

field1, field2, … fieldN)

VALUES

( value1, value2, … valueN );

You must have noticed such a syntax already during the Data Entry as well and now let us try to enter other values into the table using the same framework.

This represents the fact that there is no final table in MySQL that is totally closed for data entry and you can always add new data to the table whenever it is required.

As of now, let’s believe that there’s a new state in our country (that I never hope to see) so the details of the new state must be added into the Database.

INSERT INTO country (
sr_no,state,capital,population)
VALUES (
31,”New State 1”,” CapitalX”, 26321000);

Now, let’s try out another state but in different input order.

INSERT INTO country (
state,population,capital,sr_no ,govt)
VALUES (
”New State 2”, 11122000,”Capital Y”,32,”XYZ”);

There’s no error in the format as long as the column name and datatype matches with each other.

Hence, after the insertions of the two new states, this is how the table looks now.

Note that if you try repeating the Primary Key value in the table it’ll raise an Error since the values of the Primary Key must be unique so that there would be one to one correspondence.

DELETE Command:

For, the extreme cases where you feel that you need to delete the whole table data and start off once again, there’s a command you can use.

DELETE

from table_name;

The above command will result in a complete empty table devoid of any data.

In some cases, you may need the deletion of very particular elements (rows) that satisfies condition. As of now, you need not understand the syntax of the WHERE clause but you will eventually understand in due course of time.

Let’s delete the trains from the table of railways where the Average Delay is more than 5.

This is the Initial Table:

We’ll follow the syntax of delete to remove the trains according to the above-stated condition.

DELETE
FROM railways
WHERE avg_delay>5;

The Final Table is as follows:

Insert and Delete Command