Database Concepts
Database Concepts
Relational Data Model: The relational data model organizes data into tables (relations).
Each table consists of rows (tuples) and columns (attributes).
Candidate Key: A set of one or more attributes that uniquely identify a tuple in a
relation.
Primary Key: A candidate key selected to uniquely identify tuples in a table. It
cannot contain null values.
Alternate Key: Candidate keys that are not selected as the primary key.
Foreign Key: An attribute or set of attributes in one table that refers to the primary
key of another table.
Data Definition Language (DDL): DDL is used to define or modify the structure of a
database. Common DDL commands include:
sql
Copy code
CREATE DATABASE SchoolDB;
sql
Copy code
USE SchoolDB;
sql
Copy code
SHOW DATABASES;
sql
Copy code
DROP DATABASE SchoolDB;
sql
Copy code
SHOW TABLES;
sql
Copy code
DESCRIBE Students;
sql
Copy code
-- Add a new column
ALTER TABLE Students ADD Gender CHAR(1);
-- Remove a column
ALTER TABLE Students DROP COLUMN Gender;
sql
Copy code
DROP TABLE Students;
Data Manipulation Language (DML): DML is used to manipulate data stored in the
database. Common DML commands include:
sql
Copy code
INSERT INTO Students (StudentID, Name, Age, Course) VALUES (1,
'Alice', 20, 'CS101');
sql
Copy code
DELETE FROM Students WHERE StudentID = 1;
sql
Copy code
SELECT * FROM Students;
UPDATE: Modifies existing data in a table.
sql
Copy code
UPDATE Students SET Age = 21 WHERE StudentID = 1;
SQL Operators:
Mathematical Operators: +, -, *, /
Relational Operators: =, >, <, >=, <=, <>
Logical Operators: AND, OR, NOT
sql
Copy code
SELECT Name AS StudentName FROM Students;
sql
Copy code
SELECT DISTINCT Course FROM Students;
sql
Copy code
SELECT * FROM Students WHERE Age > 20;
sql
Copy code
SELECT * FROM Students WHERE Course IN ('CS101', 'CS102');
sql
Copy code
SELECT * FROM Students WHERE Age BETWEEN 20 AND 22;
sql
Copy code
SELECT * FROM Students ORDER BY Name ASC;
sql
Copy code
SELECT * FROM Students WHERE Age IS NULL;
sql
Copy code
SELECT * FROM Students WHERE Name LIKE 'A%';
Aggregate Functions:
sql
Copy code
SELECT MAX(Age) FROM Students;
sql
Copy code
SELECT MIN(Age) FROM Students;
sql
Copy code
SELECT AVG(Age) FROM Students;
sql
Copy code
SELECT SUM(Age) FROM Students;
sql
Copy code
SELECT COUNT(*) FROM Students;
GROUP BY: Groups rows that have the same values in specified columns.
sql
Copy code
SELECT Course, COUNT(*) FROM Students GROUP BY Course;
sql
Copy code
SELECT Course, COUNT(*) FROM Students GROUP BY Course HAVING COUNT(*)
> 1;
Joins in SQL:
Cartesian Product: Combines each row from one table with each row from another
table.
sql
Copy code
SELECT * FROM Students, Courses;
Equi-Join: Joins tables based on a condition that matches rows with equal values.
sql
Copy code
SELECT Students.Name, Courses.CourseName FROM Students
JOIN Courses ON Students.Course = Courses.CourseID;
Natural Join: Joins tables automatically based on columns with the same name.
sql
Copy code
SELECT * FROM Students NATURAL JOIN Enrollments;
1. Create a database named SchoolDB and within it create a table Students with
columns StudentID, Name, Age, and Course. Insert three records into this table.
2. Update the Age of the student with StudentID 2 to 22.
3. Delete the record of the student whose StudentID is 3.
4. Retrieve the names of students enrolled in CS101.
5. List all distinct courses from the Students table.
6. Group the students by Course and count the number of students in each course.
7. Perform a natural join between the Students table and an Enrollments table
based on a common StudentID field and retrieve all fields.