0% found this document useful (0 votes)
4 views15 pages

Database SMY PQ

This document is a comprehensive guide to SQL and database management systems, covering essential concepts, syntax, and explanations, along with past exam questions and answers. It aims to equip readers with a strong understanding of SQL, enabling them to write queries confidently and prepare for exams. Key topics include SQL syntax, data types, database characteristics, and the benefits of using a DBMS over file-based systems.

Uploaded by

mohammedbala843
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)
4 views15 pages

Database SMY PQ

This document is a comprehensive guide to SQL and database management systems, covering essential concepts, syntax, and explanations, along with past exam questions and answers. It aims to equip readers with a strong understanding of SQL, enabling them to write queries confidently and prepare for exams. Key topics include SQL syntax, data types, database characteristics, and the benefits of using a DBMS over file-based systems.

Uploaded by

mohammedbala843
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/ 15

Complete SQL Summary with Past Exam Questions and Answers

Introduction
This document provides a full summary of DBMS/SQL, covering essential concepts, syntax, and
explanations. It also includes possible exam questions and answers to help reinforce
understanding.
Let's start by understanding the basic SQL syntax and how databases work.

Objective


After reading this guide, you will:


Fully understand the concept of SQL and database management systems.


Be able to write SQL queries confidently.
Have a strong foundation for exam preparation with past questions and answers.

table containing essential SQL syntax and their explanations, starting from the basics:

SQL Syntax Explanation


Retrieves data from one or more tables. Example: SELECT column1,
SELECT
column2 FROM table_name;
Filters records based on a condition. Example: SELECT * FROM
WHERE
students WHERE age > 18;
Sorts the result set in ascending (ASC) or descending (DESC) order.
ORDER BY
Example: SELECT * FROM students ORDER BY name ASC;
Limits the number of rows returned. Example: SELECT * FROM
LIMIT
students LIMIT 5;
Adds new records to a table. Example: INSERT INTO students
INSERT INTO
(name, age) VALUES ('John Doe', 22);
Modifies existing records in a table. Example: UPDATE students
UPDATE
SET age = 23 WHERE name = 'John Doe';
Removes records from a table. Example: DELETE FROM students
DELETE
WHERE name = 'John Doe';
Defines a new table with columns and data types. Example: CREATE
CREATE TABLE TABLE students (id INT PRIMARY KEY, name
VARCHAR(100), age INT);
Modifies an existing table (adding, deleting, or modifying columns).
ALTER TABLE Example: ALTER TABLE students ADD phone
VARCHAR(20);
DROP TABLE Deletes a table permanently. Example: DROP TABLE students;
Deletes all records but keeps the table structure. Example: TRUNCATE
TRUNCATE TABLE
TABLE students;
JOIN (INNER JOIN, Combines rows from multiple tables based on related columns.
LEFT JOIN, RIGHT Example: SELECT students.name, courses.course_name
JOIN, FULL JOIN) FROM students INNER JOIN courses ON students.id

SQL Syntax Explanation
= courses.student_id;
Groups records with the same values and performs aggregate functions.
GROUP BY Example: SELECT age, COUNT(*) FROM students GROUP
BY age;
Filters grouped records after using GROUP BY. Example: SELECT
HAVING age, COUNT(*) FROM students GROUP BY age HAVING
COUNT(*) > 2;
Uniquely identifies each record in a table. Example: id INT
PRIMARY KEY
PRIMARY KEY;
Establishes a link between two tables. Example: FOREIGN KEY
FOREIGN KEY
(student_id) REFERENCES students(id);
Ensures a column cannot have NULL values. Example: name
NOT NULL
VARCHAR(100) NOT NULL;
Ensures all values in a column are unique. Example: email
UNIQUE
VARCHAR(100) UNIQUE;
Restricts values based on a condition. Example: salary
CHECK
DECIMAL(10,2) CHECK (salary > 0);
Assigns a default value if no value is specified. Example: status
DEFAULT
VARCHAR(20) DEFAULT 'active';

SQL Data Types


Common Data Types in SQL
SQL supports multiple data types for storing different kinds of information.

Data Type Description


