Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.

Similar presentations


Presentation on theme: "ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah."— Presentation transcript:

1 ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah | A2-3039 | ext:2561 | sitinurbaya@kedah.uitm.edu.my | 012-5710562 | CHAPTER 7 An Introduction To SQL Lab 1: Simple SQL Queries

2 Objectives 2

3 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 data do some operation at the data user view it or save it in the database 3 SELECT * FROM printer WHERE color = ‘red’ OR color = ‘blue’;

4 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 Note SQL is not case sensitive { SELECT is the same as select } 4 SELECT column_name(s) FROM table_name; SELECT * FROM table_name;

5 DML: Queries: SELECT Statement printerNOdescriptioncolorprice P123Dot-matrixRed249.56 HP874Laser JetBlue559.99 5 Select all data from table printer. Select printerNO and price from table printer. SELECT * FROM printer; SELECT printerNO, price FROM printer; printerNOprice P123249.56 HP874559.99

6 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;

7 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 city FROM staff; staffNOstaffNAMEcity ABC987NadzKuala Lumpur ABC988AinaJerantut ABC999HalimatonJerantut city Kuala Lumpur Jerantut

8 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_name WHERE column_name LIKE patern;

9 SQL LIKE Operator staffNOstaffNAMEcity ABC987NadzKuala Lumpur 9 SELECT * FROM staff WHERE city LIKE “K%”; The following select staffs living in a city start with “K” from table ‘staff’.

10 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 WildcardDescription % 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

11 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 staff WHERE city LIKE "La%"; SELECT * FROM staff WHERE city LIKE "%wi%";

12 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 staff WHERE staffNANE LIKE "_da"; SELECT * FROM staff WHERE staffNANE LIKE "S_ar_s";

13 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 staff WHERE staffNANE LIKE "[asp]%"; SELECT * FROM staff WHERE staffNANE LIKE "[!asp]%";

14 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;

15 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 staff VALUES (“ABC990”, “Shafiza”, “Johor”) ROLLBACK;

16 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. ROLLBACK to undo something. If we use roll-back, the particular changes made are undone. 16

17 DML: Compound Statement with SELECT SymbolMeaning = Equal to < Less than <= Less than or equal to > Greater than >= Greater than or equal to <> Not equal to* *Some SQL version use != 17 – AND to combine two search conditions which must be both true. – OR to 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.

18 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 staff WHERE city=“Merbok” AND age >= 40; SELECT * FROM staff WHERE city=“Penang” OR age >= 40;

19 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 staff WHERE (city=“Merbok” OR city=“Penang”) AND age >= 40;

20 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_name ORDER BY column_name(s) ASC|DESC

21 DML: ORDER BY Keyword The following statement will output staff average salary group by city. 21 staffNOstaffNAMEcitysalarygenderdepartNO ABC987NadzKuala Lumpur2300Male0001 ABC988AinaJerantut2500Female0002 ABC989HalimatonJerantut2200Female0001 ABC990NorainJohor2000Female0003 staff SELECT city, MIN(salary) AS minSALARY FROM staff GROUP BY city ORDER BY city DESC;


Download ppt "ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah."

Similar presentations


Ads by Google