SQL, DDL and DML: Working with Databases
Quick answer SQL is the declarative language every relational database understands, split into DDL commands that define structure and DML commands that move data, and every working session begins with CREATE DATABASE, USE and SHOW.
SQL stands for Structured Query Language. It is the standard language that every relational database — MySQL, Oracle, PostgreSQL, SQL Server — understands. The important idea is that you do not tell SQL how to search. You describe what you want, and the database engine decides how to fetch it. That is why SQL is called a non-procedural or declarative language, unlike Python where you write the loop yourself.
Three mechanical rules first, because they cost marks when forgotten:
- Every SQL statement ends with a semicolon.
- Keywords are not case-sensitive.
SELECT,selectandSelectall work. Writing keywords in capitals is only a readability convention — a good one to keep in the exam. - Text values go inside single quotes:
'Delhi'. Numbers do not:52000.
SQL is divided into sub-languages. Two of them are on your syllabus.
| Sub-language | Full form | What it changes | Commands |
|---|---|---|---|
| DDL | Data Definition Language | The structure — databases, tables, columns, constraints | CREATE, ALTER, DROP |
| DML | Data Manipulation Language | The data stored inside a table | INSERT, UPDATE, DELETE, SELECT |
The one line that decides marks in this question: DDL changes the skeleton, DML changes the contents. DROP TABLE is DDL because it removes the table itself. DELETE is DML because it removes rows and leaves the empty table standing. Note that SELECT is grouped under DML in most Class 12 material; a few books list it separately as DQL (Data Query Language). Either grouping is accepted as long as you are consistent.
Database-level commands. A table has to live inside a database, and MySQL needs to be told which database you are working in. These five commands are the start of every practical session.
Worked example. Everything below was run against MySQL 8.0.41. Two databases are created so you can watch one of them disappear:
CREATE DATABASE cs12_sql_basics;
CREATE DATABASE demo_practice;
SHOW DATABASES;Output:
+-------------------------------+
| Database |
+-------------------------------+
| cs12_database_concepts |
| cs12_sql_basics |
| cs12_sql_joins_and_aggregates |
| demo_practice |
| information_schema |
| mysql |
| performance_schema |
| sys |
+-------------------------------+Your list will look different, because SHOW DATABASES lists every database on that server. The last four — information_schema, mysql, performance_schema and sys — are MySQL's own system databases. They are always there and you must never modify them. The cs12_ entries were other practice databases that already existed on this machine.
Now remove the throwaway one:
DROP DATABASE demo_practice;
SHOW DATABASES;+-------------------------------+
| Database |
+-------------------------------+
| cs12_database_concepts |
| cs12_sql_basics |
| cs12_sql_joins_and_aggregates |
| information_schema |
| mysql |
| performance_schema |
| sys |
+-------------------------------+demo_practice is gone. Understand what that means: DROP DATABASE deletes the database together with every table and every row inside it, with no confirmation and no undo.
Next, enter the database you want to work in and confirm where you are:
USE cs12_sql_basics;
SELECT DATABASE() AS current_db;+-----------------+
| current_db |
+-----------------+
| cs12_sql_basics |
+-----------------+SHOW TABLES;0 rows returned — a brand-new database contains no tables at all. We build one in the next section.
Two errors worth recognising. Both were produced deliberately. Creating a database that already exists is rejected — the failure is reported against the second line, the one that repeated the name:
CREATE DATABASE cs12_tmp_check;
CREATE DATABASE cs12_tmp_check;
ERROR 1007 (HY000) at line 2: Can't create database 'cs12_tmp_check'; database existsAnd running a table command before choosing a database gives the single most common beginner error — you forgot USE:
SHOW TABLES;
ERROR 1046 (3D000) at line 1: No database selected- SQL is declarative: you state what data you want, not how to find it. Statements end with a semicolon and keywords are case-insensitive.
- DDL (CREATE, ALTER, DROP) defines structure; DML (INSERT, UPDATE, DELETE, SELECT) works on the data inside that structure.
- USE sets the working database. Skip it and any table command fails with 'ERROR 1046: No database selected'.
- DROP DATABASE removes the database and everything in it permanently — there is no confirmation prompt and no undo.
- SHOW DATABASES and SHOW TABLES are the two commands that tell you where you are and what exists.
