SQL. Relating Multiple Tables Relational Database Terminology Row PK Column FK Field NULL.

Slides:



Advertisements
Similar presentations
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
2 Restricting and Sorting Data Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query Sort the rows retrieved by a query.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
Copyright  Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
1Eyad alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
6-1 Copyright  Oracle Corporation, All rights reserved. Types of Subqueries Single-row subquery Main query Subquery returns CLERK Multiple-row subquery.
Multiple-Column Subqueries. Objectives After completing this lesson, you should be able to do the following: Write a Multiple-column subquery Describe.
Copyright س Oracle Corporation, All rights reserved. 7 Multiple-Column Subqueries.
Copyright  Oracle Corporation, All rights reserved. 7 Multiple-Column Subqueries.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
Session 3: SQL (B): Parts 3 & 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
o At the end of this lesson, you will be able to:  Describe the life-cycle development phases  Discuss the theoretical and physical aspects of a relational.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Functions Oracle Labs 5 & 6. 2/3/2005Adapted from Introduction to Oracle: SQL and PL/SQL 2 SQL Functions Function arg n arg 2 arg 1. Input Resulting Value.
Subqueries.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
Subqueries.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Copyright  Oracle Corporation, All rights reserved. 4 Introduction.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
SQL: Part 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
Review SQL Advanced. Capabilities of SQL SELECT Statements Selection Projection Table 1 Table 2 Table 1 Join.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
Single-Row Functions. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
Defining a Column Alias
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
Multiple-Column Subqueries
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
مقدمة في قواعد البيانات
Index Note: A bolded number or letter refers to an entire lesson or appendix. A Adding Data Through a View ADD_MONTHS Function 03-22, 03-23,
Contents Preface I Introduction Lesson Objectives I-2
Subqueries Schedule: Timing Topic 25 minutes Lecture
Review SQL Advanced.
Restricting and Sorting Data
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

SQL

Relating Multiple Tables

Relational Database Terminology Row PK Column FK Field NULL

Communicating with an RBMS Using SQL

SQL Statements Data Manipulation Language –SELECT –INSERT –UPDATE –DELETE Data Definition Language –CREATE –ALTER –DROP –RENAME –TRUNCATE Data Control Language –GRANT –REVOKE Transaction Control –COMMIT –ROLLBACK –SAVEPOINT

INSERT INTOtable [(column [, column...])] VALUES(value [, value...]); DML - INSERT Insert into MK Values (‘ ’,’SBD’3); Insert into MK(sks,nm_mk) Values (2,’Pancasila’); Insert into MK Select kd_mk,nm_mk,sks From MK1; Insert into MK(kd_mk,sks,nm_mk) Select * From MK1;

DML - UPDATE UPDATEtable SETcolumn = value [, column = value,...] [WHERE condition]; Update MK Set nm_mk = ‘SBD 1’ Where kd_mk = ‘ ’; Update MK Set sks = 4 Where kd_mk like ‘41010%’ ; Update MK Set sks = (Select sks From MK Where kd_mk = ‘ ’) Where nm_mk like ‘Akuntansi%’

DML - DELETE DELETE [FROM] table [WHERE condition]; Delete From MK Where sks = 4; Delete MK Where nm_mk = ‘SBD’ and kd_mk like ‘4%’; Delete MK Where sks = (Select sks From MK Where kd_mk = ‘ ’) and kd_mk like ‘41010%’

Capabilities of SQL SELECT Statements Selection Projection Join

Basic SELECT Statement SELECT[DISTINCT] {*, column [alias],...} FROMtable; –SELECT identifies what columns. –FROM identifies which table.

SELECT

ALIASES

LITERAL

Limiting Rows Selected –Restrict the rows returned by using the WHERE clause. –The WHERE clause follows the FROM clause. SELECT[DISTINCT] {*| column [alias],...} FROM table [WHEREcondition(s)] [ORDER BY{column, expr, alias} [ASC|DESC]];

Comparison Conditions

Logical Conditions

Using the WHERE Clause SQL> SELECT ename, job, deptno 2 FROM emp 3 WHERE job='CLERK'; ENAME JOB DEPTNO JAMES CLERK 30 SMITH CLERK 20 ADAMS CLERK 20 MILLER CLERK 10

Rules of Precedence ENAME JOB SAL KING PRESIDENT 5000 MARTIN SALESMAN 1250 ALLEN SALESMAN 1600 TURNER SALESMAN 1500 WARD SALESMAN 1250 SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE job='SALESMAN' 4 OR job='PRESIDENT' 5 AND sal>1500;

Rules of Precedence ENAME JOB SAL KING PRESIDENT 5000 ALLEN SALESMAN 1600 SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE (job='SALESMAN' 4 OR job='PRESIDENT') 5 AND sal>1500;

Using the ORDER BY Clause

Sorting

Two Types of SQL Functions Functions Single-rowfunctions Multiple-rowfunctions

Single-Row Functions Conversion Character Number Date General Single-rowfunctions

Character Functions

Function Result Case Conversion Functions Convert case for character strings LOWER( ' SQL Course ' ) UPPER( ' SQL Course ' ) INITCAP( ' SQL Course ' ) LOWER( ' SQL Course ' ) UPPER( ' SQL Course ' ) INITCAP( ' SQL Course ' ) sql course SQL COURSE Sql Course sql course SQL COURSE Sql Course

Character Manipulation Functions Manipulate character strings

Number Function

Using Date Functions

Explicit Datatype Conversion

DECODE Function Facilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE statement DECODE(col/expression, search1, result1 [, search2, result2,...,] [, default])

Using Group Functions SELECT[column,] group_function(column) FROMtable [WHEREcondition] [GROUP BYcolumn] HAVINGgroup_condition] [ORDER BYcolumn]; –AVG –COUNT –MAX –MIN –STDDEV –SUM –VARIANCE Group functions operate on sets of rows to give one result per group.

What Is a Join? Use a join to query data from more than one table. –Write the join condition in the WHERE clause. –Prefix the column name with the table name when the same column name appears in more than one table. SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2;

Types of Joins

Subqueries –The subquery (inner query) executes once before the main query. –The result of the subquery is used by the main query (outer query). SELECTselect_list FROMtable WHEREexpr operator (SELECTselect_list FROMtable);

SQL> SELECT a.ename, a.sal, a.deptno, b.salavg 2 FROM emp a, (SELECT deptno, avg(sal) salavg 3 FROM emp 4 GROUP BY deptno) b 5 WHERE a.deptno = b.deptno 6 AND a.sal > b.salavg; Using a Subquery in the FROM Clause ENAME SAL DEPTNO SALAVG KING JONES SCOTT rows selected.

CREATE TABLE [schema.]table (column datatype [DEFAULT expr] [column_constraint],... [table_constraint][,...]); Creating Table CREATE [OR REPLACE] [FORCE|NOFORCE] VIEW view [(alias[, alias]...)] AS subquery [WITH CHECK OPTION [CONSTRAINT constraint]] [WITH READ ONLY]; Creating View