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

Can we have multiple Unique keys on the table?

Yes.We can assign multiple Unique keys to Multiple Columns in a table.Although Unique Keys are unique in nature means they do not allow duplication of Data in a columns.
How to assign Unique key to any column in the table at design time?

Suppose,i want to create Table called Employee_Master and to assign a unique key to PAN_Number and Employee_Code column,just give Unique keyword after datatype as

create table Employee_Master

(
Employee_Id int primary key,
Employee_Name varchar(50),
PAN_Number varchar(10) Unique,
Employee_Code varchar(25) Unique
)

State similarities between Primary key and Unique Key?

Although both Primary key and Unique Key enforces uniqueness on the columns on which they are defined.There are some similarities between them which are as follows:-

-> Primary key and unique Key columns allow only unique values.
-> Primary key and unique Key columns will always avoid duplicates.
-> Both Primary key and unique Key columns uniquely identifies each Row or Record in the table they are defined.
State differences between Primary key and Unique Key?

As there are some similarities between Primary key and Unique Key but there are some differences between both.The differences are listed below:-

-> Primary Key does not allow duplicates where as Unique key allows only one NULL.
-> A table can have only one Primary Key whereas a table can have multiple Unique Keys.
-> By default Primary Key creates a Unique Clustered Index on the table whereas by default Unique Key creates a Unique Non-Clustered Index on the table.
How to add auto-increment primary key in a existing table?

First assign Identity to a column then assign primary key to that column.

For Example:-
alter table project_master add project_id int identity;
Now,we can assign project_id column to primary key as below:-
alter table project_master add constraint pk_project_id_project_master primary key(project_id);

How to add auto-increment while creating tables in MYSQL database?

We can use auto_increment while creating new table in MYSQL database.

Syntax:-
create table table_name(column_name1 int(5) not null auto_increment primary key,column_name2 varchar(50));

For Example:-
create table Employee_Master(Employee_Id int(5) not null auto_increment primary key,Employee_Name varchar(50));

How to add auto-increment in an Existing tables in MYSQL database?

Syntax:-
alter table table_name modify column_name data-type(length) auto_increment primary key;

For Example:-
alter table Employee_Master modify Employee_Id int(5) auto_increment primary key;

Which command is used to add identity column in MYSQL Database?

Auto-Increment command is used to add identity in any new tables or existing tables.
What is the difference between Identity and Auto-Increment command?

Both performs the same operations and are used to add auto increment to a column in a table but Identity is used in Sql-Server whereas Auto-Increment command is used in MySql Database.
How to check string length in Sql Server?

We have in-built Sql Server function called Len,which is used for checking length.It counts the number of characters in the string.
Syntax:
Len(string expression);

Give an example of Sql Server Len function?

Select Len('VishalNeeraj') As [String Length];

Output will be 12.
What is the use of Left function in Sql Server?

Left function is an in-built Sql Server function used for returning the specified number of characters from a given string starting from the very beginning i.e. from left side of the string.In short,Left function returns the left part of a character string with the specified number of characters.

Syntax:
Left(String Value,Number of Characters)

Where:
String Value -> It provides string value(field name or expression) that will be used as our input.
Number of Characters -> It provide number of characters that we would like to receive from the String Value we provided in the first expression.
What is the use of Left function in Sql Server?

Left function is an in-built Sql Server function used for returning the specified number of characters from a given string starting from the very beginning i.e. from left side of the string.In short,Left function returns the left part of a character string with the specified number of characters.

Syntax:
Left(String Value,Number of Characters)

Where:
String Value -> It provides string value(field name or expression) that will be used as our input.
Number of Characters -> It provide number of characters that we would like to receive from the String Value we provided in the first expression.
Give a simple example of Left function?

Select Left('Vishal@Neeraj',5)

Output would be 'Visha'
What is the return type of Left function?

Left function either returns varchar datatype when String Value i.e. first expression in Left function parameter is a non-Unicode character data type.

Or returns nvarchar datatype when String Value is a Unicode character data type.
What is the use of Right function in Sql Server?

Right function is also an in-build Sql Server function which is used to take specified number of characters from a string starting from the end i.e.from right side of string.

In other words,we can say that this function returns the right part of a given string with the specified number of characters.

Syntax is same as Left Function .
Give few examples of Right function?

1).
Select Right('Vishal@Neeraj',7);

Output:- @Neeraj

2).
Select Right('dotnetfunda.com',4);

Output:- .com

3).
Select Right('PQR',2);

Output:- QR
What is the return type of Right function?

The return type of Right function is same as Left Function i.e. it returns either varchar or nvarchar datatype value.
What is the best way to see all columns in a table?

Best way to sell all the columns in a table is:

Select Top 0 from table_name;


It will not show any record only show columns names.
How to replace a column value with specified value if column value is null?

Use IsNull in-built SQL-Server function to replace column value if column value is null or it has no value assigned.
Note:- IsNull will replace column value if and only if column value is null otherwise it will take column value if it has some values.

For Example:-
Select isnull(MIDDLE_NAME,'NA') as MIDDLE_NAME from employee_master;

Select isnull(Budget,0.0) as Budget from project_master;

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