Types of Database Languages

Types of Database Languages

A database language is a programming language designed to manage data in a database. There are different types of database languages, and each type serves a specific purpose. In this blog post, we will discuss the most common types of database languages.

Data Definition Language (DDL)

DDL is a type of database language used to define the structure of the database. It is used to create, alter, and delete database objects such as tables, views, indexes, and constraints.

Syntax of DDL Statements: DDL statements typically follow a similar syntax, which includes the following elements:

  • Keyword: Indicates the type of DDL statement being used, such as CREATE, ALTER, or DROP.

  • Object name: Specifies the name of the database object being created, altered, or dropped.

  • Attributes: Provides additional information about the database object, such as data types, constraints, and indexes.

Examples of DDL Statements:

  1. CREATE TABLE Statement: The CREATE TABLE statement is used to create a new table in a database. The syntax of the CREATE TABLE statement is as follows:
CREATE TABLE table_name (
    column1 datatype constraint,
    column2 datatype constraint,
    column3 datatype constraint,
    ...
);

For example, the following statement creates a new table called "customers" with three columns: "customer_id," "customer_name," and "customer_email":

CREATE TABLE customers (
    customer_id INT PRIMARY KEY,
    customer_name VARCHAR(50),
    customer_email VARCHAR(100) UNIQUE
);
  1. ALTER TABLE Statement: The ALTER TABLE statement is used to modify an existing table in a database. The syntax of the ALTER TABLE statement is as follows:
ALTER TABLE table_name
ADD column_name datatype constraint;

For example, the following statement adds a new column called "customer_address" to the "customers" table:

ALTER TABLE customers
ADD customer_address VARCHAR(200);
  1. DROP TABLE Statement: The DROP TABLE statement is used to delete a table and all of its data from a database. The syntax of the DROP TABLE statement is as follows:
DROP TABLE table_name;

For example, the following statement drops the "customers" table:

DROP TABLE customers;

Data Manipulation Language (DML)

DML stands for Data Manipulation Language, which is a type of database language used to manipulate data in a database. DML statements are used to insert, update, and delete data in a database. we will discuss DML in more detail, including its syntax and examples of DML statements.

Syntax of DML Statements: DML statements typically follow a similar syntax, which includes the following elements:

  • Keyword: Indicates the type of DML statement being used, such as INSERT, UPDATE, or DELETE.

  • Table name: Specifies the name of the table being manipulated.

  • Columns: Specifies the columns to be affected by the DML statement.

  • Values: Specifies the values to be inserted, updated, or deleted.

Examples of DML Statements:

  1. INSERT Statement: The INSERT statement is used to add new data to a table in a database. The syntax of the INSERT statement is as follows:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

For example, the following statement adds a new row to the "customers" table with values for the "customer_name" and "customer_email" columns:

INSERT INTO customers (customer_name, customer_email)
VALUES ('John Doe', 'johndoe@example.com');
  1. UPDATE Statement: The UPDATE statement is used to modify existing data in a table. The syntax of the UPDATE statement is as follows:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

For example, the following statement updates the "customer_email" column of the row where "customer_id" is equal to 1:

UPDATE customers
SET customer_email = 'newemail@example.com'
WHERE customer_id = 1;
  1. DELETE Statement: The DELETE statement is used to remove data from a table. The syntax of the DELETE statement is as follows:
DELETE FROM table_name
WHERE condition;

For example, the following statement deletes the row where "customer_id" is equal to 1:

DELETE FROM customers
WHERE customer_id = 1;

Data Control Language (DCL)

DCL stands for Data Control Language, which is a type of database language used to control access to data stored in a database. It is used to grant or revoke privileges to users, allowing them to access or modify data in a database. we will discuss DCL in more detail, including its syntax and examples of DCL statements.

Syntax of DCL Statements: DCL statements typically follow a similar syntax, which includes the following elements:

  • Keyword: Indicates the type of DCL statement being used, such as GRANT or REVOKE.

  • Privilege: Specifies the type of privilege being granted or revoked, such as SELECT or INSERT.

  • Object: Specifies the object being affected, such as a table or view.

  • User or role: Specifies the user or role being granted or revoked privileges.

Examples of DCL Statements:

  1. GRANT Statement: The GRANT statement is used to give users or roles the privilege to access or modify data in a database. The syntax of the GRANT statement is as follows:
GRANT privilege ON object TO user_or_role;

For example, the following statement grants the "SELECT" privilege on the "customers" table to the "marketing" role:

GRANT SELECT ON customers TO marketing;
  1. REVOKE Statement: The REVOKE statement is used to remove privileges from users or roles in a database. The syntax of the REVOKE statement is as follows:
REVOKE privilege ON object FROM user_or_role;

For example, the following statement revokes the "INSERT" privilege on the "orders" table from the "sales" role:

REVOKE INSERT ON orders FROM sales;

Transaction Control Language (TCL)

TCL stands for Transaction Control Language, which is a type of database language used to manage transactions within a database. It is used to control the behavior of transactions, including committing or rolling back changes made to the database.we will discuss TCL in more detail, including its syntax and examples of TCL statements.

Syntax of TCL Statements: TCL statements typically follow a similar syntax, which includes the following elements:

  • Keyword: Indicates the type of TCL statement being used, such as COMMIT or ROLLBACK.

Examples of TCL Statements:

  1. COMMIT Statement: The COMMIT statement is used to save changes made to the database since the last COMMIT or ROLLBACK statement. The syntax of the COMMIT statement is as follows:
COMMIT;

For example, the following statement commits the changes made to the database:

UPDATE customers
SET customer_email = 'newemail@example.com'
WHERE customer_id = 1;

COMMIT;
  1. ROLLBACK Statement: The ROLLBACK statement is used to undo changes made to the database since the last COMMIT statement. The syntax of the ROLLBACK statement is as follows:
ROLLBACK;

For example, the following statement rolls back the changes made to the database:

UPDATE customers
SET customer_email = 'newemail@example.com'
WHERE customer_id = 1;

ROLLBACK;