1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen 2002-2008 Introduction to Relational Databases &

Slides:



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

1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Natural Joins These slides are licensed.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
9-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language A DML statement is executed when you: – Add new rows to a table.
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.
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.
ISD3 Lecture 4 - Databases, SQL and MySQL. dept deptno dname location emp empno ename not null job not null hiredate sal comm manager The EMP DEPT database.
System Administration Accounts privileges, users and roles
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
Database System Concepts and Architecture Lecture # 3 22 June 2012 National University of Computer and Emerging Sciences.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Objects & Databases These.
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.
RDB/1 An introduction to RDBMS Objectives –To learn about the history and future direction of the SQL standard –To get an overall appreciation of a modern.
Dr. Philip Cannata 1 Programming Languages Prolog Part 3 SQL & Prolog.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
MSc IT UFIE8K-15-M Data Management Prakash Chatterjee Room 3P16
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
Subqueries.
Subqueries.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Copyright  Oracle Corporation, All rights reserved. 4 Accessing a Database Using JBCL.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
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.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
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.
8 Producing Readable Output with SQL*Plus. 8-2 Objectives At the end of this lesson, you should be able to: Produce queries that require an input variable.
Copyright  Oracle Corporation, All rights reserved. Introduction.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Implementing The Middle Tier These slides.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
Copyright  Oracle Corporation, All rights reserved. 8 Producing Readable Output with SQL*Plus.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Relational State Assertions These slides.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Oracle CONNECT BY function JAVA WEB Programming. Emp 테이블의 내용 ( 상 / 하급자 계층구조 ) SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
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.
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Inner Joins These slides are licensed.
SQL Introduction to database and SQL. Chapter 1: Databases and Database Users 6 Introduction to Databases Databases touch all aspects of our lives. Examples:
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
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
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Manipulating Data Schedule: Timing Topic 40 minutes Lecture
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
(SQL) Manipulating Data
SQL .. An overview lecture3.
Lecture 16 : The Relational Data Model
Subqueries Schedule: Timing Topic 25 minutes Lecture
Restricting and Sorting Data
Lecture 16 : The Relational Data Model
Subqueries Schedule: Timing Topic 25 minutes Lecture
Copyright © Ellis Cohen
Presentation transcript:

1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Introduction to Relational Databases & SQL These slides are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. For more information on how you may use them, please see

2© Ellis Cohen Overview of Lecture Overview of Databases Introduction to SQL Oracle SQL Tools

3© Ellis Cohen Overview of Databases

4© Ellis Cohen What's a Database Persistent Structured Information Repository Provides –API ( Application Programming Interface ) –Protocol –Language (SQL for Relational DB's) for Storing & Retrieving Information Built-in Support for –Security & Access Control –Constraints & Triggers –Transactions –Performance Tuning

5© Ellis Cohen Client/Server Architecture Database Server DB Application APIAPI Server-Side Client-Side Implements DB Operations User DB ClientDB Application Client SQL Statements are passed through API & protocol API includes: executeQuery( sqlstr )

6© Ellis Cohen Data in Relational Databases is Stored in Tables (also known as Relations) empno ename sal comm Employees 7499ALLEN MARTIN BLAKE KING TURNER STERN1500 rows, tuples, records columns, attributes, fields

7© Ellis Cohen Relational Database Applications Typical relational database applications use multiple tables For example, a company project management database application might use tables for –Employees –Departments –Projects –Assignments –etc.

8© Ellis Cohen SQL Structured Query Language for Relational DB's SELECT empno, ename FROM Employees WHERE sal < 2000 ORDER BY empno EMPNO ENAME ALLEN 7654 MARTIN 7844 TURNER 7986 STERN SQL Query (using SELECT command) Other SQL commands are used to create, modify & manage the data in the database

9© Ellis Cohen Database Features Security & Access Control –Allows control over which users can access which information Constraints & Triggers –Allows database to automatically take actions based on changes it monitors Transactions –Makes it possible to ensure related changes are all made together, or not at all (atomicity) –When operation is completed, changes are actually stored persistently (durability) –Ensures concurrent users cannot "step on each other's toes" (isolation) Performance Tuning –Allows control over aspects of how information is stored, and how storage and retrieval operations are executed

10© Ellis Cohen Introduction to SQL

11© Ellis Cohen Emps Table empno ename job hiredate sal comm SMITH CLERK 17-DEC ALLEN SALESMAN 20-FEB WARD SALESMAN 22-FEB JONES DEPTMGR 02-APR MARTIN SALESMAN 28-SEP BLAKE DEPTMGR 01-MAY CLARK DEPTMGR 09-JUN SCOTT ANALYST 19-APR KING PRESIDENT 17-NOV TURNER SALESMAN 08-SEP ADAMS CLERK 23-MAY JAMES CLERK 03-DEC FORD ANALYST 03-DEC MILLER CLERK 23-JAN Primary Key

12© Ellis Cohen SQL Queries SELECT ename FROM Emps WHERE empno = 7499 SELECT empno FROM Emps WHERE ename = 'ALLEN' SELECT empno, ename FROM Emps WHERE sal > 2975 ORDER BY ename ENAME ALLEN EMPNO ENAME FORD 7839 KING 7788SCOTT EMPNO Note symmetry of lookups Query Result Query

