T-SQL Tutorial

SQL Server Logins


SQL Server Logins are the key to controlling access to your SQL Server database. By creating SQL Server Logins, you can grant, revoke, or deny permissions to users. SUSER_SID is a built-in function that returns the security identifier (SID) of the specified user. This can be used to control access to the database.


CREATE LOGIN

For example, if you want to grant a user read-only access to your database, you can use the SUSER_SID function to return the user's SID and then use that SID in a GRANT statement. SQL Server Logins are created using the CREATE LOGIN statement. To create a SQL Server Login, you must specify a name for the login and a password. You can also specify whether the login should be enabled or disabled.

CREATE LOGIN mylogin WITH PASSWORD='mypassword';


CREATE USER

Users are created using the CREATE USER statement. To create a user, you must specify a name for the user and a SQL Server Login that the user will use to connect to the database. You can also specify whether the user should be enabled or disabled.

CREATE USER myuser FOR LOGIN mylogin;


Permissions

Once you have created SQL Server Logins and Users, you can grant, revoke, or deny permissions to them using the GRANT, REVOKE, and DENY statements.

GRANT SELECT ON mytable TO myuser;
REVOKE SELECT ON mytable FROM myuser;
DENY SELECT ON mytable TO myuser;


SQL Server Logins, Users, and Security Identifiers (SIDs) are a key part of SQL Server security. By controlling access to your SQL Server database using SQL Server Logins and Users, you can help keep your data safe.