Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL queries Data Maintenance. RHS – SOC 2 Data maintenance In addition to asking question to the database (queries), we can also maintain the data itself.

Similar presentations


Presentation on theme: "SQL queries Data Maintenance. RHS – SOC 2 Data maintenance In addition to asking question to the database (queries), we can also maintain the data itself."— Presentation transcript:

1 SQL queries Data Maintenance

2 RHS – SOC 2 Data maintenance In addition to asking question to the database (queries), we can also maintain the data itself (insert, update, delete) More of a ”command” than a query… –INSERT – adds new rows of data –UPDATE – modifies existing data –DELETE – removes rows of existing data

3 RHS – SOC 3 INSERT Simple functionality – insert a row of data into a specified table: INSERT INTO VALUES ( )

4 RHS – SOC 4 INSERT Example: INSERT INTO Movie VALUES (8, ’Memento’, ’USA’, 2000, ’Thriller’, 0)

5 RHS – SOC 5 INSERT Things to notice about INSERT –The value list must match the field list for the table into which the record is inserted –If we try to insert a record with a key field which already exists, we will get an error –Null values can be inserted if the table definition allows it –The field list can be specified explicitly (see notes)

6 RHS – SOC 6 UPDATE Updates the value(s) for specified field(s), for the rows mathcing a given condition UPDATE SET field1 = value1, field2 = value2,… WHERE

7 RHS – SOC 7 UPDATE Example: UPDATE Movie SET prodYear = 2001 WHERE (title = ’Memento’)

8 RHS – SOC 8 UPDATE Things to notice about UPDATE –For each field update, the type of the value must match the type of the field –The WHERE clause is optional – if you leave it out, all records in the table are updated! –It is not considered an error if zero rows are changed, so pay attention to the condition in the WHERE clause…

9 RHS – SOC 9 DELETE Deletes all the rows mathcing a given condition : DELETE FROM WHERE

10 RHS – SOC 10 DELETE Example: DELETE FROM Movie WHERE (title = ’Memento’)

11 RHS – SOC 11 DELETE Things to notice about DELETE –No need to specify a field list –The WHERE clause is optional – if you leave it out, all records in the table are deleted! –It is not considered an error if zero rows are deleted, so pay attention to the condition in the WHERE clause –Double-check your WHERE clause

12 RHS – SOC 12 Exercise 8 – SQL queries Use the MovieInformation database, defined in exercise 1 Before starting, take a backup of the MovieInformation database… With the data in place, run the below commands on the database –INSERT INTO Actor VALUES (8, ’John Hurt’, ’UK’, '01/22/1940', ’Yes’, 0) –INSERT INTO Casting VALUES (6,8) –UPDATE Movie SET prodYear = prodYear + 1 –DELETE FROM Casting WHERE (actorId = 8) Now formulate commands yourself, in order to: –Insert data about a movie called ”Willow” in the Movie table (you can find the data on the Internet, or make it up yourself) –Insert data representing the fact that Jean Reno appeared in Willow –Update the name of the movie ”Seven” to ”Se7en” –Update the number of Oscars won by actors from France by one –Delete all movies with the character ’7’ in the title (watch the condition!!)

13 RHS – SOC 13 Creating tables It is even possible to create new tables using SQL Possible, but we will usually do it through the DBMS GUI anyway… Complete syntax for creating a new table is rather complicated

14 RHS – SOC 14 Creating tables Creating a table (simplified): CREATE TABLE Director ( PRIMARY KEY )

15 RHS – SOC 15 Creating tables Each field definition contains –The field name –The data type for the field –Can the field be NULL or not –Must the field be unique –Does the field have a default value

16 RHS – SOC 16 Creating tables Example (simplified): CREATE TABLE Director ( directorIdnumeric(3)NOT NULL, namevarchar(30)NOT NULL, countryvarchar(20)NOT NULL, borndateNOT NULL, PRIMARY KEY (directorId))

17 RHS – SOC 17 Exercise 9 – SQL queries Use the MovieInformation database, defined in exercise 1 Create a new table in the MovieInformation database called MovieStudio, with the following fields: –MovieStudio identifier –Name of the studio –Founder of the studio –Year the studio was founded –Number of employees –City of studio headquarter –Country of studio headquarter –Is the studio still producing movies Consider how the rest of the database should be changed, in order to include information about movie studios


Download ppt "SQL queries Data Maintenance. RHS – SOC 2 Data maintenance In addition to asking question to the database (queries), we can also maintain the data itself."

Similar presentations


Ads by Google