How to using SQL statements to do below config for case 1 and 2?
Case 1: There are multiple databases DB1, DB2, DB3, ... in the Azure SQL managed instance How to create a new database user User1 that can only access DB1 and hide the other databases from this user (i.e., when User1 open the database server in the SQL Server Management Studio only show DB1 to him) At the same time, User1 needs to have privilege to perform below actions against DB1:
- Create tables
- Add columns
- CRUD access to the tables
Case 2: If the multiple databases DB1, DB2, DB3, ... are single databases (not in Managed instance) How to do the similar config as in case 1, i.e., User1 only can see DB1 with the above privileges?
I tried the solution from below question: How do I create a new user in a SQL Azure database?
CREATE LOGIN User1 WITH password='Test1234';
CREATE USER User1 FROM LOGIN User1;
But then the User1 can see all databases.
+++++++++++++++++++++++++++++++
1st Update:
Based on @Nick.McDermaid references (big thank you), I tried following SQL statements and they are working:
--the below statement grant user1 access to the database, it's working:
CREATE USER user1 WITH PASSWORD = 'Test1234';
--the below 4 statements grant user1 CRUD privilege on all tables, it's working:
GRANT SELECT TO user1;
GRANT INSERT TO user1;
GRANT UPDATE TO user1;
GRANT DELETE TO user1;
But having issue to grant create table and add column:
--intend to grant user1 creating table privilege, commands completed successfully, but when trying to create a table return below error:
--Microsoft SQL: The specified schema name "dbo" either does not exist or you do not have permission to use it.
GRANT CREATE TABLE TO user1;
--intend to grant user1 adding columns privilege, but Azure SQL return below error:
--Incorrect syntax near 'ALTER'.
GRANT ALTER TABLE TO user1;
+++++++++++++++++++++++++++++++
2nd Update:
My final SQL statements are:
CREATE USER [user1] WITH PASSWORD=N'Test1234'
GO
CREATE ROLE [test_role]
GO
ALTER ROLE [test_role] ADD MEMBER [user1];
GO
ALTER ROLE [db_ddladmin] ADD MEMBER [test_role];
GO
ALTER ROLE [db_datawriter] ADD MEMBER [test_role];
GO
ALTER ROLE [db_datareader] ADD MEMBER [test_role];
GO