SQL Server 2016 Slides for the Newbies. Prepared for a session. Covers SQL 2016 JSON support, Built-in Functions, Data Types & Pre-built system databases
2. System DBs
• master
• All logins, or roles, to which the user IDs belong
• Every system configuration setting (e.g.: data sorting information, security
implementation, default language)
• The names of and information about the databases within the server
• The location of databases
• How SQL Server is initialized
• Several System tables - System error and warning messages, etc.
• tempdb
• lifetime is the duration of a SQL Server session
• Temp tables
3. System DBs
• model
• the basis for creating the tempdb database & any other database created
• if you want all your databases to have a specific initial size or to have a
specific set of information
• msdb
• provides the necessary information to run jobs to SQL Server Agent
• SQL Server Agent: Windows service that runs any scheduled jobs
4. Data Definition Language - CREATE
• CREATE DATABASE
• CREATE DEFAULT When bound to a column, specifies a value to be
inserted
• CREATE TABLE
• CREATE VIEW
• CREATE PROCEDURE Creates a T-SQL or common language runtime (CLR) SP
• CREATE FUNCTION Creates a user-defined function in SQL Server
• CREATE TRIGGER Creates a DML, DDL, or logon trigger
• CREATE SEQUENCE Creates a sequence object and specifies its properties
• CREATE LOGIN Creates a Database Engine login for SQL Server
• CREATE ROLE Creates a new database role in the current database
• CREATE USER Adds a user to the current database
5. Login Vs User
• With a login, you can connect to SQL Server
• It cannot connect to any of the user databases
• user is an account that is database-specific
• a login cannot perform database actions without a valid user account
• The two may have exactly the same name
6. Data Types
• Exact Numerics
• Bigint (Trillions), int, smallint (-32k to +32k), tinyint
• numeric, decimal – With precision and scale. Almost the same
• bit
• money, smallmoney – Not really used
• Approximate Numerics
• Float (application state depends on the implementation of the floating point
numbers)
• Real (same as Float(24))
7. Data Types
• Date and Time
• Date 0001-01-01 through 9999-12-31
• Time 00:00:00.0000000 through 23:59:59.9999999
• Datetime January 1, 1753, through December 31, 9999 with Time
• Smalldatetime 1900-01-01 through 2079-06-06 with 00:00:00
• Character Strings
• Char Fixed-length, non-Unicode string data
• Varchar Variable-length, non-Unicode string data
• text Variable-length non-Unicode data (2^31 - 1)
8. Data Types
• Unicode Character Strings
• Nchar Fixed-length Unicode string data
• Nvarchar Variable-length Unicode string data
• Ntext Variable-length Unicode data
• Binary Strings
• Binary Fixed-length binary data
• Varbinary
• Image Variable-length binary data (2,147,483,647 bytes)
9. Data Manipulation Language
• INSERT Syntax
INSERT [INTO]
{table_name|view_name}
[{(column_name,column_name,…)}]
{VALUES (expression, expression, …)}
• BULK INSERT Example
BULK INSERT Sales.SalesOrderDetail
FROM 'computer2salesforcedailyordersneworders.txt';
10. Data Manipulation Language
• DELETE Syntax
DELETE tablename
WHERE where_condition
• TRUNCATE TABLE Example
• will delete data from a table with millions of records in only a few seconds
• cannot issue it against tables that have foreign keys referencing them
TRUNCATE TABLE CustomerDetails.Customers
11. Data Manipulation Language
• UPDATE
UPDATE dbo.Customers
SET FirstName = ‘Talha'
WHERE CustomerId = 7
• MERGE
• separate insert, update, and delete statements all within the same
statement
• A level above UPSERT: inserts rows that don't exist and updates the
rows that do exist
12. Types of Built-in Functions
Rowset Functions Return an object that can be used like table references in an SQL statement
OPENXML, OPENROWSET, OPENQUERY ( linked_server ,'query' ) , etc.
Aggregate
Functions
Operate on a collection of values but return a single, summarizing value.
AVG, MIN, MAX, COUNT, SUM, STDDEV, etc.
Ranking Functions Return a ranking value for each row in a partition.
RANK, DENSE_RANK, ROW_NUMBER
Scalar Functions
(Described Next)
Operate on a single value and then return a single value. Scalar functions can
be used wherever an expression is valid.
13. Scalar Functions
Function category Description & Examples
Conversion Functions CAST and CONVERT - one data type to another
Date and Time Data
Types and Functions
Perform operations on a date and time input values and return string, numeric, or
date and time values.
JSON Functions Validate, query, or change JSON data.
Logical Functions CHOOSE ( index, val_1, val_2,..) , IIF ( boolean_expression, true_value, false_value )
Mathematical Functions Perform calculations based on input values provided as parameters to the functions,
and return numeric values.
Metadata Functions Return information about the database and database objects.
String Functions LEFT, RIGHT, SUBSTRING, UPPER, LOWER, CONCAT, RTRIM, LTRIM, ETC.
System Functions Perform operations and return information about values, objects, and settings.
HOST_NAME, ISNULL, ISNUMERIC, @@ROWCOUNT, ERROR_NUMBER, etc
15. JSON Functions
Function Description
ISJSON Tests whether a string contains valid JSON.
JSON_VALUE Extracts a scalar value from a JSON string.
JSON_QUERY Extracts an object or an array from a JSON string.
JSON_MODIFY Updates the value of a property in a JSON string and returns the updated JSON string
16. Date and Time Functions
Syntax Return value
SYSDATETIME () & GETDATE ( ) date and time of the computer on which the instance of SQL Server is running
SYSUTCDATETIME ( ) & GETUTCDATE ( ) The date and time is returned as UTC time (Coordinated Universal Time).
DATENAME ( datepart ,date ) Returns a string that represents the specified datepart
DATEPART ( datepart ,date ) Returns an integer that represents the specified datepart
DAY ( date ) Returns an integer that represents the day part
MONTH ( date ) Returns an integer that represents the month part
YEAR ( date ) Returns an integer that represents the year part
DATEDIFF
( datepart ,startdate , enddate )
Returns the number of date or time datepart boundaries that are crossed
between two dates.
DATEADD (datepart , number, date )
Returns a new datetime value by adding an interval to the specified datepart of
the specified date.