42
ISO6 Relational Databases Simon Booth Email s.p.booth @stir.ac.uk Room Library S6 Tel: 7247

ISO6 Relational Databases

Embed Size (px)

DESCRIPTION

ISO6 Relational Databases. Simon Booth Email s.p.booth @stir.ac.uk Room Library S6 Tel: 7247. Course Aims. Develop a basic proficiency in A relational database environment The SQL database language – Oracle SQL*Plus A forms-based 4GL programming tool – Access 97 - PowerPoint PPT Presentation

Citation preview

Page 1: ISO6 Relational Databases

ISO6 Relational Databases

Simon Booth

Email [email protected]

Room Library S6

Tel: 7247

Page 2: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

2

Course Aims

Develop a basic proficiency in A relational database environment The SQL database language –

Oracle SQL*Plus A forms-based 4GL programming

tool – Access 97 Database design and Management

Page 3: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

3

Lectures and Workshops Lectures (A1)

Monday 10-11

Workshop (2A19) Monday 14-16

Page 4: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

4

Applications and Books SQL Plus Oracle 3.3/8.0 Oracle 8 database Access 97 Database Design and Management by

Nick Dowling (Letts) £10.99

          

Page 5: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

5

Proposed Course Structure

First half – SQL Plus version 3.3/8.0 running against Oracle 8

Second half – Access 97 Lecture on Monday – practical issues on

the upcoming workshops

Page 6: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

6

Database Management Systems (DBMS)Definition

“ A set of programs that act as an interface between application programs and the data in the database”

Page 7: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

7

users/programmers

Software to processQueries/Programs

Software to access Stored Data

A Simplified Database System Environment

Application Programs/QueriesDatabaseSystem

DBMSSoftware

Stored DatabaseDefinition(Meta-Data)

Stored Database

Page 8: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

8

5 primary functions of a DBMS

Define, create and organize a database (Data Definition Language)

Input data Process data (Data Manipulation Language) Maintain Data integrity and security Query database (Structured Query Language)

Page 9: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

9

Advantages of DBMS Data independence

Access to data based on its contents and its associations with other data

Physical organization of data is independent of program using it

Reduction in data redundancy Accessing and processing data

more effective

DATA

users

usersusers

usersusers

Page 10: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

10

Disadvantages of DBMS

Cost is the primary disadvantage Mainframe hardware expensive Even expensive with PC based

databases Licences Training Maintenance and Management

Page 11: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

11

Database Infrastructures

Hierarchical Network Relational

Page 12: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

12

Hierarchical

ROOT

Father

Son 2Son 1

Page 13: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

13

Network

Owner

Member

Page 14: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

14

STUDENT Name StudentNumber Year MajorSubject

Smith 17 1 COSC

Brown 8 2 COSC

Relational Model

COURSE CourseName CourseNumber UnitsPassed Dept.

CompSci Intro COSC1310 2 COSC

Data Models COSC3320 1 COSCDiscrete Math MATH2410 3 MATH

Databases COSC3380 3 COSC

TUTORIALS TutorialID CourseNumber Semester Year Tutor85 MATH2410 Aut 95 Donnelly

92 COSC1310 Aut 95 Simler102 COSC3320 Spring 96 Munro

112 MATH2410 Aut 95 Bell

EXAM_RESULT StudentNumber TutorialID Grade17 112 B17 119 C8 85 A8 92 A8 102 B

Page 15: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

15

Structured Query Language (SQL) Programming language (4GL) Data definition and data

manipulation language Used in all types of database

applications Oracle and Access

Page 16: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

16

SQL (cont.)

Provided a degree of homogeneity Industry standard Databases conforming to the SQL

standard can also use applications from other SQL databases

SQL standards approved by ANSI Incorporating SQL technology

provides a strong selling point

Page 17: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

17

SQL Plus (Sequel Plus) SQL*Plus is a program for working with an

ORACLE database Create tables Store information in the tables Retrieve information in a form you choose,

performing calculations on it and combining it in new ways

Maintain the database

Page 18: ISO6 Relational Databases

Querying & Manipulating

Databases

Using SQL*Plus

Page 19: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

19

Accessing SQL*Plus 3.3

To access Oracle SQL*Plus you must first log-on in the normal way. In Windows NT go to START, PROGRAMS, ORACLE, ORACLE WinNT, SQL Plus 3.3.

