Define Candidate Key, Alternate Key, Composite Key.: Atomicity
Define Candidate Key, Alternate Key, Composite Key.: Atomicity
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.
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,
Durability. These are the properties of a transaction. For more
information and explanation of these properties, see SQL Server books
online or any RDBMS fundamentals text book.
Atomicity
A transaction must be an atomic unit of work; either all of its data modifications are
performed, or none of them is performed.
Consistency
When completed, a transaction must leave all data in a consistent state. In a relational
database, all rules must be applied to the transaction's modifications to maintain all data
integrity. All internal data structures, such as B-tree indexes or doubly-linked lists, must
be correct at the end of the transaction.
Isolation
Modifications made by concurrent transactions must be isolated from the modifications
made by any other concurrent transactions. A transaction either sees data in the state it
was in before another concurrent transaction modified it, or it sees the data after the
second transaction has completed, but it does not see an intermediate state. This is
referred to as serializability because it results in the ability to reload the starting data and
replay a series of transactions to end up with the data in the same state it was in after the
original transactions were performed.
Durability
After a transaction has completed, its effects are permanently in place in the system. The
modifications persist even in the event of a system failure.
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's the maximum size of a row?
8060 bytes. Don't be surprised with questions like 'what is the
maximum number of columns per table'. Check out SQL Server books
online for the page titled: "Maximum Capacity Specifications".
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.
What are the new features introduced in SQL Server 2000 (or the latest
release of SQL Server at the time of your interview)? What changed between
the previous version of SQL Server and the current version?
This question is generally asked to see how current is your knowledge.
Generally there is a section in the beginning of the books online titled
"What's New", which has all such information. Of course, reading just
that is not enough, you should have tried those things to better answer
the questions. Also check out the section titled "Backward
Compatibility" in books online which talks about the changes that have
taken place in the new version.
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
emp
emp
emp
emp
emp
SELECT
SELECT
SELECT
SELECT
SELECT
1,2,'Vyas'
2,3,'Mohan'
3,NULL,'Shobha'
4,2,'Shridhar'
5,2,'Sourabh'
How to programmatically find out when the SQL Server service started? <top>
Everytime SQL Server starts, it recreates the tempdb database. So, the
creation date and time of the tempdb database tells us the date and
time at which SQL Server service started. This information is stored in
the crdate column of the sysdatabases table in master database.
Here's the query to find that out:
SELECT crdate AS 'SQL Server service started approximately
at:'
FROM master.dbo.sysdatabases
WHERE name = 'tempdb'
How to get rid of the time part from the date returned by GETDATE function?
<top>
We have to use the CONVERT function to strip the time off the date.
Any of the following commands will do this:
SELECT
SELECT
SELECT
SELECT
CONVERT(char,GETDATE(),101)
CONVERT(char,GETDATE(),102)
CONVERT(char,GETDATE(),103)
CONVERT(char,GETDATE(),1)
How to get the first day of the week, last day of the week and last day of the
month using T-SQL date functions? <top>
Here's the code:
DECLARE @Date datetime
SET @Date = '2001/08/31'
SELECT DATEADD(dd,-(DATEPART(dw, @Date) - 1),@Date) AS
'First day of the week'
SELECT DATEADD(dd,-(DATEPART(dw, @Date) - 7),@Date) AS 'Last
day of the week'
SELECT DAY(DATEADD(d,
-DAY(DATEADD(m,1,@Date)),DATEADD(m,1,@Date))) AS 'Last day
of the month'