day 5 sql server notes

Upload: elvisbooty

Post on 07-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 Day 5 SQL Server Notes

    1/2

    riggersWhen there is a change in one table and you want that the connected table should

    utomatically be updated we use the triggers. Triggers can be done on insert update and delete.You can write triggers on DDL

    atements which is possible from 2005.When you fire a insert/delete trigger the info doesnot goes directly to the table ,it goes to

    temporary table called insert/delete table. The trigger has two options of executing that it is fired before(INSTEAD OF) or

    ter(AFTER) the action.

    In deleted table, it stores all data that are marked to be deleted. It saves the data till theigger is getting fired.In Update query it creates both the table.The old query goes to the DELETED table and the

    ew record goes to INSERTED table and then from there it goes to orignal table. The Inserted and Deleted table are called as Pseudo or Magic Table . There can be only one trigger on a table. If you make multiple triggers on one table it will

    xecute whichever comes first.

    yntax:eate TRIGGER triggername

    n TABLE NAME

    FOR][INSTEAD OF][AFTER] {INSERT / UPDATE / DELETE}

    UERY

    ew Features in SQL SERVER 2005ew functions introduced

    1. row_numbers() : To write the row numbers2. rank () : to rank out the table if there are 12 rows with same id it will write 1 for all record but on 13 th it will w

    133. dense_rank() : same as rank but if there are 12 rows with same id it will write 1 for all record but on 13 th it wi

    write 24. ntile(number) :it makes groups of tuples. Suppose ntile(6) so 6 tuples in each row. Suppose if there are 40 tuples

    and I am making a group of 6 then it will make 6 groups and 4 tupes are remaining so it will add 1 tuple to eachgroup until the remainders are over.

    All Funtions in one Queryselect productname , unitprice , categoryid ,

    row_number () over (order by unitprice desc ) 'Row Number' ,rank () over (order by unitprice desc ) 'Rank' ,

    dense_rank () over (order by unitprice desc ) 'Dense Rank' ,ntile (9) over (order by unitprice desc ) 'nTile'

    from products

    artition by:You have created a groups of same elements as numbers you will find all 1's for 5-6 times, using partition by it

    ves the row number inside that same numbers.Partion by clause is included in all the row funtions but the effect is visible on the dense_row() and nTile().

    ommon Table Exprt the runtime we are creating a table.It is the temporary table.Which retrieves single rows at the same time.he processing is slow but comparison between those same tables is possible which helps us to find out the proper utcome.

    ursor are the objects of the database which reads one query at that time

  • 8/6/2019 Day 5 SQL Server Notes

    2/2

    Why CTE ? :Bcoz it is a way to use that reads the query at same timeevercisity is the term similar to recusive theory that checks the table one row at a time.A same table calls itself.

    th ytable (srno ,name )

    elect 1,'Lorem Ipsum' ),urtable (no,fname )

    elect 'Means Put Anything' ,'its Just a dummpy text where it takes any values' )lect * from mytable ,yourtable

    ew Datatypes:archar(max)

    varchar(max)

    arbinary(max)

    ransactionsou can either start a transaction or end it.uses keywords CREATE or END.

    ransaction levels:Read Uncommited: We have a transaction written and we r trying to read a record when im readinging a record

    meone has already modied the record but has not commited.What happens when he does a rollback-there will befferent results for different users.

    Read Committed :is the default transaction level onSerializable: It is the combination of repeatable read and read committed.Replacable Read: when a user is doing reading of some data that data is already committed by someone else

    hen i do a modification into it someone else are not allowed to read it.

    ocks help with the problems around in the transaction

    ypes of Locks:-hared Locks:2 ppl view a data and only 1 is allowed to do a transaction.

    Exclusive Lock: only 1 user is allowed to read and writechema lock: only 1 user can modify a structure at a time for a specific table.

    Deadlock