You are on page 1of 24

SQL-Server Interview Questions

What is RDBMS? Relational Data Base Management Systems (RDBMS) are database management systems that maintain data records and indices in tables. Relationships may be created and maintained across and among the data and tables. In a relational database, relationships between data items are expressed by means of tables. Interdependencies among these tables are expressed by data al!es rather than by pointers. "his allows a high degree of data independence. #n RDBMS has the capability to recombine the data items from different files, pro iding powerf!l tools for data !sage. What is normalization? Database normali$ation is a data design and organi$ation process applied to data str!ct!res based on r!les that help b!ild relational databases. In relational database design, the process of organi$ing data to minimi$e red!ndancy. %ormali$ation !s!ally in ol es di iding a database into two or more tables and defining relationships between the tables. "he ob&ecti e is to isolate data so that additions, deletions, and modifications of a field can be made in &!st one table and then propagated thro!gh the rest of the database ia the defined relationships. What are different normalization forms? 1NF: Eliminate Repeating Groups Ma'e a separate table for each set of related attrib!tes, and gi e each table a primary 'ey. (ach field contains at most one al!e from its attrib!te domain. 2NF: Eliminate Redundant Data If an attrib!te depends on only part of a m!lti) al!ed 'ey, remo e it to a separate table. 3NF: Eliminate Columns Not Dependent On Key If attrib!tes do not contrib!te to a description of the 'ey, remo e them to a separate table. #ll attrib!tes m!st be directly dependent on the primary 'ey BCNF: Boyce Codd Normal Form If there are non)tri ial dependencies between candidate 'ey attrib!tes, separate them o!t into distinct tables. !NF: "solate "ndependent #ultiple Relations$ips %o table may contain two or more *+n or n+m relationships that are not directly related. %NF: "solate &emantically Related #ultiple Relations$ips "here may be practical constrains on information that &!stify separating logically related many)to)many relationships. ONF: Optimal Normal Form # model limited to only simple (elemental) facts, as expressed in ,b&ect Role Model notation. DKNF: Domain Key Normal Form # model free from all modification anomalies. Remember, these normali$ation g!idelines are c!m!lati e. -or a database to be in .%-, it m!st first f!lfill all the criteria of a /%- and *%- database.

What is Stored Procedure? # stored proced!re is a named gro!p of S01 statements that ha e been pre io!sly created and stored in the ser er database. Stored proced!res accept inp!t parameters so that a single proced!re can be !sed o er the networ' by se eral clients !sing different inp!t data. #nd when the proced!re is modified, all clients a!tomatically get the new ersion. Stored proced!res red!ce networ' traffic and impro e performance. Stored proced!res can be !sed to help ens!re the integrity of the database. e.g. sp2helpdb, sp2renamedb, sp2depends etc. What is ri!!er? # trigger is a S01 proced!re that initiates an action when an e ent (I%S(R", D(1("( or 34D#"() occ!rs. "riggers are stored in and managed by the DBMS."riggers are !sed to maintain the referential integrity of data by changing the data in a systematic fashion. # trigger cannot be called or exec!ted5 the DBMS a!tomatically fires the trigger as a res!lt of a data modification to the associated table. "riggers can be iewed as similar to stored proced!res in that both consist of proced!ral logic that is stored at the database le el. Stored proced!res, howe er, are not e ent)dri e and are not attached to a specific table as triggers are. Stored proced!res are explicitly exec!ted by in o'ing a 6#11 to the proced!re while triggers are implicitly exec!ted. In addition, triggers can also exec!te stored proced!res. Nested Trigger: # trigger can also contain I%S(R", 34D#"( and D(1("( logic within itself, so when the trigger is fired beca!se of data modification it can also ca!se another data modification, thereby firing another trigger. # trigger that contains data modification logic within itself is called a nested trigger. What is "iew? # simple iew can be tho!ght of as a s!bset of a table. It can be !sed for retrie ing data, as well as !pdating or deleting rows. Rows !pdated or deleted in the iew are !pdated or deleted in the table the iew was created with. It sho!ld also be noted that as data in the original table changes, so does data in the iew, as iews are the way to loo' at part of the original table. "he res!lts of !sing a iew are not permanently stored in the database. "he data accessed thro!gh a iew is act!ally constr!cted !sing standard ")S01 select command and can come from one to many different base tables or e en other iews. What is Inde#? #n index is a physical str!ct!re containing pointers to the data. Indices are created in an existing table to locate rows more 7!ic'ly and efficiently. It is possible to create an index on one or more col!mns of a table, and each index is gi en a name. "he !sers cannot see the indexes, they are &!st !sed to speed !p 7!eries. (ffecti e indexes are one of the best ways to impro e performance in a database application. # table scan happens when there is no index a ailable to help a 7!ery. In a table scan S01 Ser er examines e ery row in the table to satisfy the 7!ery res!lts. "able scans are sometimes !na oidable, b!t on large tables, scans ha e a terrific impact on performance. Clustered indexes define the physical sorting of a database table8s rows in the storage media. -or this reason, each database table may ha e only one cl!stered index.

Non-clustered indexes are created o!tside of the database table and contain a sorted list of references to the table itself. What is the difference $etween clustered and a non-clustered inde#? # clustered index is a special type of index that reorders the way records in the table are physically stored. "herefore table can ha e only one cl!stered index. "he leaf nodes of a cl!stered index contain the data pages. # nonclustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on dis'. "he leaf node of a noncl!stered index does not consist of the data pages. Instead, the leaf nodes contain index rows. What are the different inde# confi!urations a ta$le can have? # table can ha e one of the following index config!rations+ %o indexes # cl!stered index # cl!stered index and many noncl!stered indexes # noncl!stered index Many noncl!stered indexes What is cursors? 6!rsor is a database ob&ect !sed by applications to manip!late data in a set on a row)by) row basis, instead of the typical S01 commands that operate on all the rows in the set at one time. In order to wor' with a c!rsor we need to perform some steps in the following order+ Declare c!rsor ,pen c!rsor -etch row from the c!rsor 4rocess fetched row 6lose c!rsor Deallocate c!rsor What is the use of DB%% commands? DB66 stands for database consistency chec'er. 9e !se these commands to chec' the consistency of the databases, i.e., maintenance, alidation tas' and stat!s chec's. (.g. DB66 6:(6;DB ) (ns!res that tables in the db and the indexes are correctly lin'ed. DB66 6:(6;#11,6 ) "o chec' that all pages in a db are correctly allocated. DB66 6:(6;-I1(<R,34 ) 6hec's all tables file gro!p for any damage. What is a Lin&ed Server? 1in'ed Ser ers is a concept in S01 Ser er by which we can add other S01 Ser er to a

