Data Definition Language helps you to define the database structure or schema. DDL commands help you to create the structure of the database and the other database objects. Its commands are auto-committed so, the changes are saved in the database permanently. The full form of DDL is Data Definition Language.
What is DML?
DML commands it to allow you to manage the data stored in the database, although DML commands are not auto-committed. Moreover, they are not permanent. So, It is possible to roll back the operation. The full form of DML is Data Manipulation Language.
Difference Between DDL and DML in DBMS
Here is the main difference between DDL and DML Command in DBMS:
DDLDMLData 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 databaseDDL 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 tableDDL statements affect the whole table.DML effects one or more rows.SQL Statement can’t be rollbackSQL Statement can be a rollbackDDL is declarative.DML is imperative.Why DDL?
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
Why DML?
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.
Commands for DDL
Five types of DDL commands are:
CREATE
CREATE statements is used to define the database structure schema:
Syntax:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);For example:
Create database university; Create table students; Create view for_students;DROP
Drops commands remove tables and databases from RDBMS.
Syntax:
DROP TABLE ;For example:
Drop object_type object_name; Drop database university; Drop table student;ALTER
Alters command allows you to alter the structure of the database.
Syntax:
To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-definition;To modify an existing column in the table:
ALTER TABLE MODIFY(COLUMN DEFINITION....);For example:
Alter table guru99 add subject varchar;TRUNCATE:
This command used to delete all the rows from the table and free the space containing the table.
Syntax:
TRUNCATE TABLE table_name;Example:
TRUNCATE table students;Commands for DML
Here are some important DML commands:
- INSERT
- UPDATE
- DELETE
INSERT:
This is a statement that is a SQL query. This command is used to insert data into the row of a table.
Syntax:
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2, value3, .... valueN); Or INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);For example:
Create database university; Create table students; Create view for_students; 0UPDATE:
This command is used to update or modify the value of a column in the table.
Syntax:
Create database university; Create table students; Create view for_students; 1For example:
Create database university; Create table students; Create view for_students; 2DELETE:
This command is used to remove one or more rows from a table.
Syntax:
Create database university; Create table students; Create view for_students; 3For example:
Create database university; Create table students; Create view for_students; 4DDL Command Example
CREATE
Syntax:
Create database university; Create table students; Create view for_students; 5Here,
- The parameter tableName denotes the name of the table that you are going to create.
- The parameters column_1, column_2… denote the columns to be added to the table.
- A column should be specified as either NULL or NOT NULL. If you don’t specify, SQL Server will take NULL as the default
Example:
Create database university; Create table students; Create view for_students; 6ALTER
Syntax:
Create database university; Create table students; Create view for_students; 7Example:
Create database university; Create table students; Create view for_students; 8DROP
Syntax:
Create database university; Create table students; Create view for_students; 9The parameter tableName is the name of the table that is to be deleted.
Example:
DROP TABLE ; 0DML Command Example
INSERT
In PL/SQL, we can insert the data into any table using the SQL command INSERT INTO. This command will take the table name, table column, and column values as the input and insert the value in the base table.
The INSERT command can also take the values directly from another table using ‘SELECT’ statement rather than giving the values for each column. Through ‘SELECT’ statement, we can insert as many rows as the base table contains.
Syntax:
DROP TABLE ; 1The above syntax shows the INSERT INTO command that takes the values directly from the using the SELECT command.