sp_helprolemember @rolename = 'myrole' - this displays the fixed database roles and user defined roles but not application roles.
1. Fixed Database Roles.
Similar to the SQL Server fixed Server Roles, there are a set of fixed Database Roles:
- db_owner - can administer the database and its security.
- db_accessadmin - can add or delete database users.
- db_securityadmin - can administer database roles and permissions.
- db_ddladmin - can add, change or drop database objects.
- db_backupoperator - can back up the database.
- db_datareader - can view any data in the database.
- db_datawriter - can edit any data in the database.
- db_denydatareader - can deny read access to data in the database.
- db_denydatawriter - can deny write access to data in the database.
2. User-Defined Database Roles
As well as the roles provided by SQL Server, you can define your own roles, using:
sp_addrole @rolename = 'myrole', @ownername = 'owner'
You can delete your own roles using:
sp_droprole @rolename = 'myrole'
You can't:
- Drop any of the SQL Server fixed database roles.
- Drop a role that has members.
- Drop a role that owns objects.
Once you've created a role, you'll want to add one or more users to it:
sp_addrolemember @rolename = 'myrole', @membername = 'myuser'
In the above example, 'myuser' could be:
- A SQL Server database user.
- An NT user.
- An NT group.
- A SQL Server role.
The last item in the list above is the key to setting up a security hierarchy - you can set up several user-defined database roles with different permissions, and assign more powerful roles to the less powerful roles, so that they can do whatever the less powerful role can do, PLUS whatever extra permissions you assign to them.
Each database has a special role named PUBLIC. All database users belong to this role and you can't remove or assign users. It is up to you to assign default permissions to the PUBLIC role, whatever permissions you want to be available to every user in the database.
3. Application Roles.
Application roles don't have members! Users invoke the permissions of the application role by using the application. Application roles are useful if you want users to be able to access data through an application only, and not directly, for instance through the query manager.
Here are the stored procedures you need to know - again, please go and play with them!
- To add an application role:
sp_addapprole @rolename = 'myapprole', @password = 'mypassword'
- To activate an application role:
sp_setapprole @rolename = 'myrole', @password = 'mypassword'
- To delete an application role:
sp_dropapprole @rolename = 'myrole'