When would you use ORDER BY in DELETE statement?

When you are not deleting by row ID. Such as in DELETE FROM questions ORDER BY timestamp LIMIT 1. This will delete the most recently posted question in the table questions.

Executing the DELETE statement throw an error about foreign key constraint failing. Why ?

It means the data that you’re trying to delete is still present in another table. Like if you have a table for colleges and a table for students, which contains the ID of the college they go to, running a DELETE on a college table will fail if the students table still contains people enrolled at that college.

To delete the offending data first, and then delete the college. Quick way would involve running SET foreign_key_checks = 0 before the DELETE command, and setting the parameter back to 1 after the DELETE is done. If your foreign key was formulated with ON DELETE CASCADE, the data in dependent tables will be removed automatically.

What does –i-am-a-dummy flag to do when starting MySQL?

–i-am-a-dummy flag makes the MySQL engine refuse UPDATE and DELETE commands where the WHERE clause is not present.

What is DDL, DML and DCL ?

SQL commands can be divided into three subgroups.

Data Definition Language (DDL) deals with database schema and descriptions of how the data should reside in the database, therefore language statements like CREATE TABLE or ALTER TABLE belong to DDL.

Data Manipulation Language (DML) deals with data manipulation, and therefore includes most common SQL statements such SELECT, INSERT, etc.

Data Control Language (DCL) includes commands such as GRANT, and mostly concerns with rights, permissions and other controls of the database system.