Microsoft SQL Server # Interview Questions
1.Indexes
2.avoid more number of triggers on the table
3.unnecessary
complicated joins
4.correct use of Group by clause with the select
list
5.in worst cases Denormalization
Index Optimization tips
| Employee | Phone |
| empid empname salary mgrid |
empid phnumber |
| Emp | City |
| Empid empName Salary |
Empid City |
| Datepart | Abbreviations |
|---|---|
| year | yy, yyyy |
| quarter | qq, q |
| month | mm, m |
| dayofyear | dy, y |
| day | dd, d |
| week | wk, ww |
| weekday | dw |
| hour | hh |
| minute | mi, n |
| second | ss, s |
| millisecond | ms |
| bigint | Binary | bit | char | cursor |
| datetime | Decimal | float | image | int |
| money | Nchar | ntext | nvarchar | real |
| smalldatetime | Smallint | smallmoney | text | timestamp |
| tinyint | Varbinary | Varchar | uniqueidentifier |
CREATE TRIGGER
reminder
LOCK
| Lock mode | Description |
|
Shared (S) |
Used for operations that do not change or update data (read-only operations), such as a SELECT statement. |
| Update (U) | Used on resources that can be updated. Prevents a common form of deadlock that occurs when multiple sessions are reading, locking, and potentially updating resources later. |
| Exclusive (X) | Used for data-modification operations, such as INSERT, UPDATE, or DELETE. Ensures that multiple updates cannot be made to the same resource at the same time. |
| Intent | Used to establish a lock hierarchy. The types of intent locks are: intent shared (IS), intent exclusive (IX), and shared with intent exclusive (SIX). |
| Schema | Used when an operation dependent on the schema of a table is executing. The types of schema locks are: schema modification (Sch-M) and schema stability (Sch-S). |
| Bulk Update (BU) | Used when bulk-copying data into a table and the TABLOCK hint is specified. |
| Isolation level | Dirty read | Nonrepeatable read | Phantom |
|---|---|---|---|
| Read uncommitted | Yes | Yes | Yes |
| Read committed | No | Yes | Yes |
| Repeatable read | No | No | Yes |
| Serializable | No | No | No |
| Locking hint | Description |
|---|---|
| HOLDLOCK | Hold a shared lock until completion of the transaction instead of releasing the lock as soon as the required table, row, or data page is no longer required. HOLDLOCK is equivalent to SERIALIZABLE. |
| NOLOCK | Do not issue shared locks and do not honor exclusive locks. When this option is in effect, it is possible to read an uncommitted transaction or a set of pages that are rolled back in the middle of a read. Dirty reads are possible. Only applies to the SELECT statement. |
| PAGLOCK | Use page locks where a single table lock would usually be taken. |
| READCOMMITTED | Perform a scan with the same locking semantics as a transaction running at the READ COMMITTED isolation level. By default, SQL Server 2000 operates at this isolation level. |
| READPAST | Skip locked rows. This option causes a transaction to skip rows locked by other transactions that would ordinarily appear in the result set, rather than block the transaction waiting for the other transactions to release their locks on these rows. The READPAST lock hint applies only to transactions operating at READ COMMITTED isolation and will read only past row-level locks. Applies only to the SELECT statement. |
| READUNCOMMITTED | Equivalent to NOLOCK. |
| REPEATABLEREAD | Perform a scan with the same locking semantics as a transaction running at the REPEATABLE READ isolation level. |
| ROWLOCK | Use row-level locks instead of the coarser-grained page- and table-level locks. |
| SERIALIZABLE | Perform a scan with the same locking semantics as a transaction running at the SERIALIZABLE isolation level. Equivalent to HOLDLOCK. |
| TABLOCK | Use a table lock instead of the finer-grained row- or page-level locks. SQL Server holds this lock until the end of the statement. However, if you also specify HOLDLOCK, the lock is held until the end of the transaction. |
| TABLOCKX | Use an exclusive lock on a table. This lock prevents others from reading or updating the table and is held until the end of the statement or transaction. |
| UPDLOCK | Use update locks instead of shared locks while reading a table, and hold locks until the end of the statement or transaction. UPDLOCK has the advantage of allowing you to read data (without blocking other readers) and update it later with the assurance that the data has not changed since you last read it. |
| XLOCK | Use an exclusive lock that will be held until the end of the transaction on all data processed by the statement. This lock can be specified with either PAGLOCK or TABLOCK, in which case the exclusive lock applies to the appropriate level of granularity. |
USE pubs
CREATE
TABLE #MyTempTable (cola INT PRIMARY KEY)USE
pubs
SELECT au_lname, au_fname
FROM authors
WHERE
exists
(SELECT *
FROM
publishers
WHERE authors.city =
publishers.city)
When a subquery is introduced with the
keyword EXISTS, it functions as an existence test. The WHERE clause of the
outer query tests for the existence of rows returned by the subquery. The
subquery does not actually produce any data; it returns a value of TRUE or
FALSE.
| Subordinate1 | Subordinate2 | Subordinate3 | Subordinate4 | |
| Bob | Jim | Mary | Beth | |
| Mary | Mike | Jason | Carol | Mark |
| Jim | Alan |
| Subordinates | |
| Bob | Jim, Mary, Beth |
| Mary | Mike, Jason, Carol, Mark |
| Jim | Alan |
| Subordinate | |
| Bob | Jim |
| Bob | Mary |
| Bob | Beth |
| Mary | Mike |
| Mary | Jason |
| Mary | Carol |
| Mary | Mark |
| Jim | Alan |
| MemberId | Name | Company | CompanyLoc |
| 1 | John Smith | ABC | Alabama |
| 2 | Dave Jones | MCI | Florida |
The Member table satisfies first normal form - it contains no repeating groups. It satisfies second normal form - since it doesn't have a multivalued key. But the key is MemberID, and the company name and location describe only a company, not a member. To achieve third normal form, they must be moved into a separate table. Since they describe a company, CompanyCode becomes the key of the new "Company" table.
The motivation for this is the same for second normal form: we want to
avoid update and delete anomalies. For example, suppose no members from the
IBM were currently stored in the database. With the previous design, there
would be no record of its existence, even though 20 past members were from
IBM!
Member Table
| MemberId | Name | CID |
| 1 | John Smith | 1 |
| 2 | Dave Jones | 2 |
| CId | Name | Location |
| 1 | ABC | Alabama |
| 2 | MCI | Florida |
| Software |
|
member |
|
Book |
| Software |
|
membersoftware |
|
member |
|
memberBook |
|
book |
context - Specifies the execution context in which the newly created
OLE object runs. If specified, this value must be one of the
following:
1 = In-process (.dll) OLE server only
4 = Local
(.exe) OLE server only
5 = Both in-process and local OLE server
allowed
DECLARE @object int
DECLARE @hr int
DECLARE @src varchar(255), @desc varchar(255)
EXEC @hr = sp_OACreate 'SQLDMO.SQLServer', @object OUT
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc
RETURN
END
B. Use CLSID - This example creates a SQL-DMO SQLServer object by using its CLSID.
DECLARE @object int
DECLARE @hr int
DECLARE @src varchar(255), @desc varchar(255)
EXEC @hr = sp_OACreate '{00026BA1-0000-0000-C000-000000000046}',
@object OUT
IF @hr <> 0
BEGIN
EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
SELECT hr=convert(varbinary(4),@hr), Source=@src, Description=@desc
RETURN
END
Have you ever used DBCC command? Give an example for
it.
The Transact-SQL programming language provides DBCC
statements that act as Database Console Commands for Microsoft® SQL Serve
2000. These statements check the physical and logical consistency of a
database. Many DBCC statements can fix detected problems. Database
Console Command statements are grouped into these categories.
| Statement category | Perform |
|---|---|
| Maintenance statements | Maintenance tasks on a database, index, or filegroup. |
| Miscellaneous statements | Miscellaneous tasks such as enabling row-level locking or removing a dynamic-link library (DLL) from memory. |
| Status statements | Status checks. |
| Validation statements | Validation operations on a database, table, index, catalog, filegroup, system tables, or allocation of database pages. |
| DBCC CHECKDB, DBCC CHECKTABLE, DBCC CHECKCATALOG, DBCC CHECKALLOC, DBCC SHOWCONTIG, DBCC SHRINKDATABASE, DBCC SHRINKFILE etc. | |
| Utilities |
|---|
| bcp console isql sqlagent sqldiag sqlmaint sqlservr vswitch |
| dtsrun dtswiz isqlw itwiz odbccmpt osql rebuildm sqlftwiz |
| distrib logread replmerg snapshot |
| scm |
| regxmlss |
| Sort order | Description |
|---|---|
| Binary | Sorts and compares data in Microsoft® SQL Server™ tables
based on the bit patterns defined for each character. Binary sort order
is case-sensitive, that is lowercase precedes uppercase, and
accent-sensitive. This is the fastest sorting order. If this option is not selected, SQL Server follows sorting and comparison rules as defined in dictionaries for the associated language or alphabet. |
| Case-sensitive | Specifies that SQL Server distinguish between uppercase
and lowercase letters. If not selected, SQL Server considers the uppercase and lowercase versions of letters to be equal. SQL Server does not define whether lowercase letters sort lower or higher in relation to uppercase letters when Case-sensitive is not selected. |
| Accent-sensitive | Specifies that SQL Server distinguish between accented
and unaccented characters. For example, 'a' is not equal to 'á'. If not selected, SQL Server considers the accented and unaccented versions of letters to be equal. |
| Kana-sensitive | Specifies that SQL Server distinguish between the two
types of Japanese kana characters: Hiragana and Katakana. If not selected, SQL Server considers Hiragana and Katakana characters to be equal. |
| Width-sensitive | Specifies that SQL Server distinguish between a
single-byte character (half-width) and the same character when
represented as a double-byte character (full-width). If not selected, SQL Server considers the single-byte and double-byte representation of the same character to be equal. |
---------
aijklmnef
REPLACE - Replaces all occurrences of the second given string expression
in the first string expression with a third expression.
SELECT REPLACE('abcdefghicde','cde','xxx')
GO
Here is the result set:
------------
abxxxfghixxx
http://www.smartdraw.com/resources/centers/software/erd.htm ER Diagram