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

SQL Notes

SQL (Structured Query Language) is essential for managing relational databases, encompassing data creation, manipulation, and querying. Key concepts include databases, tables, primary and foreign keys, along with commands for data definition, manipulation, and querying. Best practices emphasize meaningful naming, normalization, indexing, and security measures to enhance database performance and integrity.

Uploaded by

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

SQL Notes

SQL (Structured Query Language) is essential for managing relational databases, encompassing data creation, manipulation, and querying. Key concepts include databases, tables, primary and foreign keys, along with commands for data definition, manipulation, and querying. Best practices emphasize meaningful naming, normalization, indexing, and security measures to enhance database performance and integrity.

Uploaded by

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

SQL Notes

SQL Fundamentals
SQL (Structured Query Language) is the standard language for relational database
management systems. It's used for creating, accessing, manipulating, and managing data in
databases.

Database Concepts

 Database: Organized collection of structured data


 Table: Collection of related data organized in rows and columns
 Schema: Logical structure that defines the organization of database objects
 Primary Key: Unique identifier for each record in a table
 Foreign Key: Field that links to primary key in another table

Data Definition Language (DDL)


Creating Tables
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints,
...
PRIMARY KEY (column)
);

Common Data Types

 Numeric: INT, BIGINT, DECIMAL, FLOAT


 String: VARCHAR, CHAR, TEXT
 Date/Time: DATE, TIME, DATETIME, TIMESTAMP
 Boolean: BOOLEAN, BIT
 Binary: BLOB, BINARY

Modifying Tables
ALTER TABLE table_name ADD column_name datatype;
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE table_name MODIFY COLUMN column_name new_datatype;

Deleting Objects
DROP TABLE table_name;
DROP DATABASE database_name;
TRUNCATE TABLE table_name; -- removes all rows but keeps structure

Data Manipulation Language (DML)


Inserting Data
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);

Updating Data
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Deleting Data
DELETE FROM table_name WHERE condition;

Data Query Language (DQL)


Basic Queries
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column ASC/DESC
LIMIT number;

Aggregate Functions

 COUNT(): Returns number of rows


 SUM(): Returns sum of numeric values
 AVG(): Returns average of numeric values
 MIN(): Returns minimum value
 MAX(): Returns maximum value

Grouping Data
SELECT column, COUNT(*)
FROM table_name
GROUP BY column
HAVING condition; -- like WHERE but for grouped data

Table Relationships
JOIN Operations

 INNER JOIN: Returns records with matching values in both tables


 LEFT JOIN: Returns all records from left table and matching from right
 RIGHT JOIN: Returns all records from right table and matching from left
 FULL JOIN: Returns all records when there's a match in either table

JOIN Syntax
SELECT columns
FROM table1
JOIN table2 ON table1.column = table2.column;

Advanced SQL Concepts


Subqueries
SELECT column
FROM table
WHERE column IN (SELECT column FROM another_table WHERE condition);

Common Table Expressions (CTE)


WITH cte_name AS (
SELECT columns FROM table WHERE condition
)
SELECT * FROM cte_name;

Views
CREATE VIEW view_name AS
SELECT columns
FROM table
WHERE condition;

Indexes
CREATE INDEX index_name ON table_name (column);

Transactions
BEGIN TRANSACTION;
SQL statements;
COMMIT; -- or ROLLBACK to undo

SQL Best Practices


 Use meaningful table and column names
 Always use appropriate data types
 Normalize database structure to reduce redundancy
 Use indexes on frequently queried columns
 Write optimized queries to improve performance
 Use parameterized queries to prevent SQL injection
 Maintain proper documentation
 Regularly backup database

You might also like