ExamBank Scot · AH Computing Science question bank · All AH Computing Science questions

AH Computing Science 2017 Question 2 (CfE)

15 marks · Data dictionaries

The developers begin by designing an SQL table called RegUser. This table will be used to store the details of all of the registered users. The table will be stored in a relational database called RestaurantApp. The RegUser table will store a userID which is automatically created, the user’s title, first name and last name, a phone number for contact purposes and a password. Users will be asked to provide all of these details when they register. This data will be captured using the HTML form shown below. Title: First Name:* Last Name:* Contact Number:* Password:* New User Sign-Up Submit Clear The userID will be generated automatically by the database server when a new record is added to the table. Data values must be provided for all fields marked *. Copy and complete the two lines of the data dictionary below to show the structure of the SQL table called RegUser for the fields listed. Your data dictionary should indicate appropriate SQL data types. Field Type/Size Key Constraints/Validation userID title The HTML script used to generate the registration form is provided below. <form action="signmeup.php" method="post"> <h3>New User Sign-Up</h3> Title: <br><input name="Item1" type="text" /><br> First Name:* <br><input name="Item2" type="text" /><br> Last Name:* <br><input name="Item3" type="text" /><br> Contact Number:* <br><input name="Item4" type="text" /><br> Password:* <br><input name="Item5" type="password" /><br> <input name="Submit" type="submit" value="Submit" /> <input name="Clear" type="reset" value="Clear" /> </form> The submit button on the form is used to submit the registration data to a server-side script. This script is used to connect to the database server and add the registration details to the RegUser table of the RestaurantApp database. The RegUser table has 6 fields: userID, title, firstName, lastName, contactNo and password. The connection details used are: server name: sn001 user: anon001 password: ap001 Using a server-side scripting language with which you are familiar, write the script used to connect to the database server. The script should: assign the registration details to server-side variables create a connection to the database server execute the SQL query used to add the registration details to the RegUser table. Details of restaurant reservations will be stored in the relational database in a separate table called Reservation. Part of the Reservation table is shown below. bookingID restaurant ID regUserID date time sizeofgroup 123456 1 1 01/07/2017 18:00 4 123457 1 2 01/07/2017 18:00 2 123458 2 4 01/07/2017 18:30 2 123459 3 9 01/07/2017 18:15 5 123460 2 8 01/07/2017 19:30 2 123461 1 5 01/07/2017 19:30 6 Relationships RegUser: Reservation 1:M The manager of each restaurant needs a list of reservations made for the day. Write an SQL statement to display a list of all reservations for restaurant 1 on the 1st of July 2017. The list should show the first name, last name and title for each reservation, the time of the reservation and the size of each group. The reservation details should be displayed so that the earliest reservation for the restaurant is listed first. At the end of each month, the data captured from the app is exported to a program which is used by management to make decisions about stock levels. Within the program, a function is used to arrange the restaurant details by overall monthly sales so that restaurants in the chain are listed in ascending order. This function makes use of the quicksort algorithm below. 1. set the pivot to the first element in the array 2. partition the array into 3 separate sub-arrays: LEFT sub-array: contains all elements less than the pivot PIVOT sub-array: contains the pivot itself RIGHT sub-array: contains all elements greater than or equal to the pivot 3. repeatedly apply the quicksort algorithm to the LEFT and the RIGHT sub-arrays until each sub-array contains exactly one or zero elements 4. concatenate the sub-arrays from left to right, ignoring any zero-element sub-arrays The following array of nine elements is used to test the quicksort algorithm: [ 112,233 212,121 132,457 45,678 108,793 61,212 45,212 112,345 234,512 ] Steps 1 and 2 of the algorithm are applied to the array for the first time, and at step 3, the quicksort algorithm is applied to the LEFT sub-array. The following situation exists: LEFT sub-array [ 45,678 108,793 61,212 45,212 ] LEFT-LEFT [ 45,212 ] PIVOT RIGHT sub-array [ 112,233 ] [ 212,121 132,457 112,345 234,512 ] PIVOT LEFT-RIGHT [ 108,793 61,212 ] [ 45,678 ] LEFT-RIGHT-LEFT [ 61,212 ] PIVOT LEFT-RIGHT-RIGHT [ 108,793 ] [ ] Show how the quicksort algorithm would be applied to the RIGHT sub-array. A restaurant chain wants a new app which will allow registered users to make reservations for restaurants that are part of the chain. All users must register before they can use the app for the first time. A development team is asked to create the new app.

Marking scheme

Complete data dictionary is shown below. 1 mark for correct data types (with appropriate sizes) for both fields 1 mark for correct PK 1 mark for constraints for title (restricted choice) with appropriate constraint for userID Field Type/Size Key Constraints / Validation userID NUMBER or INTEGER PK Not null, auto increment (unique) title VARCHAR (4) or CHAR(4) One of Mr, Mrs, Miss, Ms PHP solution // Assign variables $title = $_POST['Item1']; $firstname = $_POST['Item2']; $lastname = $_POST['Item3']; $phone = $_POST['Item4']; $password = $_POST['Item5']; 1 mark for correct assignment of variables using HTML attributes and POST method // Create connection $servername = "sn001"; $username = "anon001"; $password = "ap001; $dbname="RestaurantApp"; $conn = mysql_connect($servername, $username, $password); Note: use of mysqli_connect requires 4 parameters ($dbname must be included) 1 mark for correct use made of all three connection details (note that database is not necessary for my_sql connection code) 1 mark for correct connection code // Execute query $query = "INSERT INTO RegUser (Title, FirstName, LastName, PhoneNo, Password) VALUES ('$Title','$firstname', '$lastname', '$phone', '$password'); mysqli_query($conn, $query); 1 mark for correct INSERT INTO query with 5 fields named (values must match the named sequence) 1 mark for correct use of database fields (named in part (a) answer) with matching server-side variables 1 mark for correct code use to execute SQL query SELECT title, firstname, lastname, time, sizeofparty FROM RegUser, Reservation WHERE RegUser.userID = Reservation.regUserID AND date="07/01/2017" AND restaurantID=1 ORDER BY time ASC; 1 mark each for correct – • SELECT and FROM clauses with join • criteria used in WHERE clause (date AND restaurantID) • ORDER BY clause Note: INNER JOIN may be used as alternative to equi-join eg SELECT … FROM regUser INNER JOIN reservation ON regUser.userID = reservation.regUserID WHERE … ORDER BY … RIGHT-LEFT [ 132,457 112,345 ] PIVOT [ 212,121 ] RIGHT-RIGHT [ 234,512 ] RIGHT-LEFT-LEFT [ 112,345 ] PIVOT [ 132,457 ] RIGHT-LEFT-RIGHT [ ] 1 mark for correct left sub-arrays 1 mark for correct pivots 1 mark for correct right sub-arrays RIGHT-LEFT PIVOT RIGHT-RIGHT [132,457 112,345] [ 212,121 ] [ 234,512] RIGHT-LEFT-LEFT PIVOT RIGHT-LEFT-RIGHT [ 112,345] [ 132,457 ] [ ]

Previous question: AH Computing Science 2017 Question 1 (CfE)

Next question: AH Computing Science 2017 Question 3 (CfE)