Database SMY PQ
Database SMY PQ
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:
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.
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.
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).
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.
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.
6. Explain the differences between hierarchical, network, and relational data models.
Answer:
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;
Example:
SELECT * FROM students WHERE age > 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;
Example:
SELECT * FROM students ORDER BY name ASC;
Example:
SELECT * FROM students LIMIT 5;
Example:
INSERT INTO students (name, age) VALUES ('John Doe', 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';
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';
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.
• 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);
Example:
TRUNCATE TABLE students;
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.
Example:
SELECT age, COUNT(*) FROM students GROUP BY age HAVING COUNT(*) > 2;
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)
);
@TFD