Sql Server Interview Questions and Answers (1758) - Page 31

Does the view continue to exist if the table is dropped from the database ?

If you drop the underlying tables by which the view is created then you will get an error while querying the view.

For example :-

Could not use view or function 'testdb.dbo.cutomerid_1_view_details' because of binding errors.

what is Primary Key in SQL Server

A Primary Key in a table uniquely identifies each and every row with out allowing Nulls. There are two types of Primary Keys:

1) Simple Primary Key ==> Defining primary key on a single column is called Simple Primary Key.
2) Composite Primary Key ==> Defining Primary Key on more than one column is called Composite Primary Key.
What is Unique Key in SQL Server?

A Unique Key in a table uniquely identifies each and every row and allowing Nulls per column Combination. There are two types of Unqiue Keys:

1) Simple Unique Key ==> Defining Unique key on a single column is called Simple Unique Key.
2) Composite Unique Key ==> Defining Unique Key on more than one column is called Composite Unique Key.
Difference between Primary key and unique key in SQL Server?

1)Primary Key by definition cannot be null, where as unique key can accept null values but if the unique key is defined on a column which is not null , then this unique key can also be used as an alternate primary key functionality to identify unique rows in a table.

2)By definition you can have only one primary key defined on a table where as you can have multiple unique keys defined on a table

3)Also by default Primary key is created as clustered index and unique key is created as non clustered index.
what is Check Constraint in SQL Server ?

Check constraint specifies a condition that is enforced for each row of the table on which the constraint is defined. Once constraint is defined, insert or update to the data within the tables is checked against the defined constraint.
Difference between Check Constraint and Rule?

The major difference between rule and Check is re usability. Check constraint is associated with columns in a Table. So these can't be re-used. Rules are defined with in a database and can be applied to any number of columns.
What is the table name,that contains the Primary Key, Unique Key and Foreign Key Information?

INFORMATION_SCHEMA.TABLE_CONSTRAINTS, where CONSTRAINT_TYPE column stores the information of Constraint Type.
Difference between Primary key,unique key,Alternate Key?

Primary Key ==> Is used to uniquely identify records and doesn't allow NULL values.

Unique Key ==> Is also used to uniquely identify records but does allow NULL Values per column combination.

Alternate Key ==> Is another type of Unique key, which is used to identify each record uniquely. This is not a constraint. This is just a terminology.
How to disable and Enable the constraints?

You need to use ALTER TABLE statement to disable constraint.

 alter table <tablename> nocheck constraint <constraintName></CODE]


To Enable

alter table <Tablename> check constraint <constraintName>

Can you drop a Parent Table with out affecting its child tables?

No. First you need to drop all the Foreign Key relationships and then only you can drop Parent Table.
How to Alter constraint in sql server?

Alter Constraint:

Alter table <TableName> Add Constraint <constraintname>
Check(Columnname <100)
Go

Drop Constraint:

Alter Table <TableName> Drop Constraint <constraintname>
Go

Clean up

ALTER DATABASE <DataBaseName>
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE <DataBasename>
How to load an Object(Image, Document,... Etc.,) into SQL Server Table?

We can use OPENROWSET statement to load an Object into a Table.

VARBINARY data type is the recommended one to hold an Object
Create Table LoadImage

(
ImageContent VARBINARY(MAX)
)
Go
Loading Image into a Table
INSERT LoadImage

SELECT * FROM OPENROWSET(BULK N'D:\Pandian.s\Photo.JPG', SINGLE_BLOB) AS [Image]
Go

Define Stored Procedure with Example?

Stored Procedure is an collection SQL Statements. To Reuse the code over and over again.
Stored Procedure is an Precompiled Execution. Stored Procedure have input and out put Parameters.
Stored Procedure Can reduce the Client /Server Network Traffic.
Explanation:
Before Creating store procedure whether insert data or select data..
In This example am using Adventure works DataBase

SELECT * FROM AdventureWorks.Person.Address


CREATE PROCEDURE GetAddress
AS
SELECT * FROM AdventureWorks.Person.Address
GO


EXEC GetAddress

--or just simply
uspGetAddress


To Create a Stored Procedure u can use Create Procedure or Create Proc
Just like you have the ability to use parameters with your SQL code you can also setup your stored procedures to except one or more parameter values.

CREATE PROCEDURE uspGetAddress @City nvarchar(30)

AS
SELECT *
FROM AdventureWorks.Person.Address
WHERE City = @City
GO


CREATE PROCEDURE uspGetAddress @City nvarchar(30) 

AS
SELECT *
FROM AdventureWorks.Person.Address
WHERE City LIKE @City + '%'
GO


Default Parameter Values

CREATE PROCEDURE uspGetAddress @City nvarchar(30) = NULL

AS
SELECT *
FROM AdventureWorks.Person.Address
WHERE City = @City
GO

Where the #Table is stored???

NOTE: This is objective type question, Please click question title for correct answer.
How will you copy the structure of a table without copying the data ?

By using UNIVERSAL FASLE condition like 1=2,A=B....etc.
This is the only method for copying the structure of the table without copying the data.

Example:

create table NEWTable AS select * from OLDTABLE where 1=2

Where does Profiler store its temporary data in SQL Server 2005 ?

Profiler store its temporary data in the directory stored in the system variable TEMP.
The Profiler uses the location specified for the TEMP system variable.
Explain about the command line tool SQLCMD ?

The command line tool is available in the package of Microsoft SQL server. It states about the management features of SQL server.
Queries can be written and executed using this tool.
Scripts using this command tool are stored as .sql and can be used to manage or create database.
What is DESCRIBE command in SQL Server ?

DESCRIBE command is used in Oracle.
By using this command we can see the structure of the table. In SQL Server, we use sp_help fpr the same purpose.

Example:

To describe a procedure called CUSTOMER_LOOKUP, enter

DESCRIBE customer_lookup


Output:


PROCEDURE customer_lookup
Argument Name Type In/Out Default?
---------------------- -------- -------- ---------
CUST_ID NUMBER IN
CUST_NAME VARCHAR2 OUT

Explain different isolation levels ?

An isolation level determines the degree of isolation of data between concurrent transactions. Read Committed is the default SQL Server isolation level.
The other isolation levels (in the ascending order of isolation) are:
Read Uncommitted,
Read Committed,
Repeatable Read,
Serializable.

Example:

CREATE INDEX myIndex ON myTable (myColumn)

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 is dynamically managed by SQL Server.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories