Key Differences between DDL vs DML
- 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 the 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 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.
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.
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.
Here is the main difference between DDL and DML Command in DBMS:
|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.||DML is imperative.|
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.
Five types of DDL commands are:
CREATE statements is used to define the database structure schema:
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Create database university; Create table students; Create view for_students;
Drops commands remove tables and databases from RDBMS.
DROP TABLE ;
Drop object_type object_name; Drop database university; Drop table student;
Alters command allows you to alter the structure of the database.
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....);
Alter table guru99 add subject varchar;
This command used to delete all the rows from the table and free the space containing the table.
TRUNCATE TABLE table_name;
TRUNCATE table students;
Here are some important DML commands:
This is a statement that is a SQL query. This command is used to insert data into the row of a table.
INSERT INTO TABLE_NAME (col1, col2, col3,.... col N) VALUES (value1, value2, value3, .... valueN); Or INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
INSERT INTO students (RollNo, FIrstName, LastName) VALUES ('60', 'Tom', 'Erichsen');
This command is used to update or modify the value of a column in the table.
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CONDITION]
UPDATE students SET FirstName = 'Jhon', LastName=' Wick' WHERE StudID = 3;
This command is used to remove one or more rows from a table.
DELETE FROM table_name [WHERE condition];
DELETE FROM students WHERE FirstName = 'Jhon';
CREATE TABLE tableName ( column_1 datatype [ NULL | NOT NULL ], column_2 datatype [ NULL | NOT NULL ], ... );
- 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
CREATE TABLE Students ( Student_ID Int, Student_Name Varchar(10) )
Alter TABLE <Table name> ADD Column1 datatype, Column2 datatype;
ALTER TABLE University.Students_Name ADD Course_Duration VARCHAR(20);
DROP TABLE <tableName>;
The parameter tableName is the name of the table that is to be deleted.
DROP TABLE COURSE_NAMES;
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.
BEGIN INSERT INTO <table_name>(<column1 >,<column2>,...<column_n>) VALUES(<valuel><value2>,...:<value_n>); END;
The above syntax shows the INSERT INTO command. The table name and values are mandatory fields, whereas column names are not mandatory if the insert statements have values for all the columns of the table.
The keyword ‘VALUES’ is mandatory if the values are given separately, as shown above.
BEGIN INSERT INTO <table_name>(<columnl>,<column2>,...,<column_n>) SELECT <columnl>,<column2>,.. <column_n> FROM <table_name2>; END;
The above syntax shows the INSERT INTO command that takes the values directly from the <table_name2> using the SELECT command.
The keyword ‘VALUES’ should not be present in this case, as the values are not given separately.
Below is the Syntax to delete table
DROP TABLE <TableName>;
The parameter TableName is the name of the table that is to be deleted.
DROP TABLE COURSE_NAMES;
To view data in SQL Server, we use the SELECT statement.
SELECT expression FROM tableName [WHERE condition];
SELECT * FROM Course;