General Flashcards

1
Q

transaction level isolation READ UNCOMMITTED

A

Transactions running at the READ UNCOMMITTED level do not issue shared locks to prevent other transactions from modifying data read by the current transaction. READ UNCOMMITTED transactions are also not blocked by exclusive locks that would prevent the current transaction from reading rows that have been modified but not committed by other transactions. When this option is set, it is possible to read uncommitted modifications, which are called dirty reads. Values in the data can be changed and rows can appear or disappear in the data set before the end of the transaction. This option has the same effect as setting NOLOCK on all tables in all SELECT statements in a transaction. This is the least restrictive of the isolation levels.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

transaction level isolation READ COMMITTED

A

Specifies that statements cannot read data that has been modified but not committed by other transactions. This prevents dirty reads. Data can be changed by other transactions between individual statements within the current transaction, resulting in nonrepeatable reads or phantom data. This option is the SQL Server default.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

TRANSACTION ISOLATION LEVEL REPEATABLE READ

A

Specifies that statements cannot read data that has been modified but not yet committed by other transactions and that no other transactions can modify data that has been read by the current transaction until the current transaction completes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

TRANSACTION ISOLATION LEVEL SNAPSHOT

A

Specifies that data read by any statement in a transaction will be the transactionally consistent version of the data that existed at the start of the transaction. The transaction can only recognize data modifications that were committed before the start of the transaction. Data modifications made by other transactions after the start of the current transaction are not visible to statements executing in the current transaction. The effect is as if the statements in a transaction get a snapshot of the committed data as it existed at the start of the transaction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

TRANSACTION ISOLATION LEVEL SERIALIZABLE

A
  • Statements cannot read data that has been modified but not yet committed by other transactions.
  • No other transactions can modify data that has been read by the current transaction until the current transaction completes.
  • Other transactions cannot insert new rows with key values that would fall in the range of keys read by any statements in the current transaction until the current transaction completes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

XML is case sensitive

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you generate the XSD schema?

A

XMLSchema in the For XML Clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What XML formats data like this:

A

For XML Raw

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What XML formats data like this:

1
Customer A

123

345

A

For XML Auto, Elements (optional to make it element centric otherwise the items would be rolled up to combine all the elements of oen area like custid and companyname would be one element), Root (‘CustomersOrders’)

Also you can add With XMLNameSpaces(‘TK461-CustomersOrders’ as co) to the top above the Select to add the xmlso:co=”TK461-CustomersOrders”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Where do you place the For XML Auto?

A

After the Order By Clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the order of execution?

A
FROM clause
WHERE clause
GROUP BY clause
HAVING clause
SELECT clause
ORDER BY clause
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you completely define the the elements in a created XML document?

A

Use XML Path

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does Persisted do?

A

Specifies that the Database Engine will physically store the computed values in the table, and update the values when any other columns on which the computed column depends are updated. Marking a computed column as PERSISTED allows an index to be created on a computed column that is deterministic, but not precise. For more information, see Indexes on Computed Columns. Any computed columns used as partitioning columns of a partitioned table must be explicitly marked PERSISTED. computed_column_expression must be deterministic when PERSISTED is specified.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you format a date into international datetimes for viewing?

A

Use the format function.

Returns a value formatted with the specified format and optional culture in SQL Server 2012. Use the FORMAT function for locale-aware formatting of date/time and number values as strings. For general data type conversions, use CAST or CONVERT.

FORMAT ( value, format [, culture ] )

Format can also be used to format strings and do things like add leading zeros.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you limit the data going into a field on a table to certain values?

A

Use a check constraint

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Holdlock

A

Use a hold lock to hold a lock until completion of the transaction, instead of releasing the lock as soon as the required table, row, or data page is no longer required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Rowlock

A

Use row-level locks when reading or modifying data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

xlock

A

Use exclusive locks instead of shared locks while reading a table, and use hold locks until the end of the statement or transaction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

UPDLOCK

A

UPDLOCK reads data without blocking other readers, and update it later with the assurance that the data has not changed since last read.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

If an analysis of a query plan shows table cans where the estimated row do not match the actual rows in the query, what should you do?

A

Update the statistics on all the involved tables.

Updates query optimization statistics on a table or indexed view. By default, the query optimizer already updates statistics as necessary to improve the query plan; in some cases you can improve query performance by using UPDATE STATISTICS or the stored procedure sp_updatestats to update statistics more frequently than the default updates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What types of table compression are available?

A

Page or Row

Page compression includes Row compression.

You add compression when creating or altering a table With (Data_Compression = Page or Row)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What does IAM stand for?

A

Index Allocation Map

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What levels can an index have?

A

Root, leaf, intermediate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What tracks index usage?

A

dm_db_index_usage_stats

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What does DM stand for?

A

Dynamic Management view or function

Dynamic management views and functions return server state information that can be used to monitor the health of a server instance, diagnose problems, and tune performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

How do you know there are no more rows to fetch?

A

@@fetch_status = -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

tablock

A

Use a table lock when reading or modifying data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

what are the mathematical branches that the relational model is based on?

A

Set theory and predicate logic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is the difference between newid and newsequentialid?

A

NewID is a unique GUID, but there is no way of knowing the value returned. newsequentialid is also a unique GUID but for the machine and it is sequential.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What does Coalesce do?

A

It accepts a list of inputs and it returns the first one that is not null.

Coalsece (column1, column2, ‘nothing’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the downside to Coalesce or isNull?

A

When used in the where clause they both cause a performance issue, since the index will not be used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What function returns the current date and time value as a datetime2 type?

A

SysDateTime

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

When concatenating strings, what is the difference between + and the concat function?

A

The + operator returns a null if any of the values are null, the concat treats null as an empty string.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

What are the wildcard patterns in a like statement?

A

%

_ underscore marks a single character that can be anything, so ‘_D%’ anything where the second character is D

[character list], like ‘[AC]%’ which is anything that starts with A or C

[character range], like [0-9]%

not in range [^0-9]% is everything that does not start with 0 to 9

35
Q

What is the form of a filter predicate that can rely on index ordering called?

A

A search argument or SARG for short.

No wildcards are the start of the clause and no functions, these limit the servers ability to use indexes.

36
Q

what is the language neutral date format?

A

yyyymmdd

37
Q

what is required when using offset fetch?

A

1) an order by
2) you can have “offset 25 rows”, but you cannot have “fetch next 25 rows only”, there cannot be a fetch without and offset.

38
Q

How can you help performance on foreign keys?

A

SQL server creates an index when you make a primary key or unique constraint, it does not make an index with a foreign key. You need to manually create indexes on those fields which will improve performance.

39
Q

What are the 4 forms of table expressions?

A

Derived tables
common table expressions (CTE)
Views
Inline table-valued functions

40
Q

What is the difference between the apply and join operators?

A

With a join operator, both inputs represent static relations. With apply, the left side is a static relation, but the right side can be a table expression with correlations to the elements from the left table.

41
Q

What is the set operator precedence?

A

Intersect precedes Union and Except -

Union and Except are evaluated from left to right

42
Q

What are the window ranking functions?

A

row_number
rank
dense_rank
ntile( numeric value)

These are only allowed in the Select and Order By

43
Q

What are the clauses that the different types of window functions support?

A

Partitioning, ordering, framing

44
Q

What do the delimiters Unbound Preceding and Unbounded Following represent?

A

Unbound Preceding = the beginning of the set

Unbounded Following = the end of the set

45
Q

Which data type should be used in place of timestamp?

A

Rowversion

46
Q

How does NULL work with a unique constraint?

A

Columns do not need to be NOT NULL, but only one row in the table can be NULL.

47
Q

How do you obtain a new value from a sequence, if you aren’t using the Identity option?

A

With the Next Value For function

48
Q

What is Scope_Identity for?

A

It returns the last IDENTITY value produced on a connection and by a statement in the same scope, regardless of the table that produced the value.
SCOPE_IDENTITY(), like @@IDENTITY, will return the last identity value created in the current session, but it will also limit it to your current scope as well. In other words, it will return the last identity value that you explicitly created, rather than any identity that was created by a trigger or a user defined function.

49
Q

What is @@Identity for?

A

It returns the last IDENTITY value produced on a connection, regardless of the table that produced the value, and regardless of the scope of the statement that produced the value.
@@IDENTITY will return the last identity value entered into a table in your current session. While @@IDENTITY is limited to the current session, it is not limited to the current scope. If you have a trigger on a table that causes an identity to be created in another table, you will get the identity that was created last, even if it was the trigger that created it.

50
Q

What is Ident_Current for?

A

It returns the last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope.

51
Q

How many WHEN MATCHED clauses can a single Merge statement have?

A

Two - one update and one delete

52
Q

How do you determine which action affected the Output row in a merge statement?

A

Use the $action function

53
Q

How many Output clauses can a single statement have?

A

Two - one with Into and one without Into

54
Q

What are the ACID properties of transactions?

A

Atomicity
Atomicity requires that each transaction is “all or nothing”: if one part of the transaction fails, the entire transaction fails, and the database state is left unchanged. An atomic system must guarantee atomicity in each and every situation, including power failures, errors, and crashes. To the outside world, a committed transaction appears (by its effects on the database) to be indivisible (“atomic”), and an aborted transaction does not happen.

Consistency
The consistency property ensures that any transaction will bring the database from one valid state to another. Any data written to the database must be valid according to all defined rules, including but not limited to constraints, cascades, triggers, and any combination thereof. This does not guarantee correctness of the transaction in all ways the application programmer might have wanted (that is the responsibility of application-level code) but merely that any programming errors do not violate any defined rules.

Isolation
The isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed serially, i.e. one after the other. Providing isolation is the main goal of concurrency control.

Durability
Durability means that once a transaction has been committed, it will remain so, even in the event of power loss, crashes, or errors. In a relational database, for instance, once a group of SQL statements execute, the results need to be stored permanently (even if the database crashes immediately thereafter). To defend against power loss, transactions (or their effects) must be recorded in a non-volatile memory.

55
Q

How does SQL Server implement transaction durability?

A

By first writing all changes to the database transaction log before making changes to the database data.

56
Q

What does @@Trancount values mean?

A

0 = the code is not within a transaction

>0 indicates the active transaction

57
Q

What do XACT_State() values mean?

A
0 = no active transaction
1 = there is an uncommitted transaction
-1 = there is an uncommitted transaction but it cannot be committed due to an error
58
Q

What is the default transaction isolation level?

A

Read Committed

59
Q

What should you use instead of NoLock?

A

ReadUncommited, nolock is being deprecated

60
Q

What is security level 0 used for?

A

When you issue a raiseerror with severity level of 0, only an informational message is sent.

61
Q

What are the two types of triggers?

A

After = only fires after the event is complete, this can only be used on permanent tables

Instead of = this fires instead of the event, it can be on permanent tables or views

62
Q

What are the 3 join algorithms?

A

Nested Loops, Merge, Hash

There is a 4th, but it’s part of the Hash called Bitmap Filtering (also called Star join)

63
Q

What are the query execution steps?

A

Parsing -> Binding -> Optimization -> Execution

64
Q

What are the relational engine and storage engine?

A

The relational engine is an internal component that works on a logical level. The actual execution is performed by the storage engine.

65
Q

How would you quickly measure the amount of disk IO a query is performing?

A

Set Statistics IO

66
Q

How can you get an estimated execution plan in XML format?

A

Set Showplan_XML

67
Q

Which DMO gives you detailed text of the queries executed?

A

sys.dm_exec_sql_text

68
Q

What is a heap?

A

A data heap is data in no logical or organized order.

The only way to find data in a heap is to scan the whole heap.

69
Q

What is a balanced tree?

A

A balanced tree is an organized way of storing data. It will have a single root page and at least one or more leaf pages. The data is stored in the logical order of the clustered key. A clustered index, creates the balanced tree.

70
Q

Which clauses of a query should you consider supporting with an index?

A

Select, Where, Join, Group By and Order By

71
Q

What is a nested loop algorithm?

A

In its simplest form, a nested loops join compares each row from one table (known as the outer table) to each row from the other table (known as the inner table) looking for rows that satisfy the join predicate. (Note that the terms “inner” and “outer” are overloaded; their meaning must be inferred from context. “Inner table” and “outer table” refer to the inputs to the join. “Inner join” and “outer join” refer to the logical operations.)

72
Q

What is a merge join?

A

The Merge Join transformation provides an output that is generated by joining two sorted data sets using a FULL, LEFT, or INNER join. The Merge Join transformation requires that both inputs be sorted and that the joined columns have matching meta-data. User cannot join a column that has a numeric data type with a column that has a character data type. If the data has a string data type, the length of the column in the second input must be less than or equal to the length of the column in the first input with which it is merged.

73
Q

What is a hash join?

A

This join type is probably the most common one that you will encounter. It uses a hash table to aid in joining. The join works in two phases, the build phase and the probe phase. We will refer to the two tables to be joined as the build table (commonly the smaller of the two) and the probe table. For a hash join to work, at least one of the join conditions will need to be a equijoin, that is, two columns that are equal (=) to each other.

In the build phase, the server calculates hash values for the build table. Think of this as a temporary index on the join columns. This calculated hash table is then stored in memory or swapped out to physical storage in tempdb, depending on its size. This is the main potential drawback of the hash join – if your tables are really large, this join will consume quite a bit of memory.

When the build phase is complete, the server will enter the probe phase. This phase calculates hash values for each row in the probe table and tries to match those values to corresponding hash value in the build table, completing the join. If the entire build table fits into memory, the join is called an in-memory hash join. If it has to be split to disk, it’s referred to as a grace hash join. In some cases, where you have really large amounts of data, the server will create a number of recursive merge joins, called a recursive hash join.

74
Q

What does FIRST_VALUE ( [scalar_expression ] )

OVER ( [ partition_by_clause ] order_by_clause [ rows_range_clause ] ) do?

A

It will return the first value in the selection specified in the over clause.

This:
SELECT Name, ListPrice, 
       FIRST_VALUE(Name) OVER (ORDER BY ListPrice ASC) AS LeastExpensive 
FROM Production.Product
WHERE ProductSubcategoryID = 37;

Returns:
Name ListPrice LeastExpensive
———————– ——————— ——————–
Patch Kit/8 Patches 2.29 Patch Kit/8 Patches
Road Tire Tube 3.99 Patch Kit/8 Patches
Touring Tire Tube 4.99 Patch Kit/8 Patches

75
Q

When preparing to query XML-type columns or variables using XML data type methods, what options must be set?

A

ANSI_PADDING ON
ANSI_NULLS ON
ANSI_WARNINGS ON
QUOTED_IDENTIFIER ON

If these options are not set in this manner, queries and modifications using XML data type methods fail.

76
Q

What is the capacity of tinyint?

A

1 byte

0 - 255

77
Q

What is the capacity of int?

A

4 bytes

-2,147,483,648 to 2,147,483,648

78
Q

What is the capacity of smallint?

A

2 bytes

-32,768 to 32,767

79
Q

What is the capacity of bigint?

A

8 bytes

80
Q

What is check option?

A

It is a associated to a view.

This option forces all statements that modify data executed against the view to follow criteria set within the statement that defines the view.

81
Q

Loop Join

A

This join is also commonly known as nested iteration. This kind of join is composed by an outer loop and an inner loop. When the query runs for each row of the outer loop, the inner loop is executed completely. This join is effective only when the outer loop query is small and the inner loop query has all the proper optimizations applied. This join method is very useful with small transactions.

82
Q

Merge Join

A

This join has the unique requirement for tables involved in the operation to be sorted. This join keeps both of the tables sorted in parallel and compares each table row by row simultaneously with each other. It compares one row of the first table with one row of the second table. If they are equal, that row qualifies; otherwise, this join determines which row of each table has the lower value. Once the lowest value of the table is figured out, it moves on to next row of that table and compares that to the original row. This operation keeps going on until all rows from each table are completely examined. This operation can be very expensive when tables are not sorted and it’s required to sort them before they are joined. If tables have non clustered indexes over them and joins are using the same conditions, there are pretty good chances that this join performs better than other kinds of joins.

83
Q

Hash Join

A

This is the most complex of all the other joins. There are two major components of this kind of join – build query and probe query. First, a smaller table is assigned as build query and a hash table for the same is created. This hash table is compared with the probe table. This comparison of input table and probe table is done one row at a time. One row of the probe table is hashed and compared against the other row, and qualifying rows are checked.