0% found this document useful (0 votes)
41 views

SQL SERVER Questions and Answers What Is An Entity?

The document discusses various concepts related to SQL Server including entities, attributes, ER diagrams, keys, joins, subqueries, constraints, rules, indexes, views, stored procedures, bulk copy program, data transformation services, transactions, locking, deadlocks, and triggers. It provides definitions and explanations of these concepts with examples.

Uploaded by

am_jalu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

SQL SERVER Questions and Answers What Is An Entity?

The document discusses various concepts related to SQL Server including entities, attributes, ER diagrams, keys, joins, subqueries, constraints, rules, indexes, views, stored procedures, bulk copy program, data transformation services, transactions, locking, deadlocks, and triggers. It provides definitions and explanations of these concepts with examples.

Uploaded by

am_jalu
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

SQL SERVER Questions And Answers What is an Entity? The basic data item stored in database is called entity.

An entity can be any object, item, place, person, concept, or activity about which data is stored. What is an attribute? An attribute is a property of an entity. It describes a part of an entity. Entity could have one or more attributes. What is ER diagram? An Entity Relationship Diagram is diagrammatic representation of the logical structure of a database system.

Describe the concept of keys !andidate key An attribute that uni uely identifies a row is called candidate !ey. It is also called das surrogate !ey. "rimary key A candidate !ey that you choose to identify rows uni uely is called a primary !ey. A#ternate key If there are multiple candidate !eys in a table, the candidate !eys that are chosen as primary !ey are called the alternate !eys. !omposite key "hen the !ey that uni uely identifies the rows of a table is made up of more than one attribute, it is called as a composite !ey. $oreign key Two tables can be related using a common attribute. "hen a primary !ey of one table is also available as an attribute in another related table it is called a foreign !ey.

