Unit 2 - Notes
INT306
Unit 2: Relational Query Language
1. SQL Command Classifications
SQL (Structured Query Language) is the standard language for interacting with Relational Database Management Systems (RDBMS). It is not a single block of commands but is categorized based on the type of operation performed.
A. Data Definition Language (DDL)
DDL is used to define, alter, and manage the database schema (structure). These commands deal with the descriptions of the database schema and are used to create and modify the structure of database objects in the database.
-
Characteristics:
- DDL commands affect the metadata (data about data).
- They are auto-committed (changes are saved permanently immediately; they cannot be rolled back).
-
Key Commands:
- CREATE: Used to create the database or its objects (like tables, indexes, functions, views, store procedures, and triggers).
SQLCREATE TABLE Students ( StudentID int, Name varchar(255), Age int ); - DROP: Used to delete objects from the database. It removes the table structure and all data.
SQLDROP TABLE Students; - ALTER: Used to modify the structure of the database. This includes adding, modifying, or deleting columns in an existing table.
SQLALTER TABLE Students ADD Email varchar(255); - TRUNCATE: Used to remove all records from a table, including all spaces allocated for the records are removed. The structure remains.
SQLTRUNCATE TABLE Students; - RENAME: Used to rename an object existing in the database.
- CREATE: Used to create the database or its objects (like tables, indexes, functions, views, store procedures, and triggers).
B. Data Manipulation Language (DML)
DML commands are used for managing data within schema objects. They deal with the manipulation of the data present in the database.
-
Characteristics:
- DML commands affect the actual data, not the structure.
- They are not auto-committed (changes can be rolled back unless committed).
-
Key Commands:
- INSERT: Used to insert data into a table.
SQLINSERT INTO Students (StudentID, Name, Age) VALUES (1, 'John Doe', 20); - UPDATE: Used to update existing data within a table.
SQLUPDATE Students SET Age = 21 WHERE StudentID = 1; - DELETE: Used to delete records from a database table.
SQLDELETE FROM Students WHERE StudentID = 1; - SELECT: (Often categorized under DQL - Data Query Language, but functionally manipulates the view of data). Used to retrieve data from the database.
- INSERT: Used to insert data into a table.
C. Data Control Language (DCL)
DCL commands are used to grant and take back authority from any database user. They deal with rights, permissions, and other controls of the database system.
- Key Commands:
- GRANT: Gives user's access privileges to the database.
SQLGRANT SELECT, UPDATE ON Students TO user1; - REVOKE: Withdraws user's access privileges given by using the GRANT command.
SQLREVOKE SELECT ON Students FROM user1;
- GRANT: Gives user's access privileges to the database.
D. Transaction Control Language (TCL)
TCL commands deal with the transaction within the database. They allow you to control the outcome of DML statements.
- Key Commands:
- COMMIT: Commits a transaction. It permanently saves all the changes made in the transaction to the database.
- ROLLBACK: Restores the database to the last committed state (undoes changes).
- SAVEPOINT: Sets a savepoint within a transaction to which you can later roll back.
2. Database Keys
Keys are fundamental elements in relational databases used to establish relationships between tables and ensure unique identification of records.
A. Primary Key (PK)
- Definition: A column (or set of columns) that uniquely identifies each row in a table.
- Rules:
- Values must be unique.
- Cannot contain
NULLvalues. - A table can have only one Primary Key.
- Example:
StudentIDin a Student table.
B. Candidate Key
- Definition: A set of attributes that can uniquely identify a tuple. A super key with no repeated attributes is a candidate key.
- Relationship: The Primary Key is selected from the pool of Candidate Keys.
- Example: In a table with
StudentIDandEmail(where both are unique), both are Candidate Keys.
C. Super Key
- Definition: A set of attributes that can uniquely identify a tuple. It is a superset of a Candidate Key.
- Example: If
StudentIDis a unique key, then(StudentID, Name)is a Super Key, even thoughNameis not required for uniqueness.
D. Foreign Key (FK)
- Definition: A column or group of columns in a relational database table that provides a link between data in two tables. It acts as a cross-reference between tables.
- Function: Enforces Referential Integrity. The value in a Foreign Key column must match a value in the Primary Key of the parent table, or be NULL.
- Example:
DepartmentIDin theStudentstable linking toDepartmentIDin theDepartmentstable.
E. Alternate Key (Secondary Key)
- Definition: A key that was a Candidate Key but was not selected to be the Primary Key.
- Example: If
StudentIDis chosen as PK, thenEmailbecomes the Alternate Key.
F. Composite Key
- Definition: A key that consists of two or more attributes (columns) that together uniquely identify an entity occurrence.
- Example: In a
CourseGradestable, neitherStudentIDnorCourseIDis unique alone, but the combination(StudentID, CourseID)is unique.
3. Integrity Constraints
Integrity constraints are a set of rules used to maintain the quality of information. They ensure that the data insertion, updating, and other processes do not ruin the data integrity.
A. Types of Integrity
- Entity Integrity: Ensures that each row in a table is a unique instance. (Enforced by Primary Keys).
- Referential Integrity: Ensures the consistency of data between cross-referenced tables. (Enforced by Foreign Keys).
- Domain Integrity: Ensures that all data in a column falls within defined valid limits (e.g., data types, check constraints).
B. SQL Constraints
When creating or altering tables, specific keywords define these rules:
- NOT NULL: Ensures that a column cannot have a NULL value.
SQLName varchar(255) NOT NULL - UNIQUE: Ensures that all values in a column are different.
SQLEmail varchar(255) UNIQUE - PRIMARY KEY: A combination of a
NOT NULLandUNIQUE. Uniquely identifies each row in a table. - FOREIGN KEY: Uniquely identifies a row/record in another table.
- CHECK: Ensures that all values in a column satisfy a specific condition.
SQLAge int CHECK (Age >= 18) - DEFAULT: Sets a default value for a column when no value is specified.
SQLCity varchar(255) DEFAULT 'New York'
4. SQL Basic Operations
The core of SQL usage lies in the basic operations used to query and filter data.
A. The SELECT Statement
The most commonly used statement. It allows you to select data from a database.
- Syntax:
SQLSELECT column1, column2, ... FROM table_name; - Select All: Using the asterisk (
*) selects all columns.
SQLSELECT * FROM Students; - DISTINCT: Returns only distinct (different) values.
SQLSELECT DISTINCT City FROM Students;
B. The WHERE Clause
Used to filter records. It extracts only those records that fulfill a specified condition.
- Syntax:
SQLSELECT column1, column2 FROM table_name WHERE condition;
C. SQL Operators
Operators are reserved words or characters used primarily in a WHERE clause to perform operation(s), such as comparisons and arithmetic operations.
1. Arithmetic Operators
+(Add),-(Subtract),*(Multiply),/(Divide),%(Modulus).
2. Comparison Operators
=: Equal to<>or!=: Not equal to>: Greater than<: Less than>=: Greater than or equal to<=: Less than or equal to
3. Logical Operators
- AND: Displays a record if all the conditions separated by AND are TRUE.
- OR: Displays a record if any of the conditions separated by OR is TRUE.
- NOT: Displays a record if the condition(s) is NOT TRUE.
4. Special Operators
- BETWEEN: Selects values within a given range.
SQLWHERE Age BETWEEN 18 AND 25; - LIKE: Used to search for a specified pattern in a column.
%represents zero, one, or multiple characters._represents a single character.
SQLWHERE Name LIKE 'A%'; -- Starts with A
- IN: Allows you to specify multiple values in a WHERE clause (shorthand for multiple OR conditions).
SQLWHERE City IN ('Paris', 'London', 'Berlin'); - IS NULL / IS NOT NULL: Used to test for empty values (NULL). You cannot use
=with NULL.
SQLWHERE Email IS NULL;
D. Sorting Results (ORDER BY)
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
- Default: Ascending order.
- Syntax:
SQLSELECT * FROM Students ORDER BY Age DESC; -- Sorts by Age high to low
E. Aliases (AS)
SQL aliases are used to give a table, or a column in a table, a temporary name to make it more readable.
SELECT Name AS StudentName, Age AS StudentAge
FROM Students;