SQL Command Categories
SQL (Structured Query Language) contains many commands, which are categorized based on their functionality:DDL (Data Definition Language)
Purpose: Defines or changes the structure of database objects. Commands- CREATE
- ALTER
- DROP
- Create a new table
- Add a new column to an existing table
- Delete a table from the database
- Which command would you use to create a new table?
- Which command would you use to add a new column?
- Does DDL modify the table structure or the data?
DML (Data Manipulation Language)
Purpose: Adds, updates, or removes data stored in tables. Commands- INSERT
- UPDATE
- DELETE
- Add a new student record
- Update a student’s email address
- Delete an inactive student
- Which command adds new records?
- Which command modifies existing records?
- Which command removes records?
DQL (Data Query Language)
Purpose: Retrieves data from one or more tables. Command- SELECT
- Display all students
- Display students from the CSE department
- Display students whose marks are greater than 80
- Which SQL command is used to retrieve data?
- Does
SELECTchange the data stored in the table?
TCL (Transaction Control Language)
Purpose: Controls transactions and ensures data consistency. Commands- BEGIN
- COMMIT
- ROLLBACK
- Start a transaction
- Save all changes permanently
- Undo changes before they are committed
- Which command permanently saves changes?
- Which command cancels uncommitted changes?
- Why are transactions important?
Creating Tables
Naming Conventions
[!TIP] Plural Table Names: It is a very common industry standard and database convention to name tables in their plural form (e.g.,employees,departments,students) because a table represents a collection of multiple records. Columns, however, are named in the singular form (e.g.,employee_id,salary) as they represent single attributes of a record.
Syntax
Explanation
- PRIMARY KEY — Uniquely identifies each row.
- NOT NULL — Prevents NULL values.
- FOREIGN KEY — Creates a relationship with another table.
Practice
Create astudents table with student_id, student_name, and email.
Solution
Solution
ALTER TABLE
Used to modify an existing table.Add a Column
Rename a Column
Practice
Add aphone column to the employees table.
Solution
Solution
DROP TABLE
Deletes an entire table permanently.Note: All data in the table is permanently deleted.
Practice
Delete thestudents table.
Solution
Solution
INSERT Statement
Used to insert new rows into a table.Insert a Single Row
Insert Multiple Rows
Insert by Specifying Columns
Practice
Insert an employee named Anitha with a salary of 55000.Solution
Solution
UPDATE Statement
Used to modify existing records.Syntax
Important: Always use a WHERE clause unless you intend to update every row.
Practice
Increase Rahul’s salary to ₹75,000.Solution
Solution
DELETE Statement
Deletes one or more rows.
Important: Omitting the WHERE clause deletes all rows.
Practice
Delete the employee named Sneha.Solution
Solution
Summary
In this chapter, you learned:- SQLite basics
- SQL command categories
- SQLite data types
- CREATE TABLE
- ALTER TABLE
- DROP TABLE
- INSERT
- UPDATE
- DELETE