What are %oins? #ometimes, data from multiple tables is to be displayed using select statement. $or this purpose, the tables in use must have a column that is e uated. This is termed as simple join or multiple join. #ometimes, you might want to display all records from one table and some from another. This type of join is called an outer join. An outer join is only possible between two tables. There are two types of outer join, namely left and right. In a left outer join, all the rows of the first table named in the $R%& clause are displayed. In a right outer join all the rows from the second table mentioned in the $R%& clause are displayed. In either case, all the matching rows from other table are displayed. What is a sub &uery? #ometimes the results of one uery are dependant on the results of another uery. $or this purpose one uery is nested inside another uery, this is called as sub uery. What are the types of constraints? 'ou can enforce data integrity by using (onstraints are divided in to five categories. "rimary key constraint A primary !ey constraint is defined on a column are a set of columns whose values uni uely identify the rows in a table. It cannot contain null values. 'ni&ue constraint constraints.

)ni ue constraints are used to enforce uni ueness on non* primary !ey columns. It allows null values but only one row can have a null value. &ultiple uni ue constraints can be created on a table. $oreign (ey constraint 'ou can use the foreign !ey constraint to remove the inconsistency in two tables when the data in the one table is dependant on the other table. !heck constraint It enforces domain integrity by restricting the values to be inserted in a column. It is possible to define multiple chec! constraints on a single column. These are evaluated in the order in which they are defined. Defau#t !onstraint A default constraint can be use to assign a constant value to a column and the user need not insert values in to that column. ) What is a ru#e? The re uired integrity can be enforced by specifying a chec! constraint or by defining a rule. +ut chec! constraint modifies the table structure. The constraint can there fore be implemented using rules with out changing the table structure. This rule is applied before an insert or update statement . A rule must be bound to a column or a user*define d data type. This is done using stored procedure sp,bindrule. Rules do not apply to data that has already been inserted in the table. The e-isting values in tables do not have to meet the criteria specified by the rule. What are inde*es? To speed up data retrieval inde-es are used. Inde-es also enforce the uni ueness of rows.
3

Ad+antages Improves the speed of e-ecution. Enforces uni ueness of data. #peeds up joins between tables. Disad+antages ..Ta!es dis! space to store. /.Data modification ta!es longer. 0.Ta!es time to create inde-. ,ypes of inde*es !#ustered inde* ..The data is physically sorted /.%ne clustered inde- can be created per table, so you should build it on attributes that have a high percentage of uni ue values and that are not modified often. -onc#ustered inde* ..The physical order of the rows is not the same as the indeorder. /.There can be as many as /12 nonclustered inde-es per column. What are +iews? A view is a virtual table, which gives access to a subset of columns from one or more tables. It is a uery stored as an object in the database. 3ence a view is an object that derives it data from one or more tables. Ad+antages ..A view serves as a security mechanism.
4

/.A view simplifies the usage of comple- ueries. . What are store procedures and its ad+antages? A stored procedure is collection or batch of Transact*#45 statements and control flow language that is stored under one name, and e-ecuted as single unit. It helps in improving the performance of a uery. It is a precompiled object. As it is ready to e-ecute no time is needed for parsing and compiling the procedure. Ad+antages Improved performance 6 Applications do not have to compile the procedure over and over again. Reduction in networ! congestion 6 applications need not submit multiple #45 statements to server for the purpose of processing. Enhanced accuracy 6 #45 statements included in a procedure are designed by e-perienced programmers and are therefore more efficient, error free, and tested. +etter security mechanism 6 users can be granted permission to e-ecute a stored procedure even if they do not own it. ,ypes of stored procedures ..)ser*defined /.#ystem defined These are prefi-ed with sp,. These are for administrative purpose and are stored in the database and are accessible to all users. 0.Temporary

These are prefi-ed with 7, stored in tempdb and are automatically dropped when connection terminates. 1.Remote These are created and stored in remote servers and can accessed by users with appropriate permissions. 8.E-tended These are dlls that are e-ecuted outside #45 #erver. They are prefi-ed by -p,. E*p#ain about /!" and D,S /!" The transfer data from an e-ternal source to #45 #erver is performed using +ul! (opy 9rogram utility. The e-ternal source is a flat file. Data transfer from e-ternal source to #45 #erver in +(9 I:. The transfer of data from #45 #erver to e-ternal source is +(9 %)T. D,S Data transformation services can be used to import and e-port data between heterogeneous data sources and #45 #erver. The e-ternal data sources include ;isual $o-9ro, &# E-cel, 9arado-, &# Access, Dbase, and te-t files.

What are transactions and their properties? A transaction is a se uence of operations performed together as a single unit of logical wor!. It has four properties. Atomicity 6 it states that either all the data modifications are performed or none are performed.
6

(onsistency * it states that all the data is in a consistent state after a successful completion of transaction. Isolation 6 it states any data modification made my concurrent transactions must be isolated from the modifications made by other concurrent transaction. Durability 6 it states that ay change made by a completed transaction remains permanently in the system. 0 Locking mechanism #45 #erver uses the concept of loc!ing to ensure transactional integrity and database consistency. 5oc!ing, by functionally prevents users from accessing information being changed by other users. In a multi*user environment, 5oc!ing prevents users from changing the same data at the same time. In #45 #erver loc!ing is implemented automatically. #45 #erver implements multi*granular loc!ing, which allows transactions to loc! different types of resources at different levels. #45 #erver can loc! the following resources. RID 6 is a row identifier that individually loc!s a row in a table. <ey 6 is a rowloc! with in an inde-. 9age 6 is an =! page or an inde- page. E-tent 6 is a contiguous group of =! data pages or indepages. Table 6 is the entire table, including all data and inde-es. Database 6 is the complete database. Shared Locks It is used for operations that do not change or update the data. This allows concurrent transactions to read a resource and no other transaction can modify the data on that resource. 'pdate Locks
7

This loc! is implemented when a transaction modify a row. %nly one update loc! is allowed on a resource at a time. E*c#usi+e #ocks 1ntent #ocks Schema Locks What is dead#ock? A dead loc! is a situation in which two users >or transactions? have loc!s on separate objects, and each user is waiting for a loc! on the other@s object. It usually occurs in a multi*user environment. What are triggers? A trigger is a bloc! of code that constitutes with a set of T* #45 statements that are activated in response to certain actions. A trigger can also be interpreted as a special !ind of stored procedure that is e-ecuted whenever an action, such as data modification, ta!es place. A trigger is always defined on a table, and is said to have fired whenever the data in the underlying table is affected by any of the Data &anipulation 5anguage >D&5? statements*I:#ERT, )9DATE, or DE5ETE. A trigger fires in response to an event li!e insertion, updation, and deletion of data. Triggers help in maintaining consistent, reliable, and correct data in tables. They enable the performance of compleactions and cascade these actions to other dependant tables. (haracteristics of a triggerA It can be associated with tables.

It cannot be defined on temporary tables or views. 3owever, it can reference temporary tables and views. "henever any data modification statement is issued then #45 #erver fires it automatically. It cannot be e-plicitly invo!ed or e-ecuted, as in the case of stored procedures. Triggers can be nested up to .B levels. The nesting of triggers occurs when a trigger performs an action that initiates another trigger. It prevents incorrect, unauthoriCed, and inconsistent changes in data. It cannot return data to the user. There are three types of triggers .? I:#ERT /? DE5ETE 0? )9DATE What is -orma#i2ation? E*p#ain three norma# forms? :ormaliCation is a scientific method of brea!ing down comple- table structures in to simple table structures by using certain rules. 3ence reduce redundancy in a table and eliminate inconsistency problems and dis! space usage. $irst :ormal $orm A table is said to be in .:$ when each cell of the table contains precisely one value. $unctional dependency If you have two attribute A and +, A is said to be functionally dependant on +, if for each value of +, there is e-actly one value of A. #econd :ormal $orm

Identify the functionally dependent !eys and place them in a different table. Third :ormal $orm A relation is said to be 0:$ when every non*!ey attribute is functionally dependant only on the primary !ey. What are !ursors and types of cursors? A cursor is a wor! area called private #45 area, which e-ecutes #45 statements and stores the results.

!ursor ,ypes #o you can specify the four*cursor types for Transact*#45 cursors. These cursors vary in their ability to detect changes to the result set and in the resources, such as memory and space in tempdb, they consume. The four A9I server cursor types supported by #45 #erver areA

#tatic cursors Dynamic cursors $orward*only cursors <ey set*driven cursors

#tatic cursors detect few or no changes but consume relatively few resources while scrolling, although they store the entire cursor in tempdb. Dynamic cursors detect all changes but consume more resources while scrolling, although they ma!e the lightest use of tempdb. <ey set*driven cursors lie in between, detecting most changes but at less e-pense than dynamic cursors.

10

Although the database A9I cursor models consider a forward* only cursor to be a distinct type of cursor, #45 #erver does not. #45 #erver considers both $orward only and scroll to be options that can be applied to static, !ey set*driven, and dynamic cursors. 3 What is Encryption option in SQL Ser+er? Encryption is a method for !eeping sensitive information confidential by changing data into an unreadable form. Encryption ensures that data remains secure by !eeping the information hidden from everyone, even if the encrypted data is viewed directly. Decryption is the process of changing encrypted data bac! into its original form and so authoriCed users can view it. 4 What are time5stamped data types? It is a database*wide uni ue number. The storage siCe is = bytes. A table can have only one timestamp column. The value in the timestamp column is updated every time a row containing a timestamp column is inserted or updated. This property ma!es a timestamp column a poor candidate for !eys, especially primary !eys. Any update made to the row changes the timestamp value, thereby changing the !ey value. If the column is in a primary !ey, the old !ey value is no longer valid, and foreign !eys referencing the old value are no longer valid. If the table is referenced in a dynamic cursor, all updates change the position of the rows in the cursor. If the column is in an inde- !ey, all updates to the data row also generate updates of the inde-. 6 !orre#ated &ueries In correlated ueries a sub uery is e-ecuted for each row the parent uery is e-ecuted. E-A D#elect e. E from E&9 e where e.sal F>select Avg >#al? from emp where e.deptno G emp.deptno D

11

7 What is 8LA" and how it works with SQL Ser+er? %5A9 #ervices is a new middle*tier server for online analytical processing >%5A9?. %5A9 #ervices provides wiCards, editors, and information to ma!e %5A9 technology easier to use. %5A9 #ervices supports various data and storage models to help you create and maintain an %5A9 system that meets your organiCation@s needs. .4 What are the ad+antages of SQL Ser+er 7 9 o+er SQL Ser+er 6 4? Trigger Enhancements

Recursive triggers &ultiple triggers per DE5ETE statement

I:#ERT,

)9DATE,

or

Data Transformation #ervices "eb Assistant "iCard The "eb Assistant "iCard has been enhanced in #45 #erver H.I. In addition to e-porting #45 #erver data out to an 3T&5 file, it can also import tabular data from an 3T&5 file into #45 #erver, and post to and read from 3TT9 and $T9 locations. Row*level loc!ing #45 #erver H.I supports complete row*level loc!ing on both data pages and inde- pages. #45 #erver H.I supports applications that span a broad range of platforms. Jigabytes of memory, and a terabyte or more of dis! storage. #tored 9rocedures The stored procedure model has been enhanced in #45 #erver H.I to provide improved performance and increased application fle-ibility. "hen a stored
12

procedure is compiled and placed in the procedure cache, all users of the stored procedure share that one copy of the compiled plan. The most notable feature is update replication. )sing update replication, data replicated by #45 #erver H.I can be modified at multiple sites.

