0% found this document useful (0 votes)
2 views

SQL

The document provides an overview of SQL, covering Data Definition Language (DDL) for creating and modifying database structures, as well as data types. It explains constraints for ensuring data integrity, retrieval queries including SELECT statements and joins, and the use of INSERT, DELETE, and UPDATE statements for data manipulation. Additionally, it introduces views as virtual tables that simplify complex queries and enhance security.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL

The document provides an overview of SQL, covering Data Definition Language (DDL) for creating and modifying database structures, as well as data types. It explains constraints for ensuring data integrity, retrieval queries including SELECT statements and joins, and the use of INSERT, DELETE, and UPDATE statements for data manipulation. Additionally, it introduces views as virtual tables that simplify complex queries and enhance security.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SQL (12 LHs)

1. Data Definition and Data Types

• Data Definition Language (DDL):

o Used to define, create, and modify database structures.

o Key commands:

▪ CREATE: To create a new table, database, or object.

▪ ALTER: To modify an existing table or object.

▪ DROP: To delete a table, database, or object.

• Data Types:

o Specifies the type of data that can be stored in a column.

o Examples:

▪ Numeric Types: INT, FLOAT, DECIMAL

▪ String Types: CHAR, VARCHAR, TEXT

▪ Date/Time Types: DATE, TIME, DATETIME

▪ Boolean: TRUE, FALSE

2. Specifying Constraints

• Definition:

o Rules enforced on data in a table to ensure accuracy and integrity.

• Types:

o NOT NULL: Ensures a column cannot have a NULL value.

o UNIQUE: Ensures all values in a column are unique.

o PRIMARY KEY: Combines NOT NULL and UNIQUE.

o FOREIGN KEY: Ensures referential integrity by linking tables.

o CHECK: Ensures values meet a specific condition.

o DEFAULT: Sets a default value for a column.


3. Basic Retrieval Queries

• SELECT Statement:

o Used to retrieve data from one or more tables.

o Syntax:

SELECT column1, column2, ...

FROM table_name

WHERE condition;

o Example:

SELECT Name, Age FROM Student WHERE Age > 18;

4. Complex Retrieval Queries

• Joins:

o Combine rows from two or more tables based on a related column.

o Types:

▪ INNER JOIN: Returns matching rows.

▪ LEFT JOIN: Returns all rows from the left table.

▪ RIGHT JOIN: Returns all rows from the right table.

▪ FULL OUTER JOIN: Returns all rows when there is a match.

• Subqueries:

o Nested queries inside a main query.

o Example:

SELECT Name FROM Student WHERE Age = (SELECT MAX(Age) FROM Student);

• Aggregate Functions:

o Perform calculations on data: COUNT, SUM, AVG, MAX, MIN.

5. INSERT, DELETE, and UPDATE Statements

• INSERT:

o Adds new records to a table.


o Syntax:

INSERT INTO table_name (column1, column2, ...)

VALUES (value1, value2, ...);

• DELETE:

o Removes records from a table.

o Syntax:

DELETE FROM table_name WHERE condition;

• UPDATE:

o Modifies existing records in a table.

o Syntax:

o UPDATE table_name

SET column1 = value1, column2 = value2

WHERE condition;

6. Views

• Definition:

o A virtual table based on the result of an SQL query.

• Purpose:

o Simplifies complex queries.

o Provides security by restricting access to specific columns/rows.

o Example:

CREATE VIEW StudentView AS

SELECT Name, Age FROM Student WHERE Age > 18;

• Usage:

o Query a view like a table:

SELECT * FROM StudentView;

You might also like