0% found this document useful (0 votes)
15 views12 pages

Database Concepts

Uploaded by

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

Database Concepts

Uploaded by

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

Database Concepts

1. Introduction to File System

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.

Key Functions of a File System:

 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:

Think of a computer as a large filing cabinet:

 Each folder represents a directory, while each document inside represents a file.

2. Limitations of File System

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.

3. Database Management System (DBMS)

A Database Management System (DBMS) is a software application that enables users to


create, manage, and manipulate databases efficiently. It provides a structured approach to
storing and retrieving data, overcoming the limitations of file systems.

Key Functions of a DBMS:

 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.

Key Features of the Relational Data Model:

 Tables: Each table represents an entity (e.g., Students, Courses).


 Rows (Tuples): Each row represents a single record or instance of that entity.
 Columns (Attributes): Each column represents a specific attribute of the entity.

Example:

A Students table may look like this:

Student_ID Name Age Grade

101 John 20 A

102 Alice 21 B

5. Key Concepts in Databases

Understanding the following key concepts is essential for working with databases:

1. Attribute

 Definition: An attribute is a column name in a database table that represents a


property or characteristic of an entity.
 Example: In a Students table, the attributes (or column names) might include
Student_ID, Name, Age, and Grade. Each attribute describes a specific aspect of the
student entity

2. Tuple

 Definition: A single record in a table, representing a specific instance of an entity.


 Example: A row in the Students table, such as (101, John, 20, A).

3. Domain

 Definition: The set of possible values that an attribute can take.


 Example: For the attribute Age, the domain could be any integer from 0 to 100.
4. Degree

 Definition: The number of attributes (columns) in a relation (table).


 Example: A Students table with four attributes (Student_ID, Name, Age, Grade) has
a degree of 4.

5. Cardinality

 Definition: The number of tuples (rows) in a relation (table).


 Example: If the Students table has 30 students, its cardinality is 30.

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.

Key Concepts in Databases


1. Candidate Key

 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:

Student_ID Email Name


101 [email protected] John
102 [email protected] Alice

 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

 Here, Student_ID uniquely identifies each student and cannot be null.

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:

Enrollment_ID Student_ID (Foreign Key) Course_ID (Foreign Key)


1 101 C101
2 102 C102

 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)

1. Introduction to Structured Query Language (SQL)


Structured Query Language (SQL) is a standardized programming language used for managing and
manipulating relational databases. It provides a way for users to create, read, update, and delete data in a
database efficiently. SQL is widely used because of its simplicity and powerful capabilities.

Key Features of 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.

2. Data Definition Language (DDL)


Data Definition Language (DDL) is a subset of SQL used to define and manage all database objects. It
includes commands that allow users to create, alter, and drop tables and other database structures.

Common DDL Commands:

 CREATE: Used to create a new table or database.


o Example:

CREATE TABLE Students (


Student_ID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT,
Grade CHAR(1)
);

 ALTER: Used to modify an existing table structure, such as adding or deleting columns.
o Example:

ALTER TABLE Students ADD Email VARCHAR(255);

 DROP: Used to delete a table or database.


o Example:

DROP TABLE Students;

3. Data Manipulation Language (DML)


Data Manipulation Language (DML) is another subset of SQL used for managing and manipulating data
within the database. DML commands allow users to perform operations like inserting, updating, and
deleting records.
Common DML Commands:

 INSERT: Used to add new records to a table.


o Example:

INSERT INTO Students (Student_ID, Name, Age, Grade)


VALUES (101, 'John', 20, 'A');

 UPDATE: Used to modify existing records in a table.


o Example:

UPDATE Students SET Grade = 'B' WHERE Student_ID = 101;

 DELETE: Used to remove records from a table.


o Example:

DELETE FROM Students WHERE Student_ID = 101;

 SELECT: Used to query and retrieve data from one or more tables.
o Example:

SELECT Name, Age FROM Students WHERE Grade = 'A';

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.

Common Data Types:

 INT: Stores integer values.


o Example: Student_ID INT
 VARCHAR(n): Stores variable-length strings with a maximum length of n.
o Example: Name VARCHAR(100)
 CHAR(n): Stores fixed-length strings, padding with spaces if necessary.
