Sie sind auf Seite 1von 9

1. What is a constraint in SQL?

A constraint is a restriction. Placed at either column or table level, a constraint ensures


that your data meets certain data integrity rules.
2. What is primary ey?
Primary !eys are the uni"ue identi#iers #or each ro$. %hey must contain uni"ue
values&and hence cannot be '(LL). A table can have a ma*imum o# one primary !ey.
+. ,o$ do you create a primary !ey?
We can create primary !ey using -./A%/ %A0L/ command or $ith an AL%/.
%A0L/ command
-reating the primary !ey at table creation
-./A%/ %A0L/ -ustomers
1
-ustomer'o int 23/'%2%4 '5%'(LL P.26A.4 /4,
-ustomer'ame varchar&78) '5% '(LL
9
-reating a primary !ey on an e*isting table
AL%/. %A0L/ /mployees
A33 -5'S%.A2'% P:/mployee23
P.26A.4 /4 &/mployee23)
;. What is a candidate !ey?
A table may have more than one combination o# columns that could uni"uely identi#y
the ro$s in a table< each combination is a candidate !ey
7. What are inde*es and What is the di##erence bet$een clustered and nonclustered
inde*es?
2nde*es in SQL Server are similar to the inde*es in boo!s. %hey help SQL Server
retrieve the data "uic!er
%here are clustered and nonclustered inde*es.
A clustered inde* is a special type o# inde* that reorders the $ay records in the table
are physically stored. %here#ore table can have only one clustered inde*. %he lea#
nodes o# a clustered inde* contain the data pages.
A nonclustered inde* is a special type o# inde* in $hich the logical order o# the inde*
does not match the physical stored order o# the ro$s on dis!. %he lea# node o# a
nonclustered inde* does not consist o# the data pages. 2nstead, the lea# nodes contain
inde* ro$s.
=. ,o$ can you increase SQL per#ormance ?
>ollo$ing are tips $hich $ill increase your SQl per#ormance ?@
/very inde* increases the time in ta!es to per#orm 2'S/.%S, (P3A%/S and
3/L/%/S, so the number o# inde*es should not be very much. %ry to use ma*imum
;@7 inde*es on one table, not more. 2# you have read@only table, then the number o#
inde*es may be increased.
eep your inde*es as narro$ as possible. %his reduces the siAe o# the inde* and
reduces the number o# reads re"uired to read the inde*.
%ry to create inde*es on columns that have integer values rather than character
values.
2# you create a composite &multi@column) inde*, the order o# the columns in the
!ey are very important. %ry to order the columns in the !ey as to enhance selectivity,
$ith the most selective columns to the le#tmost o# the !ey. v 2# you $ant to Boin
several tables, try to create surrogate integer !eys #or this purpose and create inde*es
on their columns.
-reate surrogate integer primary !ey &identity #or e*ample) i# your table $ill not
have many insert operations.
-lustered inde*es are more pre#erable than nonclustered, i# you need to select by a
range o# values or you need to sort results set $ith C.5(P 04 or 5.3/. 04 .
2# your application $ill be per#orming the same "uery over and over on the same
table, consider creating a covering inde* on the table.
4ou can use the SQL Server Pro#iler -reate %race WiAard $ith D2denti#y Scans o#
Large %ablesD trace to determine $hich tables in your database may need inde*es.
%his trace $ill sho$ $hich tables are being scanned by "ueries instead o# using an
inde*.
E. What is 3%S?
3%S is used to pull data #rom various sources into the star schema.
F. What is #ill#actor?
%he G#ill #actorG option speci#ies ho$ #ull SQL Server $ill ma!e each inde* page.
When there is no #ree space to insert ne$ ro$ on the inde* page, SQL Server $ill
create ne$ inde* page and trans#er some ro$s #rom the previous page to the ne$ one.
%his operation is called page splits. 4ou can reduce the number o# page splits by
setting the appropriate #ill #actor option to reserve #ree space on each inde* page. %he
#ill #actor is a value #rom 1 through 188 that speci#ies the percentage o# the inde* page
to be le#t empty. %he de#ault value #or #ill #actor is 8. 2t is treated similarly to a #ill
#actor value o# 188, the di##erence in that SQL Server leaves some space $ithin the
upper level o# the inde* tree #or >2LL>A-%5. H 8. %he #ill #actor percentage is used
only at the time the inde* is created. 2# the table contains read@only data &or data that
very rarely changed), you can set the G#ill #actorG option to 188. When the tableGs data
modi#ied very o#ten, you can decrease the #ill #actor to E8I or $hatever you thin! is
best.
J. What is .A23 and ho$ does it $or! ?
.edundant Array o# 2ndependent 3is!s &.A23) is a term used to describe the
techni"ue o# improving data availability through the use o# arrays o# dis!s and various
data@striping methodologies. 3is! arrays are groups o# dis! drives that $or! together
to achieve higher data@trans#er and 2K5 rates than those provided by single large
drives. An array is a set o# multiple dis! drives plus a specialiAed controller &an array
controller) that !eeps trac! o# ho$ data is distributed across the drives. 3ata #or a
particular #ile is $ritten in segments to the di##erent drives in the array rather than
being $ritten to a single drive.
>or speed and reliability, itGs better to have more dis!s. When these dis!s are arranged
in certain patterns and use a speci#ic controller, they are called a .edundant Array o#
2ne*pensive 3is!s &.A23) set. %here are several numbers associated $ith .A23, but
the most common are 1, 7 and 18.
.A23 1 $or!s by duplicating the same $rites on t$o hard drives. LetGs assume you
have t$o 28 Cigabyte drives. 2n .A23 1, data is $ritten at the same time to both
drives. .A231 is optimiAed #or #ast $rites.
.A23 7 $or!s by $riting parts o# data across all drives in the set &it re"uires at least
three drives). 2# a drive #ailed, the entire set $ould be $orthless. %o combat this
problem, one o# the drives stores a DparityD bit. %hin! o# a math problem, such as + L
E H 18. 4ou can thin! o# the drives as storing one o# the numbers, and the 18 is the
parity part. 0y removing any one o# the numbers, you can get it bac! by re#erring to
the other t$o, li!e this? + L M H 18. 5# course, losing more than one could be evil.
.A23 7 is optimiAed #or reads.
.A23 18 is a bit o# a combination o# both types. 2t doesnGt store a parity bit, so itGs
#ast, but it duplicates the data on t$o drives to be sa#e. 4ou need at least #our drives
#or .A23 18. %his type o# .A23 is probably the best compromise #or a database
server.
18. WhatGs the di##erence bet$een 3/L/%/ %A0L/ and %.('-A%/ %A0L/
commands?
>ollo$ing are di##erence bet$een them ?@
3/L/%/ %A0L/ synta* logs the deletes thus ma!ing the delete operation
slo$.%.('-A%/ table does not log any in#ormation but it logs in#ormation about
deallocation o# data page o# the table.So %.('-A%/ table is #aster as compared to
delete table.
11. What is 2nde*? 2tNs purpose?
2nde*es in databases are similar to inde*es in boo!s. 2n a database, an inde* allo$s
the database program to #ind data in a table $ithout scanning the entire table.
12. ,o$ to choose bet$een a -lustered 2nde* and a 'on@-lustered 2nde*?
Consider using a clustered index for:
-olumns that contain a large number o# distinct values.
Queries that return a range o# values using operators such as 0/%W//', O, OH,
P, and PH.
-olumns that are accessed se"uentially.
Non-clustered indexes have the same B-tree structure as clustered indexes, with
two significant differences:
data ro$s are not sorted and stored in order based on their non@clustered !eys.
%he lea# layer o# a non@clustered inde* does not consist o# the data pages. 2nstead,
the lea# nodes contain inde* ro$s. /ach inde* ro$ contains the non@clustered !ey
value and one or more ro$ locators that point to the data ro$ &or ro$s i# the inde* is
not uni"ue) having the !ey value.
Per table only 2;J non clustered inde*es.
1+. 3isadvantage o# inde*?
/very inde* increases the time in ta!es to per#orm 2'S/.%S, (P3A%/S and
3/L/%/S, so the number o# inde*es should not be very much.
1;. Civen a scenario that 2 have a 18 -lustered 2nde* in a %able to all their 18
-olumns. What are the advantages and disadvantages
5nly 1 clustered inde* is possible.
17. ,o$ can 2 en#orce to use particular inde*?
4ou can use inde* hint &inde*HPinde*:nameO) a#ter the table name. S/L/-%
au:lname >.56 authors &inde*Haunmind).
1=. What is 2nde* %uning?
5ne o# the hardest tas!s #acing database administrators is the selection o# appropriate
columns #or non@clustered inde*es. 4ou should consider creating non@clustered
inde*es on any columns that are #re"uently re#erenced in the W,/./ clauses o# SQL
statements. 5ther good candidates are columns re#erenced by Q52' and C.5(P 04
operations.
4ou may $ish to also consider creating non@clustered inde*es that cover all o# the
columns used by certain #re"uently issued "ueries. %hese "ueries are re#erred to as
Rcovered "ueriesS and e*perience e*cellent per#ormance gains.
2nde* %uning is the process o# #inding appropriate column #or non@clustered inde*es.
SQL Server provides a $onder#ul #acility !no$n as the 2nde* %uning WiAard $hich
greatly enhances the inde* selection process.
1E. 3i##erence bet$een 2nde* de#rag and 2nde* rebuild?
When you create an inde* in the database, the inde* in#ormation used by "ueries is
stored in inde* pages. %he se"uential inde* pages are chained together by pointers
#rom one page to the ne*t. When changes are made to the data that a##ect the inde*,
the in#ormation in the inde* can become scattered in the database. .ebuilding an
inde* reorganiAes the storage o# the inde* data &and table data in the case o# a
clustered inde*) to remove #ragmentation. %his can improve dis! per#ormance by
reducing the number o# page reads re"uired to obtain the re"uested data
30-- 2'3/M3/>.AC @ 3e#ragments clustered and secondary inde*es o# the
speci#ied table or vie$.
1F. What is sorting and $hat is the di##erence bet$een sorting T clustered inde*es?
%he 5.3/. 04 clause sorts "uery results by one or more columns up to F,8=8 bytes.
%his $ill happen by the time $hen $e retrieve data #rom database.
-lustered inde*es physically sorting data, $hile insertingKupdating the table.
1J. What are statistics, under $hat circumstances they go out o# date, ho$ do you
update them?
Statistics determine the selectivity o# the inde*es. 2# an inde*ed column has uni"ue
values then the selectivity o# that inde* is more, as opposed to an inde* $ith non@
uni"ue values. Query optimiAer uses these inde*es in determining $hether to choose
an inde* or not $hile e*ecuting a "uery.
Some situations under $hich you should update statistics?
1) 2# there is signi#icant change in the !ey values in the inde*
2) 2# a large amount o# data in an inde*ed column has been added, changed, or
removed &that is, i# the distribution o# !ey values has changed), or the table has been
truncated using the %.('-A%/ %A0L/ statement and then repopulated
+) 3atabase is upgraded #rom a previous version.
28. What are di##erent data types in SQL?
bigint 0inary 0it char cursor
datetime decimal #loat image int
money 'char 'te*t nvarchar real
smalldatetime smallint smallmoney te*t timestamp
tinyint varbinary varchar uni"ueidenti#ier
21. 3i##erence bet$een char and nvarchar K char and varchar data@type?
char[(n)] @ >i*ed@length non@(nicode character data $ith length o# n bytes. n must be
a value #rom 1 through F,888. Storage siAe is n bytes.
nvarchar(n) @ Uariable@length (nicode character data o# n characters. n must be a
value #rom 1 through ;,888. Storage siAe, in bytes, is t$o times the number o#
characters entered. %he data entered can be 8 characters in length
varchar[(n)] @ Uariable@length non@(nicode character data $ith length o# n bytes. n
must be a value #rom 1 through F,888. Storage siAe is the actual length in bytes o# the
data entered, not n bytes. %he data entered can be 8 characters in length.
22. C(23 datasiAe?
12F bit.
2+. ,o$ C(23 becoming uni"ue across machines?
%o ensure uni"ueness across machines, the 23 o# the net$or! card is used &among
others) to compute the number.
2;. What is the di##erence bet$een te*t and image data type?
%e*t and image. (se te*t #or character data i# you need to store more than 277
characters in SQL Server =.7, or more than F888 in SQL Server E.8. (se image #or
binary large obBects &0L50s) such as digital images. With te*t and image data types,
the data is not stored in the ro$, so the limit o# the page siAe does not apply.All that is
stored in the ro$ is a pointer to the database pages that contain the data.2ndividual
te*t, nte*t, and image values can be a ma*imum o# 2@C0, $hich is too long to store in
a single data ro$.
27. What are Boins?
Sometimes $e have to select data #rom t$o or more tables to ma!e our result
complete. We have to per#orm a Boin.
2=. ,o$ many types o# Qoins?
Qoins can be categoriAed as?
2nner Boins &the typical Boin operation, $hich uses some comparison operator li!e
H or PO). %hese include e"ui@Boins and natural Boins.
2nner Boins use a comparison operator to match ro$s #rom t$o tables based on the
values in common columns #rom each table. >or e*ample, retrieving all ro$s $here
the student identi#ication number is the same in both the students and courses tables.
5uter Boins. 5uter Boins can be a le#t, a right, or #ull outer Boin.
5uter Boins are speci#ied $ith one o# the #ollo$ing sets o# !ey$ords $hen they are
speci#ied in the >.56 clause?
L/>% Q52' or L/>% 5(%/. Q52' @%he result set o# a le#t outer Boin includes all
the ro$s #rom the le#t table speci#ied in the L/>% 5(%/. clause, not Bust the ones in
$hich the Boined columns match. When a ro$ in the le#t table has no matching ro$s
in the right table, the associated result set ro$ contains null values #or all select list
columns coming #rom the right table.
.2C,% Q52' or .2C,% 5(%/. Q52' @ A right outer Boin is the reverse o# a le#t
outer Boin. All ro$s #rom the right table are returned. 'ull values are returned #or the
le#t table any time a right table ro$ has no matching ro$ in the le#t table.
>(LL Q52' or >(LL 5(%/. Q52' @ A #ull outer Boin returns all ro$s in both the
le#t and right tables. Any time a ro$ has no match in the other table, the select list
columns #rom the other table contain null values. When there is a match bet$een the
tables, the entire result set ro$ contains data values #rom the base tables.
-ross Boins @ -ross Boins return all ro$s #rom the le#t table, each ro$ #rom the le#t
table is combined $ith all ro$s #rom the right table. -ross Boins are also called
-artesian products. &A -artesian Boin $ill get you a -artesian product. A -artesian
Boin is $hen you Boin every ro$ o# one table to every ro$ o# another table. 4ou can
also get one by Boining every ro$ o# a table to every ro$ o# itsel#.)
2E. What is sel# Boin?
A table can be Boined to itsel# in a sel#@Boin.
2F. What are the di##erences bet$een ('25' and Q52'S?
A join selects columns #rom 2 or more tables.
A union selects ro$s.
2J. -an 2 improve per#ormance by using the A'S2@style Boins instead o# the old@style
Boins?
Code xam!le ":
select o.name, i.name
#rom sysobBects o, sysinde*es i
$here o.id H i.id
Code xam!le #:
select o.name, i.name
#rom sysobBects o inner Boin sysinde*es i
on o.id H i.id
4ou $ill not get any per#ormance gain by s$itching to the A'S2@style Q52' synta*.
(sing the A'S2@Q52' synta* gives you an important advantage? 0ecause the Boin
logic is cleanly separated #rom the #iltering criteria, you can understand the "uery
logic more "uic!ly.
%he SQL Server old@style Q52' e*ecutes the #iltering conditions be#ore e*ecuting the
Boins, $hereas the A'S2@style Q52' reverses this procedure &Boin logic precedes
#iltering).
Perhaps the most compelling argument #or s$itching to the A'S2@style Q52' is that
6icroso#t has e*plicitly stated that SQL Server $ill not support the old@style 5(%/.
Q52' synta* inde#initely. Another important consideration is that the A'S2@style
Q52' supports "uery constructions that the old@style Q52' synta* does not support.
+8. What is derived table?
3erived tables are S/L/-% statements in the >.56 clause re#erred to by an alias or
a user@speci#ied name. %he result set o# the S/L/-% in the >.56 clause #orms a
table used by the outer S/L/-% statement. >or e*ample, this S/L/-% uses a derived
table to #ind i# any store carries all boo! titles in the pubs database?
S/L/-% S%.stor:id, S%.stor:name
>.56 stores AS S%,
&S/L/-% stor:id, -5('%&32S%2'-% title:id) AS title:count
>.o6 Sales
C.5(P 04 stor:id
)AS SA
W,/./ S%.stor:id H SA.stor:id
A'3 SA.title:count H &S/L/-% -5('%&V) >.56 titles)
+1. What is Stored procedure?
A stored procedure is a set o# Structured Query Language &SQL) statements that you
assign a name to and store in a database in compiled #orm so that you can share it
bet$een a number o# programs.
%hey allo$ modular programming.
%hey allo$ #aster e*ecution.
%hey can reduce net$or! tra##ic.
%hey can be used as a security mechanism.
+2. What are the di##erent types o# Storage Procedure?
"$ %em!orar& 'tored (rocedures @ SQL Server supports t$o types o# temporary
procedures? local and global. A local temporary procedure is visible only to the
connection that created it. A global temporary procedure is available to all
connections. Local temporary procedures are automatically dropped at the end o# the
current session. Clobal temporary procedures are dropped at the end o# the last
session using the procedure. (sually, this is $hen the session that created the
procedure ends. %emporary procedures named $ith W and WW can be created by any
user.
#$ '&stem stored !rocedures are created and stored in the master database and have
the sp: pre#i*.&or *p:) System stored procedures can be e*ecuted #rom any database
$ithout having to "uali#y the stored procedure name #ully using the database name
master. &2# any user@created stored procedure has the same name as a system stored
procedure, the user@created stored procedure $ill never be e*ecuted.)
)$ *utomaticall& xecuting 'tored (rocedures @ 5ne or more stored procedures can
e*ecute automatically $hen SQL Server starts. %he stored procedures must be created
by the system administrator and e*ecuted under the sysadmin #i*ed server role as a
bac!ground process. %he procedure&s) cannot have any input parameters.
+$ ,ser stored !rocedure$
++. ,o$ do 2 mar! the stored procedure to automatic e*ecution?
4ou can use the sp:procoption system stored procedure to mar! the stored procedure
to automatic e*ecution $hen the SQL Server $ill start. 5nly obBects in the master
database o$ned by dbo can have the startup setting changed and this option is
restricted to obBects that have no parameters.
USE master
EXEC sp_procoption 'indRebuild', 'startup', 'true')
+;. ,o$ $ill !no$ $hether the SQL statements are e*ecuted?
When used in a stored procedure, the ./%(.' statement can speci#y an integer value
to return to the calling application, batch, or procedure. 2# no value is speci#ied on
./%(.', a stored procedure returns the value 8. %he stored procedures return a
value o# 8 $hen no errors $ere encountered. Any nonAero value indicates an error
occurred.
+7. Why one should not pre#i* user stored procedures $ith sp:?
2t is strongly recommended that you do not create any stored procedures using sp: as
a pre#i*. SQL Server al$ays loo!s #or a stored procedure beginning $ith sp: in this
order?
%he stored procedure in the master database.
%he stored procedure based on any "uali#iers provided &database name or o$ner).
%he stored procedure using dbo as the o$ner, i# one is not speci#ied.
%here#ore, although the user@created stored procedure pre#i*ed $ith sp: may e*ist in
the current database, the master database is al$ays chec!ed #irst, even i# the stored
procedure is "uali#ied $ith the database name.
+=. What can cause a Stored procedure e*ecution plan to become invalidated andKor
#all out o# cache?
Server restart
Plan is aged out due to lo$ use
30-- >.//P.5--A-,/ &sometime desired to #orce it)
+E. When $e need to recompile stored procedure?
2# a ne$ inde* is added #rom $hich the stored procedure might bene#it, optimiAation
does not automatically happen &until the ne*t time the stored procedure is run a#ter
SQL Server is restarted).
+F. SQL Server provides three $ays to recompile a stored procedure
%he sp:recompile system stored procedure #orces a recompile o# a stored
procedure the ne*t time it is run.
-reating a stored procedure that speci#ies the W2%, ./-56P2L/ option in its
de#inition indicates that SQL Server does not cache a plan #or this stored procedure<
the stored procedure is recompiled each time it is e*ecuted. (se the W2%,
./-56P2L/ option $hen stored procedures ta!e parameters $hose values di##er
$idely bet$een e*ecutions o# the stored procedure, resulting in di##erent e*ecution
plans to be created each time. (se o# this option is uncommon, and causes the stored
procedure to e*ecute more slo$ly because the stored procedure must be recompiled
each time it is e*ecuted.
4ou can #orce the stored procedure to be recompiled by speci#ying the W2%,
./-56P2L/ option $hen you e*ecute the stored procedure. (se this option only i#
the parameter you are supplying is atypical or i# the data has signi#icantly changed
since the stored procedure $as created.
+J. ,o$ $ill you handle /rrors in S"l Stored Procedure?
INSERT NonFatal V!UES "#Column$)
IF ##ERR%R &'(
)E*IN
+RINT 'Error %ccured'
EN,
;8. ,o$ $ill you raise an error in s"l?
.A2S/..5. @ .eturns a user@de#ined error message and sets a system #lag to record
that an error has occurred. (sing .A2S/..5., the client can either retrieve an entry
#rom the sysmessages table or build a message dynamically $ith user@speci#ied
severity and state in#ormation. A#ter the message is de#ined it is sent bac! to the client
as a server error message.
;1. 1. WhatNs the di##erence bet$een a primary !ey and a uni"ue !ey?
0oth primary !ey and uni"ue en#orce uni"ueness o# the column on $hich they are
de#ined. 0ut by de#ault primary !ey creates a clustered inde* on the column, $here
are uni"ue creates a nonclustered inde* by de#ault. Another maBor di##erence is that,
primary !ey doesnNt allo$ '(LLs, but uni"ue !ey allo$s one '(LL only.
;2. Write a SQL Query to #ind #irst Wee! 3ay o# month
S/L/-% 3A%/'A6/&d$, 3A%/A33&dd, @ 3A%/PA.%&dd, C/%3A%/&)) L 1,
C/%3A%/&))) AS >irst3ay
;+. ,o$ to #ind =th highest salary #rom /mployee table?
S/L/-% %5P 1 salary >.56 &S/L/-% 32S%2'-% %5P = salary >.56 employee
5.3/. 04 salary 3/S-) a 5.3/. 04 salary
;;. ,o$ can 2 en#orce to use particular inde*?
4ou can use inde* hint &inde*Hinde*:name) a#ter the table name. S/L/-% au:lname
>.56 authors &inde*Haunmind)
;7. What is the .e#erential 2ntegrity?
.e#erential integrity re#ers to the consistency that must be maintained bet$een
primary and #oreign !eys, i.e. every #oreign !ey value must have a corresponding
primary !ey value
;=. ,o$ to determine the service pac! currently installed on SQL Server?
%he global variable --.ersion stores the build number o# the s"lservr.e*e, $hich is
used to determine the service pac! installed.
;E. What is the use o# S-5P/:23/'%2%4&) #unction?
.eturns the most recently created identity value #or the tables in the current e*ecution
scope.
;F. What are the di##erent $ays o# moving dataKdatabases bet$een servers and
databases in SQL Server?
%here are lots o# options available, you have to choose your option depending upon
your re"uirements. Some o# the options you have are? 0A-(PK./S%5./,
detaching and attaching databases, replication, 3%S, 0-P, logshipping,
2'S/.%...S/L/-%, S/L/-%...2'%5, creating 2'S/.% scripts to generate data.
;J. What is a deadloc!?
3eadloc! is a situation $hen t$o processes, each having a loc! on one piece o# data,
attempt to ac"uire a loc! on the otherGs piece. /ach process $ould $ait inde#initely #or
the other to release the loc!, unless one o# the user processes is terminated. SQL
Server detects deadloc!s and terminates one userGs process.
78. What are de#aults? 2s there a column to $hich a de#ault canGt be bound?
A de#ault is a value that $ill be used by a column, i# no value is supplied to that
column $hile inserting data. 23/'%2%4 columns and timestamp columns canGt have
de#aults bound to them.

Das könnte Ihnen auch gefallen