0% found this document useful (0 votes)
19 views3 pages

DBMS SQL Commands Overview with Examples

The document outlines various SQL commands categorized into Data Definition Language (DDL), Data Manipulation Language (DML), Transaction Control Language (TCL), and Data Control Language (DCL), providing examples for each. It includes commands for creating, altering, and dropping tables, as well as inserting, updating, and deleting data. Additionally, it covers transaction control commands and other useful SQL functions like indexing, grouping, and joining tables.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

DBMS SQL Commands Overview with Examples

The document outlines various SQL commands categorized into Data Definition Language (DDL), Data Manipulation Language (DML), Transaction Control Language (TCL), and Data Control Language (DCL), providing examples for each. It includes commands for creating, altering, and dropping tables, as well as inserting, updating, and deleting data. Additionally, it covers transaction control commands and other useful SQL functions like indexing, grouping, and joining tables.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DBMS SQL Commands with Examples

1. Data Definition Language (DDL)


-- CREATE

CREATE TABLE students (

id INT PRIMARY KEY,

name VARCHAR(50),

age INT

);

-- ALTER

ALTER TABLE students ADD email VARCHAR(100);

-- DROP

DROP TABLE students;

-- TRUNCATE

TRUNCATE TABLE students;

-- RENAME

RENAME TABLE students TO college_students;

2. Data Manipulation Language (DML)


-- INSERT

INSERT INTO students (id, name, age) VALUES (1, 'Rahul', 20);

-- SELECT

SELECT * FROM students;

-- UPDATE

UPDATE students SET age = 21 WHERE id = 1;

-- DELETE

DELETE FROM students WHERE id = 1;


3. Transaction Control Language (TCL)
-- START TRANSACTION

START TRANSACTION;

-- COMMIT

COMMIT;

-- ROLLBACK

ROLLBACK;

-- SAVEPOINT

SAVEPOINT my_savepoint;

-- ROLLBACK TO SAVEPOINT

ROLLBACK TO my_savepoint;

4. Data Control Language (DCL)


-- GRANT

GRANT SELECT, INSERT ON students TO 'user1'@'localhost';

-- REVOKE

REVOKE INSERT ON students FROM 'user1'@'localhost';

5. Other Useful SQL Commands


-- DESC

DESC students;

-- SHOW TABLES

SHOW TABLES;

-- USE

USE my_database;

-- CREATE INDEX

CREATE INDEX idx_name ON students(name);

-- DROP INDEX
DROP INDEX idx_name ON students;

-- GROUP BY

SELECT age, COUNT(*) FROM students GROUP BY age;

-- ORDER BY

SELECT * FROM students ORDER BY name ASC;

-- WHERE

SELECT * FROM students WHERE age > 18;

-- HAVING

SELECT age, COUNT(*) FROM students GROUP BY age HAVING COUNT(*) > 1;

-- JOIN

SELECT [Link], [Link], [Link]

FROM students s

JOIN marks m ON [Link] = m.student_id;

Common questions

Powered by AI

DROP and TRUNCATE are SQL commands used to remove data from a table, but they have distinct effects. DROP TABLE command removes the table entirely, including its structure, constraints, and data. Once executed, the table is no longer available unless recreated anew. TRUNCATE TABLE, however, only removes all data from the table while keeping the table structure intact. It is a faster operation compared to deleting row by row via DELETE statements, but unlike DROP, it does not affect the table's structure or its dependent database objects like indexes and constraints .

GRANT and REVOKE commands are essential for managing database user permissions in scenarios where access control is paramount. GRANT is used to assign specific privileges such as SELECT, INSERT, UPDATE, or DELETE on database objects to users or roles, ensuring that only authorized entities can perform certain operations. REVOKE, on the other hand, is used to remove such permissions when users no longer need them or when tightening security is necessary. These commands are crucial in environments with multiple users having varying access roles and in applications where sensitive data must be protected from unauthorized operations .

The CREATE INDEX command improves database performance by allowing faster retrieval of rows from a table, especially during search queries that involve a WHERE clause. By creating an index on columns frequently searched against, the database engine can quickly locate the data without scanning the entire table. However, creating too many indexes can negatively affect performance due to increased overhead for maintenance tasks such as updates, deletes, and inserts, which need to maintain the index structures. Therefore, it is essential to balance the number of indexes considering the read-write ratio of the database .

A RENAME TABLE operation becomes necessary when changing the table name to reflect new project requirements, such as a change in terminology, better documentation, or mergers that may cause naming conflicts. Despite RENAME TABLE being one of the simplest schema modification operations, challenges can arise due to dependencies like foreign key constraints, application code using the table name, or existing scripts or stored procedures that refer to the original table name. Proper impact analysis and updates to all references are crucial to avoid runtime errors and application failures .

SAVEPOINT in transaction management allows the setting of a point within a transaction to which a transaction can be partially rolled back, providing more granular control over transaction manipulation. Unlike a full ROLLBACK that undoes all changes from the start of the transaction, a SAVEPOINT enables rolling back to a specific point, leaving the preceding transaction changes intact. This is particularly useful in long transactions where only a part of it needs correction rather than discarding the entire transaction, thereby optimizing resource utilization and enhancing error recovery .

The ALTER TABLE command is used to modify an existing table's schema in structured databases. This command can add, modify, or delete columns within a table. For example, ALTER TABLE can be used to introduce a new column, such as an email field in the students table, with the statement ALTER TABLE students ADD email VARCHAR(100);. It can also be utilized to change data types of existing columns or to rename them. This flexibility helps database administrators manage evolving data requirements without necessitating table reconstruction .

JOIN operations in SQL enhance querying capabilities by allowing data to be combined from two or more tables based on related columns, enabling complex data retrieval scenarios that mimic real-world relationships among entities. INNER JOIN returns records that have matching values in both tables involved in the join, and it excludes records without matches. LEFT JOIN (or LEFT OUTER JOIN) includes all records from the left table and the matched records from the right table; where there is no match, NULL values fill the columns from the right table. Thus, LEFT JOIN allows the inclusion of unmatched rows, preserving more complete data from the reference table .

The ROLLBACK command is used to undo all the changes made to the database during the current transaction. When a TRANSACTION is started, any subsequent DML operations like INSERT, UPDATE, or DELETE are tentative until the transaction is committed. If an error occurs or if there's a need to revert the transaction, using ROLLBACK ensures that all changes since the last COMMIT or the start of the transaction are undone. This command is crucial for maintaining the ACID properties (Atomicity, Consistency, Isolation, Durability) of a database, as it ensures that only validated data modifications are permanently applied .

Using the COMMIT command following data manipulation operations solidifies changes made within a transaction, ensuring that they become permanent and visible to other operations. Without COMMIT, modifications remain tentative and are not saved to the database permanently. Neglecting to use COMMIT could result in data loss if a session ends unexpectedly or a system error occurs, as uncommitted data would automatically be rolled back to its previous state. Therefore, COMMIT is crucial for maintaining data integrity and consistent application state .

ORDER BY and GROUP BY clauses enhance SQL queries by organizing and aggregating data respectively. ORDER BY sorts the result set using one or more columns in ascending or descending order, improving readability and relevance of data output by prioritizing certain records. GROUP BY, on the other hand, groups rows that have the same values in specified columns into summary rows, which is often used with aggregate functions like COUNT, SUM, or AVG. The primary distinction lies in their purpose: ORDER BY arranges data, while GROUP BY combines data to produce meaningful summaries .

You might also like