15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.

Slides:



Advertisements
Similar presentations
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Advertisements

LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
Database Systems and Design
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Introduction to Structured Query Language (SQL)
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
Ceng 356-Lab1. Objectives After completing this lesson, you should be able to do the following: Get Familiar with the development environment List the.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
1 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
SQL (DDL & DML Commands)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
9/29/2005From Introduction to Oracle:SQL and PL/SQL, Oracle 1 Restricting and Sorting Data Kroenke, Chapter Two.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Copyright © 2004, Oracle. All rights reserved. M ANIPULATING D ATA.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Limiting Selected Rows. 2-2 Objectives Sort row output using the ORDER BY clause. Sort row output using the ORDER BY clause. Enter search criteria using.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
1 Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
Restricting and Sorting Data
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Retrieving Data Using the SQL SELECT Statement
Writing Basic SQL SELECT Statements
Basic select statement
ATS Application Programming: Java Programming
Manipulating Data.
Restricting and Sorting Data
Restricting and Sorting Data
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
Manipulating Data.
Restricting and Sorting Data
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Restricting and Sorting Data
Presentation transcript:

15 Structured Query Language (SQL)

2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and its purpose Create and execute a basic SELECT statement Create and execute a basic INSERT statement Create and execute a basic UPDATE statement Create and execute a basic DELETE statement Execute COMMIT and ROLLBACK on transaction

3 What is SQL? SQL (Structured Query Language) is a standard interactive programming language for getting information from and updating to a database. Statements take the form of a command language that lets you: SELECT data INSERT data UPDATE data DELETE data

4 Guidelines in writing SQL Statements SQL statements are not case-sensitive SQL statements can be on one or more lines SQL statements are optionally ended with “;” Keywords cannot be abbreviated or split across lines Clauses are usually placed on separate lines Indents are used to enhance readability Keywords are typically entered in uppercase; all other words such as table name and columns are entered in lower case

5 Basic SELECT Statement Use the SELECT statement to retrieve data from one or more tables: SELECT FROM [WHERE ] [ORDER BY ] tableis the name of the table columnis the name of the column in the table to be selected conditionidentifies the rows to be selected and is composed of column names, expressions, constraints, sub-queries and comparison operators columnis the name of the column(s) used for sorting (order by)

6 Choosing Columns To choose all the columns of the table for display, you can use the asterisk (*) SELECT * FROM TABLE_A To choose specific columns on the table for display, you specify each column separated by a comma (,) SELECT COLUMN_1, COLUMN_2 FROM TABLE_A It is best to put each column chosen in a separate line

7 Limiting Rows The method of restriction is the basis of the WHERE clause in SQL Character strings and dates in the WHERE clause must be enclosed in Single Quotation Marks (‘) Numeric Values do not need the Single Quotation marks(‘)

8 Rows may be limited by: EQUALS CONDITION Display rows based on an exact match of values. SELECT last_name, salary FROM employee WHERE salary = SELECT employee_id, last_name FROM employee WHERE manager_name = ‘RAYMOND’

9 Rows may be limited by: >, CONDITION SELECT last_name FROM employee WHERE salary > SELECT employee_id FROM employee WHERE manager_name <= ‘RAYMOND’ SELECT employee_id FROM employee WHERE status <> ‘ACTIVE’

10 Rows may be limited by: BETWEEN CONDITION Display rows based on a range of values SELECT last_name FROM employee WHERE salary BETWEEN AND IN CONDITION Display rows based on a list of values SELECT employee_id FROM employee WHERE manager_id IN (100, 200, 300)

11 Rows may be limited by: LIKE CONDITION Perform wildcard searches of valid search string values Can contain either literal characters or numbers % denotes zero or many characters _ denotes one character Use ESCAPE identifier to search for the actual % and _symbols. Identifies the backslash(\) as the escape character SELECT last_name FROM employee WHERE last_name LIKE ‘%a’

12 Rows may be limited by: LOGICAL CONDITION AND, OR, NOT SELECT last_name, job_id FROM employee WHERE job_id NOT IN ('SSE', 'TL') SELECT last_name, job_id FROM employee WHERE salary NOT between AND 15000

