如何授予用户对 SQL Server 中数据库的读取权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6688880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I grant read access for a user to a database in SQL Server?
提问by Q8Y
I want to grant access to a user to a specific database with read and write access. The user is already available in the domain but not in the DB.
我想授予用户对具有读写访问权限的特定数据库的访问权限。该用户已在域中可用,但不在数据库中。
So, how can I give them that access with creating a new user and password?
那么,我如何通过创建新用户和密码来授予他们访问权限?
Someone told me that it can be done with only specifying the user, domain & the DB that you want to give the user the access to without needing to create a new user and password.
有人告诉我,只需指定您希望授予用户访问权限的用户、域和数据库即可完成,而无需创建新用户和密码。
This is the old way that I was implementing. It works but it creates a new login and user rather than using the one that is available in the domain:
这是我正在实施的旧方法。它可以工作,但它会创建一个新的登录名和用户,而不是使用域中可用的登录名和用户:
use DBName;
create login a_2 with password='Aa123';
create user a_2 for login a_2;
grant insert to a_2;
grant select to a_2;
回答by marc_s
This is a two-step process:
这是一个两步过程:
you need to create a loginto SQL Server for that user, based on its Windows account
CREATE LOGIN [<domainName>\<loginName>] FROM WINDOWS;
you need to grant this login permission to access a database:
USE (your database) CREATE USER (username) FOR LOGIN (your login name)
您需要根据其 Windows 帐户为该用户创建一个SQL Server登录名
CREATE LOGIN [<domainName>\<loginName>] FROM WINDOWS;
您需要授予此登录权限才能访问数据库:
USE (your database) CREATE USER (username) FOR LOGIN (your login name)
Once you have that user in your database, you can give it any rights you want, e.g. you could assign it the db_datareader
database role to read all tables.
一旦您在您的数据库中拥有该用户,您就可以为其授予任何您想要的权限,例如您可以为其分配db_datareader
数据库角色以读取所有表。
USE (your database)
EXEC sp_addrolemember 'db_datareader', '(your user name)'