What is SQL?
SQL (Structured Query Language) is a standard programming language used to interact with relational databases. It is used to store, retrieve, update, and delete data.
SQL is also used to create and modify database structures such as tables, views, and indexes.
Example:
SELECT * FROM Employees;
This query retrieves all the records from the Employees table.
Example:
SELECT * FROM Employees;
This query retrieves all the records from the Employees table.
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
What is a Database?
A database is an organized collection of data that is stored and managed electronically.
It allows users to efficiently store, retrieve, update, and manage data.
Databases are used to handle large amounts of information in various applications such as websites, business systems, and applications.
Example: A customer database in an e-commerce website may store customer details like name, email, contact number, and purchase history.
- Create a Database
- Create Student table
- Insert Data into Student Table
- Select/ Retrieve data from table
- Where (Filter record based on condition)
- DISTINCT (Display Unique Record from a table)
Comprehensive
- DDL (Data Definition Language) – Defines the structure of the database. CREATE, ALTER, DROP, TRUNCATE, RENAME.
- DML (Data Manipulation Language) – Manages data stored in the database. INSERT, UPDATE, DELETE.
- DQL (Data Query Language) – Retrieves data from the database. SELECT
- DCL (Data Control Language) – Controls access to the data. GRANT, REVOKE
- TCL (Transaction Control Language) – Manages transactions in the database. COMMIT, ROLLBACK, SAVEPOINT
| DDL | DML | DQL | TCL | DCL |
|---|---|---|---|---|
| CREATE | INSERT | SELECT | COMMIT | GRANT |
| ALTER | UPDATE | ROLLBACK | REVOKE | |
| DROP | DELETE | SAVE POINT | ||
| TRUNCATE | ||||
| RENAME |
What is Primary Key?
A Primary Key is a column or a combination of columns in a table that uniquely identifies
each row in that table. It does not allow NULL values and must always contain unique
values.
Key Features:
- Uniquely identifies each record
- Cannot have duplicate values
- Cannot contain NULL values
- Only one primary key is allowed per table
This is a SQL Data Definition Language (DDL) statement.
CREATE TABLE Employee ( EmployeeID INT PRIMARY KEY, Name VARCHAR (50), Age INT ); |
|---|
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
What is Foreign Key?
A Foreign Key is a column or combination of columns in one table that refers to the Primary Key in another table.
It is used to create a relationship between two tables and enforce referential integrity.
Key Features:
- Establishes a relationship between two tables
- Can contain duplicate values
- Can accept NULL values
- Helps maintain data consistency
Parent table
CREATE TABLE Department ( DeptID INT PRIMARY KEY, DeptName VARCHAR (50) ); |
|---|
Child Table with Foreign Key
CREATE TABLE Employee ( EmployeeID INT PRIMARY KEY, Name VARCHAR (50), Age INT DeptID INT, FOREIGN KEY (DeptID) REFERENCES Department (DeptID) ); |
|---|
UNIQUE Key — simple and precise explanation
A UNIQUE key is a database rule (constraint) that does not allow duplicate values in a column (or a group of columns).
What it does
-
Every non-NULL value in the column must be different
-
It prevents duplicates
-
It allows NULL (usually only one NULL, depending on the database)
Key points (no confusion)
-
Ensures uniqueness of data
-
NULL is allowed (unlike PRIMARY KEY)
-
You can have multiple UNIQUE keys in one table
-
Used to protect important data like email, phone number, username
Example
CREATE TABLE Users ( UserID INT PRIMARY KEY, Email VARCHAR(100) UNIQUE ); |
|---|