SQL Server Logins & Users
In SQL Server, logins and users are two distinct concepts used to control access to the server and its databases.
A login is a security principal that represents a user or group of users who are allowed to connect to the SQL Server instance. For example, you might create a login named "Harsh" and assign a password to this login. Harsh can now connect to the SQL Server instance using his login name and password.
A User, on the other hand, is a security principal that represents a user within a specific database. For example, you might create a database user named "Harsh" in the "Northwind" database. You can then grant specific permissions to Harsh, such as the ability to select data from specific tables or execute specific stored procedures. When Harsh connects to the "Northwind" database using his login, he will be able to access the database and perform the tasks for which he has been granted permission.
It's important to understand that a user in one database is not automatically a user in another database. To access another database, a user must be associated with a database user in that database and have the necessary permissions.
Here's an example of how to create a login and user in SQL Server:
-- Create a login named "Harsh" with a password
CREATE LOGIN Harsh WITH PASSWORD = 'Password1';
-- Create a user named "Harsh" in the "Northwind" database
USE Northwind;
CREATE USER Harsh FOR LOGIN Harsh;
-- Grant SELECT permissions to Harsh on the "Orders" table
GRANT SELECT ON Orders TO Harsh;
Note: You must have the necessary permissions (such as the sysadmin or securityadmin role) to create logins and users in SQL Server. Additionally, the example above assumes that the "Northwind" database and the "Orders" table already exist.
Comments
Post a Comment