WHAT IS DDL
Data Definition Language (DDL) is a standard for commands that define the different structures in a database. DDL statements create, modify, and remove database objects such as tables, indexes, and users. Common DDL statements are CREATE, ALTER, and DROP.
Data definition language (DDL) describes the portion of SQL that creates, alters, and deletes database objects. These database objects include schemas, tables, views, sequences, catalogs, indexes, and aliases. Creating a schema.
Data Definition Language or DDL commands in standard query language(SQL) are used to describe/define the database schema. These commands deal with database schema creation and its further modifications. Some popularly known DDL commands are CREATE, ALTER, DROP, TRUNCATE, and COMMENT.
There is nothing to get overwhelmed by the names of DDL commands mentioned in the previous sentence. We will explore each of these commands in this post, but first, let us have a look at this summary table.
|CREATE||Used for creating database objects like a database and a database table.|
|ALTER||Used for modifying and renaming elements of an existing database table.|
|DROP||Used for removing an entire database or a database table.|
|TRUNCATE||Used to remove all the records from a database table.|
|COMMENT||Used to write comments within SQL queries.|
Commands of SQL DDL
Now we will try to understand each of the above mentioned DDL commands in great detail in the subsequent sections.
CREATE is a data definition language(DDL) command that is used for creating database objects such as database and database table.
The syntax for creating a database is as follows :
CREATE DATABASE database_name;
Here is an example to illustrate database creation in SQL.
CREATE database practice_db;
We Have created the database. Let us create a database table now. But for that, we first need to know the syntax.
The basic syntax for creating a table in SQL is as follows:
CREATE TABLE public.customers
column_name_1 datatype [NULL | NOT NULL],
column_name_2 datatype [NULL | NOT NULL],
column_name_n datatype [NULL | NOT NULL] )
Here is an example to illustrate database table creation using the CREATE command.
CREATE TABLE public.customer_details
customer_id character varying NOT NULL,
customer_name character varying(255) NOT NULL,
location character varying(255) NOT NULL,
amount_spent numeric NOT NULL,
order_id character varying NOT NULL
ALTER command in SQL is used to add, rename or modify, drop/delete columns in an existing database table. It can further be used to add and remove various constraints on an existing database table.
The syntax used for altering a table in SQL by adding a new column is as follows :
ALTER TABLE table_name
ADD (Columnname_1 datatype)
Here is an example to add a new column to an existing table.
ALTER TABLE customer_details
ADD email_address character varying(255);
The syntax used for renaming a table is as follows:
ALTER TABLE table_name_1
RENAME TO table_new_name;
Here is an example to rename an existing database table.
ALTER TABLE customer_details
RENAME TO customer_may;
The syntax used for altering a table in SQL by deleting existing columns is as follows :
ALTER TABLE table_name
DROP columnname_1 , columnname_2, ...
Here is an example to remove an existing column from a database table.
ALTER TABLE customer_may
TRUNCATE TABLE command is used to remove all the data records from the database table. It deletes all the rows permanently. Ergo, we cannot perform a rollback operation to undo a TRUNCATE command.
The generic syntax used for writing TRUNCATE command is as follows :
TRUNCATE TABLE table_name;
Now let us have a look at an example illustrating TRUNCATE command.
TRUNCATE TABLE customer_may;
We have successfully removed all the records from the database. Let us check it using the following SELECT statement.
SELECT * FROM customer_may;
So what do we observe? We observe that the TRUNCATE command erases all the data from the table but maintains the table structure.
DROP TABLE SQL command is used to delete a database object from the database. We can even delete the database using the DROP command. We cannot perform a rollback operation to undo a DROP database/table command.
The basic syntax for writing DROP command to delete a database in SQL is as follows :
DROP DATABASE database_name;
The syntax for writing DROP command to delete a database in SQL is as follows :
DROP TABLE table_name;
Here are a few examples to illustrate the use of the DROP command in SQL.
DROP TABLE customer_may;
We have successfully deleted the table. In the previous section, we learned about the TRUNCATE command. Let us check how DROP is different from the TRUNCATE command.
SELECT * FROM customer_may;
From the above image, it can be clearly observed that unlike the TRUNCATE command DROP statement deletes the entire table including the table structure.
Comments in SQL are similar to comments in other programming languages such as Java, C++, Python, etc. They are primarily used to define a code section for easier understanding. Comments can be a single line, multi-line, or even inline types.
Here are a few examples to illustrate commenting in SQL.
(i) Single line comment.
-- this is a single line comment
SELECT * FROM customers;
(ii) Multi-line comment.
/* this is a multi line comment
SELECT * FROM customers; */
SELECT customer_id FROM customers;
(iii) Inline comment.
SELECT customer_id FROM customers /* WHERE store_state = 'KA'*/;
WHAT IS DML
Data Manipulation Language or DML is a subset of operations used to insert, delete, and update data in a database. A DML is often a sublanguage of a more extensive language like SQL; DML comprises some of the operators in the language. Selecting read-only data is closely related and is sometimes also considered a component of a DML, as some users can perform both read and write selection.
A popular data manipulation language is Structured Query Language or SQL, used to retrieve and manipulate data in a relational database. With SQL, you can perform operations on the database and also use it to create a database. SQL uses specific commands like Create, Drop, Insert, etc., to carry out the required tasks.
DML is an abbreviation for Data Manipulation Language. Represents a collection of programming languages explicitly used to make changes to the database, such as:
- CRUD operations to create, read, update and delete data.
- Using INSERT, SELECT, UPDATE, and DELETE commands.
- DML commands are often part of a more extensive database language, for example, SQL (structured query language). These DMLs can have a specific syntax to handle data in that language.
DML has two main classifications which are procedural and non-procedural programming, which is also called declarative programming. The SQL dealing with the manipulation of data present in the database belongs to the DML or Data Manipulation Language, including most of the SQL statements.
Examples of DML Commands
- INSERT: command to add new or new value to the database
- UPDATE: command to change or update current/existing data to a more recent value within the database
- DELETE: command to delete or delete the values or data information of the current table in the database
Database administration is a central data management function. Once the databases have been set up, monitoring and maintenance must be done. Database administrators will do this based on the performance required to maintain acceptable response times in the database queries that users execute to obtain information from the data stored in them.
Difference Between DDL and DML Command in DBMS:
- Data Definition Language (DDL) helps you to define the database structure or schema while Data Manipulation language (DML command) allows you to manage the data stored in the database.
- DDL command is used to create the database schema while DML command is used to populate and manipulate database
- Comparing DDL vs DML, DDL statements affect the whole table whereas DML commands only affect one or more rows.
- In DDL, SQL Statement can’t be rollbacked while in DML SQL Statement can be a rollbacked.
- DDL is a declarative method while DML is an imperative method.
- Important DDL commands are: 1) CREATE, 2) ALTER, 3) DROP, 4) TRUNCATE, etc. while important DML commands are: 1) INSERT, 2) UPDATE, 3) DELETE, 4) MERGE, etc.
Here, are reasons for using DDL method:
- Allows you to store shared data
- Data independence improved integrity
- Allows multiple users
- Improved security efficient data access
Here, benefits/ pros of DML:
- The DML statements allow you to modify the data stored in a database.
- Users can specify what data is needed.
- DML offers many different flavors and capabilities between database vendors.
- It offers an efficient human interaction with the system.
Here is the main difference between DDL and DML commands:
|Data Definition Language (DDL) helps you to define the database structure or schema.||Data Manipulation Language (DML command) allows you to manage the data stored in the database.|
|DDL command is used to create the database schema.||DML command is used to populate and manipulate database|
|DDL is not classified further.||DML is classified as Procedural and Non and Procedural DMLs.|
|CREATE, ALTER, DROP, TRUNCATE AND COMMENT and RENAME, etc.||INSERT, UPDATE, DELETE, MERGE, CALL, etc.|
|It defines the column of the table.||It adds or updates the row of the table|
|DDL statements affect the whole table.||DML effects one or more rows.|
|SQL Statement can’t be rollback||SQL Statement can be a rollback|
|DDL is declarative.
Drop your comment
|DML is imperative.|