SQL5Ser+er
,runcate : De#ete ..Truncate a table means data will be lost, but structure of the table will be present in the database. Synta*; ,runcate tab#e <tab#e name=> /.If you drop a table then the structure of the table also goes along with the data. Synta*; drop tab#e <tab#e name=> 0.'ou can@t delete a tableK you can delete the data in a record wise manner. Synta*; de#ete from <tab#e name= where <condition= 1.The difference between the truncate and delete is you can delete the data at a stretch buy using the truncate, by using the delete you can delete only one record at a time. 8."hen you have truncated the table then all the records will lose. Then immediately if you add a record to that table the row number will be L.@ only. Inde-A
13

.. If you have a clustered inde- on a column, then also you can assign a primary !ey to that columnM /. 'ou are having rowid already then why you are going for the inde-M 0. 3ow many non*clustered inde-es can be allowed in a tableM Synta*; !reate inde* <inde* name=
On

<,ab#e name ?<co#)=@ <co#.=@ 5555555A=> Synta*; Drop inde* <inde* name= Ru#es 2.Rules will maintain the domain integrity. .I.Is it possible to create a rule at the time of creating a tableM :o. ...Is it possible to drop a ruleM 'es. ./."hat is the difference between the rule and chec! constraintM If a chec! constraint is applied to a table it will enforce the constraint to previously e-isting data also. If that data violates the constraint then constraint statement gives an error message.

