DML in DBMS

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

Overview

Dml in DBMS is one of the types of SQL queries which helps you in performing all types of data modification in a database like inserting, modifying, and deleting its data.

DDL (Data Definition Language) is another type of SQL query that helps in creating or modifying the structure or the schema of the table. DDL consists of Commands like CREATE, ALTER, TRUNCATE, and DROP.

In this article, we will focus on learning DML in DBMS and its commands like INSERT, UPDATE, DELETE, and SELECT, along with their syntaxes and examples.

Introduction To DML

The dml in dbms is one of the types of SQL queries. It stands for Data Manipulation Language. The DML commands in Structured Query Language (SQL) are responsible for making any change in the data present in SQL databases. The change can be anything, it can be inserting new records in the table, or altering the data present in the table or removing some existing data from the database table.

Let's say we have a table maintaining the records of our students. If any new student joins the school, then we need to add the data of that student into our table, this is called insertion of data and this can be achieved by using the INSERT command of SQL. Now, if we have to update some details of any student in the future, then we are free to update the existing record information using the UPDATE command of DML. Also, in case any student leaves the school or any other situation arises in which we no longer want to store the details of that student, then we can simply delete the record of that student using the DELETE command of the Data Manipulation Language in SQL. We can also think of the case when we want to see the data of the students then we can retrieve the data by using the SELECT keyword in SQL.

It is mainly used for selecting/retrieving and manipulating (inserting, deleting, updating) the data in a database.

NOTE: DML commands are not auto-committed which means that it can't permanently save all the changes performed in the database and so they can be easily rolled back. This can lead to loss of proper information and so as to avoid this, we can commit the DML commands to save the changes performed in the database tables and this is achieved using the COMMIT command. You can use the COMMIT statement at the end of your DML command to make all the changes permanent.

DML Commands in DBMS

Let's see various dml commands in dbms, along with their examples:

Select:

SELECT is the most widely used data manipulation command in Structured Query Language for retrieving data from the table.

It is used to fetch the data in a set of records from a table, view or a group of tables, views by making the use of SQL joins.

The returned data from the SELECT command is stored in a new table called result set.

Syntax of SELECT command:

The SELECT query is used when we want to retrieve the data from all the columns of the table.

Here, columnName1, columnName2 are the names of those columns whose data we want to show.

This syntax for the SELECT command is used when we want to retrieve the data from some specific columns of the table.

Examples:

Let's say we have a table student_details, having ROLL_NO, FIRST_NAME, LAST_NAME and MARKS as the fields, and some data has been stored in these fields.

Now, if we want to access all the data stored in the table, we will make use of the SELECT command.

NOTE: Here, * represents all the columns, which means we are selecting or retrieving the data from all the columns of a given table.

OUTPUT:

ROLL_NOFIRST_NAMELAST_NAMEMARKS
1RitaGoyal67
2KanikaMittal91
3ReshmaKumari100

Now, if we want to only show the ROLL_NO and the marks of a student. Then we can write:

OUTPUT:

ROLL_NOMARKS
167
291
3100

Here, we have specifically mentioned the column names separated by comma, for which we want to access the data.

Insert

INSERT is another important data manipulation command in Structured Query Language used to insert data into the rows of a table.

Syntax of INSERT command:

Another way is to only specify the value of data to be inserted without the column names.

Here, col_1, col_2 .. col_n represents the name of the columns and value_1, value_2, ... value_n represents the value to be inserted in the corresponding columns.

Note: Using the syntax of col1 col2 ... along with value_1, value_2, we can skip any column data and the default value to that column will be assigned, but in case of the syntax in which only values are assigned without the column names, we need to provide the data in a sequential manner.

Examples:

Taking our previous example of the student_details table, let's say we want to add the 4th entry of a student. We can write a query:

OUTPUT:

ROLL_NOFIRST_NAMELAST_NAMEMARKS
1RitaGoyal67
2KanikaMittal91
3ReshmaKumari100
4ArpitGarg82

As we can see from the output, the 4th row for the newly added student has been created.

Let's say we want to add the 5th record in our table, using the second syntax in which we only specify the value of data to be inserted without the column names.

We can write a query;

OUTPUT:

