Mastering Databases: Part 1

·

9 min read

💡
Inspired by Abid Sohail Bhutta's YouTube tutorials, this article delves into the fundamentals of databases.

A database is a shared collection of logically related data, such as a contact database with tables for storing contact information:

Contact_IDNamePhoneEmail
1Alice Smith555-1234
2Bob Jones555-5678

DBMS (Database Management System) is software used to create, manage, maintain, and control databases. Examples include MySQL, PostgreSQL, MongoDB, and Microsoft SQL Server (SSMS).

Metadata/Dictionary refers to data about data, defining the structure and characteristics of the data in a database.

DML (Data Manipulation Language) consists of commands to manipulate and query data within the database. Examples include:

  • SELECT: Retrieves data.

  • INSERT INTO: Adds new data.

  • UPDATE: Modifies existing data.

DCL (Data Control Language) commands control access and permissions to data, including:

  • GRANT: Provides specific privileges.

  • REVOKE: Removes specific privileges.

  • DENY: Denies specific privileges.

DDL (Data Definition Language) commands define and modify database structures, such as:

  • CREATE TABLE: Defines a new table.

  • ALTER TABLE: Modifies an existing table.

  • DROP TABLE: Deletes a table.

Historically, managing one file per application caused problems like data duplication, loss of integrity, and incompatible file formats. This approach was inefficient compared to modern databases.

In application development, the database structure is usually created first, followed by the development of applications that interact with this structure.

An application program is the interface through which users connect to and interact with the database. Data dependence occurs as data in the files and structure in the code.

Entity refers to an object or concept stored in a database. Examples include:

  • Employee

  • Customer

  • Product

An attribute is a characteristic or property of an entity. Examples include:

  • EmployeeID (for an Employee, uniquely identifying each employee)

  • OrderDate (for an Order, indicating when the order was placed)

  • Price (for a Product, showing the cost of the product)

A relation represents an association or link between entities in a database. Examples include:

  • Employee-Department (an Employee works in a Department)

  • Customer-Order (a Customer places an Order)

  • Order-Product (an Order includes a Product)

Having understood the basic concepts and components of databases, let's now explore their advantages and disadvantages.

Advantages and Disadvantages of Databases

Advantages

1. Data Integrity: Ensures the accuracy and reliability of data through defined constraints and rules.

2. Data Consistency: Maintains uniform data across the system, preventing discrepancies.

3. Improved Standards and Security: Adheres to standardized formats and provides robust security measures.

4. Backup: Facilitates data recovery in case of corruption or loss.

5. Concurrency: Manages multiple users accessing the same data simultaneously.

Disadvantages

1. Cost: Involves expenses related to software, hardware, and administration.

2. Complexity: Requires specialized knowledge and skills for design, management, and maintenance.

3. Size: Can grow significantly, necessitating substantial storage and management practices.

Understanding Database Levels

1. External Level: Represents the user view of the database, tailored to meet the needs of specific users or applications. For example, a class view.

2. Conceptual Level: Defines the overall structure and relationships between data elements, focusing on data organization. For example, the arrangement of chairs.

3. Internal Level: Details the physical storage of data on hardware. For example, how chairs are actually stored.

Purpose of Subschemas

Subschemas offer customized views of the database for specific users or applications, ensuring they access only relevant data. For example, an HR sub-schema might include only employee-related tables, such as EmployeeDetails and Payroll, while excluding other unrelated tables.

Data Independence

1. Logical Data Independence: Refers to the immunity of external schemas from changes in the conceptual schema. For example, adding a new column to a table does not impact existing user views.

2. Physical Data Independence: Refers to the immunity of the conceptual schema from changes in the physical schema. For example, moving data from a hard drive to an SSD does not alter data access or usage.

Database Models and Hierarchy

Databases can be organized into different models and hierarchies:

1. Flat Files: Basic data storage format with a simple, unstructured format.

2. Hierarchical: Organizes data in a tree-like structure with parent-child relationships.

3. Network: Uses a graph structure for complex relationships between data.

4. Relational: Uses tables to represent data and relationships, a flexible and widely used model.

5. Object-Oriented: Integrates object-oriented programming principles with database management.

6. Object-Oriented Relational: Combines object-oriented features with relational databases.

Data Warehousing

Data warehousing is about collecting, storing, and analyzing large amounts of data from various sources. It supports decision-making by providing a central place for historical data, which can be queried and analyzed for business insights. Unlike operational databases, data warehouses are optimized for read-heavy operations and complex queries.

Web-Enabled Data Models

Web-enabled data models include technologies like JSON, XML, cloud databases, and NoSQL databases, which often do not require a fixed schema.

Understanding Database Models and Keys: A Youth-Friendly Guide

