0% found this document useful (0 votes)
12 views16 pages

Database Notes

The document provides an overview of databases, explaining key concepts such as data, databases, DBMS, and RDBMS, along with their advantages over traditional file storage methods like Excel. It details the structure of databases, including tables, rows, columns, primary keys, and foreign keys, while also introducing SQL and its command types for managing data. Additionally, it discusses relationships between tables, including one-to-one, one-to-many, and many-to-many relationships, with examples and SQL commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Database Notes

The document provides an overview of databases, explaining key concepts such as data, databases, DBMS, and RDBMS, along with their advantages over traditional file storage methods like Excel. It details the structure of databases, including tables, rows, columns, primary keys, and foreign keys, while also introducing SQL and its command types for managing data. Additionally, it discusses relationships between tables, including one-to-one, one-to-many, and many-to-many relationships, with examples and SQL commands.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

DATA BASE

INTRODUCTION

*Data,is raw facts or unprocessed information

*Database,is a place where data is stored permanently in an organized way


or is a collection of related data stored electronically

Example:
Instead of writing data on paper or Excel, we store it in a database.
DATABASE: School

Table: Students
-----------------------
ID | Name | Age | Class
1 | John | 20 | IT
2 | Mary | 19 | CS

Why we use database?

We use databases because they are better than files and Excel.

✅ Advantages of databases:
11️⃣Store millions of records
2️⃣Data is safe and secure
3️⃣Fast searching
4️⃣No data duplication
5️⃣Multiple users can access data
6️⃣Backup and recovery
7️⃣Works with programs (Java, PHP, Python)

DIFFERENCE BETWEEN DATABASE AND EXCEL

Feature Database Excel


Data size Very large Limited
Speed Very fast Slow
Security High Low
Multi-user Yes No
Relationships Yes No
Used in apps Yes No
Automatic backup Yes No
DBMS

Stand for:database management system


Is a software used to create,manage,and control databases

Examples of DBMS:
• MySQL
• Oracle
• PostgreSQL
• MongoDB
• SQLite
DBMS helps to:

Create database
• Create tables
• Insert data
• Update data
• Delete data
• Control security

📌 Without DBMS, databases cannot work.

RDBMS

stand for: relational database management system.


Stores data in table and links using relationships.

Key features:
Data stored in tables
• Uses rows and columns
• Uses Primary Key
• Uses Foreign Key
• Supports SQL language

Examples of RDBMS:
• MySQL
• Oracle
• PostgreSQL
• SQL Server

Example of RDBMS relationship:


Students table
id (Primary Key)
name

Marks table
mark_id
student_id (Foreign Key)
score

This creates a relationship between tables.

SUMMARY
• Data → raw facts
• Database → stores data
• DBMS → software to manage database
• RDBMS → database with tables + relationships

Database Basic Terms

We’ll use one table example to understand everything.

EXAMPLE:
STUDENT
------------------------------------------------
id | name | age | class
------------------------------------------------
1 | John | 20 | IT
2 | Mary | 19 | CS
3 | Paul | 22 | SE
[Link]

table is where data is stored in database

a database contains more tables

[Link]

column store one type of data

example:

Column Meaning
id Student number
name Student name
age Student age
class Student department

[Link]

row is one complete person or object.

Example
1 | John | 20 | IT
That entire line = one student record.

[Link] KEY(PK)

A primary key is a column that uniquely identifies each row.

Rules of Primary Key:


• Must be unique
• Cannot be NULL
• Cannot repeat

Example:
id

Why?
Because:
1 → John
2 → Mary
3 → Paul No two students have the same ID.
📌 Each table must have only one primary key.

5 Foreign Key (FK)


5️⃣

A foreign key connects one table to another table

Example:
STUDENT table
id (Primary Key)

MARKS table
mark_id
student_id ← Foreign Key
score

student_id references student(id)

📌 Foreign key creates a relationship.

6️⃣Schema

A schema is the structure or design of the database.

It includes:
• Tables
• Columns
• Data types
• Relationships

Example:
school database schema

Meaning:
How tables are organized inside the database.

7️⃣Index

index makes data searching quickly.

Example:
Without index:
• Database checks row by row 😴
With index:
• Database finds data very fast ⚡
Example:
CREATE INDEX idx_name ON student(name);

📌 Index = fast search

VERY SIMPLE COMPARISON


Database term Real-life example
Database School building
Table Classroom
Row Student
Column Student detail
Primary Key Student ID card
Foreign Key Class ID
Schema Building plan
Index Book index page

Learn SQL (Structured Query Language)

sql is a language used to talk to the database

Just like Java talks to the computer,


SQL talks to the database

What is SQL?

SQL = Structured Query Language

SQL is used to:


• Create databases
• Create tables
• Insert data
• Read data
• Update data
• Delete data
📌 SQL works with:
• MySQL
• Oracle
• PostgreSQL
• SQL Server

SQL COMMAND TYPES

SQL commands are divided into 4 main groups:

Type Meaning
DDL Create structure
DML Work with data
DQL Read data
DCL Security

1 DDL – Data Definition Language


1️⃣
Used to create database and tables.

Commands:
• CREATE
• ALTER
• DROP
• TRUNCATE

Example: Create database


CREATE DATABASE school;

Use database:
USE school;

Create table
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
age INT,
class VARCHAR(20)
);

Table created successfully.

2️⃣DML – Data Manipulation Language


Used to change data.

Commands:
• INSERT
• UPDATE
• DELETE

Insert data
INSERT INTO students(name, age, class)
VALUES ('John', 20, 'IT');