14

If a rule is applied on a table it will not apply to the already e-isting data in the table. The rule will be imposed from now on the updation and insertion of data. Synta* !reate ru#e <ru#e name= as <condition= SpBbindru#e <ru#e name=@ <ob%ect name= Drop ru#e <ru#e name= (eys .0.3ow many types of !eys are thereM "hat is the importance of those !eysM .1."hat is the difference between the primary !ey and uni ueM .8."hat is the primary !ey and candidate !ey and what are there advantagesM .B.Is it possible to hide the databaseM .H. "hat is the ma-imum siCe of a rowM .=."hat is the ma-imum number of rows can a table allowedM Then how many columns are allowed in a tableM .2."hat is normaliCationM /I."hat is the redundancyM /.."hat are the normaliCation formsM //."hat is the difference between the D+&#NRD+&#M /0."hat is replicationM
15

Rep#ication; Definition; DReplication is an important and powerful technology for distributing data and the e-ecution of stored procedures across an enterprise. D The replication technology allows you to ma!e duplicate copies of your data, move those copies to different locations, and synchroniCe the data automatically so that all copies have the same data values. Replication can be implemented between databases on the same server or different servers connected by 5A:s, "A:s, or the Internet. /1."hat are the types of replicationM ,ypes of the rep#ications;

#napshot Replication %ptionA #napshot, with immediate*updating #ubscribers

