Database Assignment-2.doc

Embed Size (px)

Citation preview

  • 8/9/2019 Database Assignment-2.doc

    1/10

    Q1) Write 15 select Syntax and define Select Statement.

    Ans)The SELECT statement is used to select data from a database.

    The result is stored in a result table, called the resultset.

    SELECT ! "#$% table&name

    1) Select 'Column&name( from 'table&name(

    *) Select 'Column&name( from 'table&name( +here 'condition(

    ) Select 'Column&name( from 'table&name( +here 'condition(

    li-e 'attern(

    /) Select 'Column&name( from 'table&name( +here 'condition(order by 'exression(

    5) Select 'Column&name( from 'table&name( +here 'condition(

    0rou by 'exression(

    ) Select 'Column&name( from 'table&name( +here 'condition(

    ha2in0 'exression(

    3) Select 'Column&name1( from 'table&name1( %inus

    Select 'Column&name*( from 'table&name*(

    4) Select 'Column&name1( from 'table&name1( ntersectSelect 'Column&name*( from 'table&name*(

    6) Select 'Column&name1( from 'table&name1( 7nion

    Select 'Column&name*( from 'table&name*(

    18) Select 'Column&name1( from 'table&name1( 7nion All

    Select 'Column&name*( from 'table&name*(

    9*) Write different S9L Statements +ith Examle and define it.

    Ans) What is SQL? SQL stands for Structured Query Language SQL lets you access and manipulate databases SQL is an ANSI (American National Standards Institute) standard

    SQL Statements

    1) Select Statement

    2) :elete Statement;DELETE F!" table#name $%EE some#column&some#'alue

    3) Commit4) #ollbac-

  • 8/9/2019 Database Assignment-2.doc

    2/10

    5) Alter; ALTE TALE table#name ENA"E T! ne#table#name

    6) 7date; *+DATE table#name SET column,&'alue- column.&'alue.-/// $%EE some#column&some#'alue

    7) %odify; Alter TABLE "table_name" ALTER COLUMN "colmn !" "Ne #ata T$%e

    8) 7nion;SELECT #ate &ROM Store_'n(ormation UN'ON

    SELECT #ate &ROM 'nternet_Sales

    9) 7nion All;SELECT #ate &ROM Store_'n(ormation UN'ON ALL SELECT #ate &ROM 'nternet_Sales

    10) ntersect;SELECT #ate &ROM Store_'n(ormation 'ntersect

    SELECT #ate &ROM 'nternet_Sales11) %inus;SELECT #ate &ROM Store_'n(ormation

    MinsSELECT #ate &ROM 'nternet_Sales

    12) Where

    13) Li-e

    Q3) :efine Tri00er, tyes of Tri00er .syntax of tri00ers +ith examles

    Tri00ers

    Ans)Tri00ers are named database ob:ATE statement iserformed on the table. %yS9L does not ermit t+o tri00ers +ith the

  • 8/9/2019 Database Assignment-2.doc

    3/10

    same tri00er timin0 ?@E"$#E or A"TE#) and tri00er e2ent or

    statement ?=SE#T, :ELETE, or 7>:ATE) to be defined on a table.

    "or examle, you cannot define t+o @E"$#E =SE#T or t+o

    A"TE# 7>:ATE tri00ers for a table. All tri00ers defined on %yS9L

    are ro+ tri00ers, +hich mean that the action defined for the tri00ers is

    executed for each ro+ affected by the tri00erin0 statement.

    Error handlin0 durin0 tri00er execution for transactional tables

    ensures that either both the tri00erin0 statement and tri00er action is

    comleted successfully or neither the tri00er statement nor the tri00er

    action is executed, that is all chan0es made are rollbac- on failure. "or

    nontransactional tables, all chan0es made rior to the oint of error

    remains in effect.

    Tyes of Tri00ers in $racle

    1. @efore Tri00er

    *. After Tri00er

    . #o+ le2el tri00er

    /. Schema Tri00er

    5. nstead of Tri00er

    . :atabase Tri00er

    3. Statement le2el Tri00er

    The follo+in0 is the syntax to create a tri00er

    C#EATE T#E# 'tri00er name(

    B @E"$#E A"TE# D

    B =SE#T 7>:ATE :ELETE D

    $= 'table name(

    "$# EAC #$W

    'tri00ered action(

  • 8/9/2019 Database Assignment-2.doc

    4/10

    n $racle, tri00ers can be fired +hen one of the follo+in0 oerations

    occurs;

    :%L statements ?=SE#T, :ELETE or 7>:ATE) that modify

    data on a table or 2ie+ ::L statements

    7ser e2ents such as lo0on and lo0off

    System e2ents such as startu, shutdo+n, and error messa0es

    $racle allo+s multile tri00ers +ith the same tri00er timin0 and

    tri00er e2ent to be defined on a table ho+e2er, these tri00ers are not

    0uaranteed to execute in any secific order. Tri00ers can be defined as

    ro+ tri00ers or statement tri00ers. Statement tri00ers are fired once

    for each tri00erin0 statement re0ardless of the number of ro+s in a

    table affected by the tri00erin0 statement. "or examle if a :ELETE

    statement deletes se2eral ro+s from a table, a statement tri00er is only

    fired once.

    The execution model for $racle tri00ers is transactional. All actions

    erformed as a result of the tri00erin0 statement, includin0 the actionserformed by fired tri00ers, must all succeed other+ise, they are

    rolled bac-.

    Q4) :efine rocedure, syntax of rocedure and difference bet+een

    rocedure and stored rocedure.

    Ans)A rocedure is a subro0ram that erforms a secific action.

    Fou must declare and define a rocedure before in2o-in0 it. Fou can

    either declare or define it at the same time, or you can declare it first

    and then define it later in the same bloc- or subro0ram.

    Syntax

    >rocedure declaration; G

  • 8/9/2019 Database Assignment-2.doc

    5/10

    >rocedure headin0; G

    >rocedure definition; G

    Hey+ord and >arameter :escritions

    @ody

    The reIuired executable art of the rocedure and, otionally, the

    excetionhandlin0 art of the rocedure.

    :eclare section

    The otional declarati2e art of the rocedure. :eclarations are local

    to the rocedure, can be referenced in body, and cease to exist +hen

    the rocedure comletes execution.

    >rocedure declaration

    :eclares a rocedure, but does not define it. The definition must

    aear later in the same bloc- or subro0ram as the declaration.

    A rocedure declaration is also called a rocedure secification, or

    rocedure sec.

    >rocedure definition

    Either defines a rocedure that +as declared earlier in the same bloc-

    or subro0ram, or declares and defines a rocedure.

    >rocedure name

  • 8/9/2019 Database Assignment-2.doc

    6/10

    The name that you 0i2e to the rocedure that you are declarin0 or

    definin0.

    Stored Procedures

    Stored rocedures ro2ide a o+erful +ay to code alication lo0ic

    that can be stored on the ser2er. %yS9L and $racle both use stored

    rocedures and functions. Stored functions are similar to rocedures,

    excet that a function returns a 2alue to the en2ironment in +hich it is

    called. n %yS9L, stored rocedures and functions are collecti2ely

    called routines.

    The follo+in0 sections comare stored rocedures in %yS9L and$racle;

    ndi2idual S9L Statement

    Jariables in stored >rocedures

    Error andlin0 in Stored >rocedures

    3.2.1 Individual SQ State!ents

    This section describes considerations related to the follo+in0

    statements or constructs;

    #E>LACE Statement

    :$ Statement

    Comound :ECLA#E Statement

    Comound SET Statement

    3.2.1.1 REPLACE Statement

    The #E>LACE statement in %yS9L is a dualurose statement. t

    +or-s li-e the =SE#T statement +hen there is no record in the table

    that has the same 2alue as the ne+ record for a rimary -ey or a

    uniIue index, and other+ise it +or-s li-e the 7>:ATE statement.

  • 8/9/2019 Database Assignment-2.doc

    7/10

    $racle does not ha2e any builtin S9L statements that suorts the

    uroses of the %yS9L #E>LACE statement. To con2ert this

    statement to $racle, an emulated function usin0 both the =SE#T and

    7>:ATE statements has to be created. An attemt is first made to

    lace the data into the table usin0 the =SE#T statement and if this

    fails, the data in the table is then udated usin0 the 7>:ATE

    statement.

    DO Statement

    As its name imlies, the :$ statement in %yS9L does somethin0 but

    does not return anythin0 secifically, it executes the comma

    delimited list of exressions secified as its arameters. The :$statement is con2erted to a SELECT exr1 K, exr*,M =T$

    "#$% :7AL statement in $racle.

    Compound DECLARE Statement

    %yS9L uses the :ECLA#E statement to declare local 2ariables in

    stored rocedures. >LNS9L does not allo+ multile declarations each

    declaration must be made searately. To con2ert comound

    :ECLA#E statements into functionally eIui2alent >LNS9L code,each %yS9L multile declaration statement should be con2erted into

    lo0ically eIui2alent searate statements, one for each declaration.

    "or examle, consider the follo+in0 %yS9L simle declaration and

    multile declaration statements;

    N! Simle declaration !N

    :ECLA#E a =T

    N! Comound declaration !N

    :ECLA#E a, b =T :E"A7LT 5

    The >LNS9L functionally eIui2alent statements are;

    N! Simle declaration !N

    a =T

  • 8/9/2019 Database Assignment-2.doc

    8/10

    N! %ultile declarations !N

    a =T ;G 5

    b =T ;G 5

    Q5):efine the follo+in0 term

    1) Concurrency Control

    "e#inition

    Concurrency control is a database mana0ement systems ?:@%S)

    concet that is used to address conflicts +ith the simultaneous

    accessin0 or alterin0 of data that can occur +ith a multiuser system.

    Concurrency control, +hen alied to a :@%S, is meant to

    coordinate simultaneous transactions +hile reser2in0 data inte0rity.

    K1M The Concurrency is about to control the multiuser access of:atabase

    Illustrative $%a!&le

    To illustrate the concet of concurrency control, consider t+o

    tra2ellers +ho 0o to electronic rail+ay reser2ation counter at the same

    time to urchase a train tic-et to the same destination on the same

    train. ThereOs only one seat left in the coach, but +ithout concurrency

    control, itOs ossible that both tra2elers +ill end u urchasin0 a tic-etfor that one seat. o+e2er, +ith concurrency control, the database

    +ouldnOt allo+ this to haen. @oth tra2ellers +ould still be able to

    access the train seatin0 database, but concurrency control +ould

    reser2e data accuracy and allo+ only one tra2eler to urchase the

    seat.

    This examle also illustrates the imortance of addressin0 this issue

    in a multiuser database. $b2iously, one could Iuic-ly run into

  • 8/9/2019 Database Assignment-2.doc

    9/10

    roblems +ith the inaccurate data that can result from se2eral

    transactions occurrin0 simultaneously and +ritin0 o2er each other.

    The follo+in0 section ro2ides strate0ies for imlementin0

    concurrency control.

    i) Transaction processing

    A tye of comuter rocessin0 in +hich the comuter resonds

    immediately to user reIuests. Each reIuest is considered to be a

    transaction.Automatic teller machines for ban-s are an examle of

    transaction rocessin0.

    The oosite of transaction rocessin0 is batch rocessin0, in +hich abatch of reIuests is stored and then executed all at one time.

    Transaction rocessin0 reIuires interaction +ith a user, +hereas batch

    rocessin0 can ta-e lace +ithout a user bein0 resent.

    ii) Optimization

    The maximiPin0 or minimiPin0 of a 0i2en function ossibly sub

  • 8/9/2019 Database Assignment-2.doc

    10/10

    +ill best satisfy a selected criterion. Also -no+n as system

    otimiPation.

    An act, rocess, or methodolo0y of ma-in0 somethin0 ?as a desi0n,

    system, or decision) as fully erfect, functional, or effecti2e as

    ossible secifically; the mathematical rocedures ?as findin0 the

    maximum of a function) in2ol2ed in this