SQL COMMANDS
SQL COMMANDS
ChatGPT said:
SQL (Structured Query Language) commands are used to interact with relational
databases. These commands can be categorized into five main types:
Command Description
CREATE Creates a new table, database, index, or view.
ALTER Modifies an existing database structure (e.g., add, delete, or modify
columns).
DROP Deletes an existing database, table, or view.
TRUNCATE Removes all records from a table but keeps its structure.
RENAME Renames a table or column.
Example:
sql
Copy
Edit
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
2. Data Manipulation Language (DML)
DML commands are used to manipulate data stored in tables.
Command Description
INSERT Adds new records into a table.
UPDATE Modifies existing records in a table.
DELETE Removes specific records from a table.
Example:
sql
Copy
Edit
INSERT INTO students (id, name, age) VALUES (1, 'John Doe', 20);
UPDATE students SET age = 21 WHERE id = 1;
DELETE FROM students WHERE id = 1;
3. Data Query Language (DQL)
DQL commands are used to retrieve data from databases.
Command Description
SELECT Retrieves data from one or more tables.
Example:
sql
Copy
Edit
SELECT name, age FROM students WHERE age > 18;
4. Data Control Language (DCL)
DCL commands are used to manage access permissions.
Command Description
GRANT Gives user-specific privileges.
REVOKE Removes user-specific privileges.
Example:
sql
Copy
Edit
GRANT SELECT, INSERT ON students TO user1;
REVOKE INSERT ON students FROM user1;
5. Transaction Control Language (TCL)
TCL commands manage database transactions.
Command Description
COMMIT Saves all changes made in the transaction permanently.
ROLLBACK Reverts changes made in the transaction.
SAVEPOINT Sets a savepoint within a transaction for partial rollbacks.
Example:
sql
Copy
Edit
BEGIN TRANSACTION;
UPDATE students SET age = 22 WHERE id = 1;
SAVEPOINT sp1;
DELETE FROM students WHERE id = 2;
ROLLBACK TO sp1;
COMMIT;
Conclusion
SQL commands help in database management, ranging from defining structures (DDL),
manipulating data (DML), querying records (DQL), controlling access (DCL), and
managing transactions (TCL).