SQL Server Flashcards
What connections does Microsoft SQL Server support?
Windows Authentication (via Active Directory) and SQL Server authentication (via Microsoft SQL Server username and passwords)
What are the System Database in Sql server 2005?
- Master - Stores system level information such as user accounts, configuration settings, and info on all other databases.
- Model - database is used as a template for all other databases that are created
- Msdb - Used by the SQL Server Agent for configuring alerts and scheduled jobs etc
- Tempdb - Holds all temporary tables, temporary stored procedures, and any other temporary storage requirements generated by SQL Server.
What is the difference between TRUNCATE and DELETE commands?
- TRUNCATE is a DDL command whereas DELETE is a DML command.
- TRUNCATE is much faster than DELETE.
Reason:When you type DELETE.all the data get copied into the Rollback Tablespace first.then delete operation get performed.Thatswhy when you type ROLLBACK after deleting a table ,you can get back the data(The system get it for you from the Rollback Tablespace).All this process take time.But when you type TRUNCATE,it removes data directly without copying it into the Rollback Tablespace.Thatswhy TRUNCATE is faster.Once you Truncate you cann’t get back the data.
- You cann’t rollback in TRUNCATE but in DELETE you can rollback.TRUNCATE removes the record permanently.
- In case of TRUNCATE ,Trigger doesn’t get fired.But in DML commands like DELETE .Trigger get fired.
- You cann’t use conditions(WHERE clause) in TRUNCATE.But in DELETE you can write conditions using WHERE clause.
What is denormalization and when would you go for it?
The process of adding redundant data to get rid of complex join, in order to optimize database performance. This is done to speed up database access by moving from higher to lower form of normalization.
In other words, we can define De-Nomalization as :-
De-normalization is the process of attempting to optimize the performance of a database by adding redundant data. It’s used To introduce redundancy into a table in order to incorporate data from a related table. The related table can then be eliminated. De-normalization can improve efficiency and performance by reducing complexity in a data warehouse schema.
De-normalization is an application tool in SQL server model. There are three methods for de-normalization:.
• Entity inheritance
• Role expansion
• Lookup entities.
Entity Inheritance
This method for the de-normalization should be implemented when one entity is named as another entity. This will do with the help of inheritance. Inheritance means parent child relations of entity. This will be do with making the foreign key and candidate key. This is also in notice that creation of model creates a band of relationship and if you select the inheritance this property should be automatically deleted.
Role Expansion
This type of de-normalization should be created when it is surety that one entity has the relationship to another entity or it is a part of another entity. In this storage reason is removed. It is used with the help of Expand inline function. It use the shared schema is used in from of table.
Lookup Entities
This type of de-normalization is used when entity depend on the lookup table. It is work with the help of Is Look up property. This property applies on the entity. These three will give authority to user to create a genuine and tempting report model .This model is navigation experience for the customer.
The Reason for Denormalization
Only one valid reason exists for denormalizing a relational design - to enhance performance. However, there are several indicators which will help to identify systems and tables which are potential denormalization candidates.
These are:
- Many critical queries and reports exist which rely upon data from more than one table. Often times these requests need to be processed in an on-line environment.
- Repeating groups exist which need to be processed in a group instead of individually.
- Many calculations need to be applied to one or many columns before queries can be successfully answered.
- Tables need to be accessed in different ways by different users during the same timeframe.
- Many large primary keys exist which are clumsy to query and consume a large amount of disk space when carried as foreign key columns in related tables.
- Certain columns are queried a large percentage of the time causing very complex or inefficient SQL to be used.
Be aware that each new RDBMS release usually brings enhanced performance and improved access options that may reduce the need for denormalization. However, most of the popular RDBMS products on occasion will require denormalized data structures. There are many different types of denormalized tables which can resolve the performance problems caused when accessing fully normalized data. The following topics will detail the different types and give advice on when to implement each of the denormalization types.
Types of Denormalization
- *Pre-Joined Tables** used when the cost of joining is prohibitive
- *Report Tables** used when specialized critical reports are needed
- *Mirror Tables** used when tables are required concurrently by two different types of environments
- *Split Tables** used when distinct groups use different parts of a table
- *Combined Tables** used when one-to-one relationships exist
- *Redundant Data** used to reduce the number of table joins required
- *Repeating Groups** used to reduce I/O and (possibly) storage usage
- *Derivable Data** used to eliminate calculations and algorithms
- *Speed Tables** used to support hierarchies
How do you 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. It will be a good idea to read up a database designing fundamentals text book.
What are user defined datatypes and when you should go for them?
User defined datatypes let you extend the base SQL Server datatypes by providing a descriptive name, and format to the database.
Take for example, in your database, there is a column called Flight_Num which appears in many tables. In all these tables it should be varchar(8). In this case you could create a user defined datatype called Flight_num_type of varchar(8) and use it across all your tables.
See sp_addtype, sp_droptype in books online.
What is bit datatype and what’s the information that can be stored inside a bit column?
Bit datatype is used to store boolean information like 1 or 0 (true or false). Untill SQL Server 6.5 bit datatype could hold either a 1 or 0 and there was no support for NULL. But from SQL Server 7.0 onwards, bit datatype can represent a third state, which is NULL.
CREATE INDEX myIndex ON myTable(myColumn)What type of Index will get created after executing the above statement?
Non-clustered index. Important thing to note: By default a clustered index gets created on the primary key, unless specified otherwise.
What is lock escalation? What is its purpose?
Lock escalation: In SQL Server, if one acquires a lock at higher level, it can lock more resources than what we may consume. This kind of locking has an overhead with lower concurrency. E.g.: If we select all the rows of a table and we acquire a lock on the table, we would not need to lock rows themselves but then it will block any concurrent update transactions. Based on estimates during query compilation, SQL Server recommends the locking granularity appropriately and during query execution, based on the concurrent work load, the appropriate locking granularity is applied. While locking granularity is chosen at the beginning of query execution, during the execution SQL Server may choose to escalate the lock to higher level of granularity depending on the number of locks acquired and the availability of memory at runtime. SQL Server supports escalating the locks to the table level .i.e. the locks can only be escalated from rows to table level. Locks are never escalated from rows to the parent page.
Lock escalation is when the system combines multiple locks into a higher level one. This is done to recover resources taken by the other finer granular locks. The system automatically does this. The threshold for this escalation is determined dynamically by the server.
Purpose:
- To reduce system over head by recovering locks
- Maximize the efficiency of queries
- Helps to minimize the required memory to keep track of locks.
What are the steps you will take to improve performance of a poor performing query?
This is a very open ended question and there could be a lot of reasons behind the poor performance of a query. But some general issues that you could talk about would be: No indexes, table scans, missing or out of date statistics, blocking, excess recompilations of stored procedures, procedures and triggers without SET NOCOUNT ON, poorly written query with unnecessarily complicated joins, too much normalization, excess usage of cursors and temporary tables. Some of the tools/ways that help you troubleshooting performance problems are:
SET SHOWPLAN_ALL ON, SET SHOWPLAN_TEXT ON, SET STATISTICS IO ON, SQL Server Profiler, Windows NT /2000 Performance monitor, Graphical execution plan in Query Analyzer. Download the white paper on performance tuning SQL Server from Microsoft web site. Don’t forget to check out sql-server-performance.com
What is a deadlock and what is a live lock? How will you go about resolving deadlocks?
Deadlock is a situation when two processes, each having a lock on one piece of data, attempt to acquire a lock on the other’s piece. Each process would wait indefinitely for the other to release the lock, unless one of the user processes is terminated.
SQL Server detects deadlocks and terminates one user’s process.
A livelock is one, where a request for an exclusive lock is repeatedly denied because a series of overlapping shared locks keeps interfering. SQL Server detects the situation after four denials and refuses further shared locks. A livelock also occurs when read transactions monopolize a table or page, forcing a write transaction to wait indefinitely.
Check out SET DEADLOCK_PRIORITY and “Minimizing Deadlocks” in SQL Server books online.
Also check out the article Q169960 from Microsoft knowledge base.
What is blocking and how would you troubleshoot it?
Blocking happens when one connection from an application holds a lock and a second connection requires a conflicting lock type. This forces the second connection to wait, blocked on the first. Read up the following topics in SQL Server books online: Understanding and avoiding blocking, Coding efficient transactions. Explain CREATE DATABASE syntax Many of us are used to creating databases from the Enterprise Manager or by just issuing the command: CREATE DATABAE MyDB.
What are statistics, under what circumstances they go out of date, how do you update them?
Statistics determine the selectivity of the indexes. If an indexed column has unique values then the selectivity of that index is more, as opposed to an index with non-unique values. Query optimizer uses these indexes in determining whether to choose an index or not while executing a query.
Some situations under which you should update statistics:
1) If there is significant change in the key values in the index
2) If a large amount of data in an indexed column has been added, changed, or removed (that is, if the distribution of key values has changed), or the table has been truncated using the TRUNCATE TABLE statement and then repopulated
3) Database is upgraded from a previous version. Look up SQL Server books online for the following commands: UPDATE STATISTICS, STATS_DATE, DBCC SHOW_STATISTICS, CREATE STATISTICS, DROP STATISTICS, sp_autostats, sp_createstats, sp_updatestats
What are the different ways of moving data/databases between servers and databases in SQL Server?
There are lots of options available, you have to choose your option depending upon your requirements.
Some of the options you have are:
- BACKUP/RESTORE
- dettaching and attaching databases
- replication
- DTS
- BCP
- Logshipping
- INSERT…SELECT, SELECT…INTO
- Creating INSERT scripts to generate data.
How to determine the service pack currently installed on SQL Server?
The global variable @@Version stores the build number of the sqlservr.exe, which is used to determine the service pack installed.
To know more about this process visit SQL Server service packs and versions.
What is a join and explain different types of joins.
Joins are used in queries to explain how different tables are related. Joins also let you select data from a table depending upon data from another table.
- Types of joins:
- INNER JOIN
- OUTER JOIN
- CROSS JOIN
- OUTER JOINs are further classified as LEFT OUTER JOINS
- RIGHT OUTER JOINS and FULL OUTER JOINS.
For more information see pages from books online titled: “Join Fundamentals” and “Using Joins”.
Can you have a nested transaction?
Yes, very much. Check out BEGIN TRAN, COMMIT, ROLLBACK, SAVE TRAN and @@TRANCOUNT
What is the system function to get the current user’s user id?
- USER_ID().
- USER_NAME()
- SYSTEM_USER
- SESSION_USER
- CURRENT_USER
- USER
- SUSER_SID()
- HOST_NAME().
- What is the difference between lock, block and deadlock?
Lock: DB engine locks the rows/page/table to access the data which is worked upon according to the query.
Block: When one process blocks the resources of another process then blocking happens.
Blocking can be identified by using
- SELECT * FROM sys.dm_exec_requests where blocked <> 0
- SELECT * FROM master..sysprocesses where blocked <> 0
Deadlock: When something happens as follows: Error 1205 is reported by SQL Server for deadlock.

Explain different isolation levels
An isolation level determines the degree of isolation of data between concurrent transactions. The default SQL Server isolation level is Read Committed. Here are the other isolation levels (in the ascending order of isolation):
- Read Uncommitted
- Read Committed
- Repeatable Read
- Serializable.
What is lock escalation?
Lock escalation is the process of converting a lot of low level locks (like row locks, page locks) into higher level locks (like table locks). Every lock is a memory structure too many locks would mean, more memory being occupied by locks. To prevent this from happening, SQL Server escalates the many fine-grain locks to fewer coarse-grain locks. Lock escalation threshold was definable in SQL Server 6.5, but from SQL Server 7.0 onwards it’s dynamically managed by SQL Server.
What are constraints? Explain different types of constraints.
Constraints enable the RDBMS enforce the integrity of the database automatically, without needing you to create triggers, rule or defaults.
Types of constraints:
- NOT NULL
- CHECK
- UNIQUE
- PRIMARY KEY
- FOREIGN KEY
For an explanation of these constraints see books online for the pages titled: “Constraints” and “CREATE TABLE”, “ALTER TABLE”
Whar is an index? What are the types of indexes? How many clustered indexes can be created on a table? I create a separate index on each column of a table. what are the advantages and disadvantages of this approach?
Indexes in SQL Server are similar to the indexes in books. They help SQL Server retrieve the data quicker.
Indexes are of two types. Clustered indexes and non-clustered indexes. When you craete a clustered index on a table, all the rows in the table are stored in the order of the clustered index key. So, there can be only one clustered index per table. Non-clustered indexes have their own storage separate from the table data storage. Non-clustered indexes are stored as B-tree structures (so do clustered indexes), with the leaf level nodes having the index key and it’s row locater. The row located could be the RID or the Clustered index key, depending up on the absence or presence of clustered index on the table.
If you create an index on each column of a table, it improves the query performance, as the query optimizer can choose from all the existing indexes to come up with an efficient execution plan. At the same t ime, data modification operations (such as INSERT, UPDATE, DELETE) will become slow, as every time data changes in the table, all the indexes need to be updated.
Another disadvantage is that, indexes need disk space, the more indexes you have, more disk space is used.
What are the steps you will take, if you are tasked with securing an SQL Server?
Again this is another open ended question. Here are some things you could talk about: Preferring NT authentication, using server, databse and application roles to control access to the data, securing the physical database files using NTFS permissions, using an unguessable SA password, restricting physical access to the SQL Server, renaming the Administrator account on the SQL Server computer, disabling the Guest account, enabling auditing, using multiprotocol encryption, setting up SSL, setting up firewalls, isolating SQL Server from the web server etc.
Read the white paper on SQL Server security from Microsoft website. Also check out My SQL Server security best practices
GO
SELECT SCHEMA_NAME(schema_id) AS schema_name
,name AS table_name
FROM sys.tables
WHERE OBJECTPROPERTY(OBJECT_ID,'IsIndexed') = 0
ORDER BY schema_name, table_name;
GO
Returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.
RANK () OVER ([
Returns the rank of each row within the partition of a result set.
DENSE_RANK () OVER ([
Returns the rank of rows within the partition of a result set, without any gaps in the ranking. (Read more here )