0% found this document useful (0 votes)
28 views4 pages

SQL Refyne

Uploaded by

prashantkumarpd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
28 views4 pages

SQL Refyne

Uploaded by

prashantkumarpd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Types of SQL Commands

Data Definition Language (DDL)


Defines and manages the structure of database objects.
● CREATE: Creates a new database object (e.g., table, view).
● ALTER: Modifies an existing database object.
● DROP: Deletes a database object.
● TRUNCATE: Removes all rows from a table but retains the structure.

Data Manipulation Language (DML)


Manages data within database objects.
● SELECT: Retrieves data from the database.
● INSERT: Adds new rows to a table.
● UPDATE: Modifies existing data in a table.
● DELETE: Removes rows from a table.

Data Control Language (DCL)


Controls access and permissions within the database.
● GRANT: Gives users permission to perform tasks.
● REVOKE: Removes permissions from users.

Transaction Control Language (TCL)


Manages transactions to ensure data integrity.
● COMMIT: Saves changes made in the current transaction.
● ROLLBACK: Undoes changes made in the current transaction.
● SAVEPOINT: Sets a point within a transaction for partial rollback.
● RELEASE SAVEPOINT: Removes a savepoint.

Data Query Language (DQL)


Focused on querying the database to retrieve data.
● SELECT: Retrieves data from the database.
Simple explanations of the SQL terms:

Primary Key
A primary key uniquely identifies each record in a table. It must contain unique
values and cannot be NULL.
Foreign Key
A foreign key is a field in one table that uniquely identifies a row of another table. It
establishes a relationship between the two tables.
Unique Key
A unique key ensures that all values in a column are unique. Unlike the primary key,
a table can have multiple unique keys, and unique keys can contain NULL values.

RDBMS vs. DBMS


● RDBMS (Relational Database Management System): Stores data in related
tables and supports SQL.
● DBMS (Database Management System): Stores data in a single database
without relational capabilities.
Constraints
Constraints are rules applied to table columns to enforce data integrity. Examples
include PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, and CHECK.

Normalization vs. Denormalization


● Normalization: Organizes data to reduce redundancy and improve data
integrity by dividing a database into multiple related tables.
● Denormalization: Combines tables to reduce the complexity of queries and
improve read performance by increasing redundancy.

Triggers
Triggers are procedural codes that automatically execute in response to certain
events on a table, such as INSERT, UPDATE, or DELETE.

Clustered vs. Non-Clustered Index


● Clustered Index: Sorts and stores the table's data rows based on the index
key. There can be only one clustered index per table.
● Non-Clustered Index: Creates a separate structure from the data rows, which
contains pointers to the data. A table can have multiple non-clustered
indexes.
Types of Joins in SQL
● INNER JOIN: Returns matching rows from both tables.
● LEFT JOIN: Returns all rows from the left table and matching rows from the
right table.
● RIGHT JOIN: Returns all rows from the right table and matching rows from
the left table.
● FULL JOIN: Returns rows with a match in either table.
● CROSS JOIN: Returns the Cartesian product of both tables.

DROP vs. TRUNCATE vs. DELETE


● DROP: Deletes the table and its data.
● TRUNCATE: Removes all rows and keeps the table structure.
● DELETE: Removes specified rows and keeps the table structure.

WHERE vs. HAVING Clause


● WHERE: Filters rows before grouping.
● HAVING: Filters groups after grouping.
● GROUP BY clause groups rows that have the same values in specified columns
into summary rows,

Index
An index improves query performance by providing quick access to rows.
● Clustered Index: Sorts and stores data rows.
● Non-Clustered Index: Stores pointers to data rows.

RANK() vs. DENSE_RANK()


● RANK(): Gives ranks with gaps in case of ties.
● DENSE_RANK(): Gives ranks without gaps in case of ties.

Entities and Relationships


● Entities: Objects or things in the database (e.g., tables).
● Relationships: Associations between entities (e.g., foreign keys).

Types of Normalization
Organizes data to reduce redundancy and improve data integrity by dividing a
database into multiple related tables.
● 1NF: Eliminate duplicate columns.
● 2NF: Remove subsets of data that apply to multiple rows.
● 3NF: Remove columns that are not dependent on the primary key.

UNION, MINUS, and INTERSECT


● UNION: Combines results from two queries, removing duplicates.
● MINUS: Returns rows from the first query that are not found in the second.
● INTERSECT: Returns rows common to both queries.
Using a Cursor
Cursors are database objects used to retrieve, manipulate, and navigate through a
result set row by row.
● Declare: Define the cursor.
● Open: Execute the query and populate the cursor.
● Fetch: Retrieve the next row.
● Close: Release the cursor.
● Deallocate: Remove the cursor definition.

```sql
DECLARE cursor_name CURSOR FOR SELECT * FROM table_name;
OPEN cursor_name;
FETCH NEXT FROM cursor_name;
CLOSE cursor_name;
DEALLOCATE cursor_name;
```

You might also like