SQL Server Question & Answers
SQL Server Question & Answers
Join is a query which retrieves related columns or rows from multiple tables.
Subquery is a query whose return values are used in filtering conditions of the
main query.
6. What is correlated sub-query?
Each SQL statement within the query for Intersect, Minus, Union, Union ALL must
have the same number of fields in the result sets with similar data types.
A View can be updated/deleted/inserted if it has only one base table. If the view
is based on columns from one or more tables then insert, update and delete is
not possible.
Triggers allow us to execute a batch of SQL code when an insert, update or delete
command is executed against a specific table. Triggers are "attached" to a single
table and allow us to setup our database in such a way that whenever a record is
added, updated, or deleted from a table, then SQL server will automatically
execute a batch of SQL code after that table modification takes place.
Syntax:
FOR INSERT:
When SQL server processes this "INSERT" command, it creates a new virtual
table, which contains all of the fields in the "INSERT" command. This table is
named "Inserted", and is passed to the actual trigger. The table is named
"Inserted" because it contains all of the newly added fields and values from our
"INSERT" command.
FOR DELETE:
If we created a trigger that was activated when we deleted a record from the
"authors table (using the "FOR DELETE" syntax), then the virtual table would
contain all of the fields and values from the deleted record(s), and would be
named "Deleted".
FOR UPDATE:
Likewise, if we created a trigger for when an authors details were updated (using
the "FOR UPDATE" syntax), then both the "Inserted" and "Deleted" virtual tables
would be created and available from within the trigger. The "Deleted" table would
contain all of the fields and values for the row(s) before they were updated, and
the "Inserted" table would contain the new row(s) with the updated fields and
values.
When dealing with triggers, you must understand how they actually operate on
the data contained within their virtual tables. Let’s say that we run an "UPDATE"
command on the "authors" table, which has a trigger attached to it. The
"UPDATE" command might affect more than one row. When this is the case, the
"UPDATE" trigger is called for each row that was affected by the update
command. So, at any one time, each trigger only deals with one row.
In previous versions of SQL Server, triggers were sections of code that were
attached to tables and executed automatically after pre-defined updates took
place on a specified table. These of course still exist in SQL Server 2000. Instead
of triggers are attached to a table in a similar way, but the code inside them is
executed in place of the original updating statement.
Note that the trigger still has access to the Inserted and Deleted tables, which
work in the same way as old-style triggers. What is different is the contents the
test1 table as displayed from within the trigger: For old-style triggers the
contents of the test1 table would more closely match the contents of the Inserted
table because the trigger fires after the update, but here the data selected from
test1 is unchanged because no update is actually taking place (remember--the
trigger happens instead of the update)
14. Multiple Triggers
You cannot create more than one "Instead Of" trigger on a table or view for each
operation (Insert, Update or Delete) but you can have "(instead Of" triggers
mixing with the old style of triggers. If you do, then the following rules apply.
2) If there is no update to the original target table defined in the "Instead of"
trigger, subsequent triggers on that table will not fire
3) If the "Instead Of" trigger does update the original target table, then other
triggers defined on that table will fire as appropriate
4) If the "Instead Of" trigger converts the update operation type (for example
converting an Update into Delete and Insert operations) then the triggers that
relate to the operation defined inside the "Instead of" trigger will fire.
The definition of BCNF addresses certain (rather unlikely) situations which 3NF
does not handle. The characteristics of a relation which distinguish 3NF from
BCNF are given below. To be precise, the definition of 3NF does not deal with a
relation that:
23. What are user defined data types and when you should go for them?
User defined data types let you extend the base SQL Server data types by
providing a descriptive name, and format to the database. Take for example, in
your database, there is a column called Flight_Num which appears in many
tables. In all these tables it should be varchar(8). In this case you could create a
user defined data type called Flight_num_type of varchar(8) and use it across all
your tables.
24. What is bit data type and what's the information that can be stored
inside a bit column?
Bit data type is used to store Boolean information like 1 or 0 (true or false). Until
SQL Server 6.5 bit data type could hold either a 1 or 0 and there was no support
for NULL. But from SQL Server 7.0 onwards, bit data type can represent a third
state, which is NULL.
25. Define candidate key, alternate key, and composite key.
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.
26. What are defaults? Is there a column to which a default can't be bound?
A default is a value that will be used by a column, if no value is supplied to that
column while inserting data. IDENTITY columns and timestamp columns can't
have defaults bound to them.
Here are the other isolation levels (in the ascending order of isolation):
1) Read Uncommitted
2) Read Committed
3) Repeatable Read
4) Serializable
32. What's the difference between a primary key and a unique key?
Both primary key and unique key enforce uniqueness of the column on which
they are defined. But by default primary key creates a clustered index on the
column, where are unique creates a non-clustered index by default. Another
major difference is that, primary key doesn't allow NULLs, but unique key allows
one NULL only.
Note:
2) The check constraint defined on a table must refer to only columns in that
table. It can not refer to columns in other tables.
The syntax for creating a check constraint using a CREATE TABLE statement is:
The DISABLE keyword is optional. If you create a check constraint using the
DISABLE keyword, the constraint will be created, but the condition will not be
enforced.
Example:
CREATE TABLE suppliers
(supplier_id numeric(4),
supplier_name varchar2(50),
CONSTRAINT check_supplier_id
CHECK (supplier_id BETWEEN 100 and 9999)
);
The DISABLE keyword is optional. If you create a check constraint using the
DISABLE keyword, the constraint will be created, but the condition will not be
enforced.
1) Stored procedures split your code between the one program and process and
another program and process. Obviously, it is more convenient to have to program in
only one place. Program partitioning is an artifact of bandwidth limitations, not
something invented just to make programming easier.
3) Stored procedures are neither standardized nor likely to be in the same language
that you are using.