13 Rows may be limited by: LOGICAL CONDITION AND, OR, NOT SELECT last_name, job_id FROM employee WHERE last_name NOT LIKE 'A%' AND last_name NOT LIKE 'B%‘ SELECT last_name, job_id FROM employee WHERE commission_pct IS NOT NULL

14 Sorting Rows ORDER BY clause ASC specifies an ascending order DESC specifies a descending order SELECT last_name, salary, job_id FROM employee ORDER BY salary DESC, job_id ASC Display the result in descending order by the attribute salary. If two records have the same attribute value, the salary sorting criteria is in ascending order according to the attribute values of job_id

15 Basic INSERT Statement INSERT INTO [ (column [, column…] ) ] VALUES(value [, value…] ) tableis the name of the table columnis the name of the column in the table to populate valueis the corresponding value for the column Note: This statement with the VALUES clause adds only one row at a time to a table.

16 Basic INSERT Statement Insert a new row containing values for each column List values in the default order of the columns in the table Option: list the columns in the INSERT clause Enclose character and date values within single quotation marks INSERT INTO departments (department_id, department_name, current_date) VALUES (70, ‘Public Relations’, ’10-OCT-04’)

17 Inserting Rows from Another Table Write your INSERT statement with a subquery Do not use the VALUES clause Match the number of columns in the INSERT clause to those in the subquery INSERT INTOsales_reps (id, name, salary) SELECT employee_id, last_name, salary FROMemployees WHERE job_id LIKE ‘%REP%’

18 Basic UPDATE Statement UPDATEtable SETcolumn = value [, column = value, …] [WHEREcondition] table is the name of the table columnname of the column in the table to populate valuecorresponding value for the column conditionidentifies the rows to be updated and is composed of column names, expressions, constraints, sub-queries, and comparison operators

19 Updating Rows in a Table Specific row or rows are modified if you specify the WHERE clause All rows in the table are modified if you omit the WHERE clause UPDATE employees SETdepartment_id = 70 WHERE employee_id = 113

20 Updating Rows Based on Another Table Use subqueries in UPDATE statements to update rows in a table based on values from another table UPDATEcopy_emp SETdepartment_id = (SELECT department_id FROM employees WHERE emp_id =100) WHEREjob_id =(SELECT job_id FROM employees WHERE emp_id = 200)

21 Basic DELETE Statement DELETE[FROM] table [WHEREcondition] ; table is the name of the table conditionidentifies the rows to be updated and is composed of column names, expressions, constraints, sub-queries, and comparison operators

22 Deleting Rows in a Table A specific row or specific rows are deleted if you specify the WHERE clause All rows in the table are deleted if you omit the WHERE clause DELETE FROM employees WHERE employee_id = 113 ;

23 Deleting Rows Based on Another Table Use subqueries in DELETE statements to delete rows in a table based on values from another table DELETE FROM employees WHERE department_id = (SELECT department_id FROM departments WHEREdept_type = ‘CST’)

24 What is a Transaction? A transaction usually means a sequence of information exchange and related work (such as database updating) that is treated as a unit for the purposes of satisfying a request and for ensuring database integrity For a transaction to be completed and database changes to be made permanent, a transaction has to be completed in its entirety. A program that manages or oversees the sequence of events that are part of a transaction is sometimes called a transaction monitor Transactions are supported by SQL. When a transaction completes successfully, database changes are said to be committed; when a transaction does not complete, changes are rolled back

25 Transaction Control Transaction Control Statements COMMIT ROLLBACK

26 COMMIT Use the COMMIT statement to end your current transaction and make permanent all changes performed in the transaction If you do not explicitly commit the transaction and the program terminates abnormally, then the last uncommitted transaction is automatically rolled back SYNTAX: COMMIT;

27 ROLLBACK Use the ROLLBACK statement to undo work done in the current transaction, or to manually undo the work done by an in-doubt distributed transaction SYNTAX: ROLLBACK;

28 Key Points SQL is an industry standard language for updating, and getting information from, a database The basic and most common SQL statements are: SELECT, INSERT, UPDATE, DELETE Transaction management is implemented in SQL using COMMIT and ROLLBACK