An Introduction To SQL Part 2 (Special thanks to Geoff Leese)

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.
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
Bogdan Shishedjiev SQL1 SQL Reminder of SQL. Bogdan Shishedjiev SQL 2 Subsets of language Data definition language (DDL) –Domain definition –Schema definition.
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.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
1Eyad alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
Writing Basic SQL statement 2 July July July Create By Pantharee Sawasdimongkol.
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.
Copyright  Oracle Corporation, All rights reserved. I Introduction.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
SQL (DDL & DML Commands)
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases &
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
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.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
An Introduction To SQL - Part 1 (Special thanks to Geoff Leese)
SQL.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
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]
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Basic SQL These slides are licensed under.
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.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
9-1 © Prentice Hall, 2007 Topic 9: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich,
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.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Chapter 11: Physical Database Design Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph S. Valacich, Jeffrey A. Hoffer.
CSCI N311: Oracle Database Programming 5-1 Chapter 15: Changing Data: insert, update, delete Insert Rollback Commit Update Delete Insert Statement –Allows.
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.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
Defining a Column Alias
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
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.
Relational Normalization Theory
Aggregating Data Using Group Functions
Enhanced Guide to Oracle 10g
Subqueries.
Aggregating Data Using Group Functions
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
Interacting with the Oracle Server
(SQL) Aggregating Data Using Group Functions
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
(SQL) Manipulating Data
Database Programming Using Oracle 11g
Presentation transcript:

An Introduction To SQL Part 2 (Special thanks to Geoff Leese)

 Recap:  The SQL Data Manipulation Language (DML)has four commands: ◦ SELECT: retrieving data ◦ INSERT: creating data ◦ UPDATE: altering data ◦ DELETE: removing data

 You can display data in a different format: ◦ Show all the department names found in the DEPT table but rename the column with the heading DIFF_DEPT: SELECT DNAME AS DIFF_DEPT FROM DEPT; DIFF_DEPT ACCOUNTING RESEARCH SALES OPERATIONS

 Oracle will display your rows of data in an unordered fashion.  We use the ORDER BY clause to order the rows that are retrieved.  The ORDER BY clause should always be placed last in the query.  The default ordering is ascending ◦ Numeric ascending by order value ◦ Dates chronological order ◦ Charalphabetically ◦ DESC is used to reverse the order

 List all departments in order of their location: SELECT * FROM DEPT ORDER BY LOC; DEPTNO DNAMELOC OPERATIONS BOSTON 30 SALES CHICAGO 20 RESEARCH DALLAS 10 ACCOUNTINGNEW YORK

 Show details of employees in department 10 with the earliest hire dates first: SELECT EMPNO, ENAME, HIREDATE FROM EMP WHERE DEPTNO = 10 ORDER BY HIREDATE; EMPNO ENAMEHIREDATE CLARK KING MILLER

 Show all employees in job order but within each job place in descending order of salary: SELECT ENAME, JOB, SAL, DEPTNO FROM EMP ORDER BY JOB, SAL DESC; ◦ 14 rows will be selected.