Data Model vs. Database Model

  • Data Model: Think of it as a blueprint for organizing information. It shows how data is structured and related. For example, a data model for a school's database might show how students, teachers, and classes are interconnected.

  • Database Model: This is more about how that data is stored and accessed in a real system. It's like the actual construction work based on the blueprint. For instance, a database model would show how the data about students and classes is saved in tables on a computer.

Conceptual Model

  • ERD (Entity-Relationship Diagram): This model helps visualize how different data entities (like students and teachers) are related. It's like a map showing the connections between various points of interest.

  • OODBM (Object-Oriented Database Model): This model represents data as objects, similar to how classes and objects work in programming. For example, a "Student" object might have attributes like ID, Name, and Grade.

Logical Model

  • Hierarchical Model: Data is organized in a tree-like structure. Think of it like a family tree where one parent can have multiple children, but each child has only one parent. For example, in a bank system, an account can have multiple transactions, but each transaction belongs to only one account.

  • Network Model: Data is organized in a graph-like structure, allowing for complex relationships. Imagine a social network where users can be friends with multiple other users. This model is useful for systems with many-to-many relationships.

  • Relational Model: Data is organized in tables, similar to how you organize information in a spreadsheet. For instance, a "Students" table might have columns for ID, Name, and Age. This model is popular because it's flexible and easy to work with.

Object-Oriented Database Model (OODBM)

  • Data in Objects: Data is stored as objects, similar to programming classes. For example, a "Book" class might have attributes like Title, Author, and ISBN. This model mirrors real-world entities closely and can simplify how we interact with data.

Functions of a DBMS

  • User Interaction: You use queries to request or manipulate data, like asking a question in a database.

  • Query Processor: It interprets your query and figures out how to execute it.

  • Database Manager: Executes the commands to read or update the data.

  • Dictionary Manager: Keeps track of metadata (data about data) and enforces rules.

Database Manager Components

  • Command Processing: Executes commands from the user.

  • Query Optimization: Finds the best way to execute your queries.

  • Scheduler and Integrity: Manages transaction order and ensures data rules are followed.

  • Buffer Management: Stores data temporarily for quick access.

  • Recovery and Authorization: Recovers data after failures and manages user permissions.

Database Terminology

  • Row/Record/Tuple/Entity: Different names for the same thing—a single set of data in a table.

  • Relation: A table where data is stored in rows and columns.

  • Degree of Relation: The number of columns in a table. For example, a table with columns for ID, Name, and Age has a degree of 3.

  • Cardinality: The number of rows in a table. A table with 100 rows has a cardinality of 100.

Keys in Databases

  • Primary Key: A unique identifier for each record. For example, an "EmployeeID" in an employee table ensures that no two employees have the same ID.

  • Candidate Key: Potential primary keys. For instance, both "EmployeeID" and "Email" could uniquely identify an employee, but only one is chosen as the primary key.

  • Foreign Key: A column that creates a relationship between two tables. For example, "CustomerID" in an orders table might link to "CustomerID" in a customers table.

  • Super Key: Any combination of columns that uniquely identifies a record. For example, (EmployeeID, Email) ensures uniqueness, but EmployeeID alone is enough.

  • Composite Key: A primary key that uses multiple columns together. For example, (ClassID, StudentID) uniquely identifies a class enrollment.

  • Unique Key: Ensures all values in a column are unique but can have null values.

  • Surrogate Key: A system-generated unique identifier, like an auto-incrementing number. For instance, "EmployeeID INT IDENTITY(1,1)" in SQL Server automatically generates a new ID for each employee.

Example of Surrogate Key in MS SQL

Here, EmployeeID is a surrogate key that starts at 1 and increments by 1 for each new record.

Summary

  • Surrogate Keys: Simple, consistent, and efficient. Good for complex or changing data situations. However, they lack business context and can make data less intuitive.

  • Natural Keys: Based on real-world data attributes. They are useful but might change, affecting database integrity.

Conditions for a Relation

  1. Every relation is a table, but not every table is a relation: All relations can be represented as tables, but not all tables meet the criteria to be relations.

    • Example: A table with duplicate rows is not a relation.
  2. Each table name must be unique: Every table in a database must have a distinct name.

    • Example: Two tables cannot both be named "Employees."
  3. Each cell contains one value (atomic value): Each cell in a table must hold a single, indivisible value.

    • Example: A cell should not contain a list of values.
  4. No repeating groups: A table should not have repeating groups or arrays.

    • Example: A column should not store multiple phone numbers.
  5. Unique attribute names: Each column in a table must have a unique name.

    • Example: Two columns cannot both be named "ID."
  6. Attributes come from the same domain: All values in a column must be of the same data type.

    • Example: A column defined to store integers should not contain text.
  7. Order of tuples has no significance: The order in which rows are stored in a table does not matter.

    • Example: Swapping the order of rows does not change the data.
  8. Each tuple is distinct: Each row in a table must be unique.

    • Example: No two rows should have the same data in all columns.
Â