13© Ellis Cohen Ordering Relational Database tables are not intrinsically ordered in any way. SELECT empno, ename FROM Emps (which generates the employee number and name of every employee in the table) may come out in one order today and a different one tomorrow (if the DBA reorganizes the database) You MUST use ORDER BY if you want your results to come out in a specific order: SELECT empno, ename FROM Emps ORDER BY ename

14© Ellis Cohen Boolean Expressions SELECT empno, ename FROM Emps WHERE (sal > 1200) AND (sal 200) What will this generate? < less than<= less than or equal > greater than>= greater than or equal = equal<> != not equal

15© Ellis Cohen Boolean Expressions Answer SELECT empno, ename, sal FROM Emps WHERE (sal > 1200) AND (sal 200) EMPNO ENAME SAL WARD MARTIN 1250

16© Ellis Cohen Basic Query Parts SELECT empno, ename FROM Emps WHERE sal > 2000 ORDER BY ename 2. Projection 1. Restriction 2. Ordering * It is possible to order query results by attributes which are not projected. However, it is also possible to define and name computed attributes, and then order the results based on them

17© Ellis Cohen SQL Exercise Write SQL to query Emps( empno, ename, job, hiredate) List the employee #, employee name and job of all non-clerks hired after Sort the output by job; within employees with the same job, sort by employee name Note: this represents a date; not just a year!

18© Ellis Cohen SQL Exercise Answer SELECT empno, ename, job FROM Emps WHERE hiredate > '31-DEC-91' AND job <> 'CLERK' ORDER BY job, ename Sorts by job, and then, within employees with the same job, sorts by employee name When the Emps table was created, hiredate was specified to be a DATE, so Oracle knows to automatically convert '31-DEC-91' to a date hiredate >= '1-JAN-92' would work as well This is the default date format; it can be changed Assumes every employee's job is specified, otherwise this doesn't necessarily work

19© Ellis Cohen SQL Updates and Deletes UPDATE Emps SET sal = 2000, comm = 100 WHERE sal = 0 UPDATE Emps SET sal = sal WHERE job = 'DEPTMGR' DELETE FROM Emps WHERE sal = 0

20© Ellis Cohen SQL A language for dealing with tables –DML: Data Manipulation Language Querying: Extracting data from tables Modification: Inserting, updating, deleting rows in a tables –DDL: Data Definition Language Defining new tables, views (VDL), etc. Altering & dropping old ones –DCL: Data Control Language Security: Access Control Transaction Mgt Performance (e.g. Indexing (SDL))

21© Ellis Cohen Query-Based Create & Insert CREATE TABLE RichEmps AS SELECT empno, ename FROM Emps WHERE sal > 3000 INSERT INTO RichEmps SELECT empno, ename FROM Emps WHERE (sal > 2000) AND (sal 200) Emps RichEmps empno, ename Example DDL command

22© Ellis Cohen SQL Database Operations: Queries & Actions SQL DB Operations QueriesSQL Queries Actions SQL Insert/Update/Delete SQL DDL (e.g. create table) SQL DCL (e.g. grant access)

23© Ellis Cohen History of Standard SQL SQL-89 (SQL1) –First standard version of SQL –Based on IBM's SQL SQL-92 (SQL2) [primary focus of CS579] –Added major extensions –Basis of all major commercial RDB SQLs SQL-99 (SQL3) –Adds Programmability and OO extensions –Not generally implemented Oracle: PL/SQL, Oracle OO extensions SQL Server: Transact-SQL

24© Ellis Cohen Oracle SQL Tools

25© Ellis Cohen Connect to SQL*Plus 1) type scott 2) type tiger 3) click OK

26© Ellis Cohen SQL*Plus Example SQL> set linesize 125 SQL> set pagesize 1000 SQL> select empno, ename, sal, comm 2 from emp where deptno <> 20 3 order by sal; EMPNO ENAME SAL COMM JAMES WARD MARTIN MILLER TURNER ALLEN CLARK BLAKE KING rows selected. SQL> Start with these SQL*Plus set commands prompts End SQL command with ; Query Result also called the Result Set

27© Ellis Cohen Oracle SQL Developer Download from Click here to define a new connection

28© Ellis Cohen Create a Connection for SCOTT Fill in SCOTT as the connection and user name The password is TIGER The SID is the name of the database you used when you installed Oracle 1. Fill in the all the fields 2.

29© Ellis Cohen After SCOTT is Connected Open SCOTT

30© Ellis Cohen Connection Elements Look at SCOTT's Tables

31© Ellis Cohen SCOTT's Tables View the definition of the Emps table

32© Ellis Cohen The Emps Table Definition Look at the Data in the table

33© Ellis Cohen The Emps Table Data

34© Ellis Cohen Tools & Client-Side Access SQL*Plus (Oracle) Database Server Client-side Server-side Oracle SQL Developer DB Application API Library Passes SQL to Database Server Understands SQL*Plus Commands Implements database operations