Create SQL Server User with Windows Authentication: Step-by-Step Guide

Implementing SQL server authentication is a crucial step in securing your database. While Mixed Mode authentication allows for both Windows and SQL Server logins, often, it's more secure to rely solely on Windows authentication. In this guide, we will explore how to create a SQL user using Windows authentication.

What Default 'sa' SQL Admin user Password - Microsoft Q&A
What Default 'sa' SQL Admin user Password - Microsoft Q&A

Before we dive in, ensure you understand the difference between the two modes. Windows authentication uses Windows domain accounts, which makes it ideal for integrating with Active Directory and providing a single sign-on experience. On the other hand, SQL Server authentication relies on SQL logins and passwords, which can lead to weaker security if the passwords are not complex and frequently changed.

an image of a computer screen with text
an image of a computer screen with text

Enabling Windows Authentication

Before you can create a SQL user using Windows authentication, you need to ensure that your SQL Server is configured to use it. Here's how you can do it:

SQL
SQL

1. Open SQL Server Management Studio and log in with your Windows account. Ensure you have sufficient privileges to modify server properties.

Changing Server Authentication Mode

7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio

The first step is to switch from Mixed Mode to Windows Mode.

1. Right-click on the server in Object Explorer and select 'Properties'.

2. In the 'Server Properties' dialog box, go to the 'Security' page.

How to create a User Account in Windows 8 and Windows 7
How to create a User Account in Windows 8 and Windows 7

3. Under 'Server authentication', select 'Windows Authentication mode' and click 'OK'.

Removing SQL Server Logins

After changing the authentication mode, SQL Server logins will no longer work. To remove them, follow these steps:

an image of a computer screen with the text,'select from users where user = or
an image of a computer screen with the text,'select from users where user = or

1. In Object Explorer, expand the 'Security' folder and then click on 'Logins'.

2. Right-click on each SQL Server login and select 'Delete'.

Learn SQL Skills for Data Analysis
Learn SQL Skills for Data Analysis
SQL in 60 days
SQL in 60 days
SQL Windows Functions Explained like you are 5!
SQL Windows Functions Explained like you are 5!
SQL Window Functions Explained with Examples | Beginner SQL Tutorial
SQL Window Functions Explained with Examples | Beginner SQL Tutorial
SQL Window Functions Explained – Examples & Best Practices
SQL Window Functions Explained – Examples & Best Practices
MS SQL Full Form: Microsoft Structured Query Language
MS SQL Full Form: Microsoft Structured Query Language
an image of a computer screen with some type of text on it, and another screenshot
an image of a computer screen with some type of text on it, and another screenshot
7 SQL Commands Every Beginner Must Know
7 SQL Commands Every Beginner Must Know
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻

3. When prompted to delete the login, select 'Yes' to confirm.

Creating a SQL User with Windows Authentication

Now that your SQL Server is configured to use only Windows authentication, you can create SQL users based on Windows domain accounts.

To create a new user, follow these steps:

Using the GUI

1. In Object Explorer, expand the 'Security' folder and then click on 'Logins'.

2. Click on 'New Login' in the toolbar.

3. In the 'Login - New' dialog box, ensure 'Windows Authentication' is selected under 'Security Audit'.

4. Choose the appropriate domain under 'Domain'.

5. Select the specific Windows user under 'User name'.

Using Transact-SQL

You can also create a user using Transact-SQL. Here's an example:

```sql CREATE LOGIN [DOMAIN\Username] WITH PASSWORD = 'password', SID = 0x55555555555555555555555555555555; ```

Remember to replace 'DOMAIN', 'Username', and 'password' with your specific details.

Granting Database Access

After creating the user, you must grant them access to your database.

1. Right-click on the database in Object Explorer and select 'Properties'.

2. In the 'Database Properties' dialog box, go to the 'Files' page.

3. Note the 'Default file path' for later use.

4. Now, in a new query window, you can grant access using the following script:

```sql USE [Your_Database_Name]; GO CREATE USER [DOMAIN\Username] FOR LOGIN [DOMAIN\Username]; EXEC sp_addrolemember 'db_datareader', 'DOMAIN\Username'; EXEC sp_addrolemember 'db_datawriter', 'DOMAIN\Username'; ```

Again, replace 'DOMAIN', 'Username', and 'Your_Database_Name' with your specific details. This script adds the user to the 'db_datareader' and 'db_datawriter' roles, allowing them to read and write data.

Creating a SQL user under Windows authentication provides a secure, integrated login experience. By following this guide, you can easily configure your SQL Server and create new users. From here, you may want to explore more advanced topics like database permissions or SQL server auditing.

Related Articles

Sql Create User With Login Sql With Nolock Example W3school Sql Primary Key Sql Interview Questions Free Inner Join With 3 Tables Sql Sql Query W3resource Inner Join Example With 3 Tables Sql Not Equal To In Sql W3schools Sql Interview Questions Interactive Top 100 Sql Interview Questions For Data Analyst