INT Integer numbers (whole numbers)
FLOAT Floating-point numbers (decimal values)
CHAR(n) Fixed-length character string (e.g., CHAR(10) stores exactly 10 characters)
VARCHAR(n) Variable-length string (e.g., VARCHAR(255) stores up to 255 characters)
TEXT Large text data
DATE Stores date values (YYYY-MM-DD)
TIME Stores time values (HH:MM:SS)
DATETIME Stores both date and time values
BOOLEAN Stores TRUE or FALSE values

The handout = Summary



Chapter 1: Before the Advent of Database Systems
• File-based System: Early systems stored data in files managed by application programs.
Each department had its own files, leading to issues like:
• Data Redundancy: Same data stored in multiple places, causing inconsistency.
• Data Isolation: Difficulty in retrieving data stored in different files.
• Integrity Problems: Ensuring data correctness and consistency was challenging.
• Security Issues: Lack of centralized control over data access.
• Concurrency Problems: Multiple users accessing data simultaneously could lead to
conflicts.

Chapter 2: Fundamental Concepts


• Database: A shared collection of related data supporting organizational activities.
• Database Management System (DBMS): is a Software that enables users to create,
maintain, and control access to databases.
• Database Properties:
• Represents real-world information.
• Logical, coherent, and internally consistent.
• Designed for a specific purpose.
• Data stored in fields, which make up tables.

Chapter 3: Characteristics and Benefits of a Database


• Benefits of DBMS:
• Self-describing: Contains metadata (data about data).
• Program-Data Independence: Changes in data structure don’t affect application
programs.
• Multiple Views: Different users can have customized views of the database.
• Data Sharing: Multiple users can access the database simultaneously.
• Controlled Redundancy: Minimizes data duplication.
• Integrity Constraints: Ensures data validity (e.g., primary keys, data types).
• Security: Restricts unauthorized access.
• Backup and Recovery: Protects data from loss.

Chapter 4: Types of Data Models
• High-level Conceptual Data Models:
• Entity-Relationship (ER) Model: Represents real-world objects (entities) with
attributes and relationships.
• Record-based Logical Data Models:
• Relational Model: Data stored in tables (relations).
• Network Model: Data represented as record types with set types (one-to-many
relationships).
• Hierarchical Model: Data organized in a tree-like structure.

Chapter 5: Fundamental Structures


• Data Models:
• Hierarchical: Tree structure with parent-child relationships.
• Network: Allows multiple relationships between data.
• Relational: Data stored in tables with relationships between them.
• Object-oriented: Uses object-oriented technology for database design.
• Relational Database:
• Consists of interconnected tables.
• Tables must have unique rows, named columns, and optional row/column order.
• Normalization:
• 1NF: No repeating groups, each cell contains a single value.
• 2NF: All non-key fields depend on the entire primary key.
• 3NF: No transitive dependencies (non-key fields depend only on the primary key).
• BCNF: Stricter form of 3NF, ensuring no non-trivial dependencies.

Key Terms:
• Data Redundancy: Duplication of data.
• Data Integrity: Ensuring data is correct and consistent.
• Metadata: Data about data (e.g., table definitions).
• Primary Key: Unique identifier for a record.
• Foreign Key: Links two tables together.

• Normalization: Organizing data to reduce redundancy and improve integrity.

CSC 315:– Exam Questions and Answers


Modibbo Adama University, Yola 2023/2025

QUESTION 1 (Compulsory)
(a) Why would you choose a database system instead of simply storing data in operating
system files? When would it make sense not to use a database system?
Answer:
A database system is preferred over file-based systems because:
• Data Redundancy Reduction: Avoids duplicate storage of data.
• Data Integrity & Consistency: Ensures that data remains accurate and consistent.
• Improved Security: Allows access control and authentication.
• Efficient Data Retrieval: Uses indexing and query optimization for fast searches.
• Multi-User Access: Allows multiple users to work with the data simultaneously.
• Backup & Recovery: Ensures data safety in case of system failures.
However, a database system may not be necessary if:
• Data volume is small, making file storage sufficient.
• Performance overhead of a DBMS is unnecessary.
• High initial cost and maintenance are not justified.

(b) Cost Considerations for DBMS Features


Mr. Craig Brown needs a database system but wants to minimize costs. Explain whether he should
pay for the following features:
✅ Yes – Prevents unauthorized access, ensuring data confidentiality.
1. Security Facility:
2. Concurrency Control: ✅ Yes – Manages multiple users accessing data simultaneously to
avoid conflicts.
3. Crash Recovery: ✅ Yes – Ensures data is restored in case of system failures.
4. Views Mechanism: ❌ No (Optional) – Enhances data abstraction but may not be essential
for basic use.
5. Query Language: ✅ Yes – Allows efficient data retrieval and manipulation.

(c) Differences between File-Based Approach and Database System (Tabular Form)
Feature File-Based System Database System
Data Redundancy High Low
Data Consistency Difficult to maintain Ensured
Data Integrity Weak Strong
Security Minimal High
Data Retrieval Slow Fast
Multi-User Support Limited Efficient

QUESTION 2
(a) Define the following database terms with examples:
1. Attribute: A characteristic of an entity (e.g., "Name" in a Student table).
2. Domain: The set of allowed values for an attribute (e.g., Gender: {Male, Female}).
3. Entity: A real-world object stored in a database (e.g., a Student with ID, Name, Age).
4. Entity Relationship (ER): A connection between two entities (e.g., Student enrolled in
Course).
5. Entity Set: A collection of similar entities (e.g., all Students in a school).
6. Relationship Set: A collection of similar relationships (e.g., all "enrolled in" relationships).
7. One-to-Many Relationship: One entity is related to multiple entities (e.g., One Teacher
teaches Many Students).
8. Many-to-Many Relationship: Multiple entities are related to multiple entities (e.g.,
Students enroll in multiple Courses).

(b) ER Diagram for Employees, Departments, and Managers


• Entities: Employees (S_ID, Salary, Name, Age, Phone_No), Departments (D_ID, D_Name,
Budget).
• Relationships:
• Each employee belongs to a department.
• Each department is managed by an employee.
• Employees have children (linked via Name attribute).
• ER Diagram required for a complete answer.

(c) Two Major Disadvantages of a Database System
1. High Setup and Maintenance Cost – Requires resources for design, implementation, and
administration.
2. Complexity – Needs trained professionals to manage, design, and optimize the system.

QUESTION 3
(a) ER Diagram for Shoprite B2B Database
• Entities and Attributes:
• Seller (ID, Name, Contact, Email, Address)
• Product (ID, Name, Price, Number)
• Seller-Product Relationship (Which seller sells which product)
• Unique ID per seller and product
• Constraints:
• Seller ID must be unique.
• Each product must have a seller.
• Each product should have a unique identifier.
• Draw ERD accordingly.

(b) Five Main Functions of a File Processing System and a DBMS


Function File Processing System DBMS
Data Storage Stores files separately Stores data in structured tables
Data Retrieval Manual searching Uses SQL queries
Security Minimal security Advanced security features
Data Redundancy High Low
Data Integrity Hard to maintain Ensured through constraints

(c) Four Disadvantages of a File Processing System


1. Data Redundancy – Duplicate data stored in multiple files.
2. Data Inconsistency – Different versions of data exist.
3. Limited Data Sharing – Hard for multiple users to access files.
4. Difficult Data Retrieval – No efficient querying mechanism.

QUESTION 4
(a) Six Steps in Setting Up a Database
1. Requirement Analysis – Define objectives and data needs.
2. Conceptual Design – Create ER diagrams and data models.
3. Logical Design – Convert models into database schema.
4. Normalization – Optimize structure to avoid redundancy.
5. Implementation – Use SQL to create tables and relationships.
6. Testing & Deployment – Ensure functionality and security.

(b) SQL Queries for University Database


1. Find Titles of Courses with at Least Two Credits
sql code>
SELECT Course_Title FROM Courses WHERE Credits >= 2;

