1 / 21

ITS232 Introduction To Database Management Systems

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.

Download Presentation

ITS232 Introduction To Database Management Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITS232Introduction To Database Management Systems SitiNurbaya Ismail Faculty of Computer Science & Mathematics, UniversitiTeknologi 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 To be able to apply SQL command. • How to use DML in SQL 2 major components: • Data Definition Language (DDL) • defining database structure. • allows database objects such as schemas, domains, tables, views and indexes to be created and destroyed. • Data Manipulation Language (DML) • retrieving and updating data. • used to populate and query the tables. • data manipulation.

  3. Data Manipulation Language(DML) • Data Manipulation in SQL is an ability for manipulating the datato 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 SELECT * FROM printer WHERE color = ‘red’ OR color = ‘blue’;

  4. DML: Queries: SELECT Statement SELECT Statement • The SELECTstatement 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 theSELECTprivilege for that table. • The result of SELECTstatement 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 } SELECT column_name(s) FROM table_name; SELECT * FROM table_name;

  5. Select all data from table printer. Select printerNO and price from table printer. DML: Queries: SELECT Statement SELECT * FROM printer; SELECT printerNO, price FROM printer;

  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 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. SELECT DISTINCT city FROM staff;

  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 SELECT column_name(s) FROM table_name WHERE column_name LIKE patern;

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

  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:

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

  14. DML: COMMIT TheCOMMITstatement 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: 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: 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.

  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 DML: Compound Statement with SELECT 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 MerbokAND 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. 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. 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 SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC

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

More Related