Sie sind auf Seite 1von 4

This post was published to SQL Thoughts at 10:00:32 PM 2/28/2013

SQL 2012 Contained databases

Category [Choose a category or type a new one] ; For the DBA

A contained database is a database that has minimal dependencies on the master database or other system databases in the instance. Unlike its name it is not a fully contained database. Contained databases are useful when dealing with security and also implementing High availability solutions. DBA who have performed migration might find the obvious benefits of using contained database a quick solution to moving logins and mapping orphaned users however it is important to understand some of the limitations of contained database before deciding to take this step. The first step to setting up contained database is to enable it on the server.
use master go sp_configure 'show advanced options' , 1 go reconfigure go sp_configure go sp_configure 'contained database authentication' , 1 go reconfigure sp_configure 'show advanced options' , 0

The reason for the above step is because the contained database has restricted dependencies on the master database. There are no logins at the server level that can be used with a contained database. Since there are no server logins in a contained database the database itself contains all the information required to authenticate and connect from an application. The next step is to enable database containment at the instance level by modifying the server properties.

Once the database is created its containment option can be changed to Partial containment by modifying the database properties.

At this point we have everything thats needed to create and host contained database. However there is one last step missing at this stage and that is the authenticated account which will be used to connect to the contained database. When creating a user using SSMS there are two new options available in order to create users who are connected to a contained database.

SQL User with password is basically a SQL authenticated account with the user (not login) having a password associated with it.
CREATE USER [ContainedSQlUSer] WITH PASSWORD=N'^#\a_jOm-e DEFAULT_SCHEMA=[dbo] _pM__=',

The second option is create a contained user with a windows authenticated account which is not a login.

When connection to the contained database using contained users the default database needs to be that of the contained database ( by default this is the master database) else you will get the below error message. Login failed for user . (Microsoft SQL Server, Error: 18456) Change the connection database in SSMS or initial catalog of the connection string as shown below.

Since a contained database is meant to have as little external dependencies its useful to use the below DMV to identify any objects that are in the contained database that have mappings to objects outside the DB for example a stored procedure etc. sys.dm_db_uncontained_entities the below screenshot shows the example of a procedure which references the sys.all_columns view in the master database.

How SSMS object explorer looks when connected to a contained database.

Another important point to keep in mind when working with contained databases, is that unlike other databases where temp tables have the collation of the temp database. In contained databases the collation is that of the contained database. The below screenshot show the contained database temp table with welsh collation since the contained database has the welsh collation.