<ro!p and 7!ery both the S01 Ser er dbs !sing ")S01 Statements. 9ith a lin'ed ser er, yo! can create ery clean, easy to follow, S01 statements that allow remote data to be retrie ed, &oined and combined with local data. Storped 4roced!re sp2addlin'edser er, sp2addlin'edsr login will be !sed add new 1in'ed Ser er. What is %ollation? 6ollation refers to a set of r!les that determine how data is sorted and compared. 6haracter data is sorted !sing r!les that define the correct character se7!ence, with options for specifying case)sensiti ity, accent mar's, 'ana character types and character width. What are different t'(e of %ollation Sensitivit'? Case sensitivity # and a, B and b, etc. Accent sensitivity a and =, o and >, etc. Kana Sensitivity 9hen ?apanese 'ana characters :iragana and ;ata'ana are treated differently, it is called ;ana sensiti e. Width sensitivity 9hen a single)byte character (half)width) and the same character when represented as a do!ble)byte character (f!ll)width) are treated differently then it is width sensiti e. What)s the difference $etween a (rimar' &e' and a uni*ue &e'? Both primary 'ey and !ni7!e enforce !ni7!eness of the col!mn on which they are defined. B!t by defa!lt primary 'ey creates a cl!stered index on the col!mn, where are !ni7!e creates a noncl!stered index by defa!lt. #nother ma&or difference is that, primary 'ey doesn8t allow %311s, b!t !ni7!e 'ey allows one %311 only. +ow to im(lement one-to-one, one-to-man' and man'-to-man' relationshi(s while desi!nin! ta$les? ,ne)to),ne relationship can be implemented as a single table and rarely as two tables with primary and foreign 'ey relationships. ,ne)to)Many relationships are implemented by splitting the data into two tables with primary 'ey and foreign 'ey relationships. Many)to)Many relationships are implemented !sing a &!nction table with the 'eys from both the tables forming the composite primary 'ey of the &!nction table. What is a -.L.%/? 3sing the %,1,6; 7!ery optimiser hint is generally considered good practice in order to impro e conc!rrency on a b!sy system. 9hen the %,1,6; hint is incl!ded in a S(1(6" statement, no loc's are ta'en when data is read. "he res!lt is a Dirty Read,