SELECT ename, job, sal, comm, comm/sal as ratio,deptno From EMP Where deptno = 30 Order by ratio; ENAME JOB SAL COMM RATIO DEPTNO TURNER SALESMAN ALLEN SALESMAN WARD SALESMAN MARTIN SALESMAN BLAKE MANAGER JAMES CLERK

 Results can be grouped according to a common attribute value. We can find the sum of the salaries by department. SELECT deptno, sum(sal) FROM EMP GROUP BY deptno; DEPTNO SUM(SAL)

 Some simple functions include: ◦ Count: returns a count of rows ◦ Min:returns the lowest value for an attribute ◦ Max:returns the highest value for an attribute ◦ Sum:returns the sum of values ◦ Avg:returns the average value SELECT COUNT(*) FROM EMP; COUNT(*) ◦ RETURNS THE NUMBER OF ROWS IN THE EMP TABLE.

 You can find the highest salary: Select max(sal) from EMP; MAX(SAL) Select min(sal) as min_sal from EMP; MIN_SAL

 Sum is used on numeric functions to return the sum of all the values: ◦ SELECT sum(sal), sum(comm) from EMP; SUM(SAL) SUM(COMM) ◦ SELECT avg(SAL) from EMP; AVG(SAL)

 We can constrain the subgroups that appear by using a HAVING CLAUSE.  We can find the sum of the salaries of each department, but only request those departments with more than 5 employees. SELECT deptno, sum(sal) FROM EMP GROUP BY deptno HAVING count(*)>5; DEPTNO SUM(SAL)

 Useful to put more than one aggregate function on the same row of output.  E.g. “Count the number of people in dept 10 who receive salary, and the number in dept 10 who receive commission” SELECT COUNT(A.SAL) AS COUNTSAL, COUNT(B.COMM) AS COUNTCOMM FROM EMP A, EMP B WHERE A.EMPNO=B.EMPNO AND A.DEPTNO=10;

 Create Table  CREATE TABLE customer as follows: CREATE TABLE customer (customer_id NUMBER(5) NOT NULL, name VARCHAR2(10), house_number NUMBER(2), street VARCHAR2(15), town VARCHAR2(15));  Each column has a name, data type and column width.

 Here’s an example. What’s it doing? CREATE TABLE account (account_numnumber(6), account_namevarchar2(20), branch_namevarchar2(20), date_openeddate, balancenumber(10,2), constraint pk_account_num primary key (account_num)); The Balance column is a decimal number with a maximum Column width of ten digits and a precision of two digits i.e is stored as

 A correct definition of the table ‘customer’ might be: CREATE TABLE customer (customer_id NUMBER(5) NOT NULL, name VARCHAR2(10), house_number NUMBER(2), street VARCHAR2(15), town VARCHAR2(15), constraint pk_customer_id primary key (customer_id));

 The PRIMARY KEY indicates that all values for these columns must be unique.  NOT NULL is used to designate that the column must have a value for all its rows.

 Customer and account has a many to many relationship, we can resolve this with a table constraint by creating a composite primary key. CREATE table customer_account (customer_id number(5) NOT NULL, account_num number(6) NOT NULL, constraint pk_cust_account PRIMARY KEY (customer_id, account_num));

 In the CUSTOMER_ACCOUNT table that we have just created we have two foreign keys CUSTOMER_ID and ACCOUNT_NUM. We can specify these after creating the table: ALTER table customer_account ADD (constraint fk_customer_id FOREIGN KEY (customer_id) REFERENCES customer (customer_id)); ALTER table customer_account ADD (constraint fk_account_num FOREIGN KEY(account_num) REFERENCES account(account_num));

 DROP <object name  DROP TABLE EMP; (NOT undoable!)  ALTER object name> ADD /DROP COLUMN  ALTER TABLE EMP  ADD nickname VARCHAR(30);

 We use the SQL command INSERT to create rows. INSERT INTO customer VALUES(12345, 'DAWES', 21, 'BACK LANE', 'STAFFORD'); ◦ Remember non-numerics require quotation marks around their values. ◦ These values have been entered into the Customer table in the order in which the columns appeared in the original create table statement.

 Use the DELETE command to remove data from a relation (i.e. table). DELETE FROM customer WHERE customer_id = 12345;  Use the UPDATE command to alter the data held in the rows. i.e. Change the name in the account table from Dawes to Dalby. UPDATE customer SET name = 'DALBY' WHERE customer_id = 12345;

 You have worked through some simple SQL expressions and have learnt how to : ◦ Use the SELECT command for retrieving data. ◦ Use INSERT, UPDATE, AND DELETE commands to insert, amend and delete data. ◦ Use aggregate functions to assist with processing data, i.e. COUNT, MIN, MAX, SUM, AVG. ◦ Use DDL to create, alter and drop tables. ◦ Create some simple constraints using the WHERE clause. ◦ Use the GROUP BY, HAVING and ORDER BY functions to assist with selecting and displaying data. ◦ Create simple primary and foreign key constraints.

 Rolland chapter 5  Patrick chapters 1-3, 9-11, 13  SQL at w3schools - click to follow the link SQL at w3schools - click to follow the link