In this article, we will learn how to create a user in SQL.
To create a user for a database, we first have to create a new login for the server or we can use an existing one.
Please check How To Create Login In SQL, to create a new login.
There are 2 ways to create a new user in SQL Server.
- Using T-SQL:
Syntax
CREATE USER USER_NAME FOR LOGIN LOGIN_NAME;
Example
The subsequent statement would create a user named TestUser for login named TestLogin:
CREATE USER TestUser FOR LOGIN TestLogin;
- Using SSMS (SQL Server Management Studio):
Let’s follow the below steps to create a new database user in SQL Server.
Step 1:
Firstly, Open Microsoft SQL Server Management Studio and decide in which existing database (DBEmployee) you want to create a new user.
Step 2:
Now, Expand your database (DBEmployee) and Security directory.
Then right-click on Users directory and click on the New User… option.
Step 3:
Now, ‘Database User – New‘ dialogue box will open. Choose SQL user with login from the User type drop-down list and then enter your User name (TestUser) and Login name (TestLogin) and then click on the OK button.
Step 4:
Now, refresh Object Explorer and check new user (TestUser) is created.
Also, check How To Use NULL Functions In SQL