Transactional Replication %ptionA Transactional, #ubscribers with immediate*updating

&erge Replication

"hich type of replication you choose for your application depends on your re uirements for data consistency, site autonomy, and networ! resources. /8.Differences between the #45*server /III and #45*#erver H.I. /B."hat is the s l using by the #45*#erver and oracleM

16

#45*#erverA Transact*#45 %racleA A:#I*#45 /=.Is it possible to create a view with out a base tableM Actually the answer is :%. +ut this is possible by using the D$%R(ED ;IE"#O #ynta-A (reate force view P'Q As #elect E from QQQK /2.Inde- is the only second object, which holds data in the database. 0I. D&%DI$'O is a !eyword only it is not a clause. 0.."hat will happen if you add a not null constraint on to a column if that column is already having the null values and what is the status of those null values, which are already presentM An error is given stating that null values are found. 0/.I am doing a transaction, I am having four activities in those four fourth activity got some problem, then will be the transaction committed or roll bac!edM Automatically it will rollbac!, because a transaction is said to be committed when all the activities of that transaction is perfectly e-ecuted. Coins 00."hat is a joinM 01.3ow many types of joins are thereM 08."hat is the difference between the outer join and left outer joinM
17

0B.3ow to ma!e a copy of a table >if you have a table with some name P, is it possible to create a table with a name ' by using the table PM E*p#anation; SQL ser+er Se#ect <co#umns= into <new tab#e= from <o#d tab#e= 8rac#e !reate tab#e <tab#e name= as se#ect D from <tab#e name= "rite a uery to display the details of employees whose salary is more than the L+5A<E@ salary. # lF select +. E from E&9 A, E&9 + "here A.enameG@+5A<E@ A. #al R +.salK It is also called aliasing. It is a self*joining concept. 0=."hat is schemaM Schema; A description of a database generated by the data definition language >DD5? of the database management system >D+&#?. In %5A9 #ervices, a schema is a description of multidimensional objects such as cubes, dimensions, and so forth. 02.Is it possible to create a trigger on multiple tablesM :%.

18

1I.Is it possible to create a single stored procedure on multiple tablesM 'E#. What is SQL and where does it come from? #tructured 4uery 5anguage >#45? is a language that provides an interface to relational database systems. I+& developed #45 in the .2HIs for use in #ystem R. #45 is a de facto standard, as well as an I#% and A:#I standard. #45 is often pronounced #E4)E5. In common usage #45 also encompasses D&5 >Data &anipulation 5anguage?, for I:#ERTs, )9DATEs, DE5ETEs and DD5 >Data Definition 5anguage?, used for creating and modifying tables and other database structures. The development of #45 is governed by standards. A major revision to the #45 standard was completed in .22/, called #45/. #450 support object e-tensions and will be >partiallyM? Implemented in %racle=. Eow can 1 e#iminate dup#icates +a#ues in a tab#e? (hoose one of the following duplicate rows from a tableA &ethod .A #45F DE5ETE $R%& table,name A "3ERE R%"ID F > / #E5E(T min>rowid? $R%& table,name + 0 "3ERE A.!ey,values G +.!ey,values?K &ethod /A #45F create table table,name/ as select distinct E from table,name.K ueries to identify or remove

19

#45F drop table,name.K #45F rename table,name/ to table,name.K &ethod 0A #45F Delete from where rowid not in> #45F select ma->rowid? from #45F group by ?K &ethod 1A #45F delete from mytable t. #45F where e-ists >select S-S from mytable t/ #45F where t/.!ey,value. G t..!ey,value. #45F and t/.!ey,value/ G t..!ey,value/ #45F and t/.rowid TG t..rowid?K :oteA If you create an inde- on the joined fields in the inner loop, you for all intensive purposes eliminate :U/ operations >no need to loop through the entire table on each pass by a record?. Eow can 1 generate primary key +a#ues for my tab#e? (reate your table with a :%T :)55 column >say #E4:%?. This column can now be populated with uni ue valuesA #45F )9DATE table,name #ET se no G R%":)&K or use a se uences generatorA #45F (REATE #E4)E:(E se uence,name #TART "IT3 . I:(RE&E:T +' .K #45F )9DATE table,name #ET se uence,name.:EPT;A5K $inally, create a uni ue inde- on this column. se no G

