Information Technology Structured Query Language Grade11.

23
Information Technology Information Technology Structured Query Structured Query Language Language Grade11 Grade11

Transcript of Information Technology Structured Query Language Grade11.

Page 1: Information Technology Structured Query Language Grade11.

Information TechnologyInformation Technology

Structured Query LanguageStructured Query Language

Grade11Grade11

Page 2: Information Technology Structured Query Language Grade11.

SQL …SQL … is used to communicate with a database. is used to communicate with a database.

SQLSQL statements are used to perform tasks such as statements are used to perform tasks such as

Updating dataUpdating data in a databasein a database

It is the standard language for relational database management systems. It is the standard language for relational database management systems.

Creating data Creating data within a databasewithin a database

Reading or retrieving Reading or retrieving data from a databasedata from a database

Deleting data Deleting data from a databasefrom a database

That’s right Dain - the CRUDThat’s right Dain - the CRUD

Page 3: Information Technology Structured Query Language Grade11.

Introduction continued…Introduction continued…

Some common relational database management systems that use SQL are:Some common relational database management systems that use SQL are:

MS SQL ServerMS SQL Server

AccessAccess

The industry standard The industry standard SQLSQL commands are: commands are:

"Select", "Insert", "Update", "Delete", "Create", and "Drop" "Select", "Insert", "Update", "Delete", "Create", and "Drop"

Page 4: Information Technology Structured Query Language Grade11.

Table Basics

contains one or more objects called tables. contains one or more objects called tables.

The data or information for the database are stored in these tables. The data or information for the database are stored in these tables.

Weather.tbl

city province high low

Durban KZN 32 28

KwaMashu KZN 27 22

PE EC 18 12

Cape Town NC 17 11

London London 22 18

A relational database system…A relational database system…

Page 5: Information Technology Structured Query Language Grade11.

Selecting DataSelecting Data

The The selectselect statement is used to “query the database” and retrieve selected data… statement is used to “query the database” and retrieve selected data…

SELECTSELECT "column1" "column1" [,"column2",etc] [,"column2",etc] FROMFROM "tablename" "tablename" [[WHEREWHERE "condition"]; "condition"]; [ ] = optional [ ] = optional

Conditional selections require a Conditional selections require a wherewhere clause: clause:

……that match the criteria that you specify.”that match the criteria that you specify.”

NB.: The column names that follow the select keyword

determine which columns will be returned in the results. You

can select as many column names that you'd like, or you

can use a "*" to select all columns.

Page 6: Information Technology Structured Query Language Grade11.

The Optional WHERE Clause…The Optional WHERE Clause…

The The WHEREWHERE clause specifies which data values ( clause specifies which data values (rowsrows) will be returned/ displayed.) will be returned/ displayed.

==EqualEqual

>> Greater thanGreater than

<< Less thanLess than

>=>= Greater than or equalGreater than or equal

<=<= Less than or equalLess than or equal

<><> Not equal toNot equal to

LIKELIKE Compared to/ withCompared to/ with

Lets’ looka little closer at Lets’ looka little closer at LIKE...LIKE...

Every Every WHEREWHERE clause requires a Boolean expression/ function : clause requires a Boolean expression/ function :

Page 7: Information Technology Structured Query Language Grade11.

The percent sign "%" can be used as a “The percent sign "%" can be used as a “wild cardwild card” to match any possible character ” to match any possible character that might appear…that might appear…

SELECTSELECT first, last, city from empInfo first, last, city from empInfo WHERE WHERE first first LIKE LIKE 'Er%'; 'Er%'; ““StringsStrings” must be ” must be in in

single quotes. single quotes. Or you can specify,Or you can specify,

SELECTSELECT first, last from empInfo first, last from empInfo WHERE WHERE last last LIKE LIKE '%s'; '%s'; last names last names

that end in a 's'. that end in a 's'.

SELECTSELECT * from empInfo * from empInfo WHEREWHERE first = 'Eric'; first = 'Eric';

This will only select rows where the first name equals 'Eric' exactly.This will only select rows where the first name equals 'Eric' exactly.

LIKELIKE allows you to select only rows that are " allows you to select only rows that are " likelike" what you specify." what you specify.

beforebefore or or afterafter the characters specified. the characters specified.

Page 8: Information Technology Structured Query Language Grade11.

Now you try…Now you try…

Sample Table: empInfo

first last id age city province

John Jones 56123 45 Durban KZN

Mary Jones 34212 25 PE EC

Eric Edwards 88232 32 Senegal NC

Mary Ann Edwards 88233 32 Phoenix KZN

Ginger Howell 98002 42 Chatsworth KZN

Sebastian Smith 92001 23 Gila Bend G

Gus Gray 22322 35 Bagdad Polo

Mary Ann May 32326 52 Terror Cape

Erica Williams 32327 60 Sholo EC

Leroy Brown 32380 22 Pinetop NC

Elroy Cleaver 32382 22 Durban Cape

1.1. SELECTSELECT first, last, city first, last, city FROMFROM empInfo; empInfo; 2.2. SELECTSELECT last, city, age last, city, age FROMFROM empInfo empInfo WHERE WHERE age > 30; age > 30; 3.3. SELECTSELECT first, last, city, province first, last, city, province FROMFROM empinfo empinfo WHEREWHERE first first LIKELIKE 'J%'; 'J%'; 4.4. SELECTSELECT * from empInfo; select first, last, * from empInfo; select first, last, FROMFROM empinfo empinfo WHEREWHERE last last LIKELIKE '%s'; '%s'; 5.5. SELECTSELECT first, last, age first, last, age FROMFROM empInfoempInfo WHEREWHERE last last LIKELIKE '%illia%'; '%illia%'; 6.6. SELECTSELECT * * FROMFROM empInfoempInfo WHEREWHERE first = 'Eric'; first = 'Eric';

What will these output?What will these output?

Page 9: Information Technology Structured Query Language Grade11.

Now theNow the reversereverse……

1.1. Display the first name and age for everyone that's in the table. Display the first name and age for everyone that's in the table.

2.2. Display the first name, last name, and city for everyone that's not from Display the first name, last name, and city for everyone that's not from Bulowayo. Bulowayo.

3.3. Display all columns for everyone that is over 40 years old. Display all columns for everyone that is over 40 years old.

4.4. Display the first and last names for everyone whose last name ends in an Display the first and last names for everyone whose last name ends in an "ay". "ay".

5.5. Display all columns for everyone whose first name equals "Mary". Display all columns for everyone whose first name equals "Mary".

6.6. Display all columns for everyone whose first name contains "Mary". Display all columns for everyone whose first name contains "Mary".

Complete the following SELECT Statement exerciseComplete the following SELECT Statement exercise - -

Page 10: Information Technology Structured Query Language Grade11.

Now check your answers…Now check your answers…

Display everyone's first name and their age for everyone that's in table.Display everyone's first name and their age for everyone that's in table. SELECT first, age FROM empInfo; SELECT first, age FROM empInfo;

Display the first name, last name, and city for everyone that's not from Bulowayo.Display the first name, last name, and city for everyone that's not from Bulowayo. SELECT first, last, city FROM empInfo WHERE city <> ‘Bulowayo';SELECT first, last, city FROM empInfo WHERE city <> ‘Bulowayo';

Display all columns for everyone that is over 40 years old.Display all columns for everyone that is over 40 years old. SELECT * FROM empInfo WHERE age > 40; SELECT * FROM empInfo WHERE age > 40;

Display the first and last names for everyone whose last name ends in an "ay".Display the first and last names for everyone whose last name ends in an "ay". SELECT first, last FROM empInfo WHERE last LIKE '%ay'; SELECT first, last FROM empInfo WHERE last LIKE '%ay';

Display all columns for everyone whose first name equals "Mary".Display all columns for everyone whose first name equals "Mary". SELECT * FROM empInfo WHERE first = 'Mary'; SELECT * FROM empInfo WHERE first = 'Mary';

Display all columns for everyone whose first name contains "Mary".Display all columns for everyone whose first name contains "Mary". SELECT * FROM empInfo WHERE first LIKE '%Mary%'SELECT * FROM empInfo WHERE first LIKE '%Mary%'

Page 11: Information Technology Structured Query Language Grade11.

Inserting into a TableInserting into a Table

To insert records into a table, the key words To insert records into a table, the key words insert intoinsert into is used…followed by is used…followed by the keyword the keyword values…values…

insert intoinsert into "tablename" (first_column,...last_column) values "tablename" (first_column,...last_column) values (first_value,...last_value); (first_value,...last_value);

The The insertinsert statement is used to insert or add a row of data into the table. statement is used to insert or add a row of data into the table.

Example:Example:

insert intoinsert into employee (first, last, age, address, city, state) employee (first, last, age, address, city, state) valuesvalues ('Luke', ('Luke', 'Duke', 45, '2130 Boars Hoares', 'Hazard Co', ‘Phoenix'); 'Duke', 45, '2130 Boars Hoares', 'Hazard Co', ‘Phoenix');

Note:Note: All strings should be enclosed between All strings should be enclosed between singlesingle quotes: quotes: 'string'string''

Page 12: Information Technology Structured Query Language Grade11.

Insert Insert statementstatement exercise… exercise…

It is time to insert data into your new “employee table”.It is time to insert data into your new “employee table”. Your first three employees are the following:Your first three employees are the following:

Jonie Weber, Secretary, 28, 19500.00Jonie Weber, Secretary, 28, 19500.00Potsy Weber, Programmer, 32, 45300.00Potsy Weber, Programmer, 32, 45300.00Dirk Smirk, Programmer II, 45, 75020.00Dirk Smirk, Programmer II, 45, 75020.00

Now insert at 5 more of your own list of employees in the table.Now insert at 5 more of your own list of employees in the table.

Next, enter select statements to:Next, enter select statements to:

1.1. Select all columns for everyone in your employee table. Select all columns for everyone in your employee table. 2.2. Select all columns for everyone with a salary over 30000. Select all columns for everyone with a salary over 30000. 3.3. Select first and last names for everyone that's under 30 years old. Select first and last names for everyone that's under 30 years old. 4.4. Select first name, last name, and salary for anyone with "Programmer" in their title. Select first name, last name, and salary for anyone with "Programmer" in their title. 5.5. Select all columns for everyone whose last name contains "ebe". Select all columns for everyone whose last name contains "ebe". 6.6. Select the first name for everyone whose first name equals "Potsy". Select the first name for everyone whose first name equals "Potsy". 7.7. Select all columns for everyone over 80 years old. Select all columns for everyone over 80 years old. 8.8. Select all columns for everyone whose last name ends in "ith". Select all columns for everyone whose last name ends in "ith".

Page 13: Information Technology Structured Query Language Grade11.

Updating Records Updating Records The The updateupdate statement is used to update or change records that match a statement is used to update or change records that match a

specified criteria. This is accomplished by carefully constructing a where specified criteria. This is accomplished by carefully constructing a where clause.clause.

updateupdate "tablename" "tablename" setset "columnname" = "newvalue" [,"nextcolumn" = "columnname" = "newvalue" [,"nextcolumn" = "newvalue2"...] "newvalue2"...] wherewhere "columnname" "columnname" OPERATOROPERATOR "value" [and|or "column" "value" [and|or "column" OPERATOROPERATOR "value"]; "value"]; [ ] = optional [ ] = optional

Examples: update phone_book set area_code = 623 where prefix = 979; update phone_book set last_name = 'Smith', prefix=555, suffix=9292 where

last_name = 'Jones'; update employee set age = age+1 where first_name='Mary' and

last_name='Williams';

Update statement exercises

Page 14: Information Technology Structured Query Language Grade11.

After each update, issue a select statement to verify your After each update, issue a select statement to verify your changes.changes.

1.1. Jonie Weber just got married to Bob Williams. She has requested that her Jonie Weber just got married to Bob Williams. She has requested that her last name be updated to Weber-Williams. last name be updated to Weber-Williams.

2.2. Dirk Smith's birthday is today, add 1 to his age. Dirk Smith's birthday is today, add 1 to his age.

3.3. All secretaries are now called "Administrative Assistant". Update all titles All secretaries are now called "Administrative Assistant". Update all titles accordingly. accordingly.

4.4. Everyone that's making under 30000 are to receive a 3500 a year raise. Everyone that's making under 30000 are to receive a 3500 a year raise.

5.5. Everyone that's making over 33500 are to receive a 4500 a year raise. Everyone that's making over 33500 are to receive a 4500 a year raise.

6.6. All "Programmer II" titles are now promoted to "Programmer III". All "Programmer II" titles are now promoted to "Programmer III".

7.7. All "Programmer" titles are now promoted to "Programmer II". All "Programmer" titles are now promoted to "Programmer II".

Page 15: Information Technology Structured Query Language Grade11.

Updating Records Answers:Updating Records Answers:

Jonie Weber just got married to Bob Williams. She has requested that her last name be updated Jonie Weber just got married to Bob Williams. She has requested that her last name be updated to Weber-Williams.to Weber-Williams.

updateupdate myemployees myemployees setset lastname= 'Weber-Williams' lastname= 'Weber-Williams' wherewhere firstname= 'Jonie' and firstname= 'Jonie' andlastname= 'Weber'; lastname= 'Weber';

Dirk Smith's birthday is today, add 1 to his age.Dirk Smith's birthday is today, add 1 to his age.updateupdate myemployees myemployees setset age=age+1 age=age+1 wherewhere firstname='Dirk' and lastname='Smith'; firstname='Dirk' and lastname='Smith';

All secretaries are now called "Administrative Assistant". Update all titles accordingly.All secretaries are now called "Administrative Assistant". Update all titles accordingly.updateupdate myemployees myemployees setset title = 'Administrative Assistant' title = 'Administrative Assistant' wherewhere title = 'Secretary'; title = 'Secretary';

Everyone that's making under 30000 are to receive a 3500 a year raise.Everyone that's making under 30000 are to receive a 3500 a year raise. updateupdate myemployees myemployees setset salary = salary + 3500 salary = salary + 3500 wherewhere salary < 30000; salary < 30000;

Everyone dat’s making over 33500 are to receive a 4500 a year raise.Everyone dat’s making over 33500 are to receive a 4500 a year raise. updateupdate myemployees myemployees setset salary = salary + 4500 salary = salary + 4500 wherewhere salary > 33500; salary > 33500;

All "Programmer II" titles are now promoted to "Programmer III".All "Programmer II" titles are now promoted to "Programmer III". updateupdate myemployees myemployees setset title = 'Programmer III' title = 'Programmer III' wherewhere title = 'Programmer II' title = 'Programmer II'

All "Programmer" titles are now promoted to "Programmer II".All "Programmer" titles are now promoted to "Programmer II". updateupdate myemployees myemployees setset title = 'Programmer II' title = 'Programmer II' wherewhere title = 'Programmer' title = 'Programmer'

Page 16: Information Technology Structured Query Language Grade11.

Deleting Records

Examples:Examples:

deletedelete fromfrom employee; employee; Note:Note: if you leave off the where clause, if you leave off the where clause, all records will be deleted!all records will be deleted!

deletedelete fromfrom employee employee wherewhere lastname = 'May'; lastname = 'May';

deletedelete fromfrom employee where firstname = 'Mike' or firstname = 'Eric'; employee where firstname = 'Mike' or firstname = 'Eric';

The delete statement is used to delete records or rows from the table.

delete from "tablename“ where "columnname" OPERATOR "value" [and|or "column" OPERATOR "value"];

[ ] = optional

Delete statement exercisesDelete statement exercises

Page 17: Information Technology Structured Query Language Grade11.

Use the select statement to verify your deletes Use the select statement to verify your deletes

1.1. Jonie Weber-Williams just quit, remove her record from the table. Jonie Weber-Williams just quit, remove her record from the table.

2.2. It's time for budget cuts. Remove all employees who are making over It's time for budget cuts. Remove all employees who are making over 70000 dollars. 70000 dollars.

Page 18: Information Technology Structured Query Language Grade11.

Deleting Records Answers:Deleting Records Answers:

Jonie Weber-Williams just quit, remove her record from the table:Jonie Weber-Williams just quit, remove her record from the table:

deletedelete from myemployees from myemployees wherewhere lastname = 'Weber-Williams'; lastname = 'Weber-Williams';

It's time for budget cuts. Remove all employees who are making over 70000 It's time for budget cuts. Remove all employees who are making over 70000 dollars.dollars.

deletedelete from myemployees from myemployees wherewhere salary > 70000; salary > 70000;

Page 19: Information Technology Structured Query Language Grade11.

Drop a Table Drop a Table

To delete an entire table including all of its rows, issue the To delete an entire table including all of its rows, issue the drop tabledrop table command followed by the tablename. command followed by the tablename.

drop table "tablename" drop table "tablename"

Example:Example:

dropdrop tabletable myemployees; myemployees;

The The drop tabledrop table command is used to delete a table and all rows in the table. command is used to delete a table and all rows in the table.

End of Lesson 1 on two more to go…SQLCourse2.com

Page 20: Information Technology Structured Query Language Grade11.
Page 21: Information Technology Structured Query Language Grade11.
Page 22: Information Technology Structured Query Language Grade11.
Page 23: Information Technology Structured Query Language Grade11.