PG 9
PG 9
Solution: The four main CRUD operations are Create, Read, Update, and Delete.
2. Understanding
Question: Can you explain how the INSERT SQL command works in the context of adding new
data to a database?
Solution: The INSERT SQL command adds new records to a database table. It specifies the table
name and the values for each column that will be included in the new record. For example,
INSERT INTO users (username, email) VALUES ('john_doe', '[email protected]')
adds a new user with a username and email.
3. Applying
Question: How would you implement a function in PHP to fetch all users from a database?
Solution: To fetch all users from a database in PHP, you would create a function that prepares
and executes a SELECT statement, and then fetches the results. For example:
php
Copy code
function fetchAllUsers($pdo) {
$stmt = $pdo->prepare("SELECT * FROM users");
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
4. Analyzing
Question: What are the potential risks of using unprepared SQL statements in a web
application?
Solution: The potential risks include SQL injection attacks, where an attacker can manipulate
the SQL query to access or modify the database in unauthorized ways. This can lead to data
breaches, loss of data integrity, and unauthorized access to sensitive information.
5. Evaluating
Question: How would you assess the effectiveness of using transactions in managing data
updates?
Solution: To assess the effectiveness of using transactions, you could analyze how they prevent
data inconsistency during complex operations. For instance, by grouping multiple UPDATE
statements into a single transaction, you ensure that either all updates are applied successfully, or
none at all, thus maintaining data integrity.
6. Creating
Question: Design a simple database schema for a library management system that includes
tables for books, authors, and borrowers. Describe the relationships between these tables.
Solution: The database schema could include three tables:
Relationships:
The Books table has a foreign key author_id that references the Authors table,
indicating that each book is written by an author.
The Borrowers table has a foreign key borrowed_book_id that references the Books
table, indicating which book is borrowed by a borrower.
These questions can help foster critical thinking and deeper understanding of CRUD operations
in PHP and MySQL, following the principles of Bloom's Taxonomy.