51
The Database Environment Lecture 1

The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Embed Size (px)

Citation preview

Page 1: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

The Database Environment

Lecture 1

Page 2: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

The database environment

• To be able to function, an organisation needs information, e.g. list of books in a library, customer details in a retail business, specifications of cars and their components for a car manufacturer

• Information may be defined as data represented in a meaningful form. Same data shown in different ways will provide different information to different viewers

• A major requirement of any computer system is to store and retrieve data in a way that is meaningful to the end user – so the core of any Information System is data, which is to be transformed into information through data modelling

Page 3: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Definitions

• Data: Meaningful facts, text, graphics, images, sound, video segments.

• Database: An organized collection of logically related data.

• Information: Data processed to be useful in decision making.

• Metadata: Data that describes data.

Page 4: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Data in Context

Page 5: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Summarized data

Page 6: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Disadvantages of file processing systems

Still widely used today (e.g. for backup) but have the following problems:• Program-Data Dependence (see Fig.) – file descriptions are stored

within each application that accesses file, so change to file structure requires changes to all file descriptions in all programs.

• Data Redundancy (Duplication of data) – wasteful, inconsistent, loss of metadata integrity (same data has different names in different files, or same name may be used for different data in different files).

• Limited Data Sharing – users have little opportunity to share data outside their own applications.

• Lengthy Development Times – little opportunity to re-use previous development efforts.

• Excessive Program Maintenance – factors above combine to create heavy maintenance load

Page 7: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Three file processing systems

Page 8: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Advantages of the database approach

• Minimal Data Redundancy/Improved Consistency

• Data Integration

• Multiple Relationships

Page 9: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Database management system

• A DBMS is a data storage and retrieval system which permits data to be stored non-redundantly while making it appear to the user as if the data is well-integrated.

Page 10: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Advantages of the database approach

• Data Independence/Reduced Maintenance

• Improved Data Sharing

• Increased Application Development Productivity

• Enforcement of Standards

• Improved Data Quality (Constraints)

• Better Data Accessibility/ Responsiveness

• Security, Backup/Recovery, Concurrency

Page 11: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Costs and risks of the database approach

• New, Specialized Personnel required

• Installation Management Cost and Complexity

• Conversion Costs

• Need for Explicit Backup and Recovery

• Organizational Conflict

Page 12: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Pine valley furniture company

• We will be using this fictitious company as a case study

• The company’s first step was to create an Enterprise Data Model (a model of the organisation that provides valuable information about how the organisation functions, as well as important constraints – it stresses the integration of data and processes by focussing on entities, relationships and business rules)

• First make a list of all the high-level activities that support business activities

Page 13: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Pine valley furniture company

• Enterprise data model is a graphical model that shows the high-level entities and the associations among them (see Fig.): An ENTITY-RELATIONSHIP DIAGRAM.

• The three associations (relationships) are shown by lines connecting the entities

• Each CUSTOMER places any number of ORDERS (conversely, each ORDER is placed by exactly one CUSTOMER)

• Each ORDER contains any number of ORDER LINEs (conversely, each ORDER LINE is contained in exactly one ORDER)

Page 14: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Pine valley furniture company

• Each PRODUCT has any number of ORDER LINES (conversely, each ORDER LINE is for exactly one PRODUCT)

Page 15: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Figure 3

Segment from enterprise data model

Page 16: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Tables

• Relational databases views all data in the form of tables• Following Figs. a and b shows four tables, Customer,

Product, Order and OrderLine (the 4 entities shown in the previous ER diagram).

• Each column represents an attribute, e.g. the Customer table has attributes ID, Name, Address..etc.

• Relationships between entities are represented by values stored in columns of the corresponding tables, e.g. Customer_ID is an attribute of both the Customer table and the Order table. This makes it easy to link an order with its customer.

Page 17: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Fig. (a) Order and Order_Line tables

Page 18: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Fig. (b) Product and Customer tables

Page 19: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

The range ofdatabase applications

