Database Language
- A DBMS has appropriate languages and interfaces to express database queries and updates.
- Database languages can be used to read, store and update the data in the database.
Types of Database Language
- DDL stands for Data Definition Language. It is used to define database structure or pattern.
- It is used to create schema, tables, indexes, constraints, etc. in the database.
- Using the DDL statements, you can create the skeleton of the database.
- Data definition language is used to store the information of metadata like the number of tables and schemas, their names, indexes, columns in each table, constraints, etc.
Some of the DDL commands are:
CREATE :
- It is used to create the database or its schema objects.
- MySQL Syntax -
- To create a new database :
- To create a new table :
DROP :
- It is used to delete the database or its schema objects.
- MySQL Syntax -
- To delete an object :
- To delete an existing table :
- To delete the whole database :
ALTER :
- It is used to modify the structure of the database objects.
- MySQL Syntax -
- To add new column(s) in a table :
- To change the datatype of a column in a table :
- To remove a column from a table :
TRUNCATE :
- It is used to remove the whole content of the table along with the deallocation of the space occupied by the data, without affecting the table's structure.
- MySQL Syntax -
- To remove data present inside a table :
Some of the DDL commands are:
CREATE :
- It is used to create the database or its schema objects.
- MySQL Syntax -
- To create a new database :
- To create a new table :
DROP :
- It is used to delete the database or its schema objects.
- MySQL Syntax -
- To delete an object :
- To delete an existing table :
- To delete the whole database :
ALTER :
- It is used to modify the structure of the database objects.
- MySQL Syntax -
- To add new column(s) in a table :
- To change the datatype of a column in a table :
- To remove a column from a table :
TRUNCATE :
- It is used to remove the whole content of the table along with the deallocation of the space occupied by the data, without affecting the table's structure.
- MySQL Syntax -
- To remove data present inside a table :
NOTE - We can also use the DROP command to delete the complete table, but the DROP command will remove the structure along with the contents of the table. Hence, if you want to remove the data present inside a table but not the table itself, you can use TRUNCATE instead of DROP
COMMENT :
- It is used to add comments about the tables, views, and columns into the data dictionary. These comments can help the developers to better understand the structure of the database.
- MySQL Syntax -
- To comment on table/view :
- To comment on a column :
- To drop a comment :
RENAME :
- It is used to change the name of an existing table or a database object.
- MySQL Syntax -
- To rename a table :
- To rename a column of a table :
Highlights:
COMMENT :
- It is used to add comments about the tables, views, and columns into the data dictionary. These comments can help the developers to better understand the structure of the database.
- MySQL Syntax -
- To comment on table/view :
- To comment on a column :
- To drop a comment :
RENAME :
- It is used to change the name of an existing table or a database object.
- MySQL Syntax -
- To rename a table :
- To rename a column of a table :
Highlights:
1. Set of commands used to alter/create schema and metadata of the database.
2. All changes are auto-committed.
3. DDL are normally not used by end-users.
4. Examples - CREATE, ALTER, DROP, TRUNCATE, COMMENT, RENAME
Here are some tasks that come under DDL:
- Create: It is used to create objects in the database.
- Alter: It is used to alter the structure of the database.
- Drop: It is used to delete objects from the database.
- Truncate: It is used to remove all records from a table.
- Rename: It is used to rename an object.
- Comment: It is used to comment on the data dictionary.
These commands are used to update the database schema that's why they come under Data definition language.
2. Data Manipulation Language
DML stands for Data Manipulation Language. It is used for accessing and manipulating data in a database. It handles user requests.
Here are some tasks that come under DML:
- Select: It is used to retrieve data from a database.
- Insert: It is used to insert data into a table.
- Update: It is used to update existing data within a table.
- Delete: It is used to delete all records from a table.
- Merge: It performs UPSERT operation, i.e., insert or update operations.
- Call: It is used to call a structured query language or a Java subprogram.
- Explain Plan: It has the parameter of explaining data.
- Lock Table: It controls concurrency.
Some of the DML commands are:
SELECT :
- It is used to retrieve or fetch data from a database. The SELECT statement cannot manipulate data, it can only access it. Hence, it is known as the Data Query Language, i.e., a limited form of DML statement.
- MySQL Syntax -
- To fetch an entire table :
- To fetch particular columns from a table :
- To fetch particular columns from a table based on a condition:
- Fetching data with various clauses (General SELECT statement):
INSERT :
- It is used to insert new rows into the table.
- MySQL Syntax -
- To insert values according to the table structure :
- To insert values based on the columns :
Note: While using the INSERT command, make sure that the datatypes of each column match with the inserted value, as this may lead to unexpected scenarios and errors in the database.
UPDATE :
- It is used to update existing column(s)/value(s) within a table.
- MySQL Syntax -
- To update the columns of a table based on a condition (General UPDATE statement) :
UPDATE :
- It is used to update existing column(s)/value(s) within a table.
- MySQL Syntax -
- To update the columns of a table based on a condition (General UPDATE statement) :
Note: Here, the SET statement is used to set new values to the particular column, WHERE clause is used to select rows for which the columns are updated for the given table.
DELETE :
- It is used to delete existing records from a table, i.e., it is used to remove one or more rows from a table.
- MySQL Syntax -
- To delete rows from a table based on a condition :
DELETE :
- It is used to delete existing records from a table, i.e., it is used to remove one or more rows from a table.
- MySQL Syntax -
- To delete rows from a table based on a condition :
Note: The DELETE statement only removes the data from the table, whereas the TRUNCATE statement also frees the memory along with data removal. Hence, TRUNCATE is more efficient in removing all the data from a table.
MERGE :
- It is a combination of the INSERT, UPDATE, and DELETE statements.
- It is used to merge data from a source table or query-set with a target table based on the specified condition.
- MySQL Syntax -
- To delete an object :
CALL :
- It is used to execute a structured query language function or a Java subprogram from within SQL.
- To use the CALL statement, we need to define a function using CREATE PROCEDURE command. Its syntax is:
- Now, we can execute this procedure using the CALL statement.
- MySQL Syntax -
EXPLAIN PLAN :
- It is used to display the sequence of operations performed by the DBMS software upon the execution of a DML statement.
- MySQL Syntax -
- To display the execution plan of a query:
EXPLAIN PLAN FOR
QUERY;
MERGE :
- It is a combination of the INSERT, UPDATE, and DELETE statements.
- It is used to merge data from a source table or query-set with a target table based on the specified condition.
- MySQL Syntax -
- To delete an object :
CALL :
- It is used to execute a structured query language function or a Java subprogram from within SQL.
- To use the CALL statement, we need to define a function using CREATE PROCEDURE command. Its syntax is:
- Now, we can execute this procedure using the CALL statement.
- MySQL Syntax -
EXPLAIN PLAN :
- It is used to display the sequence of operations performed by the DBMS software upon the execution of a DML statement.
- MySQL Syntax -
- To display the execution plan of a query:
EXPLAIN PLAN FOR QUERY;
1. Set of commands used to access and manipulate data stored in existing schema objects.
2. All changes done by DML commands can be rolled back.
3. DML statements deal with the user requests.
4. Examples - SELECT, INSERT, UPDATE, DELETE, MERGE, CALL, EXPLAIN PLAN
3. Data Control Language
- DCL stands for Data Control Language. It is used to retrieve the stored or saved data.
- The DCL execution is transactional. It also has rollback parameters.
(But in Oracle database, the execution of data control language does not have the feature of rolling back.)
(But in Oracle database, the execution of data control language does not have the feature of rolling back.)
Here are some tasks that come under DCL:
- Grant: It is used to give user access privileges to a database.
- Revoke: It is used to take back permissions from the user.
There are the following operations which have the authorization of Revoke:
CONNECT, INSERT, USAGE, EXECUTE, DELETE, UPDATE and SELECT.
These commands include:
Grant :
- It is used to provide user access to the database or its objects.
- MySQL Syntax -
- To grant user privilege to specified users on a database object :
Revoke :
- It is used to revoke user access to the database system.
- MySQL Syntax -
- To revoke user privilege to specified users on a database object :
Note: In practical usage, instead of having a separate language for every operation, a combination of DDL, DML, and DCL is used as part of a single database language such as SQL.
1. Set of commands used to control the user privileges in the database system.
2. The user privileges include ALL, CREATE, SELECT, INSERT, UPDATE, DELETE, EXECUTE, etc.
3. DCL commands are transactional.
4. Examples - GRANT, REVOKE
5. In practice, a combination of DDL, DML, and DCL is used as a single database language.
4. Transaction Control Language
TCL is used to run the changes made by the DML statement. TCL can be grouped into a logical transaction.
Here are some tasks that come under TCL:
- Commit: It is used to save the transaction on the database.
- Rollback: It is used to restore the database to original since the last Commit.
COMMIT :
- It is used to permanently save all the modifications are done (all the transactions) by the DML commands in the database. Once issued, it cannot be undone.
- DBMS software implicitly uses the COMMIT command before and after every DDL command to save the change permanently in the database.
- MySQL Syntax -
ROLLBACK :
- It is used to undo the transactions that have not already been permanently saved (or committed) to the database.
- It restores the previously stored value, i.e., the data present before the execution of the transactions.
- MySQL Syntax -
- To undo a group of transactions since last COMMIT or SAVEPOINT :
- To undo a group of transactions to a certain point :
SAVEPOINT :
- It is used to create a point within the groups of transactions to save or roll back later.
- It is used to roll the transactions back to a certain point without the need to roll back the whole group of transactions.
- MySQL Syntax -
- To create a SAVEPOINT :
- To release a SAVEPOINT :
AUTOCOMMIT :
- It is used to enable/disable the auto-commit process that commits each transaction after its execution.
- MySQL Syntax -
- To enable the AUTOCOMMIT process:
- To disable the AUTOCOMMIT process:
SET AUTOCOMMIT = 0;