DBMSMIS BBA(Hons) 8th Lec 192021

download DBMSMIS BBA(Hons) 8th Lec 192021

of 40

Transcript of DBMSMIS BBA(Hons) 8th Lec 192021

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    1/40

    DBMS & MIS

    BBA (Hons) 8th

    (Lectures 16,17,18)

    Databases: The Technical Stuff

    Course Lecturer: Farhan Mir

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    2/40

    Relational Databases. Independent Tables

    Relationship through primary & secondarykeys

    SQL Select: Creates subset of rows that meet specific criteria Join: Combines relational tables to provide users with

    information

    Project: Enables users to create new tables containingonly relevant information

    Compatibility with Existing Applications Database Security

    Data Dictionaries

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    3/40

    What Makes a Database Relational?

    A database is relational when files are

    related to each other, such as this Student ID

    field in the Student file.

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    4/40

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    5/40

    Relational Database

    Data stored in related tables

    Table records have same record type

    Record = table row

    Attributes = table columns (fields) Uses common key fields for relationships

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    6/40

    Table and Record Example

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    7/40

    Database Characteristics

    Relationship: association between data

    stored in different tables

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    8/40

    Designing Databases

    Database structure: arrangement of the

    fields, tables and relationships

    Design for ease of access and

    maintenance in as small a file as possible. First determine what data must be collected

    and stored

    Next organize data into fields and define how

    the field is stored Break data into small fields (firstname,

    lastname)

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    9/40

    Data Types

    Character

    Numeric Real and integer

    Date Logical

    Memo

    Image

    Calculated field

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    10/40

    Organizing Table Data

    Sort order: order in which records are

    stored on disk

    Queries and updates are faster Sort key (one or more fields)

    Database index: list of keys and

    associated record numbers

    Doesnt affect the way records are physically

    stored

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    11/40

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    12/40

    Lets have a closer look at

    Structured query language

    SQL

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    13/40

    SQL basics

    SQL allows you to access a database

    SQL executes queries against a database

    SQL commands are case independent.

    SELECT = select

    But column names or DATA are not case independent.

    SQL command are named after english words:

    Create, select, insert, update ...

    It's easy to learn

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    14/40

    3 types of SQL commands

    1. Data Definition Language (DDL) commands - thatdefine a database, including creating, altering, and

    dropping tables and establishing constraints 2. Data Manipulation Language (DML) commands - thatmaintain and query a database

    3. Data Control Language (DCL) commands - thatcontrol a database, including administering privileges

    and committing data

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    15/40

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    16/40

    Syntax used in these notes

    Capitals = command syntax

    Lowercase = values that must be supplied by user

    Brackets = enclose optional syntax

    Each SQL command ends with a semicolon ; In interactive mode, when the user presses the RETURN

    key, the SQL command will execute

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    17/40

    Data Definition Language: create the tables

    Create table syntax

    CREATE TABLE tablename (

    column1 data_type [not null] [unique] [column_constraint] ,

    ...

    [table constraints]);

    Example:

    CREATE TABLE person (personID varchar(5) NOT NULL,

    Name varchar(25) NOT NULL,

    Firstname varchar(15) NULL);

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    18/40

    How to judge which table to

    create and what could be thecolumns (fields)

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    19/40

    Remember from the Production System

    Case Study: you are transforming an Entity to a Table

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    20/40

    Simple entity translation

    General Rule:

    Create a table with the name of the Entity. There is a column for each attribute

    The key in the diagram is the primary key of the

    table

    Actorid

    name address

    birthday

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    21/40

    student

    id

    name

    address

    id name address

    Student

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    22/40

    How to Create tables with SQL

    The columns needed for each table can be defined using

    the CREATE TABLE command. The syntax for this is

    shown in the upcoming figure. These are the seven

    steps to follow:

    1. Identify the appropriate datatype for each columns,

    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 enforcedfor every update of the table

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    23/40

    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-childrelationship should be created first. The column

    constraint REFERENCES can be used to enforce

    referential integrity

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    24/40

    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

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    25/40

    Simple entity translation and creating table

    create table Actor(id varchar(20) primary key,name varchar(40),

    birthday date,

    address varchar(100));

    Actorid

    name address

    birthday

    Relation: Actor (id, name, birthday, address)

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    26/40

    26

    Table creationGeneral syntax for CREATE TABLE

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    27/40

    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 tables 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

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    28/40

    28

    SQL database definition commands for Pine Valley Furniture

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    29/40

    29

    The final shape of the DDL in this case

    Overall table

    definitions

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    30/40

    Constraints

    NOT NULL : the column cannot take the

    NULL value

    NULL : the column can take the null value

    (default)

    UNIQUE : the column has unique values

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    31/40

    Example

    CREATE TABLE person (

    personID varchar(5) NOT NULL UNIQUE,

    Name varchar(25) NOT NULL,

    Firstname varchar(15) NULL,

    City varchar(20)

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    32/40

    DDL: alter table

    Modifying the structure of a table. Add a

    column:

    ALTER TABLE ADD [DEFAULT

    ] []

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    33/40

    DDL: drop table

    Delete a table

    DROP TABLE tablename;

    It is very easy and fast to type the drop

    table command -> be careful !!!

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    34/40

    Data Manipulation Language (DML):

    Update Operations

    INSERT - inserts a list of attributes .

    INSERT INTO PERSON VALUES (008, Tome, Hector, J);

    DELETE - removes a row from a table.DELETE FROM PERSON WHERE ID = 7;

    Attention DELETE FROM PERSON will delete

    the whole table

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    35/40

    UPDATE - changes the value of one or

    more attributes in a row.

    When modifying an attribute that is not a

    primary key or foreign key, there is usually

    no danger of violating a constraint.

    When modifying a primary key, it is

    equivalent to doing a delete followed by aninsert operation.

    UPDATE PERSON SET ID = 007 WHERE

    LastName=Tome AND FirstName=Pat;

    Update Operations

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    36/40

    Select

    Select is used to retrieve data :

    SELECT [DISTINCT]

    FROM

    [WHERE ]

    [ORDER BY ]

    In uppercase are the SQL keywords Between [] optional conditions

    Between what correspond to your table(s) definition

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    37/40

    Select all data

    SELECT FIRSTNAME, SURNAME

    FROM PERSON

    Select specific data, ordered

    SELECT FIRSTNAME, SURNAME FROM PERSON WHERE

    SURNAME = BONDORDER BY FIRSTNAME ASC

    FirstName SurName

    JAMES KIRK

    JAMES BOND

    ... ...

    Select

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    38/40

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    39/40

    Select all data where person firstname begins with

    JAM

    SELECT * FROM PERSON WHERE FIRSTNAME like 'JAM%'

    PersonID FirstName SurName MidInitial

    045 JAMES KIRK T007 JAMES BOND X

  • 8/7/2019 DBMSMIS BBA(Hons) 8th Lec 192021

    40/40

    More examples: special conditions

    Set conditions: [ ] IN ()

    SELECT firstname FROM person WHERE

    surname IN ('BOND','KIRK');