Thursday, September 21, 2023

What are the main categories of SQL statements?

 SQL (Structured Query Language) statements can be categorized into several main groups based on their primary purpose and function. These categories cover a wide range of tasks related to managing and manipulating relational databases. 

The main categories of SQL statements include:

1.Data Query Language (DQL) Statements:

  • SELECT: Used to retrieve data from one or more tables in the database. This statement is essential for data retrieval and is often used with various clauses and options to filter, sort, and aggregate data.

2.Data Definition Language (DDL) Statements:

  • CREATE: Used to create database objects such as tables, indexes, views, and schemas.
  • ALTER: Used to modify the structure of existing database objects, including adding, modifying, or deleting columns, constraints, or indexes.
  • DROP: Used to delete database objects like tables, indexes, views, or schemas.
  • TRUNCATE: Used to remove all data from a table but retains the table structure.
  • RENAME: Used to rename an existing database object, such as a table or column.

3.Data Manipulation Language (DML) Statements:

  • INSERT: Used to add new records (rows) into a table.
  • UPDATE: Used to modify existing records in a table.
  • DELETE: Used to remove records from a table.
  • MERGE: Used to perform conditional insert, update, or delete operations based on a specified condition.

4.Transaction Control Statements:

  • COMMIT: Used to save and permanently apply changes made during a transaction.
  • ROLLBACK: Used to undo changes made during a transaction.
  • SAVEPOINT: Used to set a savepoint within a transaction, allowing partial rollbacks.
  • SET TRANSACTION: Used to configure properties of a transaction, such as isolation level.

5.Data Control Language (DCL) Statements:

  • GRANT: Used to grant specific privileges or permissions to database objects to users or roles.
  • REVOKE: Used to revoke previously granted privileges or permissions from users or roles.

6.Data Integrity Statements:

  • CHECK: Used to define integrity constraints for a table, ensuring that data meets specified conditions.
  • CONSTRAINT: Used to specify constraints (e.g., primary keys, unique constraints) on table columns.
  • FOREIGN KEY: Used to define referential integrity constraints between tables.
  • UNIQUE: Used to ensure that values in a column are unique across rows.

7.Schema Manipulation Statements:

  • CREATE SCHEMA: Used to create a new schema in the database.
  • ALTER SCHEMA: Used to modify an existing schema.
  • DROP SCHEMA: Used to delete a schema and its contents from the database.

8.View and Index Statements:

  • CREATE VIEW: Used to create a virtual table based on the result of a query.
  • CREATE INDEX: Used to create an index on one or more columns of a table to improve query performance.

9.Other Statements:

  • DECLARE CURSOR: Used to declare a cursor for iterating over query results.
  • FETCH: Used to retrieve rows from a cursor.
  • CLOSE: Used to close a cursor.
  • EXECUTE: Used to execute a dynamic SQL statement.
  • SET: Used to assign values to variables or configuration settings.

These categories encompass the most commonly used SQL statements for interacting with relational databases. Depending on your specific tasks and requirements, you will use various SQL statements from these categories to manage and query your database effectively.

No comments:

Post a Comment

If you have any doubts. Please let me know

How can you create an alias for a table in a SQL query?

In SQL, you can create an alias for a table in a query to give the table a temporary, alternative name that you can use in the query. Table ...