intro to it chapter 6 db

Upload: akhlaqist

Post on 30-May-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Intro to IT Chapter 6 Db

    1/28

    DatabaseDatabase

  • 8/14/2019 Intro to IT Chapter 6 Db

    2/28

    DatabaseDatabase Database is a collection of information in a structured

    way

    Example of database use are :- Banks

    Travel Agent

    Databases store information in fields, records, and files

    A field is a single piece of informationa customers firstname, for example

    A record is one complete set of fields the customersfirst name, last name, address, phone number, andaccount information. And

    File is a collection of records a complete list of allcustomers.

    Key Field is used to uniquely identify record for easyretrieval

  • 8/14/2019 Intro to IT Chapter 6 Db

    3/28

    Database Storage HierarchyDatabase Storage Hierarchy

    Type of dataType of data ContainsContains ExampleExample

    DatabaseDatabase Several FilesSeveral Files PersonalPersonalDatabaseDatabase

    FileFile Several RecordsSeveral Records Name1Name1Name2Name2

    RecordRecord Several FieldsSeveral Fields ABDULLATIFABDULLATIFMIR, IST,MIR, IST,IslamabadIslamabadHighway nearHighway nearToll Plaza, IsbToll Plaza, Isb

    FieldField Characters

    Characters(bytes)(bytes)

    First Name:First Name:ABDULLATIFABDULLATIF

    CharactersCharacters Bits (0,1)Bits (0,1) Letter SLetter S1110 00101110 0010

  • 8/14/2019 Intro to IT Chapter 6 Db

    4/28

    Database System ComponentsDatabase System Components

    Hardware

    Computer Peripherals

    Software Operating Systems Software

    DBMS Software People

    Systems Administrators (SA) Database Administrators (DBAs) Database Designers / Programmers

    End Users Data

    Collection of facts stored in the database

  • 8/14/2019 Intro to IT Chapter 6 Db

    5/28

    Types of Database SystemsTypes of Database Systems Number of Users

    Single User Desktop Database

    Multi User Workgroup Database IST

    Enterprise Database NADRA

    Scope Desktop

    Workgroup

    Enterprise

    Location

    Centralized Database IST Distributed Database e.g. ATM/DNS

  • 8/14/2019 Intro to IT Chapter 6 Db

    6/28

    Examples of DatabaseExamples of Database

  • 8/14/2019 Intro to IT Chapter 6 Db

    7/28

    Database ModelsDatabase Models A Database Model ordatabase schemais the

    structure , format or way of arranging data

    Six models are:-

    Flat

    HierarchalNetwork

    Relational (most widely used)

    Object OrientedMultidimensional

  • 8/14/2019 Intro to IT Chapter 6 Db

    8/28

    File SystemFile System The file system that comes with computer is a very primitive

    kind of database management system

    Data are kept in big unstructurednamed clumps called files A flat file database is a database designed around a single table

    The flat file design puts all database information in one table

    A flat file may contain many fields, often, with duplicate datathat are prone to data corruption

  • 8/14/2019 Intro to IT Chapter 6 Db

    9/28

    Hierarchical DatabaseHierarchical Database Hierarchical data model organizes related record/fields in a

    tree structure There is a hierarchy ofparent and child data segments

    Child may only have one parent but a parent can have multiplechildren

    These database have well defined, pre specified accesspaths

    Hierarchical structures were widely used in the early mainframedatabase management systems

    Student

    Name Address

    CNIC Course GPA

    CourseA

    CourseB

    Course CFirstName

    LastName

    Middle

    Name

    Exam2 Exam3Grade Exam 1Exam

    2

    Grade ExamExam

    1 Grade

  • 8/14/2019 Intro to IT Chapter 6 Db

    10/28

    Network Database ModelNetwork Database Model It is built upon Hierarchal database

    Network model allows each data element to have multipleparent and child record i.e. each child record can havemore then one parent record

    Child record can be reached through more then one parent

    More flexible then Hierarchal database It is similar to Mesh network topology and allows quick retrieval

    of data

  • 8/14/2019 Intro to IT Chapter 6 Db

    11/28

    Relational Database ModelRelational Database Model The relational database stores information in tables

    Table is made up ofrecords (horizontal rows also known astuples) and fields (vertical columns also known as attributes)

    Each table is identified by a unique name and that name is usedby the database to find the table

    Primary key is used to uniquely identify record with in table

    Secondary key is used to link two tables SQL is used to search database

    Record search is very fast because of primary key

  • 8/14/2019 Intro to IT Chapter 6 Db

    12/28

    Object Oriented DatabaseObject Oriented DatabaseModelModel

    A database system that incorporates all the important object-

    oriented concepts It was designed to add database functionality to object

    programming languages like Java , C++ etc

    Database capabilities are combined with object-oriented (OO)programming language capabilities

    This model store its contents as objects The main benefit of creating a database with objects as data is

    speed

    OQL is used to query database

  • 8/14/2019 Intro to IT Chapter 6 Db

    13/28

  • 8/14/2019 Intro to IT Chapter 6 Db

    14/28

  • 8/14/2019 Intro to IT Chapter 6 Db

    15/28

    Database Management SystemDatabase Management System

    (DBMS)(DBMS) Database Management System (DBMS), is collection of

    software programs that let you to enter, organize, andselect information stored in the database e.g. Oracle,Microsoft SQL Server, MySQL etc.

  • 8/14/2019 Intro to IT Chapter 6 Db

    16/28

    DBMS Vs File SystemsDBMS Vs File Systems Speed. Retrieval of data and application

    development time Data Administration.You can administrate

    the values being entered in the database byhaving various constraints on the data field.For example, you may allow only numericfields in salary field

    Data Security. Database provide differentlevel of securities by having password toaccess different level of database whichis not provided with file systems (root user)

    Concurrent Access. Multiple users can access

    the database at a same time by using locksystem (yahoo mail)

    Crash Recovery. In case your databasecrashes, it has a special mechanism by usingyou can recover your database on the

    last saved state (RAID) Im roved Data Inte rit . Data is U to

  • 8/14/2019 Intro to IT Chapter 6 Db

    17/28

    DBMS Vs File SystemsDBMS Vs File Systems

    Problems

    Costof software/hardware and migration

    Complexity of backup and recovery

    Skilled Manpowerrequired

  • 8/14/2019 Intro to IT Chapter 6 Db

    18/28

    QueriesQueries Queries are the primary mechanism for retrieving

    information from a database It consist ofquestions presented to the database in a

    predefined format

    Different ways to query database are :-

    SQL (Structured Query Language) OLAP (online analytical processing) simple

    language query

    Data mining is a process of extracting hidden

    trends within a data ware house. For examplean insurance data ware house can be used tomine data for the most high risk people toinsure in a certain geographical area

  • 8/14/2019 Intro to IT Chapter 6 Db

    19/28

    SQLSQL SQL is structured query language for accessing and

    manipulating database systems SQL works with database programs like MS Access,

    DB2, Informix, MS SQL Server, Oracle, Sybase,etc.

    almost everything can be accomplished by usingfollowing commands :-

    Select Insert Update

    Delete Create Drop

  • 8/14/2019 Intro to IT Chapter 6 Db

    20/28

    Table BasicsTable Basics A relational database system contains one or more tables

    The data or information for the database are stored in thesetables

    Tables are uniquely identified by their names and are comprisedof columns and rows

    Columns contain the column name, data type, and any otherattributes

    Rows contain the records or data for the columns. Here is asample table called "weather". city, state, high, and low arethe columns. The rows contain the data for this table

  • 8/14/2019 Intro to IT Chapter 6 Db

    21/28

    Data TypesData Types Data types specify what the type of data can be for

    that particular column If a column called "Last_Name", is to be used to hold

    names, then that particular column should have a"varchar" (variable-length character) data type

    Here are the most common Data types

  • 8/14/2019 Intro to IT Chapter 6 Db

    22/28

    Creating TablesCreating Tables The create tablestatement is used to create a new table Syntax :-

    create table "tablename" ("column1" "data type","column2" "data type", "column3" "data type");

    Example:- create table employee (first varchar(15), last

    varchar(20), age number(3), address varchar(30),city varchar(20), state varchar(20));

    Rules :- The table and column names must start with a

    letter

    not to exceed a total of30 charactersin length Do not use any SQL reserved keywords as names

    for tables or column names (such as "select","create", "insert", etc)

  • 8/14/2019 Intro to IT Chapter 6 Db

    23/28

    Drop a TableDrop a Table

    The drop table statementis used to delete atable and all rows in the table

    To delete an entire table including all of itsrows, issue the drop table commandfollowed by the table name

    Dropping the table removes the tabledefinition as well as all of its rows

    Syntax :- drop table "tablename" Example :- drop table myemployees_ts0211;

  • 8/14/2019 Intro to IT Chapter 6 Db

    24/28

    Inserting into a TableInserting into a Table

    The insert statement is used to insert or add arow of data into the table

    Syntax :- insert into "tablename"

    (first_column,...last_column) values(first_value,...last_value);

    Example:- insert into employee (first, last, age, address,

    city, state) values ('Luke', 'Duke', 45, '2130Boars Nest', 'Hazard Co', 'Georgia');

    Note: All strings should be enclosed betweensingle quotes: 'string'

  • 8/14/2019 Intro to IT Chapter 6 Db

    25/28

    Selecting DataSelecting Data The select statement is used to query the database

    and retrieve selected data that match the criteriathat you specify

    Syntax :-

    select "column1" [,"column2",etc] from "tablename"[where "condition"];

    The column names that follow the select keyworddetermine which columns will be returned in theresults. You can select as many column names thatyou'd like, or you can use a "*" to select all columns

    The table name that follows the keyword fromspecifies the table that will be queried to retrievethe desired results

  • 8/14/2019 Intro to IT Chapter 6 Db

    26/28

    Selecting DataSelecting Data The where clause (optional) specifies which data

    values or rows will be returned or displayed, basedon the criteria described after the keyword where.

    Conditional selections used in the where clause:

    = Equal

    > Greater than < Less than

    >= Greater than

    equal

  • 8/14/2019 Intro to IT Chapter 6 Db

    27/28

    Updating RecordsUpdating Records The update statementis used to update or change

    records that match a specified criteria. This is accomplished by carefully constructing a where

    clause.

    Syntax :-

    update "tablename" set "columnname" = "newvalue"

    [,"nextcolumn" = "newvalue2"...] where"columnname" OPERATOR "value" [and|or"column" OPERATOR "value"];

    Examples:-

    update phone_book set area_code = 623 where prefix =979;

    update phone_book set last_name = 'Smith',prefix=555, suffix=9292 where last_name = 'Jones';

    update employee set age = age+1 where

    first_name='Mary' and last_name='Williams';

  • 8/14/2019 Intro to IT Chapter 6 Db

    28/28

    Deleting RecordsDeleting Records The delete statementis used to delete records or rows from

    the table Syntax:-

    delete from "tablename where condition" Examples:

    delete from employee; Note: if you leave off the whereclause, all records will be deleted!

    delete from employee where lastname = 'May';

    delete from employee where firstname = 'Mike' orfirstname = 'Eric';

    To delete an entire record/row from a table, enter "delete from"followed by the table name, followed by the where clausewhich contains the conditions to delete. If you leave off thewhere clause, all records will be deleted.