Insert more:
INSERT INTO students(name, age, class)
VALUES ('Mary', 19, 'CS');

3️⃣DQL – Data Query Language


Used to read data.

Command:
• SELECT

View all data


SELECT * FROM students;

Output:
id | name | age | class
1 | John | 20 | IT
2 | Mary | 19 | CS

Select specific columns


SELECT name, age FROM students;
4️⃣DCL – Data Control Language
Used for permissions.
Examples:
• GRANT
• REVOKE
(You will learn this later.)

MOST IMPORTANT SQL COMMANDS


You must practice these daily:
CREATE DATABASE
CREATE TABLE
INSERT
SELECT
UPDATE
DELETE

🧪 PRACTICE EXAMPLES

🔹 Update data
UPDATE students
SET age = 21
WHERE id = 1;

🔹 Delete data
DELETE FROM students
WHERE id = 2;

⚠️VERY IMPORTANT RULE


❌ Never write:
DELETE FROM students;
PRACTICE.

1. Database Basics
• Create a new database: CREATE DATABASE school_db;

• Select/Open a database to use: USE school_db;

• List all databases on the server: SHOW DATABASES;

2. Table Structure
• Create a table: (You must define columns and data types)
SQL
CREATE TABLE students (
id INT,
name VARCHAR(50),
score INT
);

• List all tables in the current database: SHOW TABLES;

• View columns and data types (Structure): DESCRIBE students;

3. Managing Data (DML)


• Insert Data: (Note: Use commas to separate columns, not periods)
SQL
INSERT INTO students (name, score)
VALUES ('Mary Jane', 60),
('Alice', 65);

• View Data: SELECT * FROM students; (The * means "all columns")

• Update Data: (Always use a WHERE clause to avoid updating every row!)

SQL
UPDATE students
SET score = 75
WHERE name = 'Mary Jane';

[Link] UPDATE command


To change existing data in a table, we use:
UPDATE table_name
SET column1 = new_value,
column2 = new_value
WHERE condition;
🔹 Example table
Assume your table is called students
+----+---------+-----+
| id | name | age |
+----+---------+-----+
| 1 | Donat | 20 |
| 2 | Alice | 18 |
| 3 | Peter | 22 |
+----+---------+-----+

🔹 Update one record


Change Donat’s age to 21
UPDATE students
SET age = 21
WHERE id = 1;

✅ Only the row with id = 1 will be updated.

🔹 Update multiple columns


UPDATE students
SET name = 'Donat Hosz',
age = 22
WHERE id = 1;

🔹 Update using a condition


Increase age for students older than 20:
UPDATE students
SET age = age + 1
WHERE age > 20;

VERY IMPORTANT WARNING


If you run:
UPDATE students
SET age = 30;
[Link] in Database
To remove data from a table, we use:
DELETE FROM table_name
WHERE condition;

🔹 Example table: students


id name age
1 Donat 21
2 Alice 18
3 Peter 22

✅ Delete one record


Delete student with id = 2
DELETE FROM students
WHERE id = 2;

✅ Delete using name


DELETE FROM students
WHERE name = 'Donat';

⚠️VERY IMPORTANT
If you write:
DELETE FROM students;

🚫 ALL records will be deleted!


Always use WHERE.

Learn Relationships Between Tables


A relationship shows how tables are connected in a database.

We connect tables using a FOREIGN KEY.

🔑 Reminder
• Primary Key (PK) → unique ID in a table
• Foreign Key (FK) → column that refers to another table’s primary key

Types of Relationships

11️⃣One-to-One (1 : 1)
📌 Meaning
One record in table A is linked to only one record in table B.

📘 Example
One person has one national ID.
Person ────► NationalID

Example tables
person

person_id name
1 Donat
2 Alice
national_id

id_id number person_id


1 11990033 1
2 22003344 2
👉 person_id is a foreign key.

SQL
CREATE TABLE person (
person_id INT PRIMARY KEY,
name VARCHAR(50)
);

CREATE TABLE national_id (


id_id INT PRIMARY KEY,
number VARCHAR(20),
person_id INT UNIQUE,
FOREIGN KEY (person_id) REFERENCES person(person_id)
);

🔹 UNIQUE makes it one-to-one.

2️⃣One-to-Many (1 : M)
📌 Meaning
One record in table A can have many records in table B.

📘 Example
One department has many students.
Department ───► Students

Tables
department

dept_id dept_name
1 IT
2 Business
student

student_id name dept_id


1 Donat 1
2 Alice 1
3 Peter 2
👉 Many students → one department.

SQL
CREATE TABLE department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);

CREATE TABLE student (


student_id INT PRIMARY KEY,
name VARCHAR(50),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);

3️⃣Many-to-Many (M : M)
📌 Meaning
Many records in table A relate to many records in table B.

📘 Example
Students take many courses,
and courses have many students.
Students ⇄ Courses

❗ Important
Many-to-many cannot be created directly.
We must use a junction (bridge) table.

Tables
students

student_id name
1 Donat
2 Alice
courses

course_id course_name
1 Java
2 Database
student_course

student_id course_id
1 1
1 2
2 1
SQL
CREATE TABLE students (
student_id INT PRIMARY KEY,
name VARCHAR(50)
);

CREATE TABLE courses (


course_id INT PRIMARY KEY,
course_name VARCHAR(50)
);

CREATE TABLE student_course (


student_id INT,
course_id INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

🧠 Summary Table
Relationship Description Example
One-to-One One ↔ One Person–ID
One-to-Many One → Many Department–Students
Many-to-Many Many ↔ Many Students–Courses

You might also like