• Personal Database PCs/PDAs, Cellphones – OK in special situations where need to share data amongst users is unlikely to arise

• Workgroup Database. Designed to support collaboration in a small team (less than 25 people)

• Department Database typically larger than a workgroup (25-100 people) and more diverse range of functions – e.g. personnel database

• Enterprise Database – scope of the whole organisation. May be more than one, as a single database for a large organisation may be impractical due to performance difficulties for large databases, diverse needs of user groups, and difficulty of achieving common definition of data (metadata) for all users.

Page 20: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Typical data from a personal computer database

Page 21: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Workgroup database with local area network

Page 22: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

An example of departmental database (Personnel Department)

Page 23: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

An enterprise data warehouse

Page 24: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Components of the database environment

• CASE Tools – automated tools used to design databases and applications

• Repository – generalised knowledge for all data definitions, relationships, screen/report formats – an extended set of metadata for managing databases and other components of the information system

• Database Management System (DBMS) – software (sometimes specialised hardware) used to define, create, maintain and provide controlled access to the database and the repository.

• Database – an organised collection of logically related data occurrences

Page 25: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Components of the database environment

• Application Programs – software used to create and maintain the database and provide information to users.

• User Interface – languages, menus etc by which users interact with other system components

• Data Administrators – people responsible for overall information resources of an organisation.

• Systems analysts/programmers and end Users – people who add, delete and modify the database and who get information from it.

Page 26: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Components of the database environment

Page 27: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Evolution ofdatabase systems

• 1960’s – file processing systems: punch cards, paper tape, magnetic tape – sequential access and batch processing

• 1970s - Hierarchical and Network (legacy, some still used today) – difficulties = hard to access data (navigational record-at-a-time procedures), limited data independence, no widely accepted theoretical model (unlike relational)

• 1980s - Relational – E.F. Codd and others developed this theoretically well-founded model – all data represented in the form of tables – Oracle, DB2, Ingres

• 1990s - Object-oriented, but some organisations have to handle large amounts of both structured and unstructured data, so Object-relational databases developed.

Page 28: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Evolution of database systems

• 2000 and beyond – multi –tier, client-server, distributed environments, web-based, content-addressable storage, data mining

Page 29: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Evolution of database technologies

Page 30: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

