MS SQL Server

ByHariharan Rajendran

Always Encrypted in SQL Server – Part 2

We can easily enable the encryption using wizard which you can check it here.

Disabling the encryption is also an easy task through wizard.

Steps to disable the encryption,

Step 1: Right Click on your database and choose Tasks->Encrypt Columns..

Step 2: Select next on the introduction page.

Step 3: When you are enter into column selection page, you can see the lock symbol on the columns which you enabled the encryption. Under encryption type, you can see the drop down values which includes “Plaintext”, “Deterministic” & “Randomized”. Choose the “Plaintext” and proceed next.

image

image

image

Step 4: Go through further steps and finish it.

Step 5: Now, run the select statement on table and you should see the result without an encryption.

 

We can define the “Encryption Key” & “Master key” Name manually.

Follow the steps to create custom names and to make use for encryption.

Step 1: Go to Security folder of your database.

image

Step 2: Expand the security folder and you should see the folder called “Always Encrypted Keys”. Expand the folder.

image

Step 3: You should see another two folders, Column Master Keys & Column Encryption Keys. Right click on Column Master Keys folder and choose “New Column Master Key..”

image

Step 4: Define the name of the master key and choose the key store where you want to store your key safely and click OK. Master will be created.

image

Step 5: Right click on Column Encryption Keys folder and choose “New Column Encryption Keys”

image

Step 6: Give the name and choose the master key from drop down. You should see the master key which you created in above step 4 and click OK.

image

Step 7: Now, go and create an encrypted column and you should see the option to choose your encryption key. Master key also will be applied automatically.

image

ByHariharan Rajendran

Always Encrypted SQL Server 2016

I have discussed “Always Encrypted” concept and covered steps to create Always encrypted columns through T-SQL Statement. Check column level encryption using Always Encrypted in SQL Server 2016

We can enable the encryption to the database table columns through wizard as well .

Before Always Encryption,

image

After Always Encryption,

image

Steps to encrypt the column,

Step 1: Right click your database where your tables are there to encrypt.

Step 2: Go to Tasks->Encrypt Columns… and choose.

image

Step 3: You will see the Introduction page, click Next

image

Step 4: Choose the columns from table and choose the encryption type. To know more about encryption type, visit here. The encryption key name will be created like “CEK_Auto”. It will keep extend like “CEK_Auto1”, …Auto2, etc. You can use the same encryption key for multiple columns.

image

image

Step 5: Once selected the required options, Next option will be enabled and click Next

image

Step 6: Master Key Configuration will be created for your database. It is an one time activity.

image

Step 7: Click Next and you can see two options,

  1. Generate PowerShell Script to run later – Basically this enabling the encryption should be done while doing database maintenance.
  2. Proceed to finish now

image

image

Step 8: Check the configuration in summary page and click next to proceed. The scripts will start run in the background and will encrypt the selected columns.

image

image

Step 9: Run the table and check the column values.

image

ByDr. SubraMANI Paramasivam

Data Driven & SQL Server 2016

Following today’s Data Driven live streaming from New York, Satya Nadella have opened the speech by providing insights on what CEOs and organization owners should see with Data being the key factor driving the business.

DataDriven_02

DataDriven_01

 

Also SQL Server 2016 RC (Release Candidate) is now available here and no more CTPs from Microsoft download site. Be the first to download the latest Release Candidate and try the brand new features of SQL Server.

DataDriven_04

DataDriven_05

ByHariharan Rajendran

[Step by Step] Azure SQL Database Linked Server

As we are familiar with the known concept “Linked Server”. This article is about, how to create a linked server for Azure SQL database.

Make sure the firewall is enabled in Azure SQL Server.

Step 1: Go to SQL Server Management Studio

Step 2: Run the below scripts. This script using a stored procedure “SP_addlinkedserver”.

EXEC sp_addlinkedserver

@server=‘Azure’, –Provide Linked Server Name

@srvproduct=,    

@provider=‘sqlncli’,

@datasrc=‘tcp:<servername>.database.windows.net,1433’, –provide Azure SQl Server name

@location=,

@provstr=,

@catalog=‘SQLDB1’ –Provide azure database name

 

Step 3: Run the below script to provide the SQL Server login for the above created linked server.

exec sp_addlinkedsrvlogin ‘Azure’, ‘FALSE’, NULL, ‘<azure sql server login>’, ‘<sql server password>’;

 

Step 4: Verify the created linked server.

image

 

Step 5: Access the azure SQL database table. You need to use 4 part query. <linked server name>.<database name>.<schema name>.<table name>

 

image

ByHariharan Rajendran

In Memory Table in SQL Server

In a normal database, we can not create a in-memory tables. To create a in-memory table, we need to follow the below rules.

1. Create or Alter database to add a memory optimized data file group

2. Add a file into the file group.

Steps to alter your database to support in-memory tables. Before that let us try to create in-memory table in normal database.

image

Step 1: Alter the database to add memory optimized file group.

ALTER DATABASE [SampleDB] ADD FILEGROUP Analytics_Mode CONTAINS MEMORY_OPTIMIZED_DATA

 

Step 2: Add a file into the file group. Try to provide the path where SQL Server is installed.

ALTER DATABASE [SampleDB] ADD FILE (name=‘Analytics_Mode’,filename=‘E:\Local_Install_Applications\SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\InMemory1’) TO FILEGROUP Analytics_Mode

 

In the above script “InMemeory1” is a directory, don’t create this directory manually. The above script will create automatically and will populate the files.

Step 3: Now run the in-memory table script.

CREATE TABLE Account (

    ID int NOT NULL PRIMARY KEY NONCLUSTERED,

    AccountDescription nvarchar (50),

    AccountType nvarchar(50),

    Unit int

    )

    WITH (MEMORY_OPTIMIZED = ON );

GO

image

ByHariharan Rajendran

Insights on StretchDB SQL Server CTP 3.3

As we know that, the current version of stretch DB is migrating all the rows from local to Azure database table when there is no filter predicate is applied (in GUI, there is no option for filter predicate, it is possible via T-SQL script). At one point of time, there is no records in local table.

In this situation, we can not get the correct performance while executing the query. I had a chance to see the query execution plan and did some analysis.

I have executed the script when stretch db is not enabled. I took a simple table without any index on it.

image

 

Then I enabled the stretch db and included the above table and got the below query plan. At this time, almost all the records were migrated from local to azure.

image

This was not the execution plan when I see immediately once enabled the stretch. At that time, local table has more records compare than azure.

ByHariharan Rajendran

Temporary Tables & Table Variable

In SQL Server, temp tables are created at run time. It is just like a normal table and it will support all the operations but these tables are created inside temp database. The scope of the table is only on session & connection.

There are two types of temporary tables,

  1. Local Temp Table
  2. Global Temp Table

Local Temp Table

It will be available only on SQL Server session or connection. It means, if the session is closed or connection is out then this temp table will not be accessible, it will be deleted.

This will be accessible only one user who creates the table.

Local Temp table name is started with “#” sign.

Syntax

CREATE TABLE #LocalTemp

(

 Columns

)

Example

–Create a local Temp Table

CREATE TABLE #LocalTemp

(

 UserID int,

 Name varchar(50),

 EmailId varchar(50),

 Phonenumber varchar(15)

)

GO

–Insert values directly

Insert into #LocalTemp values (1,‘Hari’‘Test@Test.com’‘123-245-356’)

Go

–Insert value from another table

Insert into #LocalTemp 
(UserIDName, EmailIdPhonenumber)

(SELECT UserId, NameEmailId, Phonenumber from dbo.USERS)

Go

Try to open a new query window and access the above table then see the result by yourself.

TempTable

Global Temp Table

This table will be available for all SQL Server sessions. This can be accessible by multiple users with the same connections. It will be deleted when a connection is closed or all the sessions are closed.

Global Temp table name is started with “##” sign

Syntax

CREATE TABLE ##GlobalTemp

(

 Columns

)

GO

Example

–Create a local Temp Table

CREATE TABLE ##GlobalTemp

(

 UserID int,

 Name varchar(50),

 EmailId varchar(50),

 Phonenumber varchar(15)

)

GO

–Insert values directly

Insert into ##GlobalTemp values (1,‘Hari’‘Test@Test.com’‘123-245-356’)

Go

–Insert value from another table

Insert into ##GlobalTemp 
(UserIDName, EmailIdPhonenumber)

(SELECT UserId, NameEmailId, Phonenumber from dbo.USERS)

Go

Open multiple query window and access this table then close the connection and connect again. Then try to access the table by yourself.

Table Variable

Apart from temp tables, we have table variable and the scope of this table variable only with the batch of script.

It will be dropped once it comes out of the batch.

This is also created on temp database but not on memory. This allows us to create primary key on table as like normal table when declare a table variable but the difference is we cannot create cluster index.

Example

–First Batch Start

–Create a table variable

DECLARE @Users TABLE

 (

 UserID int
identity (1,1),

 Name varchar(50),

 EmailId varchar(50),

 Phonenumber varchar(15)

 )

  –insert values from another table

 Insert into @Users
(Name, EmailIdPhonenumber)

 SELECT  NameEmailId, Phonenumber from dbo.USERS

 –Select the records

 SELECT * FROM @Users

 –First Batch End …………..

 –Second batch start

 SELECT * FROM @Users –gives error

 Go

 –Second Batch End….

ByHariharan Rajendran

Composite Key in SQL Server

This article is about to create primary key on multiple columns. Go through my previous article here to know about primary key and how to create it.

There are many scenarios where we need to create primary key on multiple columns.

For example, if you take E-learning sites. They are offering free trail and asking us to sign up using our email id and phone number to experience the free trail.

Once trail period is completed then we cannot apply for one more trail using same email id and phone number because the system will not allow inserting same combination again.

Primary key on multiple columns is called “Composite Key”.

Composite Key Syntax:

CREATE TABLE SAMPLE_TABLE
(COL1 int,
COL2 varchar(30),
COL3 varchar(50),
PRIMARY KEY (COL1, COL2));

Example.

CREATE TABLE USERS

(UserId int,

Name varchar(50),

EmailId varchar(50),

Phonenumber varchar(50)

PRIMARY KEY (EmailId, Phonenumber))

 

composite key