top of page

Step-by-Step Guide to Creating User in SQL with Essential Permissions

If you’re looking to secure your database, creating a user in SQL is crucial. Whether you’re administering a SQL Server instance or developing an application that requires database access, you need to know how to create a user account and grant the appropriate permissions. In this article, we’ll walk you through the practical steps of creating user in SQL, mapping logins to users, and setting permissions so your data remains secure and accessible to authorized personnel only.

Key Takeaways


Creating a SQL Server user involves creating a login, a database user, mapping the login to the database user, and understanding the intricacies between Windows and SQL Server Authentications.


SQL user creation and permission management can be executed via T-SQL commands such as ‘CREATE USER’, ‘GRANT’, ‘REVOKE’, ‘DENY’, and through SSMS for a more graphical approach to configuring user options, role memberships, and object permissions.


Maintaining SQL Server user accounts requires regular updates and modifications using ‘ALTER USER’, extreme caution in user removal with ‘DROP USER’, and understanding advanced options like extended properties, certificate, and asymmetric key-mapped users.

Getting Started: Understanding SQL Server User Creation

Creating users in SQL Server involves the following steps:


Create a login: This is the first step in creating a user. A login is a security principal that allows access to the SQL Server instance. You can create a login using the CREATE LOGIN statement.


Create a database user: Once the login is created, you need to create a database user. A database user is associated with a specific database and is used to control access to that database. You can create a database user using the CREATE USER statement.


Map the login to the database user: After creating the login and the database user, you need to map the login to the database user. This allows the login to connect to the specific database and access its resources. You can use the ALTER USER statement to map the login to the database user.

By following these steps, you can create users in Microsoft SQL Server and provide them with the necessary access to the required databases.

A clear understanding of the two authentication modes offered by SQL Server, namely Windows Authentication and SQL Server Authentication, is crucial before we delve into the mechanics of SQL Server user creation. These modes provide different security levels and are used depending on the circumstances.

SQL Server Authentication vs. Windows Authentication

SQL Server offers a choice between two authentication modes: Windows Authentication and SQL Server Authentication. While Windows Authentication is considered more secure, leveraging the Kerberos protocol and integrating with Windows server features, including account validation, SQL Server Authentication is suited for legacy applications and non-Windows environments. It’s worth noting that SQL Server Authentication has its drawbacks, including management complexity and security risks, such as network password interception. To mitigate these risks, it is essential to follow best practices when setting up sql server authentication login.

A clear comprehension of these authentication modes, along with their respective strengths and weaknesses, will guide your decision on which mode to implement in your SQL Server instance. The choice will depend on your specific scenario, whether you are working with legacy applications, non-Windows environments, or you prioritize security.

Preparing Your SQL Server Instance

Before you start creating users in SQL Server, you need to prepare your SQL Server instance. Here are the steps to follow:


Make sure the executing account has the ALTER ANY USER permission on the database.


If you’re creating a contained database user, make sure contained databases are enabled on the SQL Server instance.


Set the specific database to allow containment.

Configuring your SQL Server instance to enable user creation is a key step. It ensures that you have the necessary permissions to create and manage users. It is a fundamental rule of thumb to ensure your SQL Server instance is prepared and configured correctly before creating users.

Crafting a New SQL User via T-SQL

Using T-SQL syntax presents a powerful and flexible approach to create an SQL user and manage your database users. The process involves executing a T-SQL command within the specific database where you want to create the user. It’s essential to specify the correct database when executing the T-SQL command to ensure that the new user is created in the intended database context.

T-SQL’s create user command is the fundamental command for creating a new SQL user. With this command, we can create a new user and specify an existing login name to map to the new user in the targeted database. Let’s delve deeper into the CREATE USER command and how we can assign a login to the new user.

The CREATE USER Command

The CREATE USER statement in T-SQL is used to create a user in the current database. It is important to be connected to the correct database where you want the user to have access. This is because a user’s scope is within the database, and the permissions within the database are granted and denied to the database user, not the login.

The basic syntax for the CREATE USER command in T-SQL is CREATE USER [user_name] FOR LOGIN [login_name]; where [user_name] is the name of the new database user, also known as the user name, and [login_name] is the name of the associated SQL Server login.

Here’s an example of using the CREATE USER command in T-SQL to add a user to a database: CREATE USER Guru99 FOR LOGIN MyLogin;.

Assigning a Login to the New User

After creating a new SQL user, it’s necessary to assign a login to the user. This is done using the CREATE LOGIN [login_name] WITH PASSWORD = ‘[password]’; command, where [login_name] is the name of the login you want to create, and [password] is the password for the login. You can also specify password policy options such as CHECK_POLICY = {ON | OFF} and CHECK_EXPIRATION = {ON | OFF}.

After creating the login, you can link the login sql user to the existing login using the CREATE USER [user_name] FOR LOGIN [login_name]; command. It’s worth noting that it’s possible to create a user without an associated login by using the CREATE USER [user_name] WITHOUT LOGIN; command. This is often used for service accounts or contained databases.

Furthermore, a user can be mapped to multiple logins using CREATE USER [user_name] FOR LOGIN [login_name];, supporting complex security arrangements where a login user mapped to different accounts is necessary.

Utilizing SQL Server Management Studio (SSMS) for User Creation

The SQL Server Management Studio (SSMS) is another method to create a new user in SQL Server, especially for a Windows user. SSMS provides a graphical interface for defining various user properties, making it a convenient option for those who prefer a more visual approach.

To create a new user using SSMS, you can follow these steps:


Open SSMS and expand the ‘Databases’ node.


Expand the ‘Security’ folder of the target database.


Right-click on ‘Users’ and select ‘New User’ to initiate the user creation process.


Navigate to the New User Dialog Box and configure user options in SSMS.

Navigating to the New User Dialog Box

If you’re using SSMS, initially accessing the Object Explorer is the first step to create a new database user. Here, you can initiate the creation of a new database user. To do this, first, expand the Databases folder in Object Explorer, then expand the database where the new user will be created.

Once you’ve expanded the database, you can open the New User Dialog Box. To do this, simply right-click the Security folder under the chosen database, point to New, and select User. With the New User Dialog Box open, you can proceed to configure user options for the new SQL user.

Configuring User Options

When creating a new user in SSMS, there are several options you can configure. For instance, setting the default schema defines which schema owns the objects created by the user. You can manage the user’s role memberships by selecting appropriate roles in the Database User – New dialog box’s Membership page.

The Owned Schemas page allows you to add or remove schemas that the new user can own by selecting or clearing checkboxes next to the schemas. Furthermore, you can customize permissions for a SQL user using the Securables page, which lists all possible database objects that the user can access. Securable permissions can be set at a granular level in SSMS for each database object the user needs to interact with.

Setting Permissions for Database Users

After creating a user in SQL Server, they aren’t automatically granted permissions to perform actions in the database. Permissions must be explicitly assigned using GRANT, REVOKE, or DENY statements. It is worth noting that permissions in SQL Server can be categorized as explicit, inherited from roles, or as a result of ownership chaining.

The basic syntax for granting permission to a user using T-SQL includes selecting the database then assigning the permission using the grant statement. Explicit permissions are granted directly to a user or role on a specific object, such as a table or view. The principle of least privilege is recommended in SQL Server, where users are only granted the permissions they need for their role.

Now, let’s delve deeper into how to assign users to database roles and customize user permissions on specific database objects.

Database Role Membership Page

Assigning users to database roles is an effective way to manage permissions for SQL database users. Predefined roles like:


db_datareader: provides read-only access to all tables in a database


db_datawriter: provides write access to all tables in a database


db_owner: grants a user full control over the database, permitting them to carry out all configuration and maintenance activities

These roles provide quick permission setups for frequent requirements.

To include a user in a user-defined database role, follow these steps:


Navigate to the Database Role Properties dialog box via the Database Roles folder in the desired database in SSMS.


Use the Add button to add the user.


On the ‘Membership’ page of the Database User – New dialog box, you can view available database membership roles and manage role membership by selecting or clearing checkboxes.

Customizing Permissions on Securables Page

Creating custom permissions for a SQL user involves granting permissions to database objects using SQL statements after the user has been created. The GRANT statement is used to assign permissions directly on various database objects including tables, views, stored procedures, and functions.

Here are some specific GRANT statements that can be used:


GRANT SELECT ON OBJECT::dbo.YourTable TO YourUser;


GRANT EXECUTE ON OBJECT::dbo.YourProcedure TO YourUser;


GRANT SELECT ON OBJECT::dbo.YourView TO YourUser.

These statements can be used to provide different types of permissions, such as SELECT permissions for tables, EXECUTE permissions for stored procedures, and SELECT permissions for views.

The REVOKE statement is used when it’s necessary to remove permissions from a user that were previously granted, effectively revoking access to the specified database objects. Additionally, the WITH GRANT OPTION added to a GRANT statement enables the recipient user to pass on the permissions they have received to other users, extending the flexibility of permission management.

Advanced User Options: Extended Properties and More

SQL Server provides advanced user options, including extended properties and certificate/asymmetric key mapping. Extended properties can be used to add descriptive information or instructions to SQL users, which can assist with documentation and administration. Users can be mapped to a certificate or asymmetric key to allow for strong authentication, meeting requirements for scenarios that demand high levels of security.

Adding or changing extended properties for a SQL user can be accomplished via system stored procedures such as sp_addextendedproperty or sp_updateextendedproperty. The ‘CREATE USER’ command with the ‘FOR CERTIFICATE’ clause is used to create a user mapped to a certificate, while asymmetric key-mapped users use the ‘FOR ASYMMETRIC KEY’ clause.

Now, let’s delve deeper into how to add and manage extended properties for SQL users and create and manage certificate and asymmetric key mapped users.

Extended Properties Page

Extended properties allow for the addition of descriptive information or metadata to SQL user objects in the form of name/value pairs. To add an extended property, use the sp_addextendedproperty stored procedure, specifying @name for the property’s name and @value for its corresponding value. Extended properties are organized into levels, where users, as level 0 objects, can have properties associated directly with them by setting @level0type as ‘USER’ and @level0name as the user’s name.

Database users can add or modify extended properties on objects they own, or to which they have ALTER or CONTROL permissions, with a size limitation of up to 7,500 bytes for the value of a property.

Extended properties are a powerful feature that can help administrators manage SQL users more efficiently.

Certificate and Asymmetric Key Mapped Users

Users mapped to certificates or asymmetric keys in SQL Server facilitate advanced security measures, often for environments requiring compliance with regulatory data security and encryption standards. In SQL Server, the ‘CREATE USER’ statement with the ‘FROM’ clause allows the creation of a user from various sources such as Windows accounts, certificates, or asymmetric keys.

To enhance security, primarily for code signing purposes, a user can be created from a certificate using ‘CREATE USER’ followed by the ‘FOR CERTIFICATE’ option. Creating an asymmetric key windows user that is mapped to a specific asymmetric key involves the ‘CREATE USER’ statement along with the ‘FOR ASYMMETRIC KEY’ option. Users mapped to an asymmetric key cannot directly log into SQL Server but are used to sign stored procedures, functions, triggers, or assemblies to ensure controlled access via the key. The asymmetric key must first be established in the database using the ‘CREATE ASYMMETRIC KEY’ statement before a user mapped to an asymmetric key can be created.

Permissions that can be granted on an asymmetric key include:


CONTROL


TAKE OWNERSHIP


ALTER


REFERENCES


VIEW DEFINITION

These permissions enable fine-grained permission management. To manage permissions on an asymmetric key, the grantor needs ‘GRANT OPTION’ or higher implied permissions, and the ‘GRANT’ statement is used with ‘ON ASYMMETRIC KEY’ specifying the key’s name.

Maintaining User Accounts

Maintaining an SQL user account is essential once it has been set up. Modifying SQL user account details could be necessary for changing permissions, correcting user information, or updating authentication methods as security practices evolve. Additionally, there may be occasions when it is necessary to remove a user from the database. However, to do this, one must ensure that the user does not own any objects or hold any active connections to the database. The DROP USER command can then be used for correct deletion.

In SQL Server, user account maintenance involves both modifying existing users and removing users from the database. Effective user account maintenance ensures that your SQL Server remains secure and that user accounts are up-to-date. Let’s delve deeper into how to modify existing users and remove users from the database.

Modifying Existing Users

The ALTER USER Transact-SQL command can be used to modify properties of an existing SQL Server database user, such as renaming the user or changing its default schema. Assigning or changing the default schema of a user can be done using the ALTER USER command along with the WITH DEFAULT_SCHEMA = schema_name clause.

The ALTER USER command with the LOGIN option is utilized to remap a user to a different login, effectively aligning the user’s Security Identifier (SID) with that of the new login’s SID. Changing a user’s password in SQL Server is managed with the ALTER USER command by specifying the new password with the PASSWORD option, and optionally the old password with the OLD_PASSWORD option, with the latter being bypassable if the user holds ALTER ANY USER permissions.

The default language for a user in SQL Server can be set by using the DEFAULT_LANGUAGE option of the ALTER USER command.

Removing Users from the Database

To remove a user from a database in SQL Server, you use ‘DROP USER’ followed by the user’s name, and optionally include an ‘IF EXISTS’ clause to prevent errors if the user does not exist. It is important to note that removing a user with the ‘DROP USER’ command does not delete the associated login; the login remains active in the SQL Server instance and can be mapped to users in other databases.

Before a user can be removed from the database, they must be taken out of any database roles they are a member of. The ‘guest’ user cannot be removed with the ‘DROP USER’ command, instead, you can revoke its ‘CONNECT’ permission to disable it, with the exception of ‘master’ or ‘tempdb’ databases.

Summary

In conclusion, understanding the ins and outs of creating and managing users in SQL Server is essential for any database professional. Whether you’re using T-SQL commands or the SQL Server Management Studio, you now have the knowledge to create users, assign permissions, and manage user accounts efficiently. Remember, each method has its strengths and use cases, so choose the one that best fits your needs. Happy SQL Server managing!

Frequently Asked Questions

How do I create a user in SQL?

To create a user in SQL, open SQL Server Management Studio, navigate to the Security folder, right-click Logins, choose New Login, and then enter the user name in the Login name field.

How do you create a user type in SQL?

To create a user type in SQL, you can navigate to Object Explorer, expand Databases, then Programmability, and finally, right-click on User-Defined Data Types to create a new one.

What is the difference between SQL Server Authentication and Windows Authentication?

In conclusion, Windows Authentication is more secure and integrates with Windows server features, while SQL Server Authentication is better suited for legacy applications and non-Windows environments.

How can I assign a login to the new SQL user?

You can assign a login to the new SQL user using the `CREATE USER [user_name] FOR LOGIN [login_name];` command. This will create a user in the database with a corresponding login.

What are extended properties in SQL Server?

Extended properties in SQL Server enable the addition of descriptive information or metadata to SQL user objects in the form of name/value pairs, offering a way to provide additional context and documentation.

Recent Posts

See All

Opmerkingen

Beoordeeld met 0 uit 5 sterren.
Nog geen beoordelingen

Voeg een beoordeling toe

Get in Touch

Thanks for submitting!

bottom of page