o Example: Grade CHAR(1)
 DATE: Stores date values.
o Example: Enrollment_Date DATE
 FLOAT: Stores floating-point numbers.
o Example: GPA FLOAT

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:

Student_ID INT PRIMARY KEY


 FOREIGN KEY: Creates a link between two tables, ensuring that the value in one table matches a
value in another table’s primary key.
o Example:

FOREIGN KEY (Course_ID) REFERENCES Courses(Course_ID)

 UNIQUE: Ensures that all values in a column are unique across the table.
o Example:

Email VARCHAR(255) UNIQUE

 NOT NULL: Ensures that a column cannot contain null values.


o Example:

Name VARCHAR(100) NOT NULL

 CHECK: Ensures that all values in a column satisfy a specific condition.


o Example:

CHECK (Age >= 0)

Creating and Processing Tables in SQL

1. Data Definition Language (DDL)


Data Definition Language (DDL) is used to define and manage the structure of a database. Here are the
key DDL commands:

1.1 CREATE Database/Table

 CREATE DATABASE: This command creates a new database.


o Example:

CREATE DATABASE SchoolDB;

 CREATE TABLE: This command creates a new table within the database.
o Syntax:

CREATE TABLE table_name (


column1_name data_type constraints,
column2_name data_type constraints,
...
);

o Example:

CREATE TABLE Students (


Student_ID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Age INT CHECK (Age >= 0),
Grade CHAR(1)
);

1.2 ALTER Table

 The ALTER TABLE command is used to modify an existing table structure. You can add, modify, or delete
columns.

Common Alterations:

 Add a Column:

ALTER TABLE Students ADD Email VARCHAR(255);

 Modify a Column:

ALTER TABLE Students MODIFY Age TINYINT; -- Changes data type to TINYINT

 Drop a Column:

ALTER TABLE Students DROP COLUMN Email;

1.3 Describe Table

 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 | |
+-------------+--------------+------+-----+---------+----------------+

1.4 DROP Database/Table

 DROP TABLE: This command deletes a table and all its data from the database.
o Example:

DROP TABLE Students;

 DROP DATABASE: This command deletes the entire database and all its tables.
o Example:

DROP DATABASE SchoolDB;


2. Data Manipulation Language (DML)
Data Manipulation Language (DML) is used for inserting, updating, and deleting data in the database.

2.1 Insertion of Records

 The INSERT command is used to add new records to a table.


 Syntax:

INSERT INTO table_name (column1, column2, ...)


VALUES (value1, value2, ...);

 Example:

INSERT INTO Students (Student_ID, Name, Age, Grade)


VALUES (101, 'John', 20, 'A');

2.2 Data Query: SELECT Statement

 The SELECT statement is used to retrieve data from one or more tables.
 Basic Syntax:

SELECT column1, column2, ...


FROM table_name;

 Example:

SELECT Name, Age FROM Students;

 To select all columns:

SELECT * FROM Students;

2.3 Data Updation

 The UPDATE command is used to modify existing records in a table.


 Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

 Example:

UPDATE Students SET Grade = 'B' WHERE Student_ID = 101;


2.4 Data Deletion

 The DELETE command is used to remove records from a table.


 Syntax:

DELETE FROM table_name WHERE condition;

 Example:

DELETE FROM Students WHERE Student_ID = 101;

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.

Common Aggregate Functions:

 COUNT(): Returns the number of rows that match a specified condition.


o Example:

SELECT COUNT(*) FROM Students; -- Counts all students

 SUM(): Returns the total sum of a numeric column.


o Example:

SELECT SUM(Age) FROM Students; -- Total age of all students

 AVG(): Returns the average value of a numeric column.


o Example:

SELECT AVG(Age) FROM Students; -- Average age of students

 MAX(): Returns the maximum value in a set.


o Example:

SELECT MAX(Age) FROM Students; -- Maximum age among students

 MIN(): Returns the minimum value in a set.


o Example:

SELECT MIN(Age) FROM Students; -- Minimum age among students

Using Aggregate Functions with GROUP BY

 The GROUP BY clause groups rows that have the same values in specified columns into summary rows.
 Example:

SELECT Grade, COUNT(*)


FROM Students
GROUP BY Grade; -- Counts students per grade

You might also like