2. Find IDs of Students Taught by a Specific Lecturer in the Computer Science Department
sql code>
SELECT Student_ID FROM Enrollments
WHERE Lecturer_ID = (SELECT Lecturer_ID FROM Lecturers WHERE
Department = 'Computer Science');
P/Exam Questions and Answers
1. Discuss the disadvantages of file-based systems.
Answer: File-based systems have several disadvantages, including:
• Data Redundancy: Data is often duplicated across multiple files, leading to inconsistency.
• Data Inconsistency: Since updates are not synchronized across all files, inconsistencies
may arise.
• Limited Data Sharing: Different applications may not be able to access and update the
same data efficiently.
• Security Issues: File-based systems lack centralized security mechanisms, making
unauthorized access easier.
• Difficulty in Data Retrieval: Searching for data in large file-based systems can be slow and
inefficient.
• Lack of Data Integrity: There are no built-in constraints to ensure data accuracy and
consistency.

2. Explain the difference between data and information.
Answer:
• Data: Raw facts, figures, or symbols that have no specific meaning on their own (e.g.,
"1234" or "John Doe").
• Information: Processed or structured data that provides meaningful insights (e.g., "John
Doe has ID number 1234").
In short, data is unprocessed, while information is processed data that is useful for decision-
making.

3. What are the properties of a DBMS?


Answer: The key properties of a Database Management System (DBMS) include:
• Data Integrity: Ensures accuracy and consistency of stored data.
• Data Security: Provides user authentication, access control, and encryption features.
• Data Independence: Separates data from application programs, making it easier to modify
databases.
• Concurrent Access: Allows multiple users to access and manipulate data simultaneously
without conflicts.
• Backup and Recovery: Ensures data can be restored in case of failure.
• Data Abstraction: Hides complex database details and provides different levels of
abstraction.

4. Describe the benefits of using a database system over a file-based system.


Answer: A database system offers several advantages over a file-based system, including:
• Reduced Data Redundancy: Eliminates duplicate storage of data.
• Improved Data Consistency: Ensures all changes are reflected across the database.
• Enhanced Security: Provides access control, authentication, and encryption.
• Efficient Data Retrieval: Uses indexing and optimized queries for fast data access.
• Data Integrity and Validation: Enforces rules and constraints to maintain accurate data.
• Concurrent Access and Multi-User Support: Allows multiple users to work with data
simultaneously without conflicts.

5. What is normalization, and why is it important?
Answer:
Normalization is the process of organizing a database to reduce redundancy and improve integrity.
It involves dividing large tables into smaller ones and defining relationships between them.
Importance of Normalization:
• Eliminates data redundancy (duplicate data storage).
• Ensures data consistency across tables.
• Reduces data anomalies (insertion, deletion, and update issues).
• Improves database efficiency and storage optimization.
• Enhances scalability and data integrity.

6. Explain the differences between hierarchical, network, and relational data models.
Answer:

Data Model Structure Characteristics


Hierarchical Each record has a single parent and multiple children
Tree-like structure
Model (1:N relationship).
Supports multiple parent-child relationships (many-to-
Network Model Graph structure
many).
Tables with rows and Uses tables with unique keys to establish relationships
Relational Model
columns (most widely used).
Key Differences:
• The hierarchical model has a strict parent-child relationship, while the network model
allows more flexible many-to-many relationships.
• The relational model is more flexible and uses tables to store data, making it easier to
manage and query.

Structured Query Language (SQL) –


Comprehensive Guide with Syntax
Explanation
Introduction to SQL
Structured Query Language (SQL) is a programming language used to manage and manipulate
relational databases. It allows users to create databases, insert and update records, retrieve data, and
perform other operations on stored information.

SQL Syntax and Their Explanations
1. SELECT Statement
The SELECT statement is used to retrieve data from one or more tables.
Syntax:
SELECT column1, column2 FROM table_name;

• column1, column2 – The specific columns to retrieve.


• table_name – The name of the table where the data is stored.
• * – Used to select all columns in the table.

Example:
SELECT name, age FROM students;

This retrieves the name and age columns from the students table.

2. WHERE Clause
The WHERE clause is used to filter records based on a condition.
Syntax:
SELECT column1 FROM table_name WHERE condition;

• condition – A logical condition that must be met.

Example:
SELECT * FROM students WHERE age > 18;

This selects students older than 18.

3. ORDER BY Clause
The ORDER BY clause is used to sort retrieved records.
Syntax:
SELECT column1 FROM table_name ORDER BY column1 ASC|DESC;

• ASC – Sorts in ascending order (default).


• DESC – Sorts in descending order.

Example:
SELECT * FROM students ORDER BY name ASC;

This sorts students by name in ascending order.



4. LIMIT Clause
The LIMIT clause restricts the number of records returned.
Syntax:
SELECT column1 FROM table_name LIMIT number;

Example:
SELECT * FROM students LIMIT 5;

This retrieves only the first 5 records.

5. INSERT INTO Statement


The INSERT INTO statement adds new records into a table.
Syntax:
INSERT INTO table_name (column1, column2) VALUES (value1, value2);

Example:
INSERT INTO students (name, age) VALUES ('John Doe', 22);

This inserts a new student named John Doe, aged 22.

6. UPDATE Statement
The UPDATE statement modifies existing records in a table.
Syntax:
UPDATE table_name SET column1 = value1 WHERE condition;

Example:
UPDATE students SET age = 23 WHERE name = 'John Doe';

This updates John Doe’s age to 23.

7. DELETE Statement
The DELETE statement removes records from a table.
Syntax:
DELETE FROM table_name WHERE condition;

Example:
DELETE FROM students WHERE name = 'John Doe';

This deletes John Doe’s record.


8. CREATE TABLE Statement


The CREATE TABLE statement defines a new table.
Syntax:
CREATE TABLE table_name (
column1 datatype constraints,
column2 datatype constraints
);

Example:
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
age INT,
email VARCHAR(100) UNIQUE
);

