Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | vincent-fitzgerald |
View: | 228 times |
Download: | 3 times |
IT 20303
• The Relational DBMS
• Section 07
Relational Database Theory
• Using a Relational DBMS
• Data Types– Depends on the conventions used by a
particular database– ORACLE uses:
• NUMBER• CHAR - Characters
• VARCHAR2 - Characters• DATE/TIME• LOB
Relational Database Theory
• NUMBERNUMBER– Numerical dataNumerical data– Guaranteed to 38 digits accuracyGuaranteed to 38 digits accuracy– NUMBER(10) – 10 digits allowedNUMBER(10) – 10 digits allowed
• CHARCHAR– Character dataCharacter data– Fixed-Length up to 2,000 bytesFixed-Length up to 2,000 bytes– Good for 2 or 3 charactersGood for 2 or 3 characters– Y/N, T/F, USA/CANY/N, T/F, USA/CAN– CHAR(2) – 2 characters allowedCHAR(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
• LOBLOB– Large OBject data typeLarge OBject data type
• CLOBCLOB– Long variable length charactersLong variable length characters
• BLOBBLOB– Binary objects – Video, Sound, Binary objects – Video, Sound,
GraphicsGraphics• BFILEBFILE
– Reference to an OS fileReference to an OS file– Up to 4GB of data per fileUp to 4GB of data per file
Relational Database Theory
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
DEPTNO DNAME LOC
10 ACCOUNTING CHICAGO
20 RESEARCH LONDON
30 SALES LOS ANGELES
40 OPERATIONS BOSTON
DEPTEMPLOYEE
DEPARTMENT
Supervises
Works for
Relational Database Theory
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-1998 800 20
7499 ALLEN SALESMAN 7698 20-FEB-2000 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-1998 1250 500 30
7566 JONES MANAGER 7839 02-APR-1995 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-1997 1250 1400 30
7698 BLAKE MANAGER 783901-MAY-
19932850 30
7782 CLARK MANAGER 7839 09-JUN-1999 2450 10
7788 SCOTT ANALYST 7566 09-DEC-1996 3000 20
7839 KING PRESIDENT17-NOV-
19915000 10
7844 TURNER SALESMAN 7698 08-SEP-1992 1500 0 30
7876 ADAMS CLERK 7788 12-JAN-1997 1100 20
7900 JAMES CLERK 7698 03-DEC-1999 950 30
7902 FORD ANALYST 7566 03-DEC-1994 3000 20
7934 MILLER CLERK 7782 23-JAN-2000 1300 10
EMP
• Example: EMP table
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);
End 10-10-05
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’;
• ResultsEMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7788 SCOTT ANALYST 7566 09-DEC-1996 3000 20
7902 FORD ANALYST 7566 03-DEC-1994 3000 20
Relational Database Theory
• Manipulation: SELECT Example– Example 02:
• SELECT ename, job, hiredate• FROM EMP• WHERE hiredate > ’31-DEC-99’;
• ResultsENAME JOB HIREDATE
ALLEN SALESMAN 20-FEB-2000
MILLER CLERK 23-JAN-2000
Relational Database Theory
• Manipulation: SELECT Example– Example 03:
• SELECT empno, ename, loc• FROM EMP, DEPT• WHERE emp.deptno = dept.deptno• AND loc = ‘CHICAGO’;• Results
EMPNO ENAME LOC
7782 CLARK CHICAGO
7839 KING CHICAGO
7934 MILLER CHICAGO
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;
EMPNO ENAME SAL
7698 BLAKE 2850
7566 JONES 2975
7788 SCOTT 3000
7902 FORD 3000
7839 KING 5000
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’;
EMPNO ENAME 12*SAL
7788 SCOTT 36000
7902 FORD 36000
End 10-12-05
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;
Relational Database Theory
• View Example– List all the analysts showing their annual
compensation• SELECT *• FROM acct• WHERE position = ‘ANALYST’;
– Here the Query and View are merged for query execution
Relational Database Theory
• Updating through Views– Update through view modifies data in the
underlying table• Updates can be made through views
based on one table• Updates can sometimes be made through
views based on more than one table– UPDATE acct– SET position = ‘SALESMAN’– WHERE ename = ‘JAMES’;
Relational Database Theory
• Database Security is based on Password and Authorizations– Need is to protect the database from unauthorized
users– Authorization is granted to individual users to
• Access (log on to) the DBMS• Access specified attributes in the database• Execute specified operations on each attribute
– The DBA is responsible for Passwords, and Authorizations
Relational Database Theory
• The Role of the DBA– DBA privileges vary greatly from product to
product– Users can GRANT privileges on a table
provided:• User is the owner, executed CREATE
TABLE for that table or• User was granted privileges WITH
GRANT OPTION
Relational Database Theory
• The Role of the DBA– DBA has permissions to execute other
statements• Can CREATE, DROP, or ALTER users,
user profiles, tablespaces, tables, etc– Some installations create a hierarchy of
DBAs• Entire Database• Application area DBAs
– Good practice is to create a user_id for each specific DBA role
Relational Database Theory
• DCL – Data Control Language – Commands:
• GRANT• REVOKE
Relational Database Theory
• Security and SQL– GRANT statement
• GRANT privileges• ON a table or view• TO a user• (WITH GRANT OPTION)
– REVOKE statement• REVOKE privileges• ON a table or view• FROM a user
Relational Database Theory
• ANSI SQL GRANT privileges– ALL– SELECT– INSERT– UPDATE– DELETE– CONNECT
Relational Database Theory
• See Example data for Employee & Departments:– Let Miller read all of the EMP table– Part 01
• GRANT Connect• TO miller• IDENTIFIED BY 382miller;
– Part 02• GRANT Select• ON emp• TO miller;
Relational Database Theory
• See Example data for Employee & Departments:– Let Jones read and update the data for
employees in her department– Part 01
• CREATE VIEW jones AS• SELECT *• FROM emp• WHERE mgr = 7566;
Relational Database Theory
• See Example data for Employee & Departments:– Part 02
• GRANT Select, Update• ON jones• TO jones;
Relational Database Theory
• See Example data for Employee & Departments:– Let King read the data for all employees in
the emp table and update the salary, commission, and job attributes.• GRANT Select, Update(sal, comm, job)• ON emp• TO king;
Relational Database Theory
• Database Security:– Security is a mentality– Security is environment-specific– The DBA has a key role
• Builds the database• Grants user/password access into DBMS• Defines user views• Grants user access and authorization to data
– 95% of all security violations can be avoided by attention to detail
• Change master passwords after installation• Change passwords frequently
Relational Database Theory
• Questions?