SQL SERVER Interview Questions | SQL SERVER – Interview Questions and Answers – Frequently Asked Questions (Page-II)
21. What is PRIMARY KEY?
A PRIMARY KEY constraint is a unique
identifier for a row within a database table. Every table should have a primary
key constraint to uniquely identify each row and only one primary key constraint
can be created for each table. The primary key constraints are used to enforce
entity integrity.
22. What is UNIQUE KEY
constraint?
A UNIQUE constraint enforces the uniqueness of
the values in a set of columns, so no duplicate values are entered. The unique
key constraints are used to enforce entity integrity as the primary key
constraints.
23. What is FOREIGN KEY?
A FOREIGN KEY constraint prevents any actions
that would destroy links between tables with the corresponding data values. A
foreign key in one table points to a primary key in another table. Foreign keys
prevent actions that would leave rows with foreign key values when there are no
primary keys with that value. The foreign key constraints are used to enforce
referential integrity.
24. What is CHECK
Constraint?
A CHECK constraint is used to limit the values
that can be placed in a column. The check constraints are used to enforce
domain integrity.
25. What is NOT NULL
Constraint?
A NOT NULL constraint enforces that the column
will not accept null values. The not null constraints are used to enforce
domain integrity, as the check constraints.
26. How to get @@ERROR
and @@ROWCOUNT at the same time?
If @@Rowcount is checked after Error checking
statement then it will have 0 as the value of @@Recordcount as it would have
been reset. And if @@Recordcount is checked before the error-checking statement
then @@Error would get reset. To get @@error and @@rowcount at the same time do
both in same statement and store them in local variable.
SELECT @RC = @@ROWCOUNT, @ER = @@ERROR
27. What is a Scheduled
Jobs or What is a Scheduled Tasks?
Scheduled tasks let user automate processes
that run on regular or predictable cycles. User can schedule administrative
tasks, such as cube processing, to run during times of slow business activity.
User can also determine the order in which tasks run by creating job steps
within a SQL Server Agent job. E.g. back up database, Update Stats of Tables.
Job steps give user control over flow of execution. If one job fails, user can
configure SQL Server Agent to continue to run the remaining tasks or to stop
execution.
28. What are the
advantages of using Stored Procedures?
1. Stored
procedure can reduced network traffic and latency, boosting application
performance.
2. Stored procedure
execution plans can be reused, staying cached in SQL Server's memory, reducing
server overhead.
3. Stored
procedures help promote code reuse.
4. Stored
procedures can encapsulate logic. You can change stored procedure code without
affecting clients.
5. Stored
procedures provide better security to your data.
29. What is a table
called, if it has neither Cluster nor Non-cluster Index? What is it used for?
Unindexed table or Heap. Microsoft Press Books
and Book on Line (BOL) refers it as Heap. A heap is a table that does not have
a clustered index and, therefore, the pages are not linked by pointers. The IAM
pages are the only structures that link the pages in a table together. Unindexed
tables are good for fast storing of data. Many times it is better to drop all
indexes from table and then do bulk of inserts and to restore those indexes
after that.
30. Can SQL Servers
linked to other servers like Oracle?
SQL Server can be linked to any server
provided it has OLE-DB provider from Microsoft to allow a link. E.g. Oracle has
an OLE-DB provider for oracle that Microsoft provides to add it as linked
server to SQL Server group.
31. What is BCP? When
does it used?
BulkCopy is a tool used to copy huge amount of
data from tables and views. BCP does not copy the structures same as source to
destination. BULK INSERT command helps to import a data file into a database
table or view in a user-specified format.
32. How to implement
one-to-one, one-to-many and many-to-many relationships while designing tables?
One-to-One relationship can be implemented as
a single table and rarely as two tables with primary and foreign key
relationships. One-to-Many relationships are implemented by splitting the data
into two tables with primary key and foreign key relationships. Many-to-Many
relationships are implemented using a junction table with the keys from both
the tables forming the composite primary key of the junction table.
33. What is an execution
plan? When would you use it? How would you view the execution plan?
An execution plan is basically a road map that
graphically or textually shows the data retrieval methods chosen by the SQL
Server query optimizer for a stored procedure or ad- hoc query and is a very
useful tool for a developer to understand the performance characteristics of a
query or stored procedure since the plan is the one that SQL Server will place
in its cache and use to execute the stored procedure or query. From within
Query Analyzer is an option called "Show Execution Plan" (located on
the Query drop-down menu). If this option is turned on it will display query
execution plan in separate window when query is ran again.
34. What are DMVs?
Dynamic
Management Views (DMVs), are functions that give you information
on the state of the server. DMVs, for the most part, are used to monitor the
health of a server. They really just give you a snapshot of what’s going on
inside the server. They let you monitor the health of a server instance,
troubleshoot major problems and tune the server to increase performance.
35. Define a temp table
In a nutshell, a temp table
is a temporary storage structure. What does that mean? Basically, you can use a temp
table to store data temporarily so you can manipulate and change it before it
reaches its destination format.
36. What’s the difference between a local
temp table and a global temp table?
Local tables are
accessible to a current user connected to the server. These tables disappear
once the user has disconnected from the server. Global temp tables, on the
other hand, are available to all users regardless of the connection. These
tables stay active until all the global connections are closed.
37. How do you use transactions?
In
general, there are three types of transactions that you can use in the SQL
Server environment: BEGIN TRANSACTION, ROLL BACK TRANSACTION and COMMIT
TRANSACTION. The gist behind deploying transactions is that they allow you to
group multiple SQL commands into a single unit. From there, each transaction
begins with a certain task, and ends when all the tasks within the transaction
are complete. BEGIN TRANSACTION gets the ball rolling. ROLLBACK TRANSACTION
functions a lot like an “undo” command, and COMMIT TRANSACTION completes all of
the tasks within that transaction.
38. What’s the difference between a clustered and
a non-clustered index?
A
clustered index directly affects the way tabled data is stored on a
specific disk. This means that when a clustered index is used, data is stored
in sequential rows based on the index column value. This is why a table can
only contain a single clustered index. Non-clustered indexes directly affect
the way physical data is stored and managed within SQL Server.
39. What are DBCC commands?
In very
basic terms the Database
Consistency Checker (DBCC) is used to aid in server
maintenance. DBCC commands, many of which are completely undocumented, provide
a set of commands that let you perform routing maintenance, status and
validation checks. The most common DBCC commands are: DBCC CHECKALLOC (Lets you
check disk allocation); DBCC OPENTRAN (Lets you check any open transactions);
and DBCC HELP (shows a list of available DBCC commands to aid your server
maintenance processes).
40. Describe the difference between truncate and
delete.
The difference between these two processes is fairly simple.
Truncate means to simply empty out a table. On the other hand, the delete
command lets you delete entire rows from within a table, but not all of the
data within that table.
<< Page - I
<< Page - I
No comments:
Post a Comment