640 likes | 753 Views
IT 20303. The Relational DBMS Section 07. Relational Database Theory. Using a Relational DBMS. Relational Database Theory. Data Types Depends on the conventions used by a particular database ORACLE uses: NUMBER CHAR - Characters VARCHAR2 - Characters DATE/TIME LOB.
E N D
IT 20303 • The Relational DBMS • Section 07
Relational Database Theory • Using a Relational DBMS
Relational Database Theory • Data Types • Depends on the conventions used by a particular database • ORACLE uses: • NUMBER • CHAR - Characters • VARCHAR2 - Characters • DATE/TIME • LOB
Relational Database Theory • NUMBER • Numerical data • Guaranteed to 38 digits accuracy • NUMBER(10) – 10 digits allowed • CHAR • Character data • Fixed-Length up to 2,000 bytes • Good for 2 or 3 characters • Y/N, T/F, USA/CAN • CHAR(2) – 2 characters allowed
Relational Database Theory • VARCHAR2 • Character data • Variable-Length up to 4,000 bytes • VARCHAR2(15) – up to 15 characters • DATE/TIME • Date & Time data • DATE – DD-MON-YY (Default) • TIME – HH:MN:SE (Default)
Relational Database Theory • LOB • Large OBject data type • CLOB • Long variable length characters • BLOB • Binary objects – Video, Sound, Graphics • BFILE • Reference to an OS file • Up to 4GB of data per file
Relational Database Theory • SQL: Structured Query Language • Command-driven, nonprocedural language • Origin: IBM, System R, Mid-1970s, SEQUEL (SQL) • Adopted by Oracle in late 1970s • Before IBM product • State of the art (state of the art does not mean ideal)
Relational Database Theory • SQL Standards • Major standards organizations work together • American National Standards Institute (ANSI) • International Organization for Standardization (ISO)
Relational Database Theory • SQL Standards • More than 25 approved and current standards • SQL language: verbs, concepts, clauses, operations • Embedding SQL in host language • Persistent stored modules (stored procedures) • SQL routines using JAVA • On-Line analytical processing
Relational Database Theory • Structured Query Language • DDL – Data Definition Language • CREATE • DROP • ALTER
Relational Database Theory • Structured Query Language • DML – Data Manipulation Language • SELECT • INSERT • DELETE • UPDATE
Relational Database Theory • Structured Query Language • DCL – Data Control Language • GRANT • REVOKE
Relational Database Theory • Characteristics of SQL • Capabilities to • Define database objects – DDL • Tables, Views, Indexes, Users, Constraints • Manipulate Data – DML • Query capability based on relational algebra set operations • Update capability • Control Data - DCL
Relational Database Theory • Characteristics of SQL • Intended for use by • DBA • Application Developers • Some End Users • SQL is easily grasped, but not quickly mastered (Croft, 1990)
Relational Database Theory • Example: DEPT table EMPLOYEE Supervises DEPT Works for DEPARTMENT
Relational Database Theory • Example: EMP table EMP
Relational Database Theory • Table Definition: ANSI SQL • CREATE TABLE DEPT • (deptno NUMBER(2) NOT NULL, • dname VARCHAR2(14), • loc VARCHAR2(13) DEFAULT ‘Key West’);
Relational Database Theory • Table Definition: ANSI SQL • CREATE TABLE EMP • (empno NUMBER(4) NOT NULL, • ename VARCHAR2(10), • job VARCHAR2(10), • mgr NUMBER(4), • hiredate DATE, • sal NUMBER (7,2), • comm NUMBER (7,2), • deptno NUMBER(2) NOT NULL);
Relational Database Theory • Data Definition Statements • Define Table structure and constraints • CREATE TABLE makes entries in the data dictionary • Table Name • Attributes • Name, Datatype, Size, Nullability • Constraints • Primary Key • Foreign Key • Check Clause • ALTER TABLE updates the data dictionary • Adds or modifies attributes • Adds or modifies constraints • DROP TABLE removes entries from the data dictionary
Relational Database Theory • The System Tables • Also called data dictionary, catalog • System tables are tables – just like the data tables • For example: • user_tables • user_constraints • user_indexes
Relational Database Theory • The System Tables • System tables are queried by DBAs and Users via SQL • DBAs can write procedures, reports using system tables • Graphical DBA tools generate SQL to access the dictionary
Relational Database Theory • The System Tables • RDBMS accesses the system tables on every SQL request • Updated by DDL statements • Referenced on DML statements
Relational Database Theory • SQL: SELECT is used for all queries • SELECT Data Items • FROM Table(s) • WHERE Condition is True
Relational Database Theory • Manipulation: SELECT Example • Example 01: • SELECT * • FROM EMP • WHERE job = ‘ANALYST’; • Results
Relational Database Theory • Manipulation: SELECT Example • Example 02: • SELECT ename, job, hiredate • FROM EMP • WHERE hiredate > ’31-DEC-99’; • Results
Relational Database Theory • Manipulation: SELECT Example • Example 03: • SELECT empno, ename, loc • FROM EMP, DEPT • WHERE emp.deptno = dept.deptno • AND loc = ‘CHICAGO’; • Results
Relational Database Theory • Manipulation: SELECT Example • Class Practice 01: • Give me a list of all the salesman and their managers? • SELECT ename, job, mgr • FROM EMP • WHERE job = ‘SALESMAN’;
Relational Database Theory • Manipulation: SELECT Example • Class Practice 02: • Give me a list of all clerks to include their employee number, manager, hiredate and salary for those who make more than $1000? • SELECT empno, ename, mgr, hiredate, sal • FROM emp • WHERE job = ‘CLERK’ AND sal > 1000;
Relational Database Theory • Manipulation: SELECT Example • Class Practice 03: • Give me a list of employees who work in the accounting department including their salaries? • SELECT ename, sal, dname • FROM emp, dept • WHERE emp.deptno = dept.deptno • AND deptno = 10;
Relational Database Theory • SELECT is used to Read Data • User “Designs” the query • Expresses it according to SQL syntax • One SELECT query can request multiple relational algebra operations • SELECT …. which columns • FROM …. which tables • WHERE …. which rows • Each SELECT query is optimized • Transformed into a query execution plan
Relational Database Theory • SQL: SELECT…ORDER BY • SELECT Data Items • FROM Table(s) • WHERE Condition is True • ORDER BY Data Item [DESC | ASC] • Presents the result set in sorted order
Relational Database Theory • SQL: SELECT…ORDER BY • Example: • SELECT empno, ename, sal • FROM emp • WHERE sal > 2500 • ORDER BY sal;
Relational Database Theory • SELECT can present Derived Values • List all the analysts, showing their annual compensation • SELECT empno, ename, 12*sal • FROM emp • WHERE job = ‘ANALYST’;
Relational Database Theory • SQL Subqueries • Suppose we want to find out who makes more money than Jones • We could first find out Jones’ salary • Then find all employees whose salary is greater than that • Alternatively…SQL allows nesting subqueries
Relational Database Theory • SQL Subqueries • Example: • SELECT empno, ename • FROM emp • WHERE sal > • (SELECT sal • FROM emp • WHERE ename = ‘JONES’); • The subquery is executed first
Relational Database Theory • Updating the Database • INSERT • Append new rows to a table • Example: • INSERT INTO dept (deptno, dname, loc) • VALUES (40, ‘OPERATIONS’, ‘BOSTON’);
Relational Database Theory • Updating the Database • DELETE • Removes one or more rows from a table • Example: • DELETE FROM dept • WHERE loc = ‘DETROIT’;
Relational Database Theory • Updating the Database • UPDATE • Replaces values in one or more columns • Example: • UPDATE emp • SET sal = sal * 1.10 • WHERE ename = ‘MILLER’;
Relational Database Theory • SQL Query and Manipulation Summery (ANSI Standard) • Reading • SELECT • Updating • INSERT • DELETE • UPDATE
Relational Database Theory • SQL Query and Manipulation Summery (ANSI Standard) • Grouping • GROUP BY • HAVING • Sequencing • ORDER BY
Relational Database Theory • SQL Query and Manipulation Summery (ANSI Standard) • Built-in Functions • MIN • MAX • SUM • AVG • COUNT • DISTINCT
Relational Database Theory • SQL Query and Manipulation Summery (ANSI Standard) • Special Operators • NOT, AND, OR • BETWEEN, NOT BETWEEN • IN, NOT IN • LIKE, NOT LIKE • IS NULL, IS NOT NULL
Relational Database Theory • SQL Query and Manipulation Summery (ANSI Standard) • Arithmetic Operators • + • - • / • *
Relational Database Theory • SQL Query and Manipulation Summery (ANSI Standard) • Comparison Operators • = • <> (not equal) • < • > • <= • >=
Relational Database Theory • Views • Purpose of Views • Simplify • Function • Security
Relational Database Theory • View Definition • CREATE VIEW view_name AS • SELECT data_items • FROM table(s) • WHERE condition is true • View definition is saved in system tables • View is a Virtual Table • Database is accessed only when view is referenced
Relational Database Theory • View Definition Cont’d • Each user “sees” the database through their view as if • It is only one table • It contains only the data items of interest • The data items of interest have that user’s names • The data items are in exactly the order desired • Multiple views can be based on the same table
Relational Database Theory • View Example • Create a view for the accounting department to include: empno, ename, position, ancomp • CREATE VIEW acct (empno, ename, position, ancomp) AS • SELECT empno, ename, job, 12*(sal+nvl(comm, 0)) • FROM emp;