SQL Faqs
SQL Faqs
What is a trigger?
- Triggers are basically used to implement business rules. Triggers is also similar to
stored procedures.
The difference is that it can be activated when data is added or edited or deleted from
a table in a database.
What is a view? -
If we have several tables in a db and we want to view only specific columns from
specific tables we can go for views.
It would also suffice the needs of security some times allowing specfic users to see
only specific columns based on the permission that we can configure on the view.
Views also reduce the effort that is required for writing queries to access specific
2
columns every time.
What is an Index?
- When queries are run against a db, an index on that db basically helps in the way the
data is sorted to process the query for faster and data retrievals are much faster when
we have an index.
3
designer to be the primary key for the table, and any other candidate key is called an
alternate key.
What is Code Page in Sql ?
For character and Unicode data, a definition of the bit patterns that represent specific
letters, numbers, or symbols (such as 0x20 representing a blank space and 0x74
representing the character "t"). Some data types use 1 byte per character; each byte can
have 1 of 256 different bit patterns.
Types of backups you can create in SQL Sever 7.0+ are Full database backup,
differential database backup, transaction log backup, filegroup backup. Check out the
BACKUP and RESTORE commands in SQL Server books online. Be prepared to write
the commands in your interview. Books online also has information on detailed
backup/restore architecture and when one should go for a particular kind of backup.
Query Optimizer: It is a component which analysis the query and determines the most
efficient way to request the data and thus optimizing the sql statement. The process of
choosing an execution plan out of several plans is called optimizing and this is done
by query optimizer.
SQL Server Query Optimizer is a cost based optimizer. Each possible execution plan has
an associated cost in terms of computing resources. The query optimizer chooses that
plan which has lowest estimated cost.
What are the Temporary Tables and why these are?
Temporary tables are like permanent table but they are created in tempdb, and they
are deleted automatically when no longer in use.
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
What is Table Variable ?
Functions and variables can be declared to be of type table. table variables can be
used in functions, stored procedures, and batches.
6
Use table variables instead of temporary tables, whenever possible. table variables
provide the following benefits:
• A table variable behaves like a local variable. It has a well-defined scope, which
is the function, stored procedure, or batch in which it is declared.
Within its scope, a table variable may be used like a regular table. It may be
applied anywhere a table or table expression is used in SELECT, INSERT,
UPDATE, and DELETE statements. However, table may not be used in the
following statements:
INSERT INTO table_variable EXEC stored_procedure
SELECT select_list INTO table_variable statements.
table variables are cleaned up automatically at the end of the function, stored
procedure, or batch in which they are defined.
• table variables used in stored procedures result in fewer recompilations of the
stored procedures than when temporary tables are used.
• Transactions involving table variables last only for the duration of an update
on the table variable. Thus, table variables require less locking and logging
resources.
Assignment operation between table variables is not supported. In addition, because
table variables have limited scope and are not part of the persistent database, they are
not impacted by transaction rollbacks.