PCLCHS

What is SQL

SQL (Structured Query Language) is a standard programming language used to manage and manipulate relational databases. It allows you to interact with databases by performing operations like retrieving data, inserting new records, updating existing ones, and deleting unnecessary data.

What Can SQL Do?

SQL can be used to:

  • 📄Create and manage
    database structures
    (tables, views, indexes)
  • 📥Insert new records
    into a table
  • 🔍Query the database for specific information
  • ✏️ Update existing data
  • 🗑️ Delete records
  • 🔐 Set permissions on tables, procedures, and views

SQL

Basic SQL Commands (CRUD)

  1. CREATE – Creates a new table or database

sqlCopyEditCREATETABLE Students (    ID INT,    Name VARCHAR(50),    Age INT);

SQL

  1. INSERT – Adds new data

sqlCopyEditINSERTINTO Students (ID, Name, Age)VALUES (1, ‘Alice’, 20);

  1. SELECT – Retrieves data

sqlCopyEditSELECT*FROM Students;

  1. UPDATE – Modifies existing data

sqlCopyEditUPDATE StudentsSET Age =21WHERE ID =1;

  1. DELETE – Removes data

sqlCopyEditDELETEFROM StudentsWHERE ID =1;

Types of SQL Statements

  1. DDL (Data Definition Language)
    • Deals with structure: CREATE, ALTER, DROP
  2. DML (Data Manipulation Language)
    • Deals with data: SELECT, INSERT, UPDATE, DELETE
  3. DCL (Data Control Language)
    • Controls access: GRANT, REVOKE
  4. TCL (Transaction Control Language)
    • Manages transactions: COMMIT, ROLLBACK, SAVEPOINT

Where is SQL Used?

  • Web applications
  • Data analysis
  • Backend services
  • Enterprise databases (like MySQL, PostgreSQL, Oracle, SQL Server)

Let me know if you want a beginner-friendly practice project or cheat sheet!

Data Definition Language and Data Manipulation Language

Great! Let’s break down Data Definition Language (DDL) and Data Manipulation Language (DML) in a clear and simple way.

Data Definition Language (DDL)

DDL is used to define and manage the structure of database objects like tables, schemas, indexes, etc.

Common DDL Commands:

CommandDescriptionExample
CREATECreates a new database object (table, view, etc.)CREATE TABLE Students (ID INT, Name VARCHAR(50));
ALTERModifies an existing database objectALTER TABLE Students ADD Age INT;
DROPDeletes a database objectDROP TABLE Students;
TRUNCATEDeletes all data in a table (but keeps the structure)TRUNCATE TABLE Students;

Note: DDL changes are auto-committed. That means changes are permanent once executed.

Data Manipulation Language (DML)

DML is used to manipulate data stored in database tables — i.e., to add, modify, retrieve, or delete the actual records.

Common DML Commands:

CommandDescriptionExample
SELECTRetrieves data from one or more tablesSELECT * FROM Students;
INSERTAdds new data into a tableINSERT INTO Students (ID, Name) VALUES (1, ‘Alice’);
UPDATEModifies existing dataUPDATE Students SET Name = ‘Bob’ WHERE ID = 1;
DELETERemoves data from a tableDELETE FROM Students WHERE ID = 1;

Note: DML changes can be rolled back if not committed, which is useful for error recovery.

Quick Summary:
FeatureDDLDML
PurposeDefines structureManages data
Auto CommitYesNo (can be rolled back)
ExamplesCREATE, ALTER, DROPSELECT, INSERT, UPDATE, DELETE
Want a small SQL exercise using both DDL and DML to try out?
  • Data type (char(n), varchar(n), int, float, date), constraints (not null, unique, primary key), create database, use database, show databases, drop database, show tables, create table, describe table, alter table Awesome! You’re diving into thefundamentals of SQL, so here’s a quick and beginner-friendly
    breakdown of data types, constraints, and some
    important SQL commands related to databases and tables.

SQL

1. SQL Data Types
Data TypeDescriptionExample
CHAR(n)Fixed-length string, always n charactersCHAR(5) stores “Bob “
VARCHAR(n)Variable-length string, up to n charactersVARCHAR(20) stores “Alice”
INTInteger (whole number)INT → 10, -100
FLOATDecimal (floating point number)FLOAT → 3.14, 99.99
DATEStores a date in YYYY-MM-DD formatDATE → ‘2025-04-21’
2. SQL Constraints

Constraints are rules applied to table columns to ensure data integrity.

ConstraintDescriptionExample Use
NOT NULLValue must not be NULLName VARCHAR(50) NOT NULL
UNIQUEAll values in the column must be differentEmail VARCHAR(100) UNIQUE
PRIMARY KEYUniquely identifies each row (NOT NULL + UNIQUE)ID INT PRIMARY KEY

SQL

3. SQL Database & Table Commands

Database Commands

sqlCopyEditCREATE DATABASE School;        — Create a new databaseUSE School;                    — Select the database to useSHOW DATABASES;                — List all databasesDROP DATABASE School;          — Delete the database

Table Commands

sqlCopyEdit– Create a tableCREATETABLE Students (    ID INTPRIMARY KEY,    Name VARCHAR(50)
NOTNULL,    Email VARCHAR(100)
UNIQUE,    Age INT,EnrollmentDateDATE); — Show all tables in current databaseSHOW TABLES; — Describe table structureDESCRIBE Students; — Add a new column to a tableALTERTABLE Students ADD GPA FLOAT; — Modify column data typeALTERTABLE Students MODIFY Name CHAR(50); — Drop (delete) a columnALTERTABLE Students DROPCOLUMN GPA;

SQL

Create Database

sqlCopyEditCREATE DATABASE school;

 Use Database

sqlCopyEditUSE school;

Show Databases

sqlCopyEditSHOW DATABASES;

Drop Database

sqlCopyEditDROP DATABASE school;

Show Tables

sqlCopyEditSHOW TABLES;

Create Table

sqlCopyEdit

SQL

CREATETABLE students ( student_idINT,nameVARCHAR(50),ageINT,gradeVARCHAR(10));

sqlCopyEditDESCRIBE students;

 Alter Table Add a column:

sqlCopyEditALTERTABLE students ADD email VARCHAR(100);

Remove a column:

sqlCopyEditALTERTABLE students DROPCOLUMN email;

Add a primary key:

sqlCopyEditALTERTABLE students ADDPRIMARY KEY (student_id);

Drop the primary key:

sqlCopyEditALTERTABLE students DROPPRIMARY KEY;

SQL

 

Scroll to Top