This is one of the most frequently asked interview questions and let me try to take a couple of minutes to answer this.
Both of them in a way serve the same purpose which is nothing but inserting a record, however, they are slight changes in the way they handle the insert operation. Let's look at them.
Insert DML statement
- When we use a DML statement, if I have a list of multiple records there is no way I can insert the records that are valid and omit the rest of the records that are not valid. Simply put, partial insert is not supported. If an error occurs because of whatever reason, Apex Code throws an error and no record gets saved into the database.
- The option of Rollback is not supported.
- Insert is a method in a standard class called Database and it's a static method, meaning, we can invoke the method with the notation Database.Insert.
Database.Insertsupports partial insert.
Database.Insertalso supports Rollback.
Database.InsertIncludes an optional
allorNoneparameters that defaults to true.
Database.Insertwhen used in the bulk operation, in case of an error, the remaining records will be inserted. Only the records throwing an error will not be inserted.
Hope this helps!