Foreign Keys Prevents Any Actions That Would Destroy Links Between Tables With
Foreign Keys Prevents Any Actions That Would Destroy Links Between Tables With
A: 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 of all keys and constraints.
A table can have only one Primary key. Primary key creates unique clustered
indexes
Foreign keys prevents any actions that would destroy links between tables with
the corresponding data values. A column in a table that points to the primary key
in another table. It enforces data integrity of the relationship between tables.
It can accept NULLs but it is not recommended.
4) What is Trigger ?
A: A trigger is a SQL procedure that initiates an action when an event (INSERT, DELETE or
UPDATE) occurs. Triggers are stored in and managed by the DBMS. Triggers are used
to maintain integrity of data by changing the data in a systematic fashion.
5) What is normalization?
A: Database normalization is a data design and organization process applied to data
structures based on rules that help build relational databases. Normalization usually
involves dividing a database into two or more tables and defining relationships
between the tables.
6) What is De-normalization?
A: De-normalization is the process of attempting to optimize the performance of a
database by adding redundant data. It is a technique to move from higher to lower
normal forms of database modeling in order to speed up database access.
7) What is View?
A: 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. Views are database objects which are
mainly used to implement security at rows and columns levels on the base table.
• One can create a view on top of other views.
• View just needs a result set (SELECT statement).We use views just like regular
tables when it comes to query writing. (joins, sub-queries, grouping....)
• We can perform DML operations (INSERT, DELETE, UPDATE) on a view.
• It actually affects the underlying tables only those columns can be affected which
are visible in the view.
Types of views:
Regular View: It is a type of view in which you are free to make any DDL
changes on the underlying table.
Schema binding View: It is a type of view in which the schema of the view
(column) are physically bound to the schema of the
underlying table. We are not allowed to perform any
DDL changes to the underlying table for the columns
that are referred by the schema binding view structure.
Indexed View
8) What is Index?
A: An index is a physical structure containing pointers to the data. Indices are created in
an existing table to locate rows more quickly and efficiently. It is possible to create an
index on one or more columns of a table, and each index is given a name. Effective
indexes are one of the best ways to improve performance in a database application.
Syntax :
CREATE SEQUENCE [schema].[Name_of_Sequence]
[ AS <data type> ]
[ START WITH <value> ]
[ INCREMENT BY <value> ]
Ex: CREATE SEQUENCE [dbo].[NewCounter]
AS INT
START WITH 5
INCREMENT BY 5
12) Explain Execution Plan.
A: To Execute a Query we make an execution plan.
Execution Plan is a plan to execute a query with the most optimal way which is
generated by Query Optimizer.
Every query has an execution plan.
Query Optimizer analyzes statistics, resources used, I/O and CPU processing time
and etc. and comes up with a number of plans. Then it evaluates those plans and
the most optimized plan out of the plans is Execution Plan. It is shown to users as a
graphical flow chart that should be read from right to left and top to bottom.
It Provides an alternative name for another database object, referred to as the base
object, that can exist on a local or remote server.
A synonym provides you with many benefits if you use it properly.
First, specify the target object that you want to assign a synonym in the FOR clause
Second, provide the name of the synonym after the CREATE SYNONYM keywords
Note: The object for which you create the synonym does not have to exist at the time
the synonym is created.
15) What is cursors?
A: Cursor is a database object used by applications to manipulate data in a set on a
row-by-row basis, instead of the typical SQL commands that operate on all the
rows in the set at one time.
In order to work with a cursor we need to perform some steps in the following
order:
Declare cursor
Open cursor
Fetch row from the cursor
Process fetched row
Close cursor
Deallocated cursor
E.g.: DBCC CHECKDB - Ensures that tables in the db and the indexes are
correctly linked.
DBCC CHECKALLOC - To check that all pages in a db are correctly allocated.
DBCC CHECKFILEGROUP - Checks all tables file group for any damage.
17) What is difference between DELETE & TRUNCATE commands?
A: 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.
Use SQL Profiler to monitor only the events in which you are interested.
Monitoring too many events adds overhead to the server.
OUTER JOIN:
Left Outer Join: Gets all non-matching records from left table &
AND one copy of matching records from both the tables based on
the joining columns.
Right Outer Join: Gets all non-matching records from right table
& AND one copy of matching records from both the tables based on
the joining columns.
Full Outer Join: Gets all non-matching records from left table &
all non-matching records from right table & one copy of matching
records from both the tables.
Restricted Left Outer Join: gets all non-matching records from left side
Restricted Right Outer Join : gets all non-matching records from right side
20) What is a sub-query?
A: It is a query within a query
Syntax: SELECT <column_name> FROM <table_name>
WHERE <column_name> IN/NOT IN
(
<another SELECT statement>
)
Everything that we can do using sub queries can be done using Joins, but anything
that we can do using Joins may/may not be done using Sub-query.
We can also have a sub-query inside of another sub-query and so on. This is called
a nested Sub-query. Maximum one can have is 32 levels of nested Sub-Queries.
21) What are parameters?
A: Parameters are used to exchange data between stored procedures and functions
and the application or tool that called the stored procedure or function:
Input parameters allow the caller to pass a data value to the stored procedure or
function.
Output parameters allow the stored procedure to pass a data value or a cursor
variable back to the caller. User-defined functions cannot specify output
parameters.
Every stored procedure returns an integer return code to the caller. If the stored
procedure does not explicitly set a value for the return code, the return code is 0.
System Defined Function are defined by SQL Server for a different purpose.
A: They are tables just like regular tables but the main difference is its scope.
The scope of temp tables is temporary whereas regular tables permanently reside.
Temporary table are stored in tempDB.
We can do all kinds of SQL operations with temporary tables just like regular tables
like JOINs, GROUPING, ADDING CONSTRAINTS etc.
Advantages:
■ Table variables can be faster than permanent tables.
■ Table variables need less locking and logging resources.
Disadvantages:
■ Scope of Table variables is batch bound.
■ Table variables cannot have constraints.
■ Table variables cannot have indexes.
■ Table variables do not generate statistics.
■ Cannot ALTER once declared (Again, no DDL statements).
27) What are the differences between Temporary Table and Table Variable?
A:
Temporary Table Table Variable
Statement (or Can perform both Can perform only
Operation) DML and DML, but not
DDL DDL
DCL
DCL is abbreviation of Data Control Language. It is used to create roles,
permissions, and referential integrity as well it is used to control access to database
by securing it.
GRANT – Gives user’s access privileges to database
REVOKE – Withdraws user’s access privileges to database given with the
GRANT command
TCL
TCL is abbreviation of Transactional Control Language. It is used to manage
different transactions occurring within a database.
COMMIT – Saves work done in transactions
ROLLBACK – Restores database to original state since the last COMMIT
command in transactions
SAVE TRANSACTION – Sets a savepoint within a transaction