Chapter 11 Other Data Modification Aspects Flashcards
What is the IDENTITY property?
The IDENTITY is a property of a column in a table. The property automatically assigns a value to the column upon insertion. You can define it for columns with any numeric type that has a scale of 0. This means all integer types, but also NUMERIC/DECIMAL with a scale of 0.
When defining the property, you can optionally specify a seed and an increment. If you don’t, the defaults are 1 and 1. Only one column in a table can have an IDENTITY property.
Note that when you insert rows into the table, you don’t specify a value for the IDENTITY column because it gets values automatically.
What do you do when you want to specify your own values into an IDENTITY column?
You need to set a session option called SET IDENTITY_INSERT ON;.
Note that there’s no option that can be set to update an IDENTITY column.
What are the functions that can be used to query the last identity value generated?
- SCOPE_IDENTITY,
- @@IDENTITY,
- IDENT_CURRENT (Schema.Table)
What is the SCOPE_IDENTITY function?
SCOPE_IDENTITY returns the last identity value generated in your session in the current scope.
What is the @@IDENTITY function?
@@IDENTITY returns the last identity value generated in your session regardless of scope.
What is the IDENT_CURRENT function?
IDENT_CURRENT accepts a table as input and returns the last identity value generated in the input table regardless of session.
What is the difference between SCOPE_IDENTITY and @@IDENTITY?
Suppose you have a stored procedure P1 that performs the following:
- An INSERT that generates a new identity value.
- A call to a stored procedure P2 that also has an INSERT statement that generates a new identity value.
- A query to the functions SCOPE_IDENTITY and @@IDENTITY.
SCOPE_IDENTITY will return the value generated by P1 which is the same scope.
@@IDENTITY will return the value generated by P2 without consideration to scope.
Which statement DELETE or TRUNCATE affects the IDENTITY value?
TRUNCATE resets it to the initial seed.
DELETE doesn’t affect the IDENTITY value
How do you reseed the current identity value?
Use the DBCC CHECKIDENT command:
DBCC CHECKIDENT(‘Sales.MyOrders’, RESEED, 4);
Does the IDENTITY property guarantee uniqueness?
No.
The IDENTITY property does not guarantee uniqueness.
Explicit values can be entered if IDENTITY_INSERT option is turned on.
Also, the values can be reseeded.
To guarantee uniqueness, you need to use a constraint like a PK or UNIQUE constraint.
Does the IDENTITY property guarantee that there will be no gaps between the values?
No.
The IDENTITY property does not guarantee that there will be no gaps in the values.
If an INSERT statement fails, the current identity value is not changed back to the original one so the unused value is lost.
The next insertion will have a value after the one that wasn’t used.
Does the IDENTITY property have cycling support?
No.
The IDENTITY property does not have cycling support.
This means that after you reach the maximum value in the type, the next insertion will fail due to an overflow error.
To get around this, you need to reseed the current identity value before such an attempt is made.
When was the sequence object introduced?
SQL Server 2012
What are some of the benefits of using the sequence object?
- A sequence is not tied to a particular column in a particular table. However, you can use a DEFAULT constraint to assign a new value,
- Because the sequence is an independent object in the database, you can use the same sequence to generate keys that are used in different tables. This way the keys won’t conflict across tables,
- You can generate a sequence value before using it by storing the result of the NEXT VALUE FOR function in a variable,
- You can update columns with the result of the NEXT VALUE FOR function,
- A sequence supports cycling,
- A TRUNCATE statement doesn’t reset the current value of a sequence object because the sequence is independent of the tables that use it.
How do you create a sequence?
You can use the
CREATE SEQUENCE command to create a sequence.
At a minimum, you just need to specify a name for the object as follows: CREATE SEQUENCE .;
CREATE SEQUENCE Test.DecSeq
AS decimal(3,0)
START WITH 125
INCREMENT BY 25
MINVALUE 100
MAXVALUE 200
CYCLE
CACHE 3;
What data type will a sequence return?
Like IDENTITY, all numeric types with a scale of 0 are supported.
If no type is explicitly indicated, SQL Server will assume BIGINT by default.
If a different type is needed, explicitly state it by adding AS after the sequence name.
What are the options associated with the sequence object?
- START WITH - The sequence start value. The default is MINVALUE for an ascending (positive increment) and MAXVALUE for a descending one.
- INCREMENT BY - Increments value. The default is 1,
- MINVALUE - The minimum value to support. The default is the minimum value of the type,
- MAXVALUE - The maximum value to support. The default is the maximum value in the type,
- CYCLE | NO CYCLE - Defines whether to allow the sequence to cycle or not. The default is NO CYCLE,
- CACHE - Caches sequence values
What happens when you set the MINVALUE to 1?
Both the Minimum Value and Start Value is set to 1.
Not only is the minimum value set to 1, but also the start value.
If the start value is set to 1 this does not change the minimum value from -21147483648.
This will be a problem if the sequence allows cycling.
After the last value in the type, the next value generated will not be 1.
Instead it will be -2147483648.
Therefore, to generate only positive values set the MINVALUE to 1.
This will implicitly set the START WITH value to 1 as well.
How do you request a new value from the sequence?
Use the NEXT VALUE FOR function.
SELECT NEXT VALUE FOR Sales.SeqOrderIDs;
This function can be called in
- INSERT VALUES
- NSERT SELECT Statements
- SET clause of an UPDATE statement
- An assignment to a variable
- A DEFAULT constraint expression
- And the list goes on…
Can you change the properties of a sequence?
Mostly Yes, One No.
The data type of an existing sequence cannot be changed. ,
All of the other properties can be changed using the ALTER SEQUENCE command.
ALTER SEQUENCE Sales.SeqOrderIDs
RESTART WITH 1;
When using a sequence in an INSERT SELECT statement, how can you control the order in which sequence values are assigned?
An optional OVER clause with an ORDER BY list can be used to control the order in which the sequence values are assigned to the result rows:
INSERT INTO Sales.MyOrders(orderid, custid, empid, orderdate)
SELECT
NEXT VALUE FOR Sales.SeqOrderIDs
OVER
(
ORDER BY orderid
)
, custid
, empid
, orderdate
FROM Sales.Orders
WHERE custid = 1
This is a T-SQL extension to the standard.
Can you use the NEXT VALUE FOR in a DEFAULT constraint?
Yes.
Use the NEXT VALUE FOR function in a DEFAULT constraint. This will generate the constraint values automatically when you insert rows.
ALTER TABLE Sales.MyOrders
ADD CONSTRAINT DFT_MyOrders_orderid
DEFAULT( NEXT VALUE FOR Sales.SeqOrderIDs ) FOR orderid;
This option is a more flexible alternative than IDENTITY because it only assigns a default value if one wasn’t specified explicitly in the INSERT statement.