21
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah | A2-3039 | ext:2561 | [email protected] | 012-5710562 | CHAPTER 7 An Introduction To SQL Lab 1: Simple SQL Queries

ITS232 Introduction To Database Management Systems

Embed Size (px)

DESCRIPTION

ITS232 Introduction To Database Management Systems. Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA ( UiTM ), Kedah | A2-3039 | ext:2561 | [email protected] | 012-5710562 |. CHAPTER 7 An Introduction To SQL Lab 1: Simple SQL Queries. - PowerPoint PPT Presentation

Citation preview

Page 1: ITS232 Introduction To Database Management Systems

ITS232Introduction To Database Management Systems

Siti Nurbaya Ismail

Faculty of Computer Science & Mathematics,

Universiti Teknologi MARA (UiTM), Kedah

| A2-3039 | ext:2561 | [email protected] | 012-5710562 |

CHAPTER 7An Introduction To SQL

Lab 1: Simple SQL Queries

Page 2: ITS232 Introduction To Database Management Systems

Objectives

To be able to apply SQL command. How to use DML in SQL

2 major components:1. Data Definition Language (DDL)

defining database structure. allows database objects such as schemas, domains,

tables, views and indexes to be created and destroyed.

2. Data Manipulation Language (DML) retrieving and updating data. used to populate and query the tables. data manipulation.

2

Page 3: ITS232 Introduction To Database Management Systems

Data Manipulation Language(DML)

Data Manipulation in SQL is an ability for manipulating the data to provide information needs

by users.

Manipulating the data referees to process of:selecting the datado some operation at the datauser view it or save it in the database

3

SELECT * FROM printerWHERE color = ‘red’ OR color = ‘blue’;

Page 4: ITS232 Introduction To Database Management Systems

DML: Queries: SELECT Statement

SELECT Statement• The SELECT statement allows you to find, retrieve, and display

data.

• To execute the SELECT statement on a table, you must be the table owner, have DBA or SYSADM security privileges, or have the SELECT privilege for that table.

• The result of SELECT statement is a set of rows known as the result set, which meets the conditions specified in the SELECT statement

SQL SELECT Syntax

NoteSQL is not case sensitive { SELECT is the same as select }

4

SELECT column_name(s)FROM table_name;

SELECT *FROM table_name;

Page 5: ITS232 Introduction To Database Management Systems

DML: Queries: SELECT Statement

printerNO description color price

P123 Dot-matrix Red 249.56

HP874 Laser Jet Blue 559.99

5

Select all data from table printer.

Select printerNO and price from table printer.

SELECT *FROM printer;

SELECT printerNO, priceFROM printer;

printerNO

price

P123 249.56

HP874 559.99

Page 6: ITS232 Introduction To Database Management Systems

DML: Queries: SELECT DISTINCT Statement

SELECT DISTINCT Statement• In a table, some of the columns may contain duplicate

values. • The DISTINCT keyword can be used to return only distinct

(different) values.

SQL SELECT DISTINCT Syntax

6

SELECT DISTINCT column_name(s)FROM table_name;

Page 7: ITS232 Introduction To Database Management Systems

DML: Queries: SELECT DISTINCT Statement

The following statement only will select the distinct values from the column named city from table staff.

7

SELECT DISTINCT cityFROM staff;

staffNO staffNAME city

ABC987 Nadz Kuala Lumpur

ABC988 Aina Jerantut

ABC999 Halimaton Jerantut

city

Kuala Lumpur

Jerantut

Page 8: ITS232 Introduction To Database Management Systems

SQL LIKE Operator

The LIKE operator is used in a WHERE statement to search for a specified pattern in a column.

SQL LIKE Syntax

Note

The LIKE operator is commonly used with SQL Wildcards

8

SELECT column_name(s)FROM table_nameWHERE column_name LIKE patern;

Page 9: ITS232 Introduction To Database Management Systems

SQL LIKE Operator

staffNO staffNAME city

ABC987 Nadz Kuala Lumpur

9

SELECT *FROM staffWHERE city LIKE “K%”;

The following select staffs living in a city start with “K” from table ‘staff’.

Page 10: ITS232 Introduction To Database Management Systems

SQL Wildcards

• SQL wildcards can be used when searching for data in a database.• SQL wildcards can substitute for one or more characters when

searching for data in a database.• SQL wildcards must be used with the SQL LIKE operator.• With SQL, the following wildcards can be used:

10

Wildcard Description

% A substitute for zero or more characters

_ A substitute for exactly one character

[charlist] Any single character in charlist

[^charlist] or[!charlist]

Any single character not in charlist

Page 11: ITS232 Introduction To Database Management Systems

Wildcards: Using The % Wildcard

Select staff living in the city that start with ‘La’ from staff table.

Select staff living in the city that contain pattern ‘wi’ from staff table.

11

SELECT *FROM staffWHERE city LIKE "La%";

SELECT *FROM staffWHERE city LIKE "%wi%";

Page 12: ITS232 Introduction To Database Management Systems

Wildcards: Using The _ Wildcard

Select staff with a name that starts with any character, followed by ‘da’ from staff table.

Select staff with a name that starts with "S", followed by any character, followed by "ar", followed by any character, followed by "s" from staff table.

12

SELECT *FROM staffWHERE staffNANE LIKE "_da";

SELECT *FROM staffWHERE staffNANE LIKE "S_ar_s";

Page 13: ITS232 Introduction To Database Management Systems

Wildcards: Using The [charlist] Wildcard

Select staff with a name that starts with "a" or "s" or "p" from staff table.

Select staff name that do not starts with “"a" or "s" or "p" from staff table.

13

SELECT *FROM staffWHERE staffNANE LIKE "[asp]%";

SELECT *FROM staffWHERE staffNANE LIKE "[!asp]%";

Page 14: ITS232 Introduction To Database Management Systems

DML: COMMIT

The COMMIT statement terminates the current transaction and makes all changes under the transaction persistent.

COMMIT  is used for saving the data that has been changed permanently because whenever you perform any DML like UPDATE, INSERT or DELETE then you are required to write COMMIT at the end of all or every DML operation in order to save it permanently.

If you do not write COMMIT and you program crashes then your data will be restored into its previous condition.

The COMMIT statement has the following general format:

14

UPDATE printer SET indate = ‘15/01/2007' WHERE printerNO = 'F380'; COMMIT;

Page 15: ITS232 Introduction To Database Management Systems

DML: ROLLBACK

The ROLLBACK statement terminates the current transaction and cancel all changes made under the transaction.

ROLLBACK is used if you want to restore your data into its previous condition.

ROLLBACK can be write at any time after the DML queries has been written but remember once COMMIT has been written then you cannot rollback the data.

You can only rollback the DML queries that have been written after the last commit statement.

The ROLLBACK statement has the following general format:

15

INSERT INTO staffVALUES (“ABC990”, “Shafiza”, “Johor”)

ROLLBACK;

Page 16: ITS232 Introduction To Database Management Systems

DML: COMMIT & ROLLBACK

The concept of COMMIT and ROLLBACK is designed for  data consistency

because many uses manipulate data of the same table, using the same database so the user must get updated data.  That is why COMMIT and ROLLBACK are used for.

COMMIT to save whatever has been done. It is used to permanently store it in memory.

ROLLBACKto undo something. If we use roll-back, the particular changes made are undone.

16

Page 17: ITS232 Introduction To Database Management Systems

DML: Compound Statement with SELECT

Symbol

Meaning

= Equal to

< Less than

<= Less than or equal to

> Greater than

>= Greater than or equal to

<> Not equal to*

*Some SQL version use != 17

– ANDto combine two search conditions which must be both true.

– ORto combine two search conditions when one or the other (or both) must be true

You can combine simple conditions with the logical operators

AND, OR, and NOT to form compound conditions.

Page 18: ITS232 Introduction To Database Management Systems

DML: Compound Statement with SELECT

Select only the staff live in Merbok AND age greater then or equal to 40 years old from table staff.

Select only the staff live in Penang OR age greater then or equal to 40 years old from table staff.

18

SELECT *FROM staffWHERE city=“Merbok” AND age >= 40;

SELECT *FROM staffWHERE city=“Penang” OR age >= 40;

Page 19: ITS232 Introduction To Database Management Systems

DML: Compound Statement with SELECT

Select only the staff live in Merbok OR Penang AND age greater then or equal to 40 years old from table staff.

19

SELECT *FROM staffWHERE (city=“Merbok” OR city=“Penang”) AND age >= 40;

Page 20: ITS232 Introduction To Database Management Systems

DML: ORDER BY Keyword

The ORDER BY keyword • is used to sort the result-set by a specified column.• sort the records in ascending order by default.• By default the records are in ascending order or you can used ASC

keyword • If you want to sort the records in a descending order, you can use DESC

keyword.• The default order is ascending. NULL values are treated as larger that

non-null values for sorting purposes.

SQL ORDER BY Syntax

20

SELECT column_name(s)FROM table_nameORDER BY column_name(s) ASC|DESC

Page 21: ITS232 Introduction To Database Management Systems

DML: ORDER BY Keyword

The following statement will output staff average salary group by city.

21

staffNO staffNAME city salary gender departNO

ABC987 Nadz Kuala Lumpur 2300 Male 0001

ABC988 Aina Jerantut 2500 Female 0002

ABC989 Halimaton Jerantut 2200 Female 0001

ABC990 Norain Johor 2000 Female 0003

staff

SELECT city, MIN(salary) AS minSALARYFROM staffGROUP BY cityORDER BY city DESC;