Database Management System (DBMS) – Beginner to Intermediate
Guide
1. Basic Definitions
- Database: An organized collection of data stored electronically.
- DBMS: A software system that enables users to define, create, maintain, and control
access to databases.
- Table: A set of data elements organized using a model of vertical columns and
horizontal rows.
- Primary Key: A unique identifier for a record in a table.
- Foreign Key: A field that links one table to another.
- Query: A request for data or information from a database.
- Schema: The structure that defines the organization of data in a database.
2. Basic SQL Syntax (From CREATE to INNER JOIN)
- CREATE TABLE table_name (column1 datatype, column2 datatype, ...);
- INSERT INTO table_name (column1, column2) VALUES (value1, value2);
- SELECT * FROM table_name;
- SELECT column1, column2 FROM table_name;
- UPDATE table_name SET column1 = value1 WHERE condition;
- DELETE FROM table_name WHERE condition;
- ALTER TABLE table_name ADD column_name datatype;
- DROP TABLE table_name;
- SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
- SELECT * FROM table_name ORDER BY column_name ASC/DESC;
- SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;
- SELECT column_name FROM table_name WHERE condition LIMIT number;
- UPDATE table_name SET column_name = value WHERE condition;
- INNER JOIN is used to return rows that have matching values in both tables.
- ON keyword is used in JOINs to specify the matching column.
3. Example SQL Queries
Create a table
CREATE TABLE Students (ID INT PRIMARY KEY, Name VARCHAR(50), Age
INT);
Insert a record
INSERT INTO Students (ID, Name, Age) VALUES (1, 'Ali', 20);
Fetch all records
SELECT * FROM Students;
Update record
UPDATE Students SET Age = 21 WHERE ID = 1;
Delete a record
DELETE FROM Students WHERE ID = 1;
Add a new column
ALTER TABLE Students ADD Email VARCHAR(100);
Inner join two tables
SELECT S.Name, E.Course FROM Students S INNER JOIN Enrollments E ON S.ID =
E.StudentID;