Database Notes
Database Notes
Definition: Data refers to raw facts, figures, and symbols that are collected for reference,
analysis, or processing. It can be anything from numbers, text, images, or sounds. For example, a
list of names, transaction amounts, or sensor readings.
Types:
o Structured Data: Organized in a fixed format, often in tables (e.g., SQL databases).
o Unstructured Data: Not organized in a predefined manner (e.g., text files, videos).
o Semi-structured Data: Contains organizational properties but does not conform to a strict
structure (e.g., XML, JSON).
2. Database
Definition: A database is an organized collection of related data stored in a structured format,
typically managed by a Database Management System (DBMS). The purpose of a database is to
store, retrieve, and manage data efficiently.
Examples:
o Relational Databases: Organize data into tables with rows and columns (e.g., MySQL,
PostgreSQL).
o NoSQL Databases: Store data in formats like key-value pairs, documents, or graphs (e.g.,
MongoDB, Cassandra).
3. Database System
Definition: A database system refers to the combination of a database and the software (DBMS)
used to manage it. It includes all the necessary tools and infrastructure to store, process, and
retrieve data, ensuring data integrity, security, and accessibility.
Components:
o Database: The stored data.
o DBMS: The software that interacts with users, applications, and the database itself to
capture and analyze data.
o Hardware: Physical devices on which the database and DBMS run (e.g., servers, storage
systems).
o Users: Individuals or applications that interact with the database system.
4. Database Management System (DBMS)
Definition: A Database Management System (DBMS) is software that provides an interface for
users to interact with databases. It allows for the creation, retrieval, updating, and management
of data in a database.
Functions:
o Data Definition: Defining the structure of the data (schemas).
o Data Manipulation: Inserting, updating, and deleting data.
o Data Retrieval: Querying the database to get specific data.
o Data Control: Managing user access and ensuring data security and integrity.
Examples:
o Relational DBMS (RDBMS): MySQL, Oracle, SQL Server.
o NoSQL DBMS: MongoDB, CouchDB.
Key Concepts in Databases: Field, Record, Objects, Keys
1. Field
Definition: A field is a single piece of data or attribute in a database. It represents a column in a
table where each entry corresponds to a specific type of data, like a name, age, or product ID.
Example: In a table of employees, "Employee Name" could be a field that stores the names of
all employees.
2. Record
Definition: A record is a complete set of related fields, representing a single item or entity
within a database table. It corresponds to a row in the table.
Example: In an "Employees" table, a record might include fields like Employee ID, Name,
Department, and Salary, all related to a single employee.
3. Objects
Definition: In database terminology, an object often refers to any item within the database that
can be defined and manipulated, such as tables, views, indexes, and stored procedures. In
Object-Oriented Databases, an object is an instance of a class, storing both data and behavior
(methods).
Example: In a relational database, a "Customer" table is an object. In an Object-Oriented
Database, a "Customer" object might include attributes (e.g., Name, Address) and methods (e.g.,
getFullName()).
4. Keys
Definition: A key is an attribute (field) or a set of attributes used to identify a record uniquely in
a table and to establish relationships between tables.
4.1. Primary Key
Definition: A primary key is a unique identifier for a record in a table. It ensures that each
record is unique and can be retrieved or referenced without ambiguity.
Example: In a "Students" table, the "Student ID" field might be the primary key because it
uniquely identifies each student.
Student ID
Student Name Class Age
(Primary Key)
S001 Ram Sharma 12 17
S002 Sita Thapa 12 18
S003 Hari Bhandari 11 16
S004 Gita Karki 12 17
S005 Nabin Chaudhary 11 16
4.2. Alternate Key
Definition: An alternate key is any candidate key that is not chosen as the primary key. It is still
unique and can serve as a unique identifier for records.
Example: In a "Students" table, if "Email" is unique for each student but not chosen as the
primary key, it would be an alternate key.
Student ID
Student Name Email (Alternate Key) Class Age
(Primary Key)
In this table:
Therefore, both Student_ID and Email are Candidate Keys because both can uniquely identify a
student record.
4.4. Foreign Key
Definition: A foreign key is a field (or combination of fields) in one table that refers to the
primary key in another table, creating a relationship between the two tables.
Here, Student_ID is the Primary Key because it uniquely identifies each student.
Here, Student_ID in the Result table is a Foreign Key because it refers to the Student_ID in the
Student table.
Relationship Diagram
Student Table Result Table
----------------- -----------------
Student_ID (PK) -------------------------------> Student_ID (FK)
Student_Name GPA
Class Result_ID
5) This dialog box below shows that you should avoid installing XAMPP to
C:\Program Files. Click OK.
Open Command prompt. type the command .
SQL commands are divided into five types based on their functions.
DQL Data Query Language Retrieves data from the database. SELECT
Example:
step1. CREATE DATABASE Employee;
step 2. use Employee;
[Link] TABLE Employees ( EmployeeID INT PRIMARY KEY, Name VARCHAR(100),
Department VARCHAR(50));
step 4: show tables;
step 5: describe Employee;
ALTER: Modifies the structure of an existing database object, such as adding or dropping a column in
a table.
Example:
ALTER TABLE Employees ADD Salary DECIMAL(10, 2);
Key Commands:
o SELECT: Retrieves data from one or more tables based on specified criteria.
Example:
SELECT Name, Department FROM Employees WHERE Salary > 50000;
Step-by-Step Process
Start your computer in DOS mode or open a command prompt window if you're using a modern
version of Windows.
If the database server (MySQL, for example) is not running, you'll need to start it. You can
usually do this by navigating to the directory where the MySQL server is installed and running a
command like
mysqld
This starts the MySQL server and allows you to connect to it.
Once the server is running, you can connect to it using the MySQL client:
mysql -u username –p
Replace username with your MySQL username. The -p flag prompts you to enter your password.
4. Create a Database
After connecting to the MySQL client, you can create a new database with the following
command:
To use the database you've created or another existing database, use the USE command:
USE database_name;
6. Create Tables
Define the structure of your tables using the CREATE TABLE command:
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, Name VARCHAR(100),
Department VARCHAR(50),
Salary DECIMAL(10, 2));
INSERT INTO Employees (EmployeeID, Name, Department, Salary)VALUES (1, 'John Doe',
'HR', 55000);
8. Query Data
9. Update Data
EXIT;
If necessary, stop the database server by closing the command prompt window or by running a
specific shutdown command, depending on your DBMS:
Students Table
Student_ID Student_Name Class Section
S001 Aayush 11 A
S002 Sita 11 B
S003 Rohan 12 A
Subjects Table
Subject_ID Subject_Name
C101 Computer Science
M101 Mathematics
E101 English
Student_Subject Table:
Student_ID Subject_ID
S001 C101
S001 M101
S002 E101
S003 C101
Hierarchical database model is one of the oldest type database models., In this model data is
represented in the form of records. Each record has multiple fields/attributes. All records are arranged
in database as tree like structure. The relationship between the records is called parent/ child
relationship in which any child record relates to only a single parent type record. In the figure below,
there are parent records at the top level with high privileges and child records at bottom Level.
SCHOOL
(Gyan Jyoti
Model School)
Class 11
Class 11 Science
Management
Another Example:
SCHOOL
(Gyan Jyoti
Model School)
Computer Management
Department Department
The network database model replaced hierarchical database model due to some limitations on the
model. Suppose if an employee relates to two departments, then the hierarchical database model cannot
able to arrange records in proper place. So network database model was emerged to arranged non-
hierarchical database. The structure of database is more like graph rather than tree structure. A network
database model consists of collection of records which are inter-related to each other with the help of
relationship. Each record has multiple fields and each field has only one data value. In this model, each
record in the database can have multiple parents that is the relationships among data elements can have
many to many relationships.
In relational database model, the data is organized into tables which contain multiple rows and
columns. These tables are called relations. A row in a table represents a relationship among a set of
values. Since a table is a collection of such relationships, it is generally referred to the mathematical
term relation, from which the relational database model derives its name. It is also known as RDBMS.
We can notice from below table (Student and Marks), here The StudentID in the Students table is the
Primary Key, while the StudentID in the Marks table is the Foreign Key. This creates a relationship
between the two tables.
Table :Marks
Table :Student MarkID StudentID Subject Marks
StudentID (PK) Name (FK)
1 101 Computer 85
101 Hari
2 102 Computer 92
102 Sita
3 103 Computer 78
103 Ram
Advantages of Relational Database Model
The breaking of complex database table into simple database table becomes possible.
Database processing is faster than other model.
There is very less redundancy (reputation of data).
The integrity rules can easily be implemented.
Definition: A table is in 1NF if it only contains atomic (indivisible) values, and each entry in a
column contains a single value. Essentially, there should be no repeating groups or arrays in any
row.
Rules:
o Each column must contain only atomic values.
o Each column must contain values of a single type.
o Each column must have a unique name.
o The order in which data is stored does not matter.
Problem:
The Subjects column contains multiple values in a single cell, so the table is not in 1NF.
Insert Data
INSERT INTO Student VALUES(101, 'Ram', 'Computer, English'),(102, 'Sita', 'Mathematics, Science');
Problem: The Subjects column contains multiple values in a single cell, which violates First Normal
Form (1NF).
Insert Data
INSERT INTO Student_1NF VALUES (101, 'Ram', 'Computer'),(101, 'Ram', 'English'),(102, 'Sita', 'Mathematics'),
(102, 'Sita', 'Science');
Explanation:
Characteristics of 1NF
Advantages
Definition: A table is in 2NF if it is in 1NF and all non-primary key attributes are fully
functionally dependent on the primary key. This means that there should be no partial
dependency; every non-key attribute must depend on the entire primary key, not just part of it.
Rules:
o The table must be in 1NF.
o There should be no partial dependency; all non-key attributes must be fully dependent on
the primary key.
Primary Key:
Problem:
Subject Table
CREATE TABLE Subject (SubjectID VARCHAR(10) PRIMARY KEY, SubjectName VARCHAR(50));
INSERT INTO Subject VALUES ('C101','Computer'), ('M101','Mathematics');
Marks Table
CREATE TABLE Marks ( StudentID INT, SubjectID VARCHAR(10), Marks INT, PRIMARY KEY
(StudentID, SubjectID), FOREIGN KEY (StudentID) REFERENCES Student(StudentID), FOREIGN
KEY (SubjectID) REFERENCES Subject(SubjectID));
INSERT INTO Marks VALUES(101,'C101',85),(101,'M101',78),(102,'C101',90);
Student_Marks Table
Characteristics of 2NF
Advantages
Definition: A table is in 3NF if it is in 2NF and all the attributes are functionally dependent only
on the primary key. There should be no transitive dependency, meaning no non-key attribute
should depend on another non-key attribute.
Rules:
o The table must be in 2NF.
o There should be no transitive dependency.
Insert Data
INSERT INTO Student(StudentID, StudentName, DepartmentID, DepartmentName)
VALUES(101, 'Ram', 'D01', 'Computer Science'),(102, 'Sita', 'D02', 'Management'),(103, 'Hari', 'D01', 'Computer
Science');
Student Table
Problem
StudentID → DepartmentID
DepartmentID → DepartmentName
Insert Data
INSERT INTO Student VALUES(101,'Ram','D01'),(102,'Sita','D02'),(103,'Hari','D01');
Department Table
CREATE TABLE Department ( DepartmentID VARCHAR(10) PRIMARY KEY,DepartmentName VARCHAR(50));
Insert Data
INSERT INTO Department VALUES('D01','Computer Science'),('D02','Management');
Advantages
Centralized Database:
Advantages:
Since all data is stored at a single location only thus it is easier to access and coordinate data.
The centralized database has very minimal data redundancy since all data is stored in a single
place.
It is cheaper in comparison to all other databases available.
Disadvantages:
Advantages:
This database can be easily expanded as data is already spread across different physical
locations.
The distributed database can easily be accessed from different networks.
This database is more secure in comparison to a centralized database.
Disadvantages:
This database is very costly and is difficult to maintain because of its complexity.
In this database, it is difficult to provide a uniform view to users since it is spread across
different physical locations.
Data Security:
Data security involves protecting digital information from unauthorized access, corruption, or theft
throughout its lifecycle. It encompasses various practices, technologies, and standards designed to
ensure the confidentiality, integrity, and availability of data. Key aspects of data security include:
1. Encryption: Converting data into a code to prevent unauthorized access. Both data at rest
(stored data) and data in transit (data being transmitted) can be encrypted.
2. Access Control: Restricting who can view or use data. This can be managed through
authentication methods like passwords, biometric scans, or multi-factor authentication, and
authorization measures like role-based access controls.
3. Data Masking: Obscuring specific data within a database to protect it from unauthorized users,
often used in non-production environments.
4. Backup and Recovery: Regularly backing up data and ensuring that it can be recovered in case
of loss, corruption, or disaster.
5. Firewalls and Intrusion Detection Systems (IDS): Using software or hardware-based systems
to monitor and control incoming and outgoing network traffic, and detect and respond to
potential threats.
A Database Administrator is a professional responsible for managing, backing up, and ensuring
the overall performance, security, and integrity of a database system. DBAs are crucial for
maintaining the availability of data, optimizing database performance, and implementing
necessary updates or patches.
1. Data Structure
o Tables: Data is organized into tables, where each table is a collection of related data
entries. Each row in a table represents a record, and each column represents an attribute of
the record.
o Schema: The schema defines the structure of the database, including the tables, columns,
and the relationships between tables.
2. Keys
o Primary Key: A unique identifier for each record in a table. It ensures that no two rows
have the same primary key value.
o Foreign Key: A field in a table that creates a link between two tables. It is a reference to
the primary key in another table, establishing a relationship between the two tables.
o Composite Key: A primary key that consists of two or more columns to uniquely identify
a record in the table.
3. Relationships
o One-to-One: A single record in one table is related to a single record in another table.
o One-to-Many: A single record in one table is related to multiple records in another table.
o Many-to-Many: Multiple records in one table are related to multiple records in another
table. This relationship often requires a function table.
4. Data Integrity
o Entity Integrity: Ensures that each record within a table is unique, typically enforced
through the primary key.
o Referential Integrity: Ensures that foreign keys correctly reference the primary keys in
related tables, maintaining consistent relationships between tables.
o Domain Integrity: Ensures that all data in a column follows the defined format, data type,
and constraints.
5. Normalization
o Normalization: The process of organizing data in a database to reduce redundancy and
improve data integrity. This involves dividing large tables into smaller ones and defining
relationships between them.
6. SQL (Structured Query Language)
o Querying: SQL is used to retrieve data from the database. Examples include SELECT,
JOIN, GROUP BY, and ORDER BY clauses.
o Data Manipulation Command: SQL commands such as INSERT, UPDATE, and
DELETE are used to modify data.
o Data Definition Language: SQL allows defining and altering the structure of the
database using commands like CREATE TABLE, ALTER TABLE, and DROP TABLE.
o Transaction Control: SQL includes commands like BEGIN, COMMIT, and
ROLLBACK to manage transactions and ensure data integrity.