Search content within the blog

Friday, April 17, 2009

Features in SqlServer Part-II

Q. Difference between a "where" clause and a "having" clause
Having clause is used only with group functions whereas Where is not used with

Q. What is the basic difference between a join and a union?
A join selects columns from 2 or more tables. A union selects rows.

Q. What are foreign keys?
These are attributes of one table that have matching values in a primary key in another table, allowing for relationships between tables.

Q. What is a synonym? How is it used?
A synonym is used to reference a table or view by another name. The other name can then be written in the application code pointing to test tables in the development stage and to production entities when the code is migrated. The synonym is linked to the AUTHID that created it.

Q. What is a Cartesian product?
A Cartesian product results from a faulty query. It is a row in the results for every combination in the join tables.

Q. What is denormalization and when would you go for it?
As the name indicates, denormalization is the reverse process of normalization. It's the controlled introduction of redundancy in to the database design. It helps improve the query performance as the number of joins could be reduced.

Q. What's the difference between a primary key and a unique key?
Both, primary and unique key enforces uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULL values, but unique key allows one NULL only.

Q. What is 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.

Q. Define candidate key, alternate key, and composite key.
A candidate key is one that can identify each row of a table uniquely. Generally a candidate key becomes the primary key of the table. If the table has more than one candidate key, one of them will become the primary key, and the rest are called alternate keys. A key formed by combining at least two or more columns is called composite key.

Q. What are defaults? Is there a column to which a default can't be bound?
A default is a value that will be used by a column, if no value is supplied to that column while inserting data. IDENTITY columns and timestamp columns can't have defaults bound to them.

Q. What is a transaction and what are ACID properties?
A transaction is a logical unit of work in which, all the steps must be performed or none. ACID stands for Atomicity, Consistency, Isolation, and Durability.

Q. 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:

Read Uncommitted: - A transaction can read any data, even if it is being modified by another transaction. This is the least safe isolation level but allows the highest concurrency.

Read Committed: - A transaction cannot read data that is being modified by another transaction that has not committed. This is the default isolation level in Microsoft SQL Server.

Repeatable Read: - Data read by a current transaction cannot be changed by another transaction until the current transaction finishes. Any type of new data can be inserted during a transaction.

Serialized: - Data read by the current transaction cannot be changed by another transaction until the current transaction finishes. No new data can be inserted that would affect the current transaction.

Q. What type of Index will get created with: CREATE INDEX myIndex ON myTable (myColumn)?
Non-clustered index; important thing to note: By default a clustered index gets created on the primary key, unless specified otherwise.

Q. What's the difference between DELETE TABLE and TRUNCATE TABLE commands?
DELETE TABLE is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow. TRUNCATE TABLE also deletes all the rows in a table, but it won't log the deletion of each row, instead it logs the deallocation of the data pages of the table, which makes it faster. Of course, TRUNCATE TABLE can be rolled back. The TRUNCATE TABLE command sets the identity value to one whereas the DELETE command retains the identity value. The TRUNCATE TABLE cannot activate a trigger.

Q. 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

Q. What is RAID and what are different types of RAID configurations?
RAID stands for Redundant Array of Inexpensive Disks, used to provide fault tolerance to database servers. There are six RAID levels 0 through 5 offering different levels of performance, fault tolerance.

Q. 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.

Q. What is database replication? What are the different types of replication you can set up in SQL Server?
Replication is the process of copying/moving data between databases on the same or different servers. SQL Server supports the following types of replication scenarios:
• Snapshot replication
• Transactional replication (with immediate updating subscribers, with queued updating subscribers)
• Merge replication

Q. What are cursors? Explain different types of cursors. What are the disadvantages of cursors? How can you avoid cursors?
Cursor is a database object used to manipulate data in a set on a row-by-row basis, instead of the typical SQL commands that operate on all the rows in the set at one time.

Cursor Type --------- Description
ForwardOnly ---------You can only scroll forward through records. This is the default cursor type.
Static ---------A static copy of a set of records that you can use to find data or generate reports. Additions, changes, or deletions by other users are not visible. The Recordset is fully navigable, forward and backward.
Dynamic ---------Additions, changes, and deletions by other users are visible, and all types of movement through the Recordset are allowed.
Keyset ---------Like the dynamic cursor type, except that you can’t see records that other users add. Deletions and other modifications made by other users are still visible.

Disadvantages of cursors: Each time you fetch a row from the cursor, it results in a network roundtrip; where as a normal SELECT query makes only one roundtrip, however large the resultset is. Cursors are also costly because they require more resources and temporary storage (results in more IO operations). Further, there are restrictions on the SELECT statements that can be used with some types of cursors.
Another situation in which developers tend to use cursors: You need to call a stored procedure when a column in a particular row meets certain condition. You don't have to use cursors for this. This can be achieved using WHILE loop, as long as there is a unique key to identify each row.

Q. How will you copy the structure of a table without copying the data?
Create table EMPTEMP as select * from EMP where 1 = 2;

Q. 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, detaching and attaching databases, replication, DTS, BCP, logshipping, INSERT...SELECT, SELECT...INTO, creating INSERT scripts to generate data.

Q. What is database replication? What are the different types of replication you can set up in SQL Server?
Replication is the process of copying/moving data between databases on the same or different servers. SQL Server supports the following types of replication scenarios:
? Snapshot replication
? Transactional replication (with immediate updating subscribers, with queued updating subscribers)
? Merge replication

Q. How many triggers you can have on a table? How to invoke a trigger on demand?
You can create multiple triggers per each action. But in 7.0 there's no way to control the order in which the triggers fire. In SQL Server 2000 you could specify which trigger fires first or fires last using sp_settriggerorder

Q. What is an extended stored procedure? Can you instantiate a COM object by using T-SQL?
An extended stored procedure is a function within a DLL (written in a programming language like C, C++ using Open Data Services (ODS) API) that can be called from T-SQL, just the way we call normal stored procedures using the EXEC statement. Yes, you can instantiate a COM object from T-SQL by using sp_OACreate stored procedure.

Q. What is a self join? Explain it with an example.
Self join is just like any other join, except that two instances of the same table will be joined in the query. Here is an example: Employees table which contains rows for normal employees as well as managers. So, to find out the managers of all the employees, you need a self join.

CREATE TABLE emp (empid int, mgrid int, empname char(10) )
INSERT emp SELECT 1,2,'Vyas'
INSERT emp SELECT 2,3,'Mohan'
INSERT emp SELECT 4,2,'Shridhar'
INSERT emp SELECT 5,2,'Sourabh'

SELECT t1.empname [Employee], t2.empname [Manager]
FROM emp t1, emp t2 WHERE t1.mgrid = t2.empid

Here's an advanced query using a LEFT OUTER JOIN that even returns the employees without managers

SELECT t1.empname [Employee], COALESCE(t2.empname, 'No manager') [Manager]
FROM emp t1 LEFT OUTER JOIN emp t2 ON t1.mgrid = t2.empid

SQL Server Limitations
Object ---------Maximum sizes/numbers
Bytes per text, ntext or image column--------- 2 GB-2
Clustered indexes per table--------- 1
Columns per index--------- 16
Columns per foreign key--------- 16
Columns per primary key--------- 16
Columns per base table--------- 1,024
Columns per SELECT statement--------- 4,096
Columns per INSERT statement--------- 1,024
Connections per client--------- Maximum value of configured connections
Database size--------- 1,048,516 TB
Databases per instance of SQL Server--------- 32,767
Files per database--------- 32,767
File size (data) --------- 32 TB
Identifier length (in characters) --------- 128
Locks per connection--------- Max. locks per server
Nested stored procedure levels--------- 32
Nested subqueries--------- 32
Nested trigger levels--------- 32
Nonclustered indexes per table--------- 249
Objects in a database--------- 2,147,483,6474
Parameters per stored procedure--------- 2,100
REFERENCES per table--------- 253
Rows per table--------- Limited by available storage
Tables per database--------- Limited by number of objects in a database
Tables per SELECT statement--------- 256
Triggers per table--------- Limited by number of objects in a database
UNIQUE indexes or constraints per table--------- 249 nonclustered and 1 clustered

No comments:

Post a Comment