Is user mapping required for database login in MS SQL Server 2019?

kvdb 21 Reputation points
2021-12-22T15:32:29.623+00:00

Hi!

After importing a database backup from MS SQL Server 2012 into MS SQL Server 2019 I quickly found out I was not able to login into it anymore.

In SSMS there is an appropriate entry (an AD user group) under Databases -> [database name] -> Security -> Users, and up till now this sufficed to be able to login.
However, it now seems necessary to also add an entry to Security -> Logins, and then map this entity to the database user. Anyway, that was the only way I could make it work.

So, I'm wondering:

  1. Is user mapping in MS SQL Server 2019 a requirement to be able to login into a database? (It was not in versions 2008 and 2012.)
  2. Or is there another way to login just as a database user?
  3. If the answer to question 1 is yes, then what is the use of being able to add a database user / group from a domain?

Thank you for your time!

SQL Server Other
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-12-22T23:01:32.567+00:00

    Tom has already answered the important points. I just like to cover the last question:

    If the answer to question 1 is yes, then what is the use of being able to add a database user / group from a domain?

    Keep in mind that a user can have access to the instance from one or more AD groups. But then the user or an AD group may need specific permissions in a database, and therefore you may need to add the user on this level, although the user has no login of its own.


3 additional answers

Sort by: Most helpful
  1. Tom Phillips 17,771 Reputation points
    2021-12-22T17:58:33.253+00:00

    Yes, unless your login is a sysadmin, you must map a server "login" to a database "user".

    This has not changed in SQL 2019. This has always been a requirement for non-sysadmin users.

    The user information is stored in the database. Therefore, when you remove or restore a database, that information is lost and must be recreated.


  2. Seeya Xi-MSFT 16,586 Reputation points
    2021-12-23T07:36:11.453+00:00

    Hi @kvdb ,

    Welcome to Microsoft Q&A!
    Yes. Agree with Tom.
    I also want to add the third point:
    https://www.sqlshack.com/sql-server-logins-users-security-identifiers-sids/

    Best regards,
    Seeya


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2022-01-04T22:16:13.5+00:00

    Do you mean that the data in sys.server_principals should include / reflect what is listed under Security -> Logins?

    Rather the other way round. What you see in Security -> Logins should come from sys.server_principals.

    Is there a way to find out which login is mapped to a database user, other than looking at Security -> Logins or sys.server_principals?

    Run this in your database. Add columns as needed:

    SELECT sp.name AS LoginName, sp.principal_id AS Server_principal_id, sp.SID, sp.type_desc AS login_type, 
           dp.name AS UserName, dp.principal_id AS Database_principal_id, dp.type_desc AS user_type
    FROM  sys.server_principals sp
    FULL  JOIN sys.database_principals dp ON dp.sid = sp.sid
    

    NULLS to the left -> Only in database. NULLS to the right -> only in server.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.