Modifying Database Data - CompEdNet Insert, Update & Delete Modifying Database Data It is important...

6
Computational Thinking Exercises - SET 2 S Q L © Copyright Mr Greg Reid, April 2016 1 Name: ___________________________________ Mark: / 37 Insert, Update & Delete Modifying Database Data It is important that any store of information is accurate and up-to-date. Structured Query Language includes syntax that allows programmers to insert new rows (records), update existing information and delete information from one or more tables in a database. Starting with this table, called “Hardware”, the examples below show how the table can be modified using SQL instructions. Example 1 Example 2 Example 3 The INSERT INTO…VALUES instruction is used to select the name of the table and the columns (fields) into which new data will be added. If a complete new row is being added there is no need to list the columns. INSERT INTO Hardware would work just as well in the example. INSERT INTO Hardware (StockCode, Manufacturer, Category, Price) VALUES (‘MOU667’,’Microsoft’,’Mouse’,35.00); Hardware The UPDATE instruction is used to SET or change data in a database (for example SET Price = 33.5). The correct row or rows are identified using the WHERE command. Note: Without the WHERE identifying one or more rows, every value in the price column would be SET to 33.5. UPDATE Hardware SET Price = 33.5 WHERE StockCode = ‘MOU667’; The DELETE FROM instruction is used to remove a row from a database table. The WHERE part of the instruction is used to identify the one or more rows that will be deleted. Although the example only shows one row being deleted from the database, in a larger table (with more data) every Keyboard manufactured by Dell would be removed from the database. DELETE FROM Hardware WHERE Manufacturer = ‘Dell’ AND Category = ‘Keyboard’;

Transcript of Modifying Database Data - CompEdNet Insert, Update & Delete Modifying Database Data It is important...

Page 1: Modifying Database Data - CompEdNet Insert, Update & Delete Modifying Database Data It is important that any store of information is accurate and up-to-date. Structured Query Language

Computational Thinking Exercises - SET 2

SQL

© Copyright Mr Greg Reid, April 2016 1

Name: ___________________________________ Mark: / 37

Insert, Update & Delete

Modifying Database DataIt is important that any store of information is accurate and up-to-date. Structured Query Language includessyntax that allows programmers to insert new rows (records), update existing information and deleteinformation from one or more tables in a database.

Starting with this table, called“Hardware”, the examples belowshow how the table can be modifiedusing SQL instructions.

Example 1

Example 2

Example 3

The INSERT INTO…VALUESinstruction is used to select the name ofthe table and the columns (fields) intowhich new data will be added.

If a complete new row is being addedthere is no need to list the columns.INSERT INTO Hardwarewould work just as well in the example.

INSERT INTO Hardware (StockCode, Manufacturer, Category, Price)VALUES (‘MOU667’,’Microsoft’,’Mouse’,35.00);

Hardware

The UPDATE instruction is used to SETor change data in a database (forexample SET Price = 33.5). The correctrow or rows are identified using theWHERE command.

Note: Without the WHERE identifyingone or more rows, every value in theprice column would be SET to 33.5.

UPDATE HardwareSET Price = 33.5WHERE StockCode = ‘MOU667’;

The DELETE FROM instruction is usedto remove a row from a database table.

The WHERE part of the instruction isused to identify the one or more rowsthat will be deleted.

Although the example only shows onerow being deleted from the database, ina larger table (with more data) everyKeyboard manufactured by Dell wouldbe removed from the database.

DELETE FROM HardwareWHERE Manufacturer = ‘Dell’ AND Category = ‘Keyboard’;

Page 2: Modifying Database Data - CompEdNet Insert, Update & Delete Modifying Database Data It is important that any store of information is accurate and up-to-date. Structured Query Language

Computational Thinking Exercises - SET 2SQL

2

Problems 1 - What’s Changed?Starting with a table called“Bikes” you must calculate whatSQL instruction has been used tomake the changes shown from theoriginal table.

Bikes

For example______________________________________________________________________________

Modified Bikes

Throughout these exercises you will be awarded 1 mark for every correct line in each SQL instruction.

Each of your answers in the first set of problems should include one INSERT or UPDATE or DELETEinstruction and may involve two or three lines of SQL. Your thinking should be ordered as follows:● work out which type of instruction has been used first (INSERT, UPDATE or DELETE)● then work out what specifically has changed in the table● finally work out how the rows where the changes occurred were identified.

a) What SQL instruction has modified the original Bikestable to create the version shown on the right?

________________________________________ ________________________________________ ________________________________________

b) What SQL instruction has modified the original Bikestable to create the version shown on the right?

________________________________________ ________________________________________ ________________________________________

c) What SQL instruction has modified the original Bikestable to create the version shown on the right?

________________________________________ ________________________________________ ________________________________________

DELETE FROM BikesWHERE Maker = ‘Boardman’;

DELETE FROM BikesWHERE Maker = ‘Raleigh’;

DELETE FROM BikesWHERE Type = ‘Mountain’;

UPDATE BikesSET Gears = 24WHERE Model = ‘MB Comp’;

Page 3: Modifying Database Data - CompEdNet Insert, Update & Delete Modifying Database Data It is important that any store of information is accurate and up-to-date. Structured Query Language

Computational Thinking Exercises - SET 2SQL

3

Bikes

d) What SQL instruction has modified the original Bikestable to create the version shown on the right?

________________________________________ ________________________________________ ________________________________________

