The Microsoft MVP Award Program Blog

Independent Experts. Real World Answers.

The Microsoft MVP Award Program Blog

Independent Experts. Real World Answers.

Contained Databases in SQL Server 2012

Contained Databases in SQL Server 2012

  • Comments 2

Editor's Note: The following MVP Monday post is by SQL Server MVP Artemakis Artemiou and is part of our special series on SQL Server 2012.


One of the many new features shipped with the release of SQL Server 2012 are“Contained Databases”.

A contained database is isolated from the SQL Server instance on which is hosted, as well as from the rest of the databases. It somehow exists in its own world, meaning that it does not interact with anything outside itself and does not depend on anything else outside its scope.

The contained databases feature changes the SQL Server security model by making it even more robust as access to contained databases is permitted through special users (known as “contained database users”) which do not require logins. The main benefit of this is that contained databases can become fully portable thus easily allowing the Database Administrator to move them among SQL Server instances without having to resolve issues such as orphaned database users (database users not associated to a login), etc.

Before proceeding with an example, it must be noted that currently, SQL Server 2012 provides partial database containment, meaning that a partially contained database can also allow some features that are outside of its scope.

Now let’s see how we can create and use a partially contained database in SQL Server 2012.

First, we need to enable “contained database authentication” on the SQL Server instance:

sp_configure 'contained database authentication', 1;

GO

RECONFIGURE

GO

 

Then, we create the partially contained database (along with a sample table for demo purposes):

--Create partially contained database

USEmaster

GO

 

CREATE  DATABASE[ContDB]

CONTAINMENT=PARTIAL

GO

 

--Create sample table with sample records

USE [ContDB]

GO

 

CREATE TABLEtblSample(

id int ,

descrvarchar (250)

)

GO

 

INSERT INTO tblSample

VALUES

(10,'Sample value 1'),

(20,'Sample value 2'),

(30,'Sample value 3')

GO

The last step is to create the user(s) that will be accessing the contained database:

USE [ContDB]

GO

 

CREATE USER ContUser1WITHPASSWORD=N'secure1$',DEFAULT_SCHEMA=[dbo]

GO

 

EXEC sp_addrolemember'db_owner', 'ContUser1'

GO

 

*Note: You are also able to use a Windows login if preferred.

 

 

Now let’s try to log into the database using the user “ContUser1”:

Step 1: We enter the database user credentials.

clip_image001[6]

 

Step 2: We enter the database name to connect to and click on“Connect”.

clip_image002[6]

 

That’s it! As you can see from the screenshot below, the user “ContUser1” was able to successfully connect to the SQL Server instance’s database engine and has access only to the partially contained database he/she belongs to:

clip_image003[6]

The last step is to run a simple query against the earlier created table just for checking out that our contained database user has access to the database’s objects:

clip_image004[6]

As you can see from the above screenshot the table is fully accessible.

Contained databases are a significant new feature in SQL Server 2012. The portability derived by this feature provides a solution to critical business questions, when it comes to the underlying data platform. Portability has always been a key factor in the software world. When it comes to the infrastructure level and more specifically to the data platform, portability is something extremely useful and with contained databases in SQL Server 2012 is now available!

 

Artemakis_Artemiou_Photo

Artemakis Artemiou Biography

Artemakis Artemiou is a SQL Server MVP and works as a Systems Engineer in the Information Technology Division of the Bank of Cyprus Group, specializing on advanced database systems engineering and infrastructure systems development. He holds many SQL Server certifications (MCTS, MCITP) on different versions of SQL Server. Artemakis is the president of the Cyprus .NET User Group (CDNUG) and the INETA-Europe Country Leader for Cyprus. He regularly writes articles on different topics on SQL Server and publishes them on his blog or on other blogs as guest articles. Artemakis also participates as a speaker on many local user group events as well as an invited speaker on local Microsoft conferences and workshops. You can find his blog at: http://aartemiou.blogspot.com. You can also find him on Twitter at: http://twitter.com/artemakis

About MVP Mondays

The MVP Monday Series is created by Melissa Travers. In this series we work to provide readers with a guest post from an MVP every Monday. Melissa is a Community Program Manager for Dynamics, Excel, Office 365, Platforms and SharePoint in the United States. She has been working with MVPs since her early days as Microsoft Exchange Support Engineer when MVPs would answer all the questions in the old newsgroups before she could get to them.

melissa

  • Contained databases are useful and they actually help the migration of platform simple.

  • It is a good feature but because of Audit and Risk concern organization not going to allow !

    Regards.

Page 1 of 1 (2 items)
Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post