Databases (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
A program is being designed to store the names of students and their corresponding marks in a class. The program needs to efficiently store the names and marks. Suggest suitable data types for storing the names and marks, explaining your reasoning for each choice.
For storing student names, the most suitable data type is string. Strings are designed to hold sequences of characters, which is exactly what names are. They are flexible and can accommodate names of varying lengths.
For storing marks, an integer data type is appropriate. Marks are typically whole numbers (e.g., 0, 5, 78). Integers are specifically designed to store whole numbers and provide a range of values suitable for representing marks. Using a floating-point type (like a float) would be unnecessary and could introduce rounding errors.
2.
A database stores information about students. The table 'Students' has the following columns: StudentID (integer, primary key), Name (text), Subject (text), and Grade (text). Write an SQL query to select the names and subjects of all students who are in 'Mathematics' or 'Physics'.
SELECT Name, Subject
FROM Students
WHERE Subject = 'Mathematics' OR Subject = 'Physics';
Explanation:
- SELECT Name, Subject: This specifies that we want to retrieve the 'Name' and 'Subject' columns.
- FROM Students: This indicates that we are querying the 'Students' table.
- WHERE Subject = 'Mathematics' OR Subject = 'Physics': This filters the results to include only rows where the 'Subject' column is either 'Mathematics' or 'Physics'. The OR operator ensures that students in either subject are included.
3.
A database field 'Grade' stores the letter grade achieved by a student (e.g., A, B+, C). Explain why it is important to use a lookup table to validate the values entered into the 'Grade' field. Provide an example of how a lookup table would be structured.
It is crucial to use a lookup table to validate the 'Grade' field because it ensures data consistency and prevents invalid grades from being entered. Without a lookup table, users could enter arbitrary text, leading to errors in reporting and analysis. A lookup table enforces a predefined set of valid grades.
Example Lookup Table Structure:
Implementation: The application would query the lookup table to check if the entered 'Grade' value exists in the table. If it doesn't, an error message is displayed, prompting the user to enter a valid grade.