Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1 views

SQL Detailed notes

This document provides a comprehensive overview of Structured Query Language (SQL), covering its features, data types, commands, constraints, joins, subqueries, indexes, views, stored procedures, triggers, normalization, keys, functions, and performance optimization tips. SQL is essential for interacting with relational databases, allowing for efficient data management and manipulation. Key components include various command categories, data types, and methods for ensuring data integrity and performance.

Uploaded by

varunnani2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

SQL Detailed notes

This document provides a comprehensive overview of Structured Query Language (SQL), covering its features, data types, commands, constraints, joins, subqueries, indexes, views, stored procedures, triggers, normalization, keys, functions, and performance optimization tips. SQL is essential for interacting with relational databases, allowing for efficient data management and manipulation. Key components include various command categories, data types, and methods for ensuring data integrity and performance.

Uploaded by

varunnani2003
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Structured Query Language (SQL) - Detailed Notes

1. Introduction to SQL
SQL (Structured Query Language) is a standard language used to interact with relational
databases. It enables users to create, retrieve, update, and delete data efficiently. SQL is widely
used in database management systems like MySQL, PostgreSQL, Oracle, and SQL Server.

Features of SQL:

●​ Declarative Language
●​ High Performance
●​ Portability
●​ Security
●​ Scalability

2. SQL Data Types


SQL provides different data types to store various kinds of data.

Common SQL Data Types:

●​ Numeric: INT, FLOAT, DECIMAL, BIGINT


●​ String: CHAR, VARCHAR, TEXT
●​ Date & Time: DATE, TIME, TIMESTAMP
●​ Boolean: BOOLEAN
●​ Binary: BLOB

3. SQL Commands
SQL commands are categorized into five types:

a) Data Definition Language (DDL)

Used to define and modify database schema.

●​ CREATE - Creates a new table or database


●​ ALTER - Modifies an existing database object
●​ DROP - Deletes a table or database
b) Data Manipulation Language (DML)

Used for manipulating data within tables.

●​ INSERT - Adds new data


●​ UPDATE - Modifies existing data
●​ DELETE - Removes data

c) Data Query Language (DQL)

Used to retrieve data from a database.

●​ SELECT - Retrieves data

d) Data Control Language (DCL)

Used to control access to data.

●​ GRANT - Provides access


●​ REVOKE - Removes access

e) Transaction Control Language (TCL)

Used to manage transactions in a database.

●​ COMMIT - Saves changes permanently


●​ ROLLBACK - Reverts changes
●​ SAVEPOINT - Sets a savepoint within a transaction

4. Constraints in SQL
Constraints are used to enforce rules on data columns.

●​ NOT NULL - Ensures a column cannot have NULL values


●​ UNIQUE - Ensures all values in a column are unique
●​ PRIMARY KEY - Uniquely identifies each record
●​ FOREIGN KEY - Ensures referential integrity between tables
●​ CHECK - Ensures values meet a condition
●​ DEFAULT - Assigns a default value

5. Joins and Subqueries


SQL Joins

Joins are used to combine data from multiple tables.

●​ INNER JOIN - Matches records with common values


●​ LEFT JOIN - Includes all records from the left table
●​ RIGHT JOIN - Includes all records from the right table
●​ FULL JOIN - Includes all records from both tables

SQL Subqueries

A subquery is a query nested inside another query.

●​ Used with SELECT, INSERT, UPDATE, DELETE


●​ Helps in complex filtering and aggregation

6. Indexes and Views


Indexes

Indexes speed up searches by creating pointers to data.

●​ Clustered Index - Determines physical storage


●​ Non-Clustered Index - Separate from data storage

Views

A view is a virtual table based on a query.

●​ Simplifies complex queries


●​ Improves security

7. Stored Procedures and Triggers


Stored Procedures

A stored procedure is a set of SQL statements stored in the database.

●​ Reduces redundancy
●​ Improves performance

Triggers
Triggers are automatic responses to specific database events.

●​ Used for auditing and enforcing business rules

8. Normalization and Keys


Normalization

Normalization minimizes redundancy and improves data integrity.

●​ 1NF (First Normal Form) - Removes duplicate columns


●​ 2NF (Second Normal Form) - Eliminates partial dependencies
●​ 3NF (Third Normal Form) - Eliminates transitive dependencies
●​ BCNF (Boyce-Codd Normal Form) - Stronger 3NF

Keys

Keys uniquely identify records in a table.

●​ Primary Key - Uniquely identifies a row


●​ Foreign Key - Links two tables
●​ Candidate Key - Can be a primary key
●​ Composite Key - Consists of multiple columns

9. SQL Functions
Aggregate Functions

●​ COUNT() - Counts rows


●​ SUM() - Adds numeric values
●​ AVG() - Calculates average
●​ MAX() - Returns the maximum value
●​ MIN() - Returns the minimum value

String Functions

●​ UPPER() - Converts text to uppercase


●​ LOWER() - Converts text to lowercase
●​ CONCAT() - Combines strings

Date Functions
●​ NOW() - Returns the current date/time
●​ DATEADD() - Adds a time interval
●​ DATEDIFF() - Calculates difference between dates

10. Performance Optimization Tips


●​ Use Indexes effectively
●​ Avoid using SELECT *, specify columns
●​ Use Joins instead of Subqueries when possible
●​ Optimize Query Execution Plans
●​ Partition large tables for efficiency

You might also like