Create user that can only SEE one database, and only select from it?

我们两清 提交于 2019-12-05 03:58:45
Russell Fox

1) Create the user on the server

2) Add the user to the given database

3) Grant read-only access to the database

USE [master]
CREATE LOGIN [SomeUserName] WITH PASSWORD=N'someStr0ngP@ssword', DEFAULT_DATABASE=[c], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=ON, CHECK_POLICY=ON
GO

USE [c]
CREATE USER [SomeUserName] FOR LOGIN [SomeUserName] WITH DEFAULT_SCHEMA=[dbo]
GO

EXEC sp_addrolemember N'db_datareader', N'SomeUserName'

deny is the default behavior when it comes to permission so if you create a user and add it to the db_datareader role on the necessary db, that's the only permission it will have. It wont be able to access the other databases

EDIT:

use [master]
GO
DENY VIEW ANY DATABASE TO [login]
GO
use [master]
GO
DENY VIEW SERVER STATE TO [login]
GO

that will remove the login's abbility to view the databases. Then go to the one you WANT him to see and make him owner of that DB

Step-1: Create the LogIn

Step-2: Remove all DB view permission from the login

deny view any database to LogInName

Step-3: Give the login authorization of database

alter authorization on database:: DataBaseName to LogInName

Note: No need to specify username or database name with in single quotation marks

Maybe this will work.

Add user to server and map to the correct data base. Delete user from Server.

Server will inform you Deleting server users does not delete the data base users associated with this login.

Click OK and you will have user with access only to the one data base.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!