20

Eow can 1 get the time difference between two date co#umns select floor>>date.*date/?E/1EBIEBI?N0BII? VV S 3%)R# S VV floor>>>>date.*date/?E/1EBIEBI? * floor>>>date.*date/?E/1EBIEBI?N0BII?E0BII?NBI? VV S &I:)TE# S VV round>>>>date.*date/?E/1EBIEBI? * floor>>>date.*date/?E/1EBIEBI?N0BII?E0BII * >floor>>>>date.*date/?E/1EBIEBI? * floor>>>date.*date/?E/1EBIEBI?N0BII?E0BII?NBI?EBI??? VV S #E(# S time,difference from ... Eow does one count different data +a#ues in a co#umn? select dept, sum> decode>se-,S&S,.,I?? &A5E, sum> decode>se-,S$S,.,I?? $E&A5E, count>decode>se-,S&S,.,S$S,.?? T%TA5 from my,emp,table group by deptK Eow does one se#ect e+ery n th row from a tab#e? &ethod .A )sing a sub uery #E5E(T E $R%& emp "3ERE >R%"ID,I? I: >#E5E(T R%"ID, &%D>R%":)&,1? $R%& emp?K &ethod /A )se dynamic views >available from %racleH./?A #E5E(T E $R%& > #E5E(T rownum rn, empno, ename $R%& emp ? temp "3ERE &%D>temp.R%":)&,1? G IK Eow does one se#ect the top n rows from a tab#e?

21

#E5E(T E $R%& TableP a "3ERE .I FG >#E5E(T (%):T>DI#TI:(T ma-col? $R%& tableP b "3ERE b.ma-col FG a.ma-col? %RDER +' ma-col DE#(K Eow to imp#ement if5then5e#se in a se#ect statement? The %racle decode function acts li!e a procedural statement inside an #45 statement to return different values or columns based on the values of other columns in the select statement. E-ampleA select decode>se-, S&S, S&aleS, S$S, S$emaleS, S)n!nownS ? from employeesK :oteA The decode function is not A:#I #45 and are rarely implemented in other RD+&# offerings. It is one of the good things about %racle, but use it sparingly if portability is re uired. Eow can one dumpF e*amine the e*act content of a database co#umn? #E5E(T D)&9>col.? $R%& tab. "3ERE cond. G val.K D)&9>(%5.? ************************************* TypG2B 5enG1A B8,BB,BH,0/

22

$or this e-ample the type is 2B, indicating (3AR, and the last byte in the column is 0/, which is the A#(II code for a space. This tells us that this column is blan!*padded. !an one drop a co#umn from a tab#e? %racle does not provide a way to DR%9 a column. .. update t. set column,to,drop G :)55K rename t. to t.,baseK create view t. as select Rspecific columnsF from t.,baseK /. create table t/ as select Rspecific columnsF from t.K drop table t.K rename t/ to t.K !an one rename a co#umn in a tab#e? :o .. rename t. to t.,baseK create view t. Rcolumn list with new nameF as select E from t.,baseK /. create table t/ Rcolumn list with new nameF as select E from t.K drop table t.K rename t/ to t.K Eow can 1 change my 8rac#e password? Issue the following #45 commandA A5TER )#ER RusernameF IDE:TI$IED +' Rnew,passwordF N $rom %racle= you can just type WpasswordW from #45E9lus, or if you need to change another userSs password, type Wpassword user,nameW. Workaround co#umns for snapshots on tab#es with L8-G

23

'ou can use the #45E9lus (%9' command instead of snapshots if you need to copy 5%:J and 5%:J RA" variables from one location to another. EgA (%9' T% #(%TTNTIJERXRE&%TE * (REATE I&AJE,TA+5E )#I:J * #E5E(T I&AJE,:%, I&AJE * $R%& I&AJE#K :oteA If you run %racle=, convert your 5%:Js to 5%+s, as it can be replicated.

24

You might also like