which means that another process co!ld be !pdating the data at the exact time yo! are reading it. "here are no g!arantees that yo!r 7!ery will retrie e the most recent data. "he ad antage to performance is that yo!r reading of data will not bloc' !pdates from ta'ing place, and !pdates will not bloc' yo!r reading of data. S(1(6" statements ta'e Shared (Read) loc's. "his means that m!ltiple S(1(6" statements are allowed sim!ltaneo!s access, b!t other processes are bloc'ed from modifying the data. "he !pdates will 7!e!e !ntil all the reads ha e completed, and reads re7!ested after the !pdate will wait for the !pdates to complete. "he res!lt to yo!r system is delay(bloc'ing). What is difference $etween D0L0 0 1 R2-%3 0 commands? Delete command remo es the rows from a table based on the condition that we pro ide with a 9:(R( cla!se. "r!ncate will act!ally remo e all the rows from a table and there will be no data in the table after we r!n the tr!ncate command. 'R(NC)'E "R3%6#"( is faster and !ses fewer system and transaction log reso!rces than D(1("(. "R3%6#"( remo es the data by deallocating the data pages !sed to store the table8s data, and only the page deallocations are recorded in the transaction log. "R3%6#"( remo es all rows from a table, b!t the table str!ct!re and its col!mns, constraints, indexes and so on remain. "he co!nter !sed by an identity for new rows is reset to the seed for the col!mn. Ao! cannot !se "R3%6#"( "#B1( on a table referenced by a -,R(I<% ;(A constraint. Beca!se "R3%6#"( "#B1( is not logged, it cannot acti ate a trigger. "R3%6#"( can not be Rolled bac' !sing logs. "R3%6#"( is DD1 6ommand. "R3%6#"( Resets identity of the table. DE*E'E D(1("( remo es rows one at a time and records an entry in the transaction log for each deleted row. If yo! want to retain the identity co!nter, !se D(1("( instead. If yo! want to remo e table definition and its data, !se the DR,4 "#B1( statement. D(1("( 6an be !sed with or witho!t a 9:(R( cla!se D(1("( #cti ates "riggers. D(1("( 6an be Rolled bac' !sing logs. D(1("( is DM1 6ommand. D(1("( does not reset identity of the table. Difference $etween 4unction and Stored Procedure? 3D- can be !sed in the S01 statements anywhere in the 9:(R(B:#CI%<BS(1(6" section where as Stored proced!res cannot be. 3D-s that ret!rn tables can be treated as another rowset. "his can be !sed in ?,I%s with other tables. Inline 3D-8s can be tho!gh of as iews that ta'e parameters and can be !sed in ?,I%s and other Rowset operations.

When is the use of 2PD3 05S 3 IS I%S command? "his command is basically !sed when a large processing of data has occ!rred. If a large amo!nt of deletions any modification or B!l' 6opy into the tables has occ!rred, it has to !pdate the indexes to ta'e these changes into acco!nt. 34D#"(2S"#"IS"I6S !pdates the indexes on these tables accordingly. What t'(es of 6oins are (ossi$le with S*l Server? ?oins are !sed in 7!eries to explain how different tables are related. ?oins also let yo! select data from a table depending !pon data from another table. "ypes of &oins+ I%%(R ?,I%s, ,3"(R ?,I%s, 6R,SS ?,I%s. ,3"(R ?,I%s are f!rther classified as 1(-" ,3"(R ?,I%S, RI<:" ,3"(R ?,I%S and -311 ,3"(R ?,I%S. What is the difference $etween a +3"I-7 %L32S0 and a W+0R0 %L32S0? Specifies a search condition for a gro!p or an aggregate. :#CI%< can be !sed only with the S(1(6" statement. :#CI%< is typically !sed in a <R,34 BA cla!se. 9hen <R,34 BA is not !sed, :#CI%< beha es li'e a 9:(R( cla!se. :a ing 6la!se is basically !sed only with the <R,34 BA f!nction in a 7!ery. 9:(R( 6la!se is applied to each row before they are part of the <R,34 BA f!nction in a 7!ery. :#CI%< criteria is applied after the the gro!ping of rows has occ!rred. What is su$-*uer'? 0#(lain (ro(erties of su$-*uer'8 S!b)7!eries are often referred to as s!b)selects, as they allow a S(1(6" statement to be exec!ted arbitrarily within the body of another S01 statement. # s!b)7!ery is exec!ted by enclosing it in a set of parentheses. S!b)7!eries are generally !sed to ret!rn a single row as an atomic al!e, tho!gh they may be !sed to compare al!es against m!ltiple rows with the I% 'eyword. # s!b7!ery is a S(1(6" statement that is nested within another ")S01 statement. # s!b7!ery S(1(6" statement if exec!ted independently of the ")S01 statement, in which it is nested, will ret!rn a res!lt set. Meaning a s!b7!ery S(1(6" statement can standalone and is not depended on the statement in which it is nested. # s!b7!ery S(1(6" statement can ret!rn any n!mber of al!es, and can be fo!nd in, the col!mn list of a S(1(6" statement, a -R,M, <R,34 BA, :#CI%<, andBor ,RD(R BA cla!ses of a ")S01 statement. # S!b7!ery can also be !sed as a parameter to a f!nction call. Basically a s!b7!ery can be !sed anywhere an expression can be !sed. +roperties o, &u- .uery # s!b7!ery m!st be enclosed in the parenthesis. # s!b7!ery m!st be p!t in the right hand of the comparison operator, and # s!b7!ery cannot contain a ,RD(R)BA cla!se. # 7!ery can contain more than one s!b)7!eries. What are t'(es of su$-*ueries? Single)row s!b7!ery, where the s!b7!ery ret!rns only one row.

M!ltiple)row s!b7!ery, where the s!b7!ery ret!rns m!ltiple rows,.and M!ltiple col!mn s!b7!ery, where the s!b7!ery ret!rns m!ltiple col!mns. What is SQL Profiler? S01 4rofiler is a graphical tool that allows system administrators to monitor e ents in an instance of Microsoft S01 Ser er. Ao! can capt!re and sa e data abo!t each e ent to a file or S01 Ser er table to analy$e later. -or example, yo! can monitor a prod!ction en ironment to see which stored proced!res are hampering performance by exec!ting too slowly. 3se S01 4rofiler to monitor only the e ents in which yo! are interested. If traces are becoming too large, yo! can filter them based on the information yo! want, so that only a s!bset of the e ent data is collected. Monitoring too many e ents adds o erhead to the ser er and the monitoring process and can ca!se the trace file or trace table to grow ery large, especially when the monitoring process ta'es place o er a long period of time. What is 2ser Defined 4unctions? 3ser)Defined -!nctions allow to define its own ")S01 f!nctions that can accept F or more parameters and ret!rn a single scalar data al!e or a table data type. What &ind of 2ser-Defined 4unctions can $e created? "here are three types of 3ser)Defined f!nctions in S01 Ser er /FFF and they are Scalar, Inline "able)Cal!ed and M!lti)statement "able) al!ed. &calar (ser De,ined Function # Scalar !ser)defined f!nction ret!rns one of the scalar data types. "ext, ntext, image and timestamp data types are not s!pported. "hese are the type of !ser)defined f!nctions that most de elopers are !sed to in other programming lang!ages. Ao! pass in F to many parameters and yo! get a ret!rn al!e. "nline 'a-le /alue (ser De,ined Function #n Inline "able)Cal!e !ser)defined f!nction ret!rns a table data type and is an exceptional alternati e to a iew as the !ser)defined f!nction can pass parameters into a ")S01 select command and in essence pro ide !s with a parameteri$ed, non)!pdateable iew of the !nderlying tables. #ulti statement 'a-le /alue (ser De,ined Function # M!lti)Statement "able)Cal!e !ser)defined f!nction ret!rns a table and is also an exceptional alternati e to a iew as the f!nction can s!pport m!ltiple ")S01 statements to b!ild the final res!lt where the iew is limited to a single S(1(6" statement. #lso, the ability to pass parameters into a ")S01 select command or a gro!p of them gi es !s the capability to in essence create a parameteri$ed, non)!pdateable iew of the data in the !nderlying tables. 9ithin the create f!nction command yo! m!st define the table str!ct!re that is being ret!rned. #fter creating this type of !ser)defined f!nction, It can be !sed in the -R,M cla!se of a ")S01 command !nli'e the beha ior fo!nd when !sing a stored proced!re which can also ret!rn record sets.

Which %P9IP (ort does SQL Server run on? +ow can it $e chan!ed? S01 Ser er r!ns on port *@... It can be changed from the %etwor' 3tility "64BI4 properties HI 4ort n!mber.both on client and the ser er. What are the authentication modes in SQL Server? +ow can it $e chan!ed? 9indows mode and mixed mode (S01 J 9indows). "o change a!thentication mode in S01 Ser er click Start, Programs, Microsoft SQL
Server and click SQL Enterprise Manager to run SQL Enterprise Manager from the Microsoft SQL Server program group. Select the server then from the Tools menu select SQL Server Configuration Properties, and choose the Security page.

Where are SQL server users names and (asswords are stored in s*l server? "hey get stored in master db in the sysxlogins table. Which command usin! Quer' 3nal'zer will !ive 'ou the version of SQL server and o(eratin! s'stem?
SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')

What is SQL server a!ent? S01 Ser er agent plays an important role in the day)to)day tas's of a database administrator (DB#). It is often o erloo'ed as one of the main tools for S01 Ser er management. Its p!rpose is to ease the implementation of tas's for the DB#, with its f!ll) f!nction sched!ling engine, which allows yo! to sched!le yo!r own &obs and scripts. %an a stored (rocedure call itself or recursive stored (rocedure? +ow man' level SP nestin! (ossi$le? Aes. Beca!se "ransact)S01 s!pports rec!rsion, yo! can write stored proced!res that call themsel es. Rec!rsion can be defined as a method of problem sol ing wherein the sol!tion is arri ed at by repetiti ely applying it to s!bsets of the problem. # common application of rec!rsi e logic is to perform n!meric comp!tations that lend themsel es to repetiti e e al!ation by the same processing steps. Stored proced!res are nested when one stored proced!re calls another or exec!tes managed code by referencing a 61R ro!tine, type, or aggregate. Ao! can nest stored proced!res and managed code references !p to ./ le els. What is ::0RR.R? "he KK(RR,R a!tomatic ariable ret!rns the error code of the last "ransact)S01 statement. If there was no error, KK(RR,R ret!rns $ero. Beca!se KK(RR,R is reset after each "ransact)S01 statement, it m!st be sa ed to a ariable if it is needed to process it f!rther after chec'ing it. What is Raiseerror? Stored proced!res report errors to client applications ia the R#IS(RR,R command. R#IS(RR,R doesn8t change the flow of a proced!re5 it merely displays an error

message, sets the KK(RR,R a!tomatic ariable, and optionally writes the message to the S01 Ser er error log and the %" application e ent log. What is lo! shi((in!?
Log shipping is the process of automating the ackup of data ase and transaction log files on a production SQL server, and then restoring them onto a stand y server. Enterprise Editions only supports log shipping. !n log shipping the transactional log file from one server is automatically updated into the ackup data ase on the other server. !f one server fails, the other server "ill have the same d can e used this as the #isaster $ecovery plan. The key feature of log shipping is that is "ill automatically ackup transaction logs throughout the day and automatically restore them on the stand y server at defined interval.

What is the difference $etween a local and a !lo$al varia$le? # local temporary table exists only for the d!ration of a connection or, if defined inside a compo!nd statement, for the d!ration of the compo!nd statement. # global temporary table remains in the database permanently, b!t the rows exist only within a gi en connection. 9hen connection are closed, the data in the global temporary table disappears. :owe er, the table definition remains with the database for access when database is opened next time. What command do we use to rename a d$? sp2renamedb Moldname8 , Mnewname8 If someone is !sing db it will not accept sp2renmaedb. In that case first bring db to single !ser !sing sp2dboptions. 3se sp2renamedb to rename database. 3se sp2dboptions to bring database to m!lti !ser mode. What is s(5confi!ure commands and set commands? 3se sp2config!re to display or change ser er)le el settings. "o change database)le el settings, !se #1"(R D#"#B#S(. "o change settings that affect only the c!rrent !ser session, !se the S(" statement. What are the different t'(es of re(lication? 0#(lain8 "he S01 Ser er /FFF)s!pported replication types are as follows+

"ransactional Snapshot Merge

&naps$ot replication distrib!tes data exactly as it appears at a specific moment in time and does not monitor for !pdates to the data. Snapshot replication is best !sed as a method for replicating data that changes infre7!ently or where the most !p)to)date al!es (low latency) are not a re7!irement. 9hen synchroni$ation occ!rs, the entire snapshot is generated and sent to S!bscribers.

'ransactional replication, an initial snapshot of data is applied at S!bscribers, and then when data modifications are made at the 4!blisher, the indi id!al transactions are capt!red and propagated to S!bscribers. #erge replication is the process of distrib!ting data from 4!blisher to S!bscribers, allowing the 4!blisher and S!bscribers to ma'e !pdates while connected or disconnected, and then merging the !pdates between sites when they are connected. What are the .S services that the SQL Server installation adds? MS S01 S(RC(R S(RCI6(, S01 #<(%" S(RCI6(, D"6 (Distrib!tion transac co) ordinator) What are three SQL &e'words used to chan!e or set someone)s (ermissions?
%$&'T, #E'(, and $E)*+E.

What does it mean to have *uoted5identifier on? What are the im(lications of havin! it off?

,hen SET Q-*TE#.!#E'T!/!E$ is *', identifiers can e delimited y dou le 0uotation marks, and literals must e delimited y single 0uotation marks. ,hen SET Q-*TE#.!#E'T!/!E$ is *//, identifiers cannot e 0uoted and must follo" all Transact1SQL rules for identifiers.

What is the S 244 function and how does it differ from the R0PL3%0 function? S"3-- f!nction to o erwrite existing characters. 3sing this syntax, S"3--(string2expression, start, length, replacement2characters), string2expression is the string that will ha e characters s!bstit!ted, start is the starting position, length is the n!mber of characters in the string that are s!bstit!ted, and replacement2characters are the new characters inter&ected into the string. R(41#6( f!nction to replace existing characters of all occ!rance. 3sing this syntax R(41#6((string2expression, search2string, replacement2string), where e ery incidence of search2string fo!nd in the string2expression will be replaced with replacement2string. 2sin! *uer' anal'zer, name ; wa's to !et an accurate count of the num$er of records in a ta$le? S(1(6" O -R,M table* S(1(6" 6,3%"(O) -R,M table* S(1(6" rows -R,M sysindexes 9:(R( id P ,B?(6"2ID(table*) #%D indid Q / +ow to re$uild Master Data$ase? Sh!tdown Microsoft S01 Ser er /FFF, and then r!n Reb!ildm.exe. "his is located in the 4rogram -ilesRMicrosoft S01 Ser erRLFR"oolsRBinn directory. In the Reb!ild Master dialog box, clic' Browse. In the Browse for -older dialog box, select the RData folder on the S01 Ser er /FFF compact disc or in the shared networ' directory from which S01 Ser er /FFF was installed, and then clic' ,;. 6lic' Settings. In the 6ollation Settings dialog box, erify or change settings !sed for the master database and all other databases. *F

Initially, the defa!lt collation settings are shown, b!t these may not match the collation selected d!ring set!p. Ao! can select the same settings !sed d!ring set!p or select new collation settings. 9hen done, clic' ,;. In the Reb!ild Master dialog box, clic' Reb!ild to start the process. "he Reb!ild Master !tility reinstalls the master database. "o contin!e, yo! may need to stop a ser er that is r!nning. So!rce+ http+BBmsdn/.microsoft.comBen)!sBlibraryBaa*NGNDF(S01.LF).aspx What is the $asic functions for master, msd$, model, tem(d$ data$ases? "he #aster database holds information for all databases located on the S01 Ser er instance and is the gl!e that holds the engine together. Beca!se S01 Ser er cannot start witho!t a f!nctioning master database, yo! m!st administer this database with care. "he msd- database stores information regarding database bac'!ps, S01 #gent information, D"S pac'ages, S01 Ser er &obs, and some replication information s!ch as for log shipping. "he tempd- holds temporary ob&ects s!ch as global and local temporary tables and stored proced!res. "he model is essentially a template database !sed in the creation of any new !ser database created in the instance. What are (rimar' &e's and forei!n &e's? +rimary 0eys are the !ni7!e identifiers for each row. "hey m!st contain !ni7!e al!es and cannot be n!ll. D!e to their importance in relational databases, 4rimary 'eys are the most f!ndamental of all 'eys and constraints. # table can ha e only one 4rimary 'ey. Foreign 0eys are both a method of ens!ring data integrity and a manifestation of the relationship between tables. What is data inte!rit'? 0#(lain constraints? Data integrity is an important feat!re in S01 Ser er. 9hen !sed properly, it ens!res that data is acc!rate, correct, and alid. It also acts as a trap for otherwise !ndetectable b!gs within applications.
& PRIMARY KEY constraint is a uni0ue identifier for a ro" "ithin a data ase ta le. Every ta le should have a primary key constraint to uni0uely identify each ro" and only one primary key constraint can e created for each ta le. The primary key constraints are used to enforce entity integrity. & UNIQUE constraint enforces the uni0ueness of the values in a set of columns, so no duplicate values are entered. The uni0ue key constraints are used to enforce entity integrity as the primary key constraints. & FOREIGN KEY constraint prevents any actions that "ould destroy links et"een ta les "ith the corresponding data values. & foreign key in one ta le points to a primary key in another ta le. /oreign keys prevent actions that "ould leave ro"s "ith foreign key values "hen there are no primary keys "ith that value. The foreign key constraints are used to enforce referential integrity. & CHECK constraint is used to limit the values that can e placed in a column. The check constraints are used to enforce domain integrity.

**

& NOT NULL constraint enforces that the column "ill not accept null values. The not null constraints are used to enforce domain integrity, as the check constraints.

What are the (ro(erties of the Relational ta$les? Relational tables ha e six properties+

Cal!es are atomic. 6ol!mn al!es are of the same 'ind. (ach row is !ni7!e. "he se7!ence of col!mns is insignificant. "he se7!ence of rows is insignificant. (ach col!mn m!st ha e a !ni7!e name.

What is De-normalization? De)normali$ation is the process of attempting to optimi$e the performance of a database by adding red!ndant data. It is sometimes necessary beca!se c!rrent DBMSs implement the relational model poorly. # tr!e relational DBMS wo!ld allow for a f!lly normali$ed database at the logical le el, while pro iding physical storage of data that is t!ned for high performance. De)normali$ation is a techni7!e to mo e from higher to lower normal forms of database modeling in order to speed !p database access. +ow to !et ::error and ::rowcount at the same time? If KKRowco!nt is chec'ed after (rror chec'ing statement then it will ha e F as the al!e of KKRecordco!nt as it wo!ld ha e been reset. #nd if KKRecordco!nt is chec'ed before the error)chec'ing statement then KK(rror wo!ld get reset. "o get KKerror and KKrowco!nt at the same time do both in same statement and store them in local ariable. S(1(6" KR6 P KKR,96,3%", K(R P KK(RR,R What is Identit'? Identity (or #!to%!mber) is a col!mn that a!tomatically generates n!meric al!es. # start and increment al!e can be set, b!t most DB# lea e these at *. # <3ID col!mn also generates n!mbers, the al!e of this cannot be controled. IdentityB<3ID col!mns do not need to be indexed. What is a Scheduled 6o$s or What is a Scheduled as&s? Sched!led tas's let !ser a!tomate processes that r!n on reg!lar or predictable cycles. 3ser can sched!le administrati e tas's, s!ch as c!be processing, to r!n d!ring times of slow b!siness acti ity. 3ser can also determine the order in which tas's r!n by creating &ob steps within a S01 Ser er #gent &ob. (.g. Bac' !p database, 3pdate Stats of "ables. ?ob steps gi e !ser control o er flow of exec!tion. If one &ob fails, !ser can config!re S01 Ser er #gent to contin!e to r!n the remaining tas's or to stop exec!tion. What is a ta$le called, if it does not have neither %luster nor -on-cluster Inde#? What is it used for? 3nindexed table or 1eap. Microsoft 4ress Boo's and Boo' ,n 1ine (B,1) refers it as

*/

:eap. # heap is a table that does not ha e a cl!stered index and, therefore, the pages are not lin'ed by pointers. "he I#M pages are the only str!ct!res that lin' the pages in a table together. 3nindexed tables are good for fast storing of data. Many times it is better to drop all indexes from table and than do b!l' of inserts and to restore those indexes after that. What is B%P? When does it used? B!l'6opy is a tool !sed to copy h!ge amo!nt of data from tables and iews. B64 does not copy the str!ct!res same as so!rce to destination. +ow do 'ou load lar!e data to the SQL server data$ase? B!l'6opy is a tool !sed to copy h!ge amo!nt of data from tables. B31; I%S(R" command helps to Imports a data file into a database table or iew in a !ser)specified format. %an we rewrite su$*ueries into sim(le select statements or with <oins? S!b7!eries can often be re)written to !se a standard o!ter &oin, res!lting in faster performance. #s we may 'now, an o!ter &oin !ses the pl!s sign (S) operator to tell the database to ret!rn all non)matching rows with %311 al!es. :ence we combine the o!ter &oin with a %311 test in the 9:(R( cla!se to reprod!ce the res!lt set witho!t !sing a s!b)7!ery. %an SQL Servers lin&ed to other servers li&e .racle? S01 Ser er can be lined to any ser er pro ided it has ,1()DB pro ider from Microsoft to allow a lin'. (.g. ,racle has a ,1()DB pro ider for oracle that Microsoft pro ides to add it as lin'ed ser er to S01 Ser er gro!p. +ow to &now which inde# a ta$le is usin!? S(1(6" table2name,index2name -R,M !ser2constraints +ow to co(' the ta$les, schema and views from one SQL server to another? Microsoft S01 Ser er /FFF Data "ransformation Ser ices (D"S) is a set of graphical tools and programmable ob&ects that lets !ser extract, transform, and consolidate data from disparate so!rces into single or m!ltiple destinations. What is Self 6oin? "his is a partic!lar case when one table &oins to itself, with one or two aliases to a oid conf!sion. # self &oin can be of any type, as long as the &oined tables are the same. # self &oin is rather !ni7!e in that it in ol es a relationship with only one table. "he common example is when company ha e a hierarchal reporting str!ct!re whereby one member of staff reports to another. What is %ross 6oin? # cross &oin that does not ha e a 9:(R( cla!se prod!ces the 6artesian prod!ct of the tables in ol ed in the &oin. "he si$e of a 6artesian prod!ct res!lt set is the n!mber of

*.

rows in the first table m!ltiplied by the n!mber of rows in the second table. "he common example is when company wants to combine each prod!ct with a pricing table to analy$e each prod!ct at each price. Which virtual ta$le does a tri!!er use? Inserted and Deleted. List few advanta!es of Stored Procedure8

Stored proced!re can red!ced networ' traffic and latency, boosting application performance. Stored proced!re exec!tion plans can be re!sed, staying cached in S01 Ser er8s memory, red!cing ser er o erhead. Stored proced!res help promote code re!se. Stored proced!res can encaps!late logic. Ao! can change stored proced!re code witho!t affecting clients. Stored proced!res pro ide better sec!rity to yo!r data.

What is DataWarehousin!?

Su$<ect-oriented, meaning that the data in the database is organi$ed so that all the data elements relating to the same real)world e ent or ob&ect are lin'ed together5 ime-variant, meaning that the changes to the data in the database are trac'ed and recorded so that reports can be prod!ced showing changes o er time5 -on-volatile, meaning that data in the database is ne er o er)written or deleted, once committed, the data is static, read)only, b!t retained for f!t!re reporting5 Inte!rated, meaning that the database contains data from most or all of an organi$ation8s operational applications, and that this data is made consistent.

What is .L P=.nLine ransaction Processin!>? In ,1"4 ) online transaction processing systems relational database design !se the discipline of data modeling and generally follow the 6odd r!les of data normali$ation in order to ens!re absol!te data integrity. 3sing these r!les complex information is bro'en down into its most simple str!ct!res (a table) where all of the indi id!al atomic le el elements relate to each other and satisfy the normali$ation r!les. +ow do SQL server ?@@@ and AML lin&ed? %an AML $e used to access data? FOR 2#* 3RO45 )('O5 E2+*"C"'6 Ao! can exec!te S01 7!eries against existing relational databases to ret!rn res!lts as TM1 rather than standard rowsets. "hese 7!eries can be exec!ted directly or from within stored proced!res. "o retrie e TM1 res!lts, !se the -,R TM1 cla!se of the S(1(6" statement and specify an TM1 mode of R#9, #3",, or (T41I6I". O+EN2#* ,4(%TM1 is a "ransact)S01 'eyword that pro ides a relationalBrowset iew o er an in)

*@

memory TM1 doc!ment. ,4(%TM1 is a rowset pro ider similar to a table or a iew. ,4(%TM1 pro ides a way to access TM1 data within the "ransact)S01 context by transferring data from an TM1 doc!ment into the relational tables. "h!s, ,4(%TM1 allows yo! to manage an TM1 doc!ment and its interaction with the relational en ironment. What is an e#ecution (lan? When would 'ou use it? +ow would 'ou view the e#ecution (lan? #n exec!tion plan is basically a road map that graphically or text!ally shows the data retrie al methods chosen by the S01 Ser er 7!ery optimi$er for a stored proced!re or ad) hoc 7!ery and is a ery !sef!l tool for a de eloper to !nderstand the performance characteristics of a 7!ery or stored proced!re since the plan is the one that S01 Ser er will place in its cache and !se to exec!te the stored proced!re or 7!ery. -rom within 0!ery #naly$er is an option called UShow (xec!tion 4lanV (located on the 0!ery drop) down men!). If this option is t!rned on it will display 7!ery exec!tion plan in separate window when 7!ery is ran again. +ow do 'ou im(lement one-to-one, one-to-man' and man'-to-man' relationshi(s while desi!nin! ta$les? ,ne)to),ne relationship can be implemented as a single table and rarely as two tables with primary and foreign 'ey relationships. ,ne)to)Many relationships are implemented by splitting the data into two tables with primary 'ey and foreign 'ey relationships. Many)to)Many relationships are implemented !sing a &!nction table with the 'eys from both the tables forming the composite primary 'ey of the &!nction table. It will be a good idea to read !p a database designing f!ndamentals text boo'. What)s the difference $etween a (rimar' &e' and a uni*ue &e'? Both primary 'ey and !ni7!e enforce !ni7!eness of the col!mn on which they are defined. B!t by defa!lt primary 'ey creates a cl!stered index on the col!mn, where are !ni7!e creates a noncl!stered index by defa!lt. #nother ma&or difference is that, primary 'ey doesn8t allow %311s, b!t !ni7!e 'ey allows one %311 only. What are user defined datat'(es and when 'ou should !o for them? 3ser defined datatypes let yo! extend the base S01 Ser er datatypes by pro iding a descripti e name, and format to the database. "a'e for example, in yo!r database, there is a col!mn called -light2%!m which appears in many tables. In all these tables it sho!ld be archar(L). In this case yo! co!ld create a !ser defined datatype called -light2n!m2type of archar(L) and !se it across all yo!r tables. See sp2addtype, sp2droptype in boo's online. What is $it datat'(e and what)s the information that can $e stored inside a $it column?

*D

Bit datatype is !sed to store boolean information li'e * or F (tr!e or false). 3ntill S01 Ser er E.D bit datatype co!ld hold either a * or F and there was no s!pport for %311. B!t from S01 Ser er G.F onwards, bit datatype can represent a third state, which is %311. Define candidate &e', alternate &e', com(osite &e'8 # candidate 'ey is one that can identify each row of a table !ni7!ely. <enerally a candidate 'ey becomes the primary 'ey of the table. If the table has more than one candidate 'ey, one of them will become the primary 'ey, and the rest are called alternate 'eys. # 'ey formed by combining at least two or more col!mns is called composite 'ey. What are defaults? Is there a column to which a default can)t $e $ound? # defa!lt is a al!e that will be !sed by a col!mn, if no al!e is s!pplied to that col!mn while inserting data. ID(%"I"A col!mns and timestamp col!mns can8t ha e defa!lts bo!nd to them. See 6R(#"( D(-#31" in boo's online. What is a transaction and what are 3%ID (ro(erties? # transaction is a logical !nit of wor' in which, all the steps m!st be performed or none. #6ID stands for #tomicity, 6onsistency, Isolation, D!rability. "hese are the properties of a transaction. -or more information and explanation of these properties, see S01 Ser er boo's online or any RDBMS f!ndamentals text boo'. (xplain different isolation le els #n isolation le el determines the degree of isolation of data between conc!rrent transactions. "he defa!lt S01 Ser er isolation le el is Read 6ommitted. :ere are the other isolation le els (in the ascending order of isolation)+ Read 3ncommitted, Read 6ommitted, Repeatable Read, Seriali$able. See S01 Ser er boo's online for an explanation of the isolation le els. Be s!re to read abo!t S(" "R#%S#6"I,% IS,1#"I,% 1(C(1, which lets yo! c!stomi$e the isolation le el at the connection le el. Read 6ommitted ) # transaction operating at the Read 6ommitted le el cannot see changes made by other transactions !ntil those transactions are committed. #t this le el of isolation, dirty reads are not possible b!t nonrepeatable reads and phantoms are possible. Read 3ncommitted ) # transaction operating at the Read 3ncommitted le el can see !ncommitted changes made by other transactions. #t this le el of isolation, dirty reads, nonrepeatable reads, and phantoms are all possible. Repeatable Read ) # transaction operating at the Repeatable Read le el is g!aranteed not to see any changes made by other transactions in al!es it has already read. #t this le el of isolation, dirty reads and nonrepeatable reads are not possible b!t phantoms are possible. Seriali$able ) # transaction operating at the Seriali$able le el g!arantees that all conc!rrent transactions interact only in ways that prod!ce the same effect as if each transaction were entirely exec!ted one after the other. #t this isolation le el, dirty reads, nonrepeatable reads, and phantoms are not possible. %R03 0 I-D0A m'Inde# .- m' a$le=m'%olumn>What t'(e of Inde# will !et created after e#ecutin! the a$ove statement?

*E

%on)cl!stered index. Important thing to note+ By defa!lt a cl!stered index gets created on the primary 'ey, !nless specified otherwise. What)s the ma#imum size of a row? LFEF bytes. Don8t be s!rprised with 7!estions li'e Mwhat is the maxim!m n!mber of col!mns per table8. *F/@ col!mns per table. 6hec' o!t S01 Ser er boo's online for the page titled+ WMaxim!m 6apacity SpecificationsW. (xplain #cti eB#cti e and #cti eB4assi e cl!ster config!rations :opef!lly yo! ha e experience setting !p cl!ster ser ers. B!t if yo! don8t, at least be familiar with the way cl!stering wor's and the two cl!sterning config!rations #cti eB#cti e and #cti eB4assi e. S01 Ser er boo's online has eno!gh information on this topic and there is a good white paper a ailable on Microsoft site. (xplain the architect!re of S01 Ser er "his is a ery important 7!estion and yo! better be able to answer it if consider yo!rself a DB#. S01 Ser er boo's online is the best place to read abo!t S01 Ser er architect!re. Read !p the chapter dedicated to S01 Ser er #rchitect!re. What is loc& escalation? 1oc' escalation is the process of con erting a lot of low le el loc's (li'e row loc's, page loc's) into higher le el loc's (li'e table loc's). ( ery loc' is a memory str!ct!re too many loc's wo!ld mean, more memory being occ!pied by loc's. "o pre ent this from happening, S01 Ser er escalates the many fine)grain loc's to fewer coarse)grain loc's. 1oc' escalation threshold was definable in S01 Ser er E.D, b!t from S01 Ser er G.F onwards it8s dynamically managed by S01 Ser er. What)s the difference $etween D0L0 0 3BL0 and R2-%3 0 3BL0 commands? D(1("( "#B1( is a logged operation, so the deletion of each row gets logged in the transaction log, which ma'es it slow. "R3%6#"( "#B1( also deletes all the rows in a table, b!t it won8t log the deletion of each row, instead it logs the deallocation of the data pages of the table, which ma'es it faster. ,f co!rse, "R3%6#"( "#B1( can be rolled bac'. "R3%6#"( "#B1( is f!nctionally identical to D(1("( statement with no 9:(R( cla!se+ both remo e all rows in the table. B!t "R3%6#"( "#B1( is faster and !ses fewer system and transaction log reso!rces than D(1("(. "he D(1("( statement remo es rows one at a time and records an entry in the transaction log for each deleted row. "R3%6#"( "#B1( remo es the data by deallocating the data pages !sed to store the table8s data, and only the page deallocations are recorded in the transaction log. "R3%6#"( "#B1( remo es all rows from a table, b!t the table str!ct!re and its col!mns, constraints, indexes and so on remain. "he co!nter !sed by an identity for new rows is reset to the seed for the col!mn. If yo! want to retain the identity co!nter, !se D(1("( instead. If yo! want to remo e table definition and its data, !se the DR,4 "#B1( statement. Ao! cannot !se "R3%6#"( "#B1( on a table referenced by a -,R(I<% ;(A constraint5 instead, !se D(1("( statement witho!t a 9:(R( cla!se.

*G

Beca!se "R3%6#"( "#B1( is not logged, it cannot acti ate a trigger. "R3%6#"( "#B1( may not be !sed on tables participating in an indexed iew 0#(lain the stora!e models of .L3P 6hec' o!t M,1#4, R,1#4 and :,1#4 in S01 Ser er boo's online for more infomation. What are the new features introduced in SQL Server ?@@@ =or the latest release of SQL Server at the time of 'our interview>? What chan!ed $etween the (revious version of SQL Server and the current version? "his 7!estion is generally as'ed to see how c!rrent is yo!r 'nowledge. <enerally there is a section in the beginning of the boo's online titled W9hat8s %ewW, which has all s!ch information. ,f co!rse, reading &!st that is not eno!gh, yo! sho!ld ha e tried those things to better answer the 7!estions. #lso chec' o!t the section titled WBac'ward 6ompatibilityW in boo's online which tal's abo!t the changes that ha e ta'en place in the new ersion. What are constraints? 0#(lain different t'(es of constraints8 6onstraints enable the RDBMS enforce the integrity of the database a!tomatically, witho!t needing yo! to create triggers, r!le or defa!lts. "ypes of constraints+ %," %311, 6:(6;, 3%I03(, 4RIM#RA ;(A, -,R(I<% ;(A. -or an explanation of these constraints see boo's online for the pages titled+ W6onstraintsW and W6R(#"( "#B1(W, W#1"(R "#B1(W What is an inde#? What are the t'(es of inde#es? +ow man' clustered inde#es can $e created on a ta$le? I create a se(arate inde# on each column of a ta$le8 What are the advanta!es and disadvanta!es of this a((roach? Indexes in S01 Ser er are similar to the indexes in boo's. "hey help S01 Ser er retrie e the data 7!ic'er. Indexes are of two types. 6l!stered indexes and non)cl!stered indexes. 9hen yo! create a cl!stered index on a table, all the rows in the table are stored in the order of the cl!stered index 'ey. So, there can be only one cl!stered index per table. %on) cl!stered indexes ha e their own storage separate from the table data storage. %on) cl!stered indexes are stored as B)tree str!ct!res (so do cl!stered indexes), with the leaf le el nodes ha ing the index 'ey and it8s row locater. "he row located co!ld be the RID or the 6l!stered index 'ey, depending !p on the absence or presence of cl!stered index on the table. If yo! create an index on each col!mn of a table, it impro es the 7!ery performance, as the 7!ery optimi$er can choose from all the existing indexes to come !p with an efficient exec!tion plan. #t the same t ime, data modification operations (s!ch as I%S(R", 34D#"(, D(1("() will become slow, as e ery time data changes in the table, all the indexes need to be !pdated. #nother disad antage is that, indexes need dis' space, the more indexes yo! ha e, more dis' space is !sed.

*L

What is R3ID and what are different t'(es of R3ID confi!urations? R#ID stands for Red!ndant #rray of Inexpensi e Dis's, !sed to pro ide fa!lt tolerance to database ser ers. "here are six R#ID le els F thro!gh D offering different le els of performance, fa!lt tolerance. MSD% has some information abo!t R#ID le els and for detailed information, chec' o!t the R#ID ad isory board8s homepage What are the ste(s 'ou will ta&e to im(rove (erformance of a (oor (erformin! *uer'? "his is a ery open ended 7!estion and there co!ld be a lot of reasons behind the poor performance of a 7!ery. B!t some general iss!es that yo! co!ld tal' abo!t wo!ld be+ %o indexes, table scans, missing or o!t of date statistics, bloc'ing, excess recompilations of stored proced!res, proced!res and triggers witho!t S(" %,6,3%" ,%, poorly written 7!ery with !nnecessarily complicated &oins, too m!ch normali$ation, excess !sage of c!rsors and temporary tables. Some of the toolsBways that help yo! tro!bleshooting performance problems are+ S(" S:,941#%2#11 ,%, S(" S:,941#%2"(T" ,%, S(" S"#"IS"I6S I, ,%, S01 Ser er 4rofiler, 9indows %" B/FFF 4erformance monitor, <raphical exec!tion plan in 0!ery #naly$er. Download the white paper on performance t!ning S01 Ser er from Microsoft web site. Don8t forget to chec' o!t s7l) ser er)performance.com What are the ste(s 'ou will ta&e, if 'ou are tas&ed with securin! an SQL Server? #gain this is another open ended 7!estion. :ere are some things yo! co!ld tal' abo!t+ 4referring %" a!thentication, !sing ser er, databse and application roles to control access to the data, sec!ring the physical database files !sing %"-S permissions, !sing an !ng!essable S# password, restricting physical access to the S01 Ser er, renaming the #dministrator acco!nt on the S01 Ser er comp!ter, disabling the <!est acco!nt, enabling a!diting, !sing m!ltiprotocol encryption, setting !p SS1, setting !p firewalls, isolating S01 Ser er from the web ser er etc. Read the white paper on S01 Ser er sec!rity from Microsoft website. #lso chec' o!t My S01 Ser er sec!rity best practices What is a deadloc& and what is a live loc&? +ow will 'ou !o a$out resolvin! deadloc&s? Deadloc' is a sit!ation when two processes, each ha ing a loc' on one piece of data, attempt to ac7!ire a loc' on the other8s piece. (ach process wo!ld wait indefinitely for the other to release the loc', !nless one of the !ser processes is terminated. S01 Ser er detects deadloc's and terminates one !ser8s process. # li eloc' is one, where a re7!est for an excl!si e loc' is repeatedly denied beca!se a series of o erlapping shared loc's 'eeps interfering. S01 Ser er detects the sit!ation after fo!r denials and ref!ses f!rther shared loc's. # li eloc' also occ!rs when read transactions monopoli$e a table or page, forcing a write transaction to wait indefinitely. 6hec' o!t S(" D(#D1,6;24RI,RI"A and WMinimi$ing Deadloc'sW in S01 Ser er boo's online. #lso chec' o!t the article 0*ENNEF from Microsoft 'nowledge base.

*N

What is $loc&in! and how would 'ou trou$leshoot it? Bloc'ing happens when one connection from an application holds a loc' and a second connection re7!ires a conflicting loc' type. "his forces the second connection to wait, bloc'ed on the first. Read !p the following topics in S01 Ser er boo's online+ 3nderstanding and a oiding bloc'ing, 6oding efficient transactions. (xplain 6R(#"( D#"#B#S( syntax Many of !s are !sed to creating databases from the (nterprise Manager or by &!st iss!ing the command+ 6R(#"( D#"#B#( MyDB. But what if 'ou have to create a data$ase with two file!rou(s, one on drive % and the other on drive D with lo! on drive 0 with an initial size of B@@ MB and with a !rowth factor of CDE? "hat8s why being a DB# yo! sho!ld be familiar with the 6R(#"( D#"#B#S( syntax. 6hec' o!t S01 Ser er boo's online for more information. +ow to restart SQL Server in sin!le user mode? +ow to start SQL Server in minimal confi!uration mode? S01 Ser er can be started from command line, !sing the S01S(RCR.(T(. "his (T( has some ery important parameters with which a DB# sho!ld be familiar with. )m is !sed for starting S01 Ser er in single !ser mode and )f is !sed to start the S01 Ser er in minimal config!ration mode. 6hec' o!t S01 Ser er boo's online for more parameters and their explanations. 3s a (art of 'our <o$, what are the DB%% commands that 'ou commonl' use for data$ase maintenance? DB66 6:(6;DB, DB66 6:(6;"#B1(, DB66 6:(6;6#"#1,<, DB66 6:(6;#11,6, DB66 S:,96,%"I<, DB66 S:RI%;D#"#B#S(, DB66 S:RI%;-I1( etc. B!t there are a whole load of DB66 commands which are ery !sef!l for DB#s. 6hec' o!t S01 Ser er boo's online for more information. What are statistics, under what circumstances the' !o out of date, how do 'ou u(date them? Statistics determine the selecti ity of the indexes. If an indexed col!mn has !ni7!e al!es then the selecti ity of that index is more, as opposed to an index with non)!ni7!e al!es. 0!ery optimi$er !ses these indexes in determining whether to choose an index or not while exec!ting a 7!ery. Some sit!ations !nder which yo! sho!ld !pdate statistics+ *) If there is significant change in the 'ey al!es in the index /) If a large amo!nt of data in an indexed col!mn has been added, changed, or remo ed (that is, if the distrib!tion of 'ey al!es has changed), or the table has been tr!ncated !sing the "R3%6#"( "#B1( statement and then repop!lated .) Database is !pgraded from a pre io!s ersion. 1oo' !p S01 Ser er boo's online for the following commands+ 34D#"( S"#"IS"I6S,

/F

S"#"S2D#"(, DB66 S:,92S"#"IS"I6S, 6R(#"( S"#"IS"I6S, DR,4 S"#"IS"I6S, sp2a!tostats, sp2createstats, sp2!pdatestats What are the different wa's of movin! data9data$ases $etween servers and data$ases in SQL Server? "here are lots of options a ailable, yo! ha e to choose yo!r option depending !pon yo!r re7!irements. Some of the options yo! ha e are+ B#6;34BR(S",R(, dettaching and attaching databases, replication, D"S, B64, logshipping, I%S(R"XS(1(6", S(1(6"XI%",, creating I%S(R" scripts to generate data. 0#(lain different t'(es of B3%/2Ps avaiala$e in SQL Server? 7iven a (articular scenario, how would 'ou !o a$out choosin! a $ac&u( (lan? "ypes of bac'!ps yo! can create in S01 Se er G.FS are -!ll database bac'!p, differential database bac'!p, transaction log bac'!p, filegro!p bac'!p. 6hec' o!t the B#6;34 and R(S",R( commands in S01 Ser er boo's online. Be prepared to write the commands in yo!r inter iew. Boo's online also has information on detailed bac'!pBrestore architect!re and when one sho!ld go for a partic!lar 'ind of bac'!p. What is data$ase re(lication? What are the different t'(es of re(lication 'ou can set u( in SQL Server? Replication is the process of copyingBmo ing data between databases on the same or different ser ers. S01 Ser er s!pports the following types of replication scenarios+ Y Snapshot replication Y "ransactional replication (with immediate !pdating s!bscribers, with 7!e!ed !pdating s!bscribers) Y Merge replication See S01 Ser er boo's online for indepth co erage on replication. Be prepared to explain how different replication agents f!nction, what are the main system tables !sed in replication etc. +ow to determine the service (ac& currentl' installed on SQL Server? "he global ariable KKCersion stores the b!ild n!mber of the s7lser r.exe, which is !sed to determine the ser ice pac' installed. "o 'now more abo!t this process isit S01 Ser er ser ice pac's and ersions. What are cursors? 0#(lain different t'(es of cursors8 What are the disadvanta!es of cursors? +ow can 'ou avoid cursors?
o

6!rsors allow row)by)row processing of the res!ltsets. "ypes of c!rsors+ Static, Dynamic, -orward)only, ;eyset)dri en. See boo's online for more information. Disad antages of c!rsors+ (ach time yo! fetch a row from the c!rsor, it res!lts in a networ' ro!ndtrip, where as a normal S(1(6" 7!ery ma'es only one ro!ndtrip, howe er large the res!ltset is. 6!rsors are also costly beca!se they re7!ire more reso!rces and temporary storage (res!lts in more I, operations). -!rther, there are restrictions on the S(1(6"

/*

statements that can be !sed with some types of c!rsors. Most of the times, set based operations can be !sed instead of c!rsors. :ere is an example+ If yo! ha e to gi e a flat hi'e to yo!r employees !sing the following criteria+ Salary between .FFFF and @FFFF Z DFFF hi'e Salary between @FFFF and DDFFF Z GFFF hi'e Salary between DDFFF and EDFFF Z NFFF hi'e. In this sit!ation many de elopers tend to !se a c!rsor, determine each employee8s salary and !pdate his salary according to the abo e form!la. B!t the same can be achie ed by m!ltiple !pdate statements or can be combined in a single 34D#"( statement as shown below+ UPDATE t l!e"p SET s#l#r$ % CASE &'E( s#l#r$ )ET&EE( *++++ A(D ,++++ T'E( s#l#r$ - .+++ &'E( s#l#r$ )ET&EE( ,++++ A(D ..+++ T'E( s#l#r$ - /+++ &'E( s#l#r$ )ET&EE( ..+++ A(D 0.+++ T'E( s#l#r$ 1++++ E(D #nother sit!ation in which de elopers tend to !se c!rsors+ Ao! need to call a stored proced!re when a col!mn in a partic!lar row meets certain condition. Ao! don8t ha e to !se c!rsors for this. "his can be achie ed !sing 9:I1( loop, as long as there is a !ni7!e 'ey to identify each row. -or examples of !sing 9:I1( loop for row by row processing, chec' o!t the MMy code library8 section of my site or search for 9:I1(. 9rite down the general syntax for a S(1(6" statements co ering all the options. :ere8s the basic syntax+ (#lso chec'o!t S(1(6" in boo's online for ad anced syntax). SELECT select!list 23(TO ne4!t# le!5 6RO7 t# le!source 2&'ERE se#rc8!condition5 29ROUP )Y :roup! $!e;pression5 2'AV3(9 se#rc8!condition5 2ORDER )Y order!e;pression 2ASC < DESC5 5

What is a <oin and e#(lain different t'(es of <oins8 ?oins are !sed in 7!eries to explain how different tables are related. ?oins also let yo! select data from a table depending !pon data from another table. "ypes of &oins+ I%%(R ?,I%s, ,3"(R ?,I%s, 6R,SS ?,I%s. ,3"(R ?,I%s are f!rther classified as 1(-" ,3"(R ?,I%S, RI<:" ,3"(R ?,I%S and -311 ,3"(R ?,I%S. -or more information see pages from boo's online titled+ W?oin -!ndamentalsW and W3sing ?oinsW. %an 'ou have a nested transaction? Aes, ery m!ch. 6hec' o!t B(<I% "R#%, 6,MMI", R,11B#6;, S#C( "R#% and KK"R#%6,3%" What is an e#tended stored (rocedure? %an 'ou instantiate a %.M o$<ect $' usin! -SQL? #n extended stored proced!re is a f!nction within a D11 (written in a programming lang!age li'e 6, 6SS !sing ,pen Data Ser ices (,DS) #4I) that can be called from ")

//

S01, &!st the way we call normal stored proced!res !sing the (T(6 statement. See boo's online to learn how to create extended stored proced!res and how to add them to S01 Ser er. Aes, yo! can instantiate a 6,M (written in lang!ages li'e CB, C6SS) ob&ect from ")S01 by !sing sp2,#6reate stored proced!re. #lso see boo's online for sp2,#Method, sp2,#<et4roperty, sp2,#Set4roperty, sp2,#Destroy. -or an example of creating a 6,M ob&ect in CB and calling it from ")S01, see MMy code library8 section of this site. What is the s'stem function to !et the current user)s user id? 3S(R2ID(). #lso chec' o!t other system f!nctions li'e 3S(R2%#M((), SAS"(M23S(R, S(SSI,%23S(R, 63RR(%"23S(R, 3S(R, S3S(R2SID(), :,S"2%#M((). What are tri!!ers? +ow man' tri!!ers 'ou can have on a ta$le? +ow to invo&e a tri!!er on demand? "riggers are special 'ind of stored proced!res that get exec!ted a!tomatically when an I%S(R", 34D#"( or D(1("( operation ta'es place on a table. In S01 Ser er E.D yo! co!ld define only . triggers per table, one for I%S(R", one for 34D#"( and one for D(1("(. -rom S01 Ser er G.F onwards, this restriction is gone, and yo! co!ld create m!ltiple triggers per each action. B!t in G.F there8s no way to control the order in which the triggers fire. In S01 Ser er /FFF yo! co!ld specify which trigger fires first or fires last !sing sp2settriggerorder. "riggers can8t be in o'ed on demand. "hey get triggered only when an associated action (I%S(R", 34D#"(, D(1("() happens on the table on which they are defined. "riggers are generally !sed to implement b!siness r!les, a!diting. "riggers can also be !sed to extend the referential integrity chec's, b!t where er possible, !se constraints for this p!rpose, instead of triggers, as constraints are m!ch faster. "ill S01 Ser er G.F, triggers fire only after the data modification operation happens. So in a way, they are called post triggers. B!t in S01 Ser er /FFF yo! co!ld create pre triggers also. Search S01 Ser er /FFF boo's online for I%S"(#D ,- triggers. #lso chec' o!t boo's online for Minserted table8, Mdeleted table8 and 6,13M%S234D#"(D() here is a tri!!er defined for I-S0R o(erations on a ta$le, in an .L P s'stem8 he tri!!er is written to instantiate a %.M o$<ect and (ass the newl' insterted rows to it for some custom (rocessin!8 What do 'ou thin& of this im(lementation? %an this $e im(lemented $etter? Instantiating 6,M ob&ects is a time cons!ming process and since yo! are doing it from within a trigger, it slows down the data insertion process. Same is the case with sending emails from triggers. "his scenario can be better implemented by logging all the necessary data into a separate table, and ha e a &ob which periodically chec's this table and does the needf!l. What is a self <oin? 0#(lain it with an e#am(le8

/.

o o

Self &oin is &!st li'e any other &oin, except that two instances of the same table will be &oined in the 7!ery. :ere is an example+ (mployees table which contains rows for normal employees as well as managers. So, to find o!t the managers of all the employees, yo! need a self &oin. CREATE TA)LE e"p ( e"pid int, ":rid int, e"pn#"e c8#r(1+) ) 3(SERT e"p SELECT 1,=,>V$#s> 3(SERT e"p SELECT =,*,>7o8#n> 3(SERT e"p SELECT *,(ULL,>S8o 8#> 3(SERT e"p SELECT ,,=,>S8rid8#r> 3(SERT e"p SELECT .,=,>Sour# 8> SELECT t1?e"pn#"e 2E"plo$ee5, t=?e"pn#"e 27#n#:er5 6RO7 e"p t1, e"p t= &'ERE t1?":rid % t=?e"pid 'ere>s #n #dv#nced @uer$ usin: # LE6T OUTER AO3( t8#t even returns t8e e"plo$ees 4it8out "#n#:ers (super osses) SELECT t1?e"pn#"e 2E"plo$ee5, COALESCE(t=?e"pn#"e, B(o "#n#:er>) 27#n#:er5 6RO7 e"p t1 LE6T OUTER AO3( e"p t= O( t1?":rid % t=?e"pid

/@

You might also like