ROLL_NOFIRST_NAMELAST_NAMEMARKS
1RitaGoyal67
2KanikaMittal91
3ReshmaKumari100
4ArpitGarg 82
5KevinMehta23

As we can see from the output, the 5th row for the newly added student has been created.

Update:

UPDATE command is another important data manipulation command in Structured Query Language, it allows us to update or modify the existing data in tables. It changes the data from one or more records in a table.

Syntax of UPDATE command:

Here, the specified condition helps us to select the rows for which the value of columns needs to be updated.

Examples:

Again, coming to our previous example of the student_details table, let's say, we want to change the marks of a student whose roll_no is given to us.

We can write the query to update the marks, by specifying the roll_no for which we are updating the marks.

Now, the updated table looks like this.

OUTPUT:

ROLL_NOFIRST_NAMELAST_NAMEMARKS
1RitaGoyal67
2KanikaMittal91
3ReshmaKumari100
4ArpitGarg92

We can also update the value of multiple fields in a table by setting the values separated by commas.

Now, the updated table looks like this.

Note: We have specified the condition using the WHERE clause, which states that the ROLL_NO should be equal to 4 for the student whose data we want to update.

OUTPUT:

ROLL_NOFIRST_NAMELAST_NAMEMARKS
1RitaGoyal67
2KanikaMittal91
3ReshmaKumari100
4ArpitGupta92

Delete:

DELETE is another useful data manipulation command in Structured Query Language, which allows us to remove single or multiple records from the database tables depending upon the condition we specify in the WHERE clause.

If no condition is specified in the WHERE clause, then it will delete all the rows of a table.

Delete command keeps the record for each deleted row and so it does not remove the stored data permanently from the database and we can always roll back to the previous stage using the ROLLBACK command as the log for that last deleted entry has already been stored in the DELETE command logs.

Syntax of DELETE command:

As we can see here no condition has been specified so it will delete all the rows from the table.

OR

Here, the condition is specified using the WHERE clause and only those records that fulfill the condition are deleted.

Examples:

Coming to our example of the student_details table, let's say we want to delete all the records of the students whose marks are less than 80. Here, only one record for ROLL_NO 1 will be deleted

Original table:

ROLL_NOFIRST_NAMELAST_NAMEMARKS
1RitaGoyal67
2KanikaMittal91
3ReshmaKumari100
4ArpitGarg92

OUTPUT:

ROLL_NOFIRST_NAMELAST_NAMEMARKS
2KanikaMittal91
3ReshmaKumari100
4ArpitGarg92

Now, let's say we want to delete all the records from the table, so we can simply write.

Its output will be an empty table with no data.

  • As we discussed above the DELETE command in SQL maintains transaction logs for each deleted record and so it allows us to restore the deleted data by using the ROLLBACK command.

Let's discuss the example for the same.

We will first delete all the rows in the student_details table.

Let's try to display the data from the table.

OUTPUT: It's an empty table.

Now, let's use the Rollback command to restore the data.

Now, let's try to retrieve the data from the table again.

OUTPUT:

ROLL_NOFIRST_NAMELAST_NAMEMARK
1RitaGoyal67
2KanikaMittal91
3ReshmaKumari100
4ArpitGarg92

Conclusion

Learn more about difference between ddl and dml in dbms

  • DML is an abbreviation of Data Manipulation Language.

  • The DML in DBMS is responsible for performing CRUD (Create Read Update and Delete) operations on the data of the database table.

  • Data Manipulation Language (DML) allows you to modify the database tables by inserting, modifying, and deleting their data.

  • DML commands are not auto-commited and hence to store the data change permanently we need to commit our change.

  • SELECT command is used for retrieving the data from the database.

  • A SELECT statement retrieves zero or more rows from one or more database tables.

  • INSERT command is used to insert data into a table.

  • Using INSERT command, you may insert one row at a time, or select several rows from an existing table and insert them all at once.

  • UPDATE command is used to update existing records within a table.

  • If we want to update a particular record value, we use the WHERE clause along with the UPDATE command. If you do not use the WHERE clause, all the rows will be affected by the UPDATE command.

  • The DELETE command is used to delete existing records from a database table. The WHERE clause is used with the DELETE command which specifies the record(s) that should be deleted. If you omit the WHERE clause, it will delete the whole table.

Read More: