0% found this document useful (0 votes)
13 views21 pages

Databases

Uploaded by

Nehal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views21 pages

Databases

Uploaded by

Nehal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

DATABASES

WHAT IS A DATABASE?

• A database is a structured collection of data that can be easily


accessed, managed, and updated.
• Databases organize large amounts of data, making it easier to retrieve
specific information.
• Banking: Customer information, transaction history, and account
details are stored and managed in databases.
• Healthcare: Patient records, treatment history, and lab results are
organized in databases.
• Education: Student records, grades, and schedules are maintained in
school databases.
A SIMPLE DATABASE
A SINGLE-TABLE DATABASE

• A single-table database is a database structure with only one table where


all data is stored in rows and columns.
• Ideal for small datasets with a single set of related information.
• Key Components of a Single-Table Database:
• Fields (Columns): Define the type of data each column will store (e.g., Name,
Age, Address).
• Records (Rows): Each row is a unique entry or instance of data (e.g., an
individual’s details).
• Imagine a simple Rules
• Validation: table applied
for "Students" with
to fields fields like
to ensure StudentID,
data accuracyName, Age, and Class.
and consistency
• Each row represents one student’s information.
DATA TYPES – HOW TO SELECT THEM ?

• A data type defines the kind of data a field can hold.


• Ensures that each field in a database table stores data in a consistent
and meaningful way.
• Helps in optimizing storage space and improving query efficiency.
BASIC DATA TYPES IN DATABASES

• TEXT / ALPHANUMERIC
• Stores letters, numbers, and symbols.
• Examples: Names, addresses, or descriptions.

• CHARACTER
• Stores a single letter or symbol.
• Example: Gender field with 'M' or 'F’.

• BOOLEAN
• Stores true/false or yes/no values.
• Example: IsEnrolled field where True = Enrolled, False = Not Enrolled.
BASIC DATA TYPES IN DATABASES

• INTEGER
• Stores whole numbers.
• Examples: Age, quantity, or number of items.

• REAL
• Stores decimal numbers for precise values.
• Examples: Price, height, or weight.

• DATE / TIME
• Stores dates and/or times.
• Examples: Date of birth, appointment time, or order date.
• Standard Date Format- YYYY-MM-DD or MM-DD-YYYY
• Time format – HH:MI:SS
EXAMPLE OF A DATA TYPE SELECTION

Date of IsGraduate
• For a Student Database: Name Age
Enrollment d
• Name: Text Alice 2022-08-
21 False
Johnson 15
• Age: Integer
2021-06-
• Date of Enrollment: Date Bob Smith 23 True
01
• IsGraduated: Boolean 2023-01-
Charlie Lee 22 False
10
Diana 2020-09-
25 True
Brown 20
Emma 2022-05-
24 False
Davis 05
ACTIVITY 1: DETERMINE THE RIGHT DATA
TYPE
CONCEPT OF A PRIMARY KEY

• A primary key is a unique identifier for each record (row) in a database table.
• Ensures that every entry in the table is distinct and can be referenced
individually.
• Primary keys prevent duplicate records and ensure data integrity.
• Characteristics of a Primary Key:
• Unique: No two records can have the same primary key value.
• Non-Null: Every record must have a primary key value (it cannot be empty).

• Examples of Primary Keys:


• Student Database: StudentID (e.g., 001, 002, 003).
• Employee Database: EmployeeID (e.g., E123, E124).
HOW TO CHOOSE A PRIMARY KEY ?

• Often a unique ID number or code that is automatically generated.


• Avoid using fields that may change, like names or contact information,
as primary keys.
ACTIVITY 2 : TO DETERMINE PRIMARY KEYS

EnrollmentID StudentID CourseID EnrollmentDate

1001 1 101 2022-08-20


1002 2 102 2021-07-01
1003 3 101 2023-02-15

Q. Whats the primary key here ?


1. EnrollmentID
2. StudentID
3. CourseID
4. EnrollmentID and StudentID
5. EnrollmentID and CourseID
6. EnrollmentID and StudentID and
CourseID
FOREIGN KEY

• A foreign key is a column or columns in a table that links to a primary


key or unique key in another table.
WHAT IS SQL ?

• Structured Query Language (SQL): A language used to


communicate with databases.
• It is particularly useful in handling structured data, i.e., data
incorporating relations among entities and variables.
• SQL allows us to retrieve, insert, update, and delete data from a
database.
• MySQL is an open-source relational database management system
(RDBMS) that stores data in tables.
COMMON SQL COMMANDS:

• SELECT: Used to retrieve data from one or more columns in a table.


• FROM: Specifies the table from which to retrieve data.
• WHERE: Filters records based on a specified condition.
• ORDER BY: Sorts the results in ascending or descending order.
• SUM: Calculates the total of values in a numeric column.
• COUNT: Counts the number of rows that match a specified condition.
EXAMPLE TABLE

•Overview of the "Students" Table:


•A table to store essential student information in a structured way.
•Key fields include unique ID, name, age, course, and enrollment details.
•Table Definition:
•StudentID: Unique identifier for each student (Primary Key).
•Name: Stores the student’s name (VARCHAR, up to 50 characters).
•Age: Student’s age (INT).
•Gender: Represents gender as a single character, e.g., 'M' or 'F'.
•Course: Name of the course the student is enrolled in (VARCHAR, up to
100 characters).
•Grade: Stores grade as a two-character string (e.g., 'A+', 'B-').
•Credits: Number of credits completed by the student (INT).
•EnrollmentDate: Date when the student enrolled (DATE).
HOW TO DEFINE A TABLE ?

• CREATE TABLE Students (


• StudentID INT PRIMARY KEY,
• Name VARCHAR(50) NOT NULL,
• Age INT,
• Gender CHAR(1),
• Course VARCHAR(100),
• Grade CHAR(2),
• Credits INT,
• EnrollmentDate DATE

• );
EXAMPLE OF A SQL QUERY

• Query :Select all records from the table.


• =>SELECT * FROM Students;
• Query :Selecting specific columns.
• =>SELECT Name, Age, Course FROM Students;
• Query :Filtering results using WHERE
• => SELECT Name, Age FROM Students WHERE Age > 18;
• Query :Using ORDER BY to sort the results
• =>SELECT * FROM Students ORDER BY Age DESC;
• Query :Counting records
• => SELECT COUNT(*) FROM Students;
ACTIVITY 3 – LEARNING HOW TO WRITE SQL
QUERIES
• 1. Retrieve the names and grades of all students from the "Students" table.
• 2. Retrieve all students who are older than 18 and enrolled in "Computer Science.“
• 3. Retrieve all students, sorted by their grade in descending order.
• 4. Find the total number of credits completed by all students.
• 5. Count the number of students who are enrolled in "Mathematics.“
• 6. Calculate the average age of students in the "Students" table.
• 7. Retrieve all unique course names from the "Students" table.
• 8. Retrieve all students whose names start with "A.“
• 9. Retrieve all students who enrolled between January 1, 2023, and June 30, 2023.
• 10. Find the number of students in each course.
SOLUTIONS!

• 1. SELECT Name, Grade FROM Students;


• 2. SELECT Name, Age, Course FROM Students WHERE Age > 18 AND Course = 'Computer
Science’;
• 3. SELECT Name, Grade FROM Students ORDER BY Grade DESC;
• 4. SELECT SUM(Credits) FROM Students;
• 5. SELECT COUNT(*) FROM Students WHERE Course = 'Mathematics’;
• 6. SELECT AVG(Age) FROM Students;
• 7. SELECT DISTINCT Course FROM Students;
• 8. SELECT Name FROM Students WHERE Name LIKE 'A%’;
• 9. SELECT Name, EnrollmentDate FROM Students WHERE EnrollmentDate BETWEEN '2023-01-
01' AND '2023-06-30’;
• 10. SELECT Course, COUNT(*) FROM Students GROUP BY Course;
THANK YOU

You might also like