Contents |
Fresh installed database has no security. To secure it, first, create an administrator account:
CREATE ADMIN [myadmin] WITH PASSWORD [mypwd]
The command will create administrator "myadmin". The administrator inherits all privileges. If you use a manager, create a new connection in servers’ explorer tree with specifying username/password.
To create a user, execute SQL:
CREATE USER [myuser] WITH PASSWORD [mypassword];
or, windows user (for windows integrated security):
CREATE USER [mydomain\myuser]
User is transparent to all databases. There can be only one user with the distinct name.
The executing user must have [CREATE USER] system permission.
To drop a user, execute SQL:
DROP USER [myuser]
The executing user must have [DROP USER] system permission.
The user created database role sets the database object privileges that can be passed to a group of users. To create a role SQL:
USE [mydb]; GO; CREATE ROLE [myrole];
The roles are defined per database. Therefore, either execute "USE.." command to set the roles' database or specify database in
roles name:
CREATE ROLE [mydb.myrole];
The executing user must have [CREATE ROLE] database permission.
USE [mydb]; GO; DROP ROLE [myrole];
or
DROP ROLE [mydb.myrole];
The executing user must have [DROP ROLE] database permission.
To change roles'/users' membership, the logged user must have [ALTER MEMBERSHIP] database permission. To add role to another role, execute:
USE [mydb]; GO; ADD ROLE [myrole] TO ROLE [myOtherRole];
or
ADD ROLE [mydb.myrole] TO ROLE [mydb.myOtherRole];
After execution, [myOtherRole] will inherit roles' [myrole] privileges.
To add role to user, execute:
USE [mydb]; GO; ADD ROLE [myrole] TO USER [myUser];
To remove role membership from role, execute:
USE [mydb]; GO; REMOVE ROLE [myrole] FROM ROLE [myOtherRole]
To remove role membership from user, execute:
USE [mydb]; GO; REMOVE ROLE [myrole] FROM USER [myUser]
The grant privilege can be per database level, i.e. for all database objects. For example, following Sql will grant to role [myRole] SELECT/UPDATE/INSERT/DELETE rights on all tables and EXECUTE to all procedures:
USE mydb; GO; GRANT SELECT TO ROLE [myRole] WITH GRANT OPTION GRANT UPDATE TO ROLE [myRole] WITH GRANT OPTION GRANT INSERT TO ROLE [myRole] WITH GRANT OPTION GRANT DELETE TO ROLE [myRole] WITH GRANT OPTION GRANT EXECUTE TO ROLE [myRole] WITH GRANT OPTION GRANT ALTER MEMBERSHIP TO ROLE [myRole] WITH GRANT OPTION
For user grant:
USE mydb; GO; GRANT SELECT TO USER [myUser] WITH GRANT OPTION GRANT UPDATE TO USER [myUser] WITH GRANT OPTION GRANT INSERT TO USER [myUser] WITH GRANT OPTION GRANT DELETE TO USER [myUser] WITH GRANT OPTION GRANT EXECUTE TO USER [myUser] WITH GRANT OPTION
To grant privileges for specific object, for example grant SELECT/UPDATE/INSERT/DELETE for the [mytable] and EXECUTE for [myproc], execute:
USE mydb; GO; GRANT SELECT ON [mytable] TO ROLE [myRole] WITH GRANT OPTION GRANT UPDATE ON [mytable] TO ROLE [myRole] WITH GRANT OPTION GRANT INSERT ON [mytable] TO ROLE [myRole] WITH GRANT OPTION GRANT DELETE ON [mytable] TO ROLE [myRole] WITH GRANT OPTION GRANT EXECUTE ON [myproc] TO ROLE [myRole] WITH GRANT OPTION
For user grant:
USE mydb; GO; GRANT SELECT ON [mytable] TO USER [myUser] WITH GRANT OPTION GRANT UPDATE ON [mytable] TO USER [myUser] WITH GRANT OPTION GRANT INSERT ON [mytable] TO USER [myUser] WITH GRANT OPTION GRANT DELETE ON [mytable] TO USER [myUser] WITH GRANT OPTION GRANT EXECUTE ON [myproc] TO USER [myUser] WITH GRANT OPTION
The privileges specified per database object table/procedure always precedes the global databases' permissions
The deny command creates a negative privilege(s). As with grant privileges, the denied privileges specified per database object (table/procedure,..) always will precedes the global databases' permissions. For example, if user is a member of role that has [SELECT] to all tables per database level, but has exclusively DENY'd [SELECT] permissions to the specific table, the attempt to select from the table will fail.
For role:
USE mydb; GO; DENY SELECT TO ROLE [myRole]
For user:
USE mydb; GO; DENY SELECT TO USER [myUser]
To deny privileges for specific object, for example grant SELECT/UPDATE/INSERT/DELETE for the [mytable] and EXECUTE for [myproc], execute:
USE mydb; GO; DENY SELECT ON [mytable] TO ROLE [myRole] WITH GRANT OPTION
For user grant:
USE mydb; GO; DENY SELECT ON [mytable] TO USER [myUser] WITH GRANT OPTION
Revoking privileges is permitted per database level, i.e. for all database objects (tables/procedures) or per specific table/procedure. For example, following Sql will revoke DELETE/UPDATE privileges for all tables from role [myRole] or user [myuser]:
USE mydb; GO; REVOKE UPDATE FROM ROLE [myRole] REVOKE DELETE FROM ROLE [myRole]
or,
USE mydb; GO; REVOKE UPDATE FROM USER [myUser] REVOKE DELETE FROM USER [myUser]
Revoking existing [mytable] tables' [UPDATE/DELETE] permissions;
USE mydb; GO; REVOKE UPDATE ON [mytable] FROM ROLE [myRole] REVOKE DELETE ON [mytable] FROM ROLE [myRole]
or,
USE mydb; GO; REVOKE UPDATE ON [mytable] FROM USER [myUser] REVOKE DELETE ON [mytable] FROM USER [myUser]
The privileges specified per database object table/procedure will be removed. For example, if user is a member of role that has revoked DELETE from all tables, but has exclusively DENY'ed DELETE permissions to the specific table, the attempt to delete from the table will fail, unless the exclusive tables' permission has been revoked.
There are 3 types of roles: server, database and performance roles.
Server roles apply globally to all databases. There are 2 roles (the administrator user will be added as the member):
Performance role controls the query execution priority and priority throttling (dynamic decrease) depending of amount of resources the transaction use. Each user can be a member of a single performance role. When user has no performance role bind or database is not secured, the default query priority and throttling will be used. The default parameters are set in registry, in addition, the query priority can be set in a connection string ("... Priority=[1,100]..").
To create the performance role, execute SQL command:
CREATE performance ROLE [myperfrole] priority <1,100> Quants <ThrottleHalfQuants> disk_reads <ThrottleHalfPageAccess> page_faults <ThrottleHalfPageFaults> net_reads <ThrottleHalfNetworkIO>
where constants,
Example:
CREATE performance ROLE [myperfrole] priority 100 quants 100 disk_reads 100 page_faults 100 net_reads 100
Simply add the role to the user. The example bellow, user [myuser] will inherit query execution parameters from role [myperfrole].
ADD PERFORMANCE ROLE [myperfrole] TO USER [myuser]
To remove performance role, execute:
REMOVE PERFORMANCE ROLE [myperfrole] FROM USER [myuser]
To alter [myperfrole] execute following SQL:
ALTER performance ROLE [myperfrole] priority 80 quants 100 disk_reads 10 page_faults 0 net_reads 0
Altering performance role will affect all users bind to the performance role [myperfrole] execution control.
To delete [myperfrole] execute following SQL:
DROP ROLE [myperfrole]
The following permissions used to control access to the database objects:
Each permission has unique ID value, which is used instead of privilege name in [syspermissions] table.
| Permission | Values |
|---|---|
| ALTER TABLE | 1 |
| ALTER PROCEDURE | 2 |
| ALTER DATABASE | 4 |
| BACKUP DATABASE | 8 |
| CREATE DATABASE | 32 |
| CREATE TABLE] | 64 |
| CREATE PROCEDURE | 128 |
| SELECT | 256 |
| UPDATE | 512 |
| DELETE | 1024 |
| INSERT | 2048 |
| EXECUTE | 4096 |
| SHOWPLAN | 8192 |
| ALTER CLUSTER | 32768 |
| CHANGE PRIORITY | 65536 |
| MANAGE SESSION | 131072 |
| CREATE USER | 262144 |
| CREATE ROLE | 524288 |
| DROP USER | 1048576 |
| DROP ROLE | 2097152 |
| ALTER MEMBERSHIP | 4194304 |
The objects' permissions can either be granted per table/procedure, database or system levels. If the privilege doesn't exist per table, privileges per tables' database will be inspected, and, if database has no privileges, the system privileges will be used.
For example:
Grant [SELECT] permissions to any table in testdb database to user myuser:
USE tedtdb GRANT SELECT TO USER myuser
Deny [SELECT] permissions to <mytable> table in <testdb> database to user <myuser>:
USE tedtdb DENY SELECT ON [mytable] TO USER [myuser]
The user will fail to access [mytable], unless tables' DENY’ed privilege is REVOKE'd.
The procedure [show_user_roles] will show all the roles the user [myuser] has membership:
USE system; EXEC show_user_roles [myuser]
The procedure displays, explicit users' permissions:
USE system; EXEC show_user_permissions [myuser]
The procedure displays, database roles' permissions:
USE system; EXEC show_role_permissions [mydb],[myuser]