Page 20: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

20

Accessing SQL

Enter your oracle username and password. You will also be asked for a ‘host string’ enter ora1a11

Page 21: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

21

Accessing SQL

Having entered these, you will then see the SQL prompt from Oracle:

SQL> You are now ready to type

SQL commands

Page 22: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

22

Accessing SQL

SQL>SELECT job FROM emp 2

Note: Oracle itself is not case-sensitive. I have used a mixture of upper and lower case to indicate SQL commands (uppercase) and the components I select (lowercase)

Page 23: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

23

Accessing SQL

SQL>SELECT job FROM emp 2

Note: Oracle inserts the “2” to indicate that we are on the second line. To properly terminate the command, enter ;. Oracle will now process the command.

Page 24: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

24

A table Artist Title FormatVerve Urban HymnsCDOasis Be Here NowCassette

RadioheadBends CD

The above is a table named called “music” with three columns: Artist, Title and Format. There are presently three rows.

Page 25: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

25

The Select command

To list the columns Artist and Format from the table music:

SQL> SELECT artist, format FROM music;

Page 26: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

26

The WHERE clause

We can select based on rows that meet a certain condition. For instance, only CD’s:

SQL> SELECT * FROM music 2 WHERE format LIKE ‘CD’;

* means all columns

Page 27: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

27

The WHERE clause(2)

Another Example:

SQL> SELECT ename, deptno, sal 2 FROM emp 3 WHERE sal >= 1500;

Columns are listed in the order they appear and can appear more than once

Page 28: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

28

The ORDER BY clause

Oracle will list back the information we ask in order it appears in the table. If we want it alphabetically or by numerical order we must add an ORDER BY and specify the column(s) to order by.

Page 29: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

29

The ORDER BY clause(2) Example:

SQL> SELECT ename, deptno, sal 2 FROM emp 3 ORDER BY sal DESC;

Page 30: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

30

SQL

Group FunctionsThese functions (AVG, MAX, MIN, etc) can act on entire tables or subsets of the table

SELECT AVG(sal) FROM emp;SELECT MAX(sal) FROM empWHERE job = ‘MANAGER’;SELECT COUNT(*) FROM empWHERE deptno = 20;

Page 31: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

31

GROUP BY

Group functions can also be used with the GROUP BY clause, which splits the table into specified groups, returning one row for each group

SELECT AVG(sal), deptno FROM empGROUP BY deptno;

Page 32: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

32

Clauses

Clauses can be combined:

SQL> SELECT ename, deptno, sal 2 FROM emp 3 WHERE sal > 1500 4 ORDER BY sal DESC;

Page 33: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

33

Second example

Rows maybe selected by WHERE

SELECT AVG(sal), deptno FROM empWHERE job != ‘MANAGER’GROUP BY deptno;

Page 34: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

34

Update

The SQL command update is used to modify all or some rows in the table:

UPDATE tableSET column = expr [, column = expr][WHERE condition];

Page 35: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

35

Update

Example: correct name for employee 7369

UPDATE empSET ename = ‘jones’WHERE empno = 7369;

Page 36: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

36

Delete

The SQL command delete is used to remove all or some rows in the table:

DELETE FROM table[WHERE condition];

Page 37: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

37

Delete

Example: remove employee 7369

DELETE FROM empWHERE empno = 7369;

Page 38: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

38

Copying Tables

We can copy tables existing tables:

CREATE TABLE stolen_table ASSELECT * FROM another_table;

Page 39: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

39

Creating Tables(2)

If we only want specific columns, we name them in the select;If we only want specific rows, we use a WHERE clause:

CREATE TABLE stolen_table ASSELECT video_no, title FROM videoWHERE title = ‘T%’;

Page 40: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

40

Populating Tables

To place data in a table we use the INSERT command:

INSERT INTO table [(column-name, …)]VALUES (value, … );

Which adds one row to the table. To add more, we have more INSERT commands

Page 41: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

41

Populating Tables

Examples:

INSERT INTO dept (deptno, dname)VALUES (50, ‘Marketing’);

INSERT INTO deptVALUES (50, ‘Marketing’, ‘Stirling’);

Page 42: ISO6 Relational Databases

PF Lecture 1PF Lecture 1

42

Summary of SQL commands

SQLSELECTUPDATEDELETECREATE ASINSERT