This creates a students table with columns for id, name, age, and email.

9. ALTER TABLE Statement


The ALTER TABLE statement modifies an existing table.
Syntax:
• Add a new column:
ALTER TABLE table_name ADD column_name datatype;

• Modify a column:
ALTER TABLE table_name MODIFY COLUMN column_name new_datatype;

• Delete a column:
ALTER TABLE table_name DROP COLUMN column_name;

Example:
ALTER TABLE students ADD phone VARCHAR(20);

This adds a phone column.

10. DROP TABLE Statement


The DROP TABLE statement permanently deletes a table.
Syntax:
DROP TABLE table_name;

Example:
DROP TABLE students;

This removes the students table.

11. TRUNCATE TABLE Statement


The TRUNCATE TABLE statement removes all data but keeps the table structure.
Syntax:
TRUNCATE TABLE table_name;

Example:
TRUNCATE TABLE students;

This deletes all records from students.

12. JOIN Clause


The JOIN clause combines rows from multiple tables based on a common column.

INNER JOIN – Returns only matching records.


SELECT students.name, courses.course_name
FROM students
INNER JOIN courses ON students.id = courses.student_id;

LEFT JOIN – Returns all records from the left table and matching records from the right
table.
SELECT students.name, courses.course_name
FROM students
LEFT JOIN courses ON students.id = courses.student_id;

RIGHT JOIN – Returns all records from the right table and matching records from the left
table.
SELECT students.name, courses.course_name
FROM students
RIGHT JOIN courses ON students.id = courses.student_id;

FULL JOIN – Returns all records when there is a match in either table.
SELECT students.name, courses.course_name
FROM students
FULL JOIN courses ON students.id = courses.student_id;

13. GROUP BY Clause
The GROUP BY clause groups rows that have the same values in specified columns.
Syntax:
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name;

Example:
SELECT age, COUNT(*) FROM students GROUP BY age;

This groups students by age and counts how many share the same age.

14. HAVING Clause


The HAVING clause filters groups after using GROUP BY.
Syntax:
SELECT column_name, COUNT(*) FROM table_name GROUP BY column_name HAVING
condition;

Example:
SELECT age, COUNT(*) FROM students GROUP BY age HAVING COUNT(*) > 2;

This selects age groups with more than two students.

15. SQL Constraints


Constraints ensure data integrity.

Constraint Description
PRIMARY KEY Uniquely identifies a record in a table.
FOREIGN KEY Ensures referential integrity between tables.
NOT NULL Ensures a column cannot have NULL values.
UNIQUE Ensures all values in a column are unique.
CHECK Ensures column values meet a condition.
DEFAULT Sets a default value for a column.
Example:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
salary DECIMAL(10,2) CHECK (salary > 0)
);

This ensures salaries must be positive.

@TFD

You might also like