SQL Is:• Structured Query Language (some for historical reasons

call it ‘Sequel’• The standard and most common language for relational

database management systems • An SQL-based relational database application involves a

user interface, a set of tables in the database, and a RDBMS with an SQL capability

• Within the RDBMS SQL will be used to create the tables, translate user requests, maintain the data dictionary and system catalog, update an maintain the tables, establish security, and carry out backup and recovery procedures

Page 31: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

The SQL environment• Following Fig. Shows a schematic of an SQL environment• Each database is contained in a catalog, which describes any object that is

part of the database (regardless of which user created that object). Most companies keep at least two versions of any database they are using. PROD_C is the live production version, which captures real business data and this must be very tightly controlled and monitored. DEV_C is the development version used when the database is being built and continues to serve as a development tool where enhancements and maintenance efforts can be tested before application to the production database.

• Each database will have a set of schemas associated with a catalog.• Schema = the structure that contains descriptions of objects created by a

user (base tables, views, constraints)

Page 32: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

A simplified schematic of a typical SQL environment

Page 33: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

3 types of SQL commands

• 1. Data Definition Language (DDL) commands - that define a database, including creating, altering, and dropping tables and establishing constraints

• 2. Data Manipulation Language (DML) commands - that maintain and query a database

• 3. Data Control Language (DCL) commands - that control a database, including administering privileges and committing data

Page 34: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

DDL, DML, DCL, and the database development process

Page 35: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

SQL Data types• CHAR(n) – fixed-length character data, n characters long

Maximum length = 2000 bytes

• VARCHAR2(n) – variable length character data, maximum 4000 bytes

• LONG – variable-length character data, up to 4GB. Maximum 1 per table

• NUMBER(p,q) – general purpose numeric data type• INTEGER(p) – signed integer, p digits wide• FLOAT(p) – floating point in scientific notation with p

binary digits precision• DATE – fixed-length date/time in dd-mm-yy form

Page 36: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Syntax used in these notes

• Capitals = command syntax (may not be required by the RDBMS)

• Lowercase = values that must be supplied by user

• Brackets = enclose optional syntax

• Ellipses (...) = indicate that the accompanying syntactic clause may be repeated as necessary

• Each SQL command ends with a semicolon ‘;’

• In interactive mode, when the user presses the RETURN key, the SQL command will execute

Page 37: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

SQL Database Definition• Data Definition Language (DDL) has these

major CREATE statements:– CREATE SCHEMA – defines a portion of the database

owned by a particular user. Schemas are dependent on a catalog and contain schema objects, including base tables and views, domains, constraints, assertions, character sets, collations etc.

– CREATE TABLE – defines a new table and its columns. The table may be a base table or a derived table. Tables are dependent on a schema. Derived tables are created by executing a query that uses one or more tables or views.

Page 38: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

SQL Database Definition

• CREATE VIEW – defines a logical table from one or more tables or views. There are limitations on updating data through a view. Where views can be updated, those changes can be transferred to the underlying base tables originally referenced to create the view.

Page 39: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

SQL database definition

• Each of the previous create commands may be reversed using a DROP command, so DROP TABLE will destroy a table (including its definitions, contents and any schemas or views associated with it)

• Usually only the table creator may delete the table

• DROP SCHEMA and DROP VIEW will also delete the named schema or view.

• ALTER TABLE may be used to change the definition of an existing table

Page 40: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Creating tables

• Once data model is designed and normalised, the columns needed for each table can be defined using the CREATE TABLE command. The syntax for this is shown in the following Fig. These are the seven steps to follow:

• 1. Identify the appropriate datatype for each , including length and precision

• 2. Identify those columns that should accept null values. Column controls that indicate a column cannot be null are established when a table is created and are enforced for every update of the table

Page 41: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Creating tables

• 3. Identify those columns that need to be UNQUE - when the data in that column must have a different value (no duplicates) for each row of data within that table. Where a column or set of columns is designated as UNIQUE, this is a candidate key. Only one candidate key may be designated as a PRIMARY KEY

• 4. Identify all primary key-foreign key mates. Foreign keys can be established immediately or later by altering the table. The parent table in such a parent-child relationship should be created first. The column constraint REFERENCES can be used to enforce referential integrity

Page 42: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Creating tables

• 5. Determine values to be inserted into any columns for which a DEFAULT value is desired - can be used to define a value that is automatically inserted when no value is provided during data entry.

• 6. Identify any columns for which domain specifications may be stated that are more constrained than those established by data type. Using CHECK it is possible to establish validation rules for values to be inserted into the database

• 7. Create the table and any desired indexes using the CREATE TABLE and CREATE INDEX statements

Page 43: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Table creationGeneral syntax for CREATE TABLE

Page 44: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

Table creation

• The following Fig. Shows SQL database definition commands

• Here some additional column constraints are shown, and primary and foreign keys are given names

• For example, the CUSTOMER table’s primary key is CUSTOMER_ID

• The primary key constraint is named CUSTOMER_PK, without the constraint name a system identifier would be assigned automatically and the identifier would be difficult to read

Page 45: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

SQL database definition commands for Pine Valley Furniture

Page 46: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

STEP 1

Defining attributes and their data types

Page 47: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

STEP2

Non-nullable specifications

Note: primary keys should not be null

Page 48: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

STEP 3

Identifying primary keys

This is a composite primary key

Page 49: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

STEP 4

Identifying foreign keys and establishing relationships

Page 50: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

STEPS 5 and 6

Default values and domain constraints

Page 51: The Database Environment Lecture 1 The database environment To be able to function, an organisation needs information, e.g. list of books in a library,

STEP 7

Overall table definitions