What are store procedures and its advantages?
A stored procedure is collection or batch of Transact-SQL statements and control flow language that is stored under one name, and executed as single unit. It helps in improving the performance of a query. It is a precompiled object. As it is ready to execute no time is needed for parsing and compiling the procedure.
Advantages
Improved performance – Applications do not have to compile the procedure over and over again.
Reduction in network congestion – applications need not submit multiple SQL statements to server for the purpose of processing.
Enhanced accuracy – SQL statements included in a procedure are designed by experienced programmers and are therefore more efficient, error free, and tested.
Better security mechanism – users can be granted permission to execute a stored procedure even if they do not own it.
Types of stored procedures
1.User-defined
2.System defined
These are prefixed with sp_. These are for administrative purpose and are stored in the database and are accessible to all users.
3.Temporary
These are prefixed with #, stored in tempdb and are automatically dropped when connection terminates.
4.Remote
These are created and stored in remote servers and can accessed by users with appropriate permissions.
5.Extended
These are dlls that are executed outside SQL Server. They are prefixed by xp_.
Explain about BCP and DTS.
BCP
The transfer data from an external source to SQL Server is performed using Bulk Copy Program utility. The external source is a flat file. Data transfer from external source to SQL Server in BCP IN. The transfer of data from SQL Server to external source is BCP OUT.
DTS
Data transformation services can be used to import and export data between heterogeneous data sources and SQL Server. The external data sources include Visual FoxPro, MS Excel, Paradox, MS Access, Dbase, and text files.
What are transactions and their properties?
A transaction is a sequence of operations performed together as a single unit of logical work. It has four properties.
Atomicity – it states that either all the data modifications are performed or none are performed.
Consistency - it states that all the data is in a consistent state after a successful completion of transaction.
Isolation – it states any data modification made my concurrent transactions must be isolated from the modifications made by other concurrent transaction.
Durability – it states that ay change made by a completed transaction remains permanently in the system.
Locking mechanism.
SQL Server uses the concept of locking to ensure transactional integrity and database consistency. Locking, by functionally prevents users from accessing information being changed by other users. In a multi-user environment, Locking prevents users from changing the same data at the same time. In SQL Server locking is implemented automatically.
SQL Server implements multi-granular locking, which allows transactions to lock different types of resources at different levels. SQL Server can lock the following resources.
RID – is a row identifier that individually locks a row in a table.
Key – is a rowlock with in an index.
Page – is an 8k page or an index page.
Extent – is a contiguous group of 8k data pages or index pages.
Table – is the entire table, including all data and indexes.
Database – 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.
Update Locks
This lock is implemented when a transaction modify a row. Only one update lock is allowed on a resource at a time.
Exclusive locks
Intent locks
Schema Locks
What is deadlock?
A dead lock is a situation in which two users (or transactions) have locks on separate objects, and each user is waiting for a lock on the other’s object. It usually occurs in a multi-user environment.
What are triggers?
A trigger is a block of code that constitutes with a set of T-SQL statements that are activated in response to certain actions. A trigger can also be interpreted as a special kind of stored procedure that is executed whenever an action, such as data modification, takes 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 Manipulation Language (DML) statements-INSERT, UPDATE, or DELETE. A trigger fires in response to an event like insertion, updation, and deletion of data.
Triggers help in maintaining consistent, reliable, and correct data in tables. They enable the performance of complex actions and cascade these actions to other dependant tables.
Characteristics of a trigger:
It can be associated with tables.
It cannot be defined on temporary tables or views. However, it can reference temporary tables and views.
Whenever any data modification statement is issued then SQL Server fires it automatically.
It cannot be explicitly invoked or executed, as in the case of stored procedures.
Triggers can be nested up to 16 levels. The nesting of triggers occurs when a trigger performs an action that initiates another trigger.
It prevents incorrect, unauthorized, and inconsistent changes in data.
It cannot return data to the user.
There are three types of triggers
1) INSERT
2) DELETE
3) UPDATE
What is Normalization? Explain three normal forms?
Normalization is a scientific method of breaking down complex table structures in to simple table structures by using certain rules. Hence reduce redundancy in a table and eliminate inconsistency problems and disk space usage.
First Normal Form
A table is said to be in 1NF when each cell of the table contains precisely one value.
Functional dependency
If you have two attribute A and B, A is said to be functionally dependant on B, if for each value of B, there is exactly one value of A.
Second Normal Form
Identify the functionally dependent keys and place them in a different table.
Third Normal Form
A relation is said to be 3NF when every non-key attribute is functionally dependant only on the primary key.
What are Cursors and types of cursors?
A cursor is a work area called private SQL area, which executes SQL statements and stores the results.
CURSOR TYPES
So you can specify the four-cursor types for Transact-SQL 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 API server cursor types supported by SQL Server are:
Static cursors
Dynamic cursors
Forward-only cursors
Key set-driven cursors
Static 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 make the lightest use of tempdb.
Key set-driven cursors lie in between, detecting most changes but at less expense than dynamic cursors.
Although the database API cursor models consider a forward-only cursor to be a distinct type of cursor, SQL Server does not. SQL Server considers both Forward only and scroll to be options that can be applied to static, key set-driven, and dynamic cursors.
What is Encryption option in SQL Server?
Encryption is a method for keeping sensitive information confidential by changing data into an unreadable form. Encryption ensures that data remains secure by keeping the information hidden from everyone, even if the encrypted data is viewed directly. Decryption is the process of changing encrypted data back into its original form and so authorized users can view it.
What are time-stamped data types?
It is a database-wide unique number. The storage size is 8 bytes. A table can have only one timestamp column. The value in the timestamp column is updated every time a row containing a timestampcolumn is inserted or updated. This property makes a timestamp column a poor candidate for keys, especially primary keys. Any update made to the row changes the timestamp value, thereby changing the key value. If the column is in a primary key, the old key value is no longer valid, and foreign keys 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 index key, all updates to the data row also generate updates of the index.