Database server

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Database server

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does a database server do?
A Displays web pages to users in a browser
B Stores a database and answers queries from other programs
2. A web app needs a student's marks. Which sends the SQL query to the database server?
A The web server (server-side code)
B The user's monitor
3. Which statement is correct about the web server and the database server?
A They are separate roles, even if they sometimes run on the same computer
B They are always exactly the same thing
4. A query SELECT name FROM students WHERE house = 'Red' reaches the database server. What does it send back?
A The whole database with every table
B The rows that match the query
5. Why do many systems keep the database on a database server instead of letting users open the data files directly?
A To protect the stored data and control who can access it
B To make the data public so anyone can edit it

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1SELECT * FROM students WHERE house = 'Red'; AA query sent to the database server, which searches the stored students table and returns only the matching rows
2db_server = "db.school.local" BThe database is stored on the database server, not the web server. The web server only sends queries and uses the results.
3A database server returns query results to the program that asked, such as a web server CUsers do not connect directly to the database server. The web server sends queries on their behalf, which keeps the stored data protected.
4A database server protects stored data by only letting programs with the right login connect DThe address of a database server, kept separate from the web server so the web app knows where to send its queries
5The web server stores the database, so the database server is not needed. Edb.connect(user="webapp", password="s3cret")
6A user's browser connects straight to the database server and reads the tables. Frows = db.execute("SELECT name FROM products")

Definitions

database server

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______ firstName, house FROM students;
2.
_____ SELECT ON students TO web_user;
3.
SELECT name, price FROM ________ WHERE stock = 0;
4.
REVOKE ______ ON students FROM web_user;
5.
SELECT firstName, points FROM students WHERE points > 100 ORDER BY points ____;

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import sqlite3
B db = sqlite3.open('a.db')
C cur = db.cursor()
D cur.execute('SELECT * FROM t')
Question 2
A cur = db.cursor()
B cur.execute('SELECT * FROM p')
C rows = cur.fetchall
D print(rows)
Question 3
A -- sent to the database server
B SELECT name, price,
C FROM products
D WHERE stock > 0;
Question 4
A name = input('Name: ')
B sql = 'SELECT * FROM students'
C sql += ' WHERE firstName = ?'
D cur.execute(sql + name)
Question 5
A Browser requests the page
B Web server queries browser
C DB server returns the rows
D Web server builds the page

Answer Key

Database server
Section A: Quick Questions
Q1
B — Stores a database and answers queries from other programs
Q2
A — The web server (server-side code)
Q3
A — They are separate roles, even if they sometimes run on the same computer
Q4
B — The rows that match the query
Q5
A — To protect the stored data and control who can access it
Section B: Match the Pairs
Q1
SELECT * FROM students WHERE house = 'Red'; → A query sent to the database server, which searches the stored students table and returns only the matching rows
Q2
db_server = "db.school.local" → The address of a database server, kept separate from the web server so the web app knows where to send its queries
Q3
A database server returns query results to the program that asked, such as a web server → rows = db.execute("SELECT name FROM products")
Q4
A database server protects stored data by only letting programs with the right login connect → db.connect(user="webapp", password="s3cret")
Q5
The web server stores the database, so the database server is not needed. → The database is stored on the database server, not the web server. The web server only sends queries and uses the results.
Q6
A user's browser connects straight to the database server and reads the tables. → Users do not connect directly to the database server. The web server sends queries on their behalf, which keeps the stored data protected.
Section C: Definitions
Q1
database server — A computer or program that stores a database and answers queries sent by other programs, such as a web server, by returning the matching data while keeping the stored data protected.
Section D: Fill in the Blanks
Q1
SELECT firstName, house FROM students;
Q2
GRANT SELECT ON students TO web_user;
Q3
SELECT name, price FROM products WHERE stock = 0;
Q4
REVOKE DELETE ON students FROM web_user;
Q5
SELECT firstName, points FROM students WHERE points > 100 ORDER BY points DESC;
Section E: Spot the Error
Q1
B — sqlite3 has no open() function. A program connects to a database with sqlite3.connect('a.db') before it can send queries.
Q2
C — fetchall is missing its brackets, so it is not called. It must be cur.fetchall() to return the query results as rows.
Q3
B — A trailing comma after the last field is a syntax error. The database server cannot run the query, so it returns no results. It should be SELECT name, price.
Q4
D — Joining user input straight into the SQL lets someone inject harmful commands and puts the stored data at risk. Pass the value as a parameter: cur.execute(sql, (name,)).
Q5
B — The web server does not store the data. It sends its query to the separate database server, not to the browser.