Database Concepts
Database Concepts
A file system is a method of organizing and storing data on a storage device, allowing users
to manage files efficiently. It provides the structure for how data is stored, retrieved, and
manipulated.
File Creation: Users can create new files to store data (e.g., documents, images).
File Management: Users can rename, move, copy, and delete files as needed.
Data Organization: Files are organized in directories or folders, similar to physical
filing cabinets.
Example:
Each folder represents a directory, while each document inside represents a file.
While file systems provide basic functionalities, they have several limitations that make
them less effective for managing large amounts of data:
Key Limitations:
Data Redundancy: The same data can be stored in multiple files, leading to
inconsistencies.
o Example: If a customer’s address is stored in two different files and one is
updated, discrepancies may arise.
Data Isolation: Data is often isolated in separate files, making it challenging to relate
and access related data.
o Example: Retrieving a student's course enrollment may require accessing
multiple files.
Limited Data Access: Searching for specific data in a large collection of files can be
time-consuming.
o Example: Finding a particular student's record among hundreds of files may
take considerable time.
Concurrency Issues: Multiple users may face difficulties accessing and modifying
the same data simultaneously.
o Example: If two users try to edit the same file at the same time, it could result
in data loss or corruption.
Security Issues: File systems often lack robust security features, making sensitive
data vulnerable.
o Example: Any user with access to the system may view or change files without
restrictions.
Data Definition: Allows users to define the structure of data and relationships within
the database.
Data Manipulation: Enables users to insert, update, delete, and retrieve data through
queries.
Data Administration: Provides tools for data security, backup, and recovery.
Advantages of DBMS
1. Data Integrity: Ensures the accuracy and consistency of data across the database.
o Example: Updating a student's information in one location automatically
updates related records.
2. Reduced Redundancy: Minimizes duplicate data storage, which saves space and
reduces inconsistency.
o Example: A student's record is stored once in the database instead of multiple
times across files.
3. Enhanced Security: Provides user authentication and access controls to protect
sensitive data.
o Example: Teachers may access student records while students cannot view
other students’ data.
4. Improved Data Sharing: Allows multiple users to access and manipulate data
concurrently without conflicts.
o Example: Multiple staff members can view or update records without
interfering with each other’s work.
5. Backup and Recovery: Facilitates easier data backup and restoration in the event of
data loss.
o Example: Regular automated backups ensure data can be restored if deleted
accidentally.
4. Relational Data Model
The relational data model, introduced by Edgar F. Codd in 1970, organizes data into
tables (also called relations). Each table consists of rows and columns, providing a structured
way to store and manage data.
Example:
101 John 20 A
102 Alice 21 B
Understanding the following key concepts is essential for working with databases:
1. Attribute
2. Tuple
3. Domain
5. Cardinality
6. Schema
Definition: The overall structure of the database, including tables, attributes, and
relationships.
Example: The schema for a university database might include tables for Students,
Courses, and Enrollments, with defined attributes for each.
Definition: A candidate key is a set of one or more attributes (columns) that can uniquely identify a
record (row) in a table. Each table can have multiple candidate keys.
Characteristics:
o Uniqueness: Each value of the candidate key must be unique across the table, meaning no
two rows can have the same value for this key.
o Minimality: No subset of the candidate key can uniquely identify records. If you remove any
attribute from the candidate key, it should no longer be able to uniquely identify records.
Example: Consider a Students table:
In this case, both Student_ID and Email can serve as candidate keys because each can uniquely
identify a student.
2. Primary Key
Definition: A primary key is a specific candidate key selected to uniquely identify records in a table.
It is used to enforce entity integrity.
Characteristics:
o Uniqueness: The values in a primary key column must be unique for each record.
o Non-nullability: A primary key cannot contain null values. Every record must have a valid
value for the primary key.
Example: Continuing from the previous example, if we choose Student_ID as the primary key, it
would look like this:
Student_ID (Primary Key) Email Name
101 [email protected] John
102 [email protected] Alice
3. Alternate Key
Definition: An alternate key is any candidate key that is not chosen as the primary key. Alternate
keys are also unique identifiers for records but are not used as the main key for the table.
Characteristics:
o Alternate keys can still enforce uniqueness but serve as backup options for identifying
records.
Example: In the previous Students table, if we selected Student_ID as the primary key, then Email
would be considered an alternate key. Both Student_ID and Email can uniquely identify students,
but only one is designated as the primary key.
4. Foreign Key
Definition: A foreign key is an attribute or a set of attributes in one table that refers to the primary
key of another table. It is used to establish a relationship between the two tables.
Characteristics:
o Referential Integrity: A foreign key must match an existing value in the primary key of the
referenced table or be null. This ensures that relationships between tables remain valid.
o Linking Tables: Foreign keys are used to create connections between tables, enabling the
organization of data across different entities.
Example: Consider a Courses table:
Course_ID Course_Name
C101 Database Systems
C102 Web Development
Now, if we have an Enrollments table that records which students are enrolled in which courses:
In this case:
o Student_IDin the Enrollments table is a foreign key that references Student_ID in the
Students table.
o Course_ID in the Enrollments table is a foreign key that references Course_ID in the
Courses table.
Structured Query Language (SQL)
Declarative Language: SQL allows users to specify what data they want to retrieve without needing to
specify how to retrieve it.
Standardized: SQL is standardized by organizations like ANSI and ISO, ensuring consistent syntax across
different database systems.
Versatile: SQL can be used for various tasks, including querying data, modifying database structures, and
managing access permissions.
ALTER: Used to modify an existing table structure, such as adding or deleting columns.
o Example:
SELECT: Used to query and retrieve data from one or more tables.
o Example:
4. Data Types
Data types specify the kind of data that can be stored in a database column. Choosing the right data type is
important for efficient storage and accurate data representation.
5. Constraints
Constraints are rules applied to columns in a table to ensure the accuracy and integrity of the data. They help
enforce data quality and define relationships between tables.Common Types of Constraints:
PRIMARY KEY: Uniquely identifies each record in a table. A primary key cannot contain null
values.
o Example:
UNIQUE: Ensures that all values in a column are unique across the table.
o Example:
CREATE TABLE: This command creates a new table within the database.
o Syntax:
o Example:
The ALTER TABLE command is used to modify an existing table structure. You can add, modify, or delete
columns.
Common Alterations:
Add a Column:
Modify a Column:
ALTER TABLE Students MODIFY Age TINYINT; -- Changes data type to TINYINT
Drop a Column:
The DESCRIBE or DESC command is used to display the structure of a table, showing its columns, data types,
and constraints.
o Example:
DESCRIBE Students;
o Output:
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| Student_ID | INT | NO | PRI | NULL | |
| Name | VARCHAR(100) | NO | | NULL | |
| Age | INT | YES | | NULL | |
| Grade | CHAR(1) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
DROP TABLE: This command deletes a table and all its data from the database.
o Example:
DROP DATABASE: This command deletes the entire database and all its tables.
o Example:
Example:
The SELECT statement is used to retrieve data from one or more tables.
Basic Syntax:
Example:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
Example:
3. Aggregate Functions
Aggregate functions perform calculations on a set of values and return a single value. They are often used
with the GROUP BY clause to group results based on one or more columns.
The GROUP BY clause groups rows that have the same values in specified columns into summary rows.
Example: