SKILL 2.4. Query Temporal data and non-relational data Flashcards
Temporal Tables - Requirements
A primary key constraint
Two DATETIME2 columns with chosen precision to store the start and the end of the validity period of the row
The start column needs to be marked with the clause GENERATED ALWAYS AS ROW START
The end column needs to be marked with GENERATED ALWAYS AS ROW END
The table option SYSTEM_VERSIONING needs to be set to ON
A linked history table which SQL server creates for you
PERIOD FOR SYSTEM_TIME(“startcolumn”,”endcolumn”)
Temporal Tables - format
CREATE TABLE dbo.Products
(
productid INT NOT NULL
CONSTRAINT PK_dboProducts PRIMARY KEY(productid),
productname NVARCHAR(40) NOT NULL,
supplierid INT NOT NULL,
categoryid INT NOT NULL,
unitprice MONEY NOT NULL,
– below are additions related to temporal table
validfrom DATETIME2(3)
GENERATED ALWAYS AS ROW START HIDDEN NOT NULL,
validto DATETIME2(3)
GENERATED ALWAYS AS ROW END HIDDEN NOT NULL,
PERIOD FOR SYSTEM_TIME (validfrom, validto)
)
WITH ( SYSTEM_VERSIONING = ON ( HISTORY_TABLE = dbo.ProductsHistory ) );
What does HIDDEN property do in a Temporal table
When using SELECT * it does not return that property
FOR SYSTEM_TIME AS OF
returns a table with rows which are between SysStartTime and SystemEndTime
FOR SYSTEM_TIME FROM datetime TO datetime
Returns table with values for all row versions that were active any time during the time specified, excluding the upper boundary
FOR SYSTEM_TIME BETWEEN datetime AND datetime
Returns table with values for all row versions that were active any time during the time specified, including the upper boundary
FOR SYSTEM_TIME CONTAINED IN(datetime,datetime)
Returns a table with the values for all row versions that were opened and closed within the specified range
FOR SYSTEM_TIME ALL
returns the union of rows that belong to the current history table
AT TIMEZONE
Period coulumns store time in the UTC time zone
XML document - characteristics
It is “well-formed”
XML documents are ordered - means not ordered by a criteria but the position of the elements matter
Case-Sensitive Unicode text
Difference between a Document and a Fragment
XML
The document has a single root node. If you delete that element you get a XML fragment
Most used metadata description of XML documents
XML Schema Description (XSD)
FOR XML RAW - output
FOR XML RAW - charcteristics
Its not a XML document because the root node is missing.
In RAW mode every row from the returned result set converts to a single element named row, and columns translate t attributes of this element
FOR XML AUTO
easy to use and returns query results as nested XML elements
What is important in a XML statement
Use ORDER BY statement because without the ORDER BY statement the order of the elements is unpredictable
Order of the SELECT statement defines the returned XML
XQuery
is a standard language for browsing XML
Case sensitive
FOR JSON AUTO
formats the JSON automatically.
You do not have much influence on the format of JSON
FOR JSON PATH
You have much more influence than in JSON AUTO
You can nest elements with a “dot”
SELECT contactname AS [Contact.Name]
FROM Sales.Customers
FOR JSON PATH;
Result:
[
{
"CustomerId":1, "Company":"Customer NRZBB", "Contact":{ "Name":"Allen, Michael" }
}
]
FOR JSON - additional clauses
ROOT - adds single, top level member
INCLUDE_NULL_VALUES - includes nulls in the output - they are excluded by default
WITHOUT_ARRAY_WRAPPER - removes square brackets
SWITCHOFFSET
SWITCHOFFSET function takes 2 arguments, the data type to be returned and the timezone required