sql DDL query
sql DDL query
Create Table
The CREATE TABLE command is used to create a new table in the database.
sql
Example:
sql
2. Alter Table
The ALTER TABLE command is used to modify an existing table, like adding, deleting, or
modifying columns.
sql
sql
To modify a column:
sql
To drop a column:
sql
ALTER TABLE Employees
DROP COLUMN Email;
3. Drop Table
The DROP TABLE command is used to delete an existing table and all of its data.
sql
Example:
sql
4. Create Index
The CREATE INDEX command is used to create an index on one or more columns of a table to
improve query performance.
sql
Example:
sql
5. Drop Index
sql
Example:
sql
6. Create Database
The CREATE DATABASE command is used to create a new database.
sql
Example:
sql
7. Alter Database
The ALTER DATABASE command is used to modify a database's properties (like changing the
name or altering its settings).
sql
sql
8. Drop Database
The DROP DATABASE command is used to delete an existing database and all of its contents.
sql
Example:
sql
9. To rename table.
• Syntax: ALTER TABLE table_name RENAME TO new_table_name;
• Columns can also be given a new name with the use of ALTER TABLE.
1. Database
Purpose: It stores data and allows for management, querying, and security of data.
Components: It can contain multiple tables, views, indexes, stored procedures,
triggers, etc.
Example: CompanyDB, CustomerDB, etc.
sql
2. Table
A table is a collection of rows and columns used to store data in a structured format. A table is a
fundamental unit of storage within a database.
Purpose: It holds the actual data for an application, organized into rows and columns.
Each table corresponds to an entity (like "employees" or "orders") in the system.
Structure: Tables have columns (fields) that define the type of data each row (record)
can store.
Example: A table called Employees might have columns like EmployeeID, FirstName,
LastName, and HireDate.
sql
3. Index
An index is a database object that improves the speed of data retrieval operations on a table. It is
used to quickly locate rows based on the values in one or more columns of a table.
Purpose: Indexes speed up query processing by allowing the database to quickly locate
data without having to scan the entire table. They work like an index in a book—helping
you find specific data more efficiently.
Structure: It is built on one or more columns of a table and works by creating a sorted
data structure (typically a B-tree) that allows fast searching.
Example: An index on the LastName column of the Employees table could speed up
queries filtering by last name.
sql
Key Differences:
In short:
1. SELECT
The SELECT statement is used to retrieve data from one or more tables in a database. It is the
most commonly used query in SQL to fetch records.
sql
sql
2. INSERT INTO
The INSERT INTO statement is used to add new records (rows) into a table.
sql
Example:
sql
3. UPDATE
sql
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
sql
UPDATE Employees
SET LastName = 'Smith'
WHERE EmployeeID = 101;
4. DELETE FROM
sql
Example:
sql
5. MERGE INTO
The MERGE INTO statement is used for merging two tables by updating, inserting, or deleting data
based on conditions. It’s often called an "upsert" operation (update or insert).
sql
Example:
sql
6. TRUNCATE TABLE
The TRUNCATE TABLE statement is used to remove all rows from a table quickly and efficiently.
Unlike DELETE, TRUNCATE does not log individual row deletions and cannot be rolled back (in
most cases).
Purpose: To delete all rows from a table without affecting the structure of the table.
Syntax:
sql
Example:
sql
GRANT Command:
The GRANT command is used to give specific privileges (permissions) to users or roles for
database objects.
Syntax:
sql
Example:
sql
This gives the user john permission to select and insert data into the employees table.
REVOKE Command:
The REVOKE command is used to remove specific privileges that have been previously granted to
a user or role.
Syntax:
sql
Example:
sql
This removes the INSERT privilege for the user john on the employees table.
COMMIT:
The COMMIT command is used to save all the changes made during the current transaction to the
database. Once a transaction is committed, all the operations within it (e.g., inserts, updates,
deletes) are permanently stored in the database, and the transaction is complete.
Syntax:
sql
COMMIT;
Example:
sql
BEGIN TRANSACTION;
UPDATE employees SET salary = salary + 1000 WHERE department = 'HR';
COMMIT;
In this example, the UPDATE statement is applied to the employees table, and the changes are
committed, making them permanent in the database.
2. ROLLBACK:
The ROLLBACK command is used to undo all changes made during the current transaction. If a
transaction fails or you want to discard all the changes made in the transaction, you can use
ROLLBACK. This restores the database to the state it was in before the transaction began.
Syntax:
sql
ROLLBACK;
Example:
sql
BEGIN TRANSACTION;
UPDATE employees SET salary = salary + 1000 WHERE department = 'HR';
ROLLBACK;
In this example, the UPDATE statement changes the employees table, but the ROLLBACK command
undoes these changes, so no permanent modification occurs.
3. SAVEPOINT:
A SAVEPOINT allows you to set a point within a transaction to which you can later ROLLBACK. It’s
useful when you want to partially undo a transaction. You can roll back to a specific savepoint
instead of rolling back the entire transaction.
Syntax:
sql
SAVEPOINT savepoint_name;
sql
sql
BEGIN TRANSACTION;
UPDATE employees SET salary = salary + 1000 WHERE department = 'HR';
SAVEPOINT before_update;
UPDATE employees SET salary = salary - 500 WHERE department = 'HR';
ROLLBACK TO SAVEPOINT before_update;
COMMIT;
In this example: