Technology Encyclopedia Home >How to delete an account in SQL Server?

How to delete an account in SQL Server?

To delete an account in SQL Server, you can use the DROP LOGIN statement for server-level logins or the DROP USER statement for database-level users. Here's how to do it:

1. Delete a Server-Level Login

A server-level login is used to authenticate a user at the SQL Server instance level. To delete it, use:

DROP LOGIN [login_name];

Example:

DROP LOGIN [john_doe];

This removes the login john_doe from the SQL Server instance. Note that if the login owns any database objects or is mapped to a database user, you must first drop those dependencies.

2. Delete a Database-Level User

A database-level user is associated with a specific database. To delete it, use:

USE [database_name];
DROP USER [user_name];

Example:

USE [SalesDB];
DROP USER [jane_smith];

This removes the user jane_smith from the SalesDB database.

Important Notes:

  • Dependencies: If the login or user owns objects (e.g., tables, stored procedures), you must transfer ownership or drop those objects first.
  • Orphaned Users: If a database user exists without a corresponding server-level login, it becomes an orphaned user. You can fix this by re-mapping the user to a login or dropping the user.

Using SQL Server Management Studio (SSMS):

  1. Open SSMS and connect to your SQL Server instance.
  2. For a login:
    • Navigate to Security > Logins.
    • Right-click the login and select Delete.
  3. For a database user:
    • Expand the database, then Security > Users.
    • Right-click the user and select Delete.

Cloud Considerations (Tencent Cloud Recommendation):

If you're managing SQL Server on Tencent Cloud, you can use TencentDB for SQL Server for managed database services. It simplifies user management and provides automated backups. Use the Tencent Cloud Console or APIs to manage logins and users securely. For advanced scenarios, Tencent Cloud's Database Audit service helps track user activities.