e) What SQL instruction has modified the original Bikestable to create the version shown on the right?

________________________________________ ________________________________________ ________________________________________

f) What SQL instruction has modified the original Bikestable to create the version shown on the right?

________________________________________ ________________________________________ ________________________________________

g) What SQL instruction has modified the original Bikestable to create the version shown on the right?

________________________________________ ________________________________________ ________________________________________

INSERT INTO BikesVALUES (‘Raleigh’,‘Hybrid’,‘Edale’,21);

UPDATE BikesSET Gears = 1WHERE Maker = ‘Boardman’;

UPDATE BikesSET Type = ‘MBX’WHERE Type = ‘Mountain’;

DELETE FROM BikesWHERE Gears < 20;

Page 4: Modifying Database Data - CompEdNet Insert, Update & Delete Modifying Database Data It is important that any store of information is accurate and up-to-date. Structured Query Language

Computational Thinking Exercises - SET 2SQL

4

Problems 2 - Show Your WorkingThese questions will again use the “Bikes” table.

The next set of questions will include two or more SQL instructions. You will be expected to note the effectof executing these instructions on a copy of the Bikes table.

For the questions below mark the following on each copy of the bikes table:● for a DELETE instruction score out the identified row/rows● for an UPDATE instruction score out the previous value and write in the new one● for an INSERT instruction write the new values in the space provided

For example:

DELETE FROM BikesWHERE Maker = ‘Boardman’ AND Type = ‘Road’;

UPDATE BikesSET Type = ‘Hybrid’, Model = ‘Duo’WHERE Maker = ‘Carerra’ AND Type = ‘Mountain’;

INSERT INTO BikesVALUES (‘Raleigh’,‘Hybrid’,‘Edale’,21);

Note that a few of these questions include changes that would never actually be made to a ‘Bikes’ database(for example, changing the Maker of a bike because it has a certain number of Gears). Please ignore this asthe questions are designed simply to test your problem solving skills.

a) DELETE FROM Bikes WHERE Type = ‘Road’;

UPDATE Bikes SET Maker = ‘Apollo’, Gears = 16 WHERE Model = ‘Kraken’;

b) UPDATE Bikes SET Maker = ‘Falcon’, Type = ‘Hybrid’ WHERE Model = ‘Cuckoo’;

DELETE FROM Bikes WHERE Type = ‘Hybrid’;

1 mark for each instructionthat is correctly noted onthe above table. (2)

1 mark for each instructionthat is correctly noted onthe above table. (2)

Page 5: Modifying Database Data - CompEdNet Insert, Update & Delete Modifying Database Data It is important that any store of information is accurate and up-to-date. Structured Query Language

Computational Thinking Exercises - SET 2SQL

5

c) INSERT INTO Bikes VALUES (‘Viking’,‘Road’,‘Axe’,27)

UPDATE Bikes SET Type = ‘Road, WHERE Type=‘Hybrid’ AND Gears>20;

DELETE FROM Bikes WHERE Type = ‘Road’ OR Gears > 10;

d) UPDATE Bikes SET Maker = ‘Carerra’ WHERE Model = ‘Raleigh’;

UPDATE Bikes SET Type = ‘Hybrid’ WHERE Gears > 20;

DELETE FROM Bikes WHERE Maker = ‘Carerra’ AND Type = ‘Hybrid’;

e) Note that the symbol <> means ‘notequal to’

UPDATE Bikes SET Type = ‘Road’ WHERE Gears >= 20;

UPDATE Bikes SET Maker = ‘Racer’ WHERE Type = ‘Road’;

DELETE FROM Bikes WHERE Maker <> ‘Racer’;

1 mark for each instructionthat is correctly noted onthe above table. (3)

1 mark for each instructionthat is correctly noted onthe above table. (3)

1 mark for each instructionthat is correctly noted onthe above table. (3)

Page 6: Modifying Database Data - CompEdNet Insert, Update & Delete Modifying Database Data It is important that any store of information is accurate and up-to-date. Structured Query Language

Computational Thinking Exercises - SET 2SQL

6

Problems 3 - Challenging ExampleThese questions will use the“Tickets” table shown on theright and combine everythingyou have learned so far.

a) The SQL instructions below are executed on the above table. Each instruction modifies the table insome way. As you did in the previous set of questions, use the above copy of the “Tickets” table to notethe changes made by each instruction.

UPDATE Tickets SET SeasonTicket = 1488 WHERE Name = ‘Bob’ AND Surname = ‘Mortimer’;

INSERT INTO Tickets VALUES (2655,‘Matthew’,‘Phair’,‘Junior’,#31/03/2030#);

UPDATE Tickets SET SeasonTicket = 1522, Type = ‘Senior’ WHERE Name = ‘Zac’ AND Surname = ‘Allan’;

UPDATE Tickets SET Type = ‘Junior’ WHERE SeasonTicket <= 3000;

DELETE FROM Tickets WHERE SeasonTicket = 2999;

b) A further SQL instruction is used to output a selected listof names from the modified table. Use your notes on theabove “Tickets” table to complete the list of names theSQL instruction would produce as output.

SELECT FirstName, Surname FROM Tickets WHERE Type = ‘Junior’ AND Surname > ‘M’;

Expected OutputFirstName SurnameCameron McKayAnna SmithBlair MaloneBob MortimerMatthew Phair2 marks for all correct,1 mark for 1 error

1 mark for each instructionthat is correctly noted onthe above table. (5)