Contained Database SQL Server

 Contained Database

A contained database is a database that is isolated from other databases and from the instance of SQL Server.

To use a contained database, it must first be enabled at the server level using the following command.

EXEC sp_configure 'contained database authentication', 1;

RECONFIGURE;

  • Easier Migration: Contained databases simplify the process of migrating databases between servers by eliminating the need to migrate server-level logins.
  • Self-Contained Management: They make it easier to manage all database settings within the database itself.

  • Create a Contained Database

    Once contained database authentication is enabled, you can create a new contained database by specifying the CONTAINMENT option as PARTIAL. Here’s an example

    -- Create a contained database

    CREATE DATABASE ContainedDB

    CONTAINMENT = PARTIAL;

    Create Contained Users in the Database

    Now that the contained database is created, you can add users directly within the database. There are two types of contained users:

    • SQL user with password
    • Windows user

    Connect to the Contained Database

    To connect to the contained database, specify the database name in the connection string or in the SQL Server Management Studio (SSMS) connection options. This allows the user to authenticate directly with the contained database.


    Limitations

    • Some server-level features are not supported within contained databases, such as cross-database queries that use three- or four-part naming conventions.
    • The feature is available only in certain editions of SQL Server (starting from SQL Server 2012).

    Comments

    Popular posts from this blog

    disable Instant File Initialization (IFI) in SQL Server

    common questions related to updating patches, Cumulative Updates (CUs), Service Packs, and security patches for SQL Server

    Azure Private Endpoint