Both primary key 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 NULLs, but unique key allows one NULL only.
Showing posts with label SQL Interview Question And Answer. Show all posts
Showing posts with label SQL Interview Question And Answer. Show all posts
Thursday, 10 November 2011
What is User Defined Functions? What kind of User-Defined Functions can be created?
09:38
User-Defined Functions allow defining its own T-SQL functions that can accept 0 or more parameters and return a single scalar data value or a table data type.
Different Kinds of User-Defined Functions created are:
Different Kinds of User-Defined Functions created are:
- Scalar User-Defined Function A Scalar user-defined function returns one of the scalar data types. Text, ntext, image and timestamp data types are not supported. These are the type of user-defined functions that most developers are used to in other programming languages. You pass in 0 to many parameters and you get a return value.
- Inline Table-Value User-Defined Function An Inline Table-Value user-defined function returns a table data type and is an exceptional alternative to a view as the user-defined function can pass parameters into a T-SQL select command and in essence provide us with a parameterized, non-updateable view of the underlying tables.
- Multi-statement Table-Value User-Defined Function A Multi-Statement Table-Value user-defined function returns a table and is also an exceptional alternative to a view as the function can support multiple T-SQL statements to build the final result where the view is limited to a single SELECT statement. Also, the ability to pass parameters into a TSQL select command or a group of them gives us the capability to in essence create a parameterized, non-updateable view of the data in the underlying tables. Within the create function command you must define the table structure that is being returned. After creating this type of user-defined function, It can be used in the FROM clause of a T-SQL command unlike the behavior found when using a stored procedure which can also return record sets.
Name 3 ways to get an accurate count of the number of records in a table?
09:37
SELECT * FROM table1
SELECT COUNT(*) FROM table1
SELECT rows FROM sysindexes WHERE id = OBJECT_ID(table1) AND indid < 2
SELECT COUNT(*) FROM table1
SELECT rows FROM sysindexes WHERE id = OBJECT_ID(table1) AND indid < 2
What is difference between DELETE and TRUNCATE commands?
09:35
Delete command removes the rows from a table based on the condition that we provide with a WHERE clause. Truncate will actually remove all the rows from a table and there will be no data in the table after we run the truncate command.
- TRUNCATE:
- TRUNCATE is faster and uses fewer system and transaction log resources than DELETE.
- TRUNCATE removes the data by deallocating the data pages used to store the table's data, and only the page deallocations are recorded in the transaction log.
- TRUNCATE removes all rows from a table, but the table structure, its columns, constraints, indexes and so on, remains. The counter used by an identity for new rows is reset to the seed for the column.
- You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint. Because TRUNCATE TABLE is not logged, it cannot activate a trigger.
- TRUNCATE cannot be rolled back.
- TRUNCATE is DDL Command.
- TRUNCATE Resets identity of the table
- DELETE:
- DELETE removes rows one at a time and records an entry in the transaction log for each deleted row.
- If you want to retain the identity counter, use DELETE instead. If you want to remove table definition and its data, use the DROP TABLE statement.
- DELETE Can be used with or without a WHERE clause
- DELETE Activates Triggers.
- DELETE can be rolled back.
- DELETE is DML Command.
- DELETE does not reset identity of the table.
What is a Linked Server?
09:34
Linked Servers is a concept in SQL Server by which we can add other SQL Server to a Group and query both the SQL Server dbs using T-SQL Statements. With a linked server, you can create very clean, easy to follow, SQL statements that allow remote data to be retrieved, joined and combined with local data. Stored Procedure sp_addlinkedserver, sp_addlinkedsrvlogin will be used add new Linked Server.
What is OLTP (Online Transaction Processing)?
09:33
In OLTP - online transaction processing systems relational database design use the discipline of data modeling and generally follow the Codd rules of data normalization in order to ensure absolute data integrity. Using these rules complex information is broken down into its most simple structures (a table) where all of the individual atomic level elements relate to each other and satisfy the normalization rules.
What are the properties and different Types of Sub-Queries?
08:22
1. Properties of Sub-Query
1. A sub-query must be enclosed in the parenthesis.
2. A sub-query must be put in the right hand of the comparison operator, and
3. A sub-query cannot contain an ORDER-BY clause.
4. A query can contain more than one sub-query.
2. Types of Sub-Query
1. Single-row sub-query, where the sub-query returns only one row.
2. Multiple-row sub-query, where the sub-query returns multiple rows,. and
3. Multiple column sub-query, where the sub-query returns multiple columns
Share Our Frineds
What is CHECK Constraint?
07:54
What is FOREIGN KEY?
07:54
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.
What is UNIQUE KEY constraint?
07:53
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.
What is PRIMARY KEY?
07:53
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.
What is Different Types of Join ?
07:50
JOIN
: Return rows when there is at least one match in both tables
LEFT JOIN : Return all rows from the left table, even if there are no
matches in the right table
RIGHT JOIN : Return
all rows from the right table, even if there are no matches in the left table
FULL JOIN : Return rows when there is a match in one of the
tables
What is a View?
07:50
A simple view
can be thought of as a subset of a table. It can be used for retrieving data as
well as updating or deleting rows. Rows updated or deleted in the view are
updated or deleted in the table the view was created with. It should also be
noted that as data in the original table changes, so does the data in the view
as views are the way to look at parts of the original table. The results of
using a view are not permanently stored in the database. The data accessed
through a view is actually constructed using standard T-SQL select command and
can come from one to many different base tables or even other views.
What is Difference between Commit and Rollback when Used in Transactions?
07:49
The usual
structure of the TRANSACTION is as follows:
BEGIN
TRANSACTION
Operations
COMMIT
TRANSACTION or ROLLBACK TRANSACTION
When Commit is
executed, every statement between BEGIN and COMMIT becomes persistent to
database. When Rollback is executed, every statement between BEGIN and ROLLBACK
are reverted to the state when BEGIN was executed.
What are sp_configure Commands and SET Commands?
07:48
Use sp_configure
to display or change server-level settings. To change the database-level
settings, use ALTER DATABASE. To change settings that affect only the current
user session, use the SET statement.
e.g.
sp_CONFIGURE 'show advanced', 0GORECONFIGUREGOsp_CONFIGUREGO
You can run the
following command and check the advanced global configuration settings.
sp_CONFIGURE 'show advanced', 1GORECONFIGUREGOsp_CONFIGUREGOWhat is the difference between UNION and UNION ALL?
07:48
The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the UNION command all selected columns need to be of the same data type. With
UNION
ALL
The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.
The difference
between UNION and UNION ALL is that UNION ALL will not eliminate
duplicate rows, instead it just pulls all rows from all the tables fitting your
query specifics and combines them into a table.
What are Primary Keys and Foreign Keys?
07:47
Primary keys are
the unique identifiers for each row. They must contain unique values and cannot
be null. Due to their importance in relational databases, Primary keys are the
most fundamental aspect of all keys and constraints. A table can have only one
primary key.
Foreign keys are
a method of ensuring data integrity and manifestation of the relationship
between tables.
What is the Difference between a Function and a Stored Procedure?
07:47
UDF can be used
in the SQL statements anywhere in the WHERE/HAVING/SELECT section, whereas
Stored procedures cannot be. UDFs that return tables can be treated as another
rowset. This can be used in JOINs with other tables. Inline UDF’s can be
thought of as views that take parameters and can be used in JOINs and other
Rowset operations.
What are the advantages of using Stored Procedures?
07:46
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.
What are the Different Types of Triggers?
07:45
There are two
types of Triggers.
1) DML
Trigger
There are two
types of DML Triggers
- 1.Instead of Trigger
Instead of Triggers are fired in place
of the triggering action such as an insert, update, or delete.
- 2. After Trigger
After triggers execute following the triggering
action, such as an insert, update, or delete.
2) DDL Trigger
This type of
trigger is fired against Drop Table, Create Table, Alter Table or Login events.
DDL Triggers are always After Triggers.
Subscribe to:
Posts (Atom)

