10 Database and file concepts (3)

Resources | Revision Questions | Information Technology IT

Login to see all questions

Click on a question to view the answer

1.

Explain how the choice of data type for a date field (e.g., DateOfRegistration) can impact the accuracy of calculations and comparisons. Provide examples of potential issues that could arise if an inappropriate data type is used and suggest the correct data type to avoid these issues.

2.

Question 2

You are designing a database for a library. The books table stores information about books with columns book_id (integer, primary key), title (text), author (text), and publication_year (integer). The members table stores information about library members with columns member_id (integer, primary key), member_name (text), and borrowed_books (text). Write SQL queries to:

  1. Create the books table.
  2. Delete a book from the books table based on its book_id.
  3. Update the author of a book.

Question 3

A university is creating a database to manage student records. The students table contains columns for student_id (integer, primary key), student_name (text), course_id (integer, foreign key referencing the courses table), and grade (text). The courses table contains columns for course_id (integer, primary key) and course_name (text). Write SQL queries to:

  1. Create the students table.
  2. Update the course a student is enrolled in.
  3. Delete a student from the students table if they are no longer enrolled in any courses.

Question 3 Answer:

1. Create the students table:


CREATE TABLE students (
    student_id INT PRIMARY KEY,
    student_name VARCHAR(255),
    course_id INT,
    grade VARCHAR(2)
);
  

2. Update the course a student is enrolled in:


UPDATE students
SET course_id = 456
WHERE student_id = 789;
  

3. Delete a student from the students table if they are no longer enrolled in any courses:

This requires a more complex query involving a subquery to identify students not in the courses table. Here's an example:


DELETE FROM students
WHERE student_id IN (SELECT student_id FROM students WHERE student_id NOT IN (SELECT student_id FROM courses));
  
```
3.

Consider a database for a university. Students can enroll in multiple courses, and each course can have multiple students enrolled. Describe the database relationship that should be used to represent this situation. Explain how you would implement this relationship in the database schema, including the roles of primary and foreign keys.