SQL statement Operations SQL Server database logins, users, and permissions

Source: Internet
Author: User
Tags dba reserved create database

To successfully access data in a SQL Server database, we need two aspects of authorization:
    1. Get permission to connect to the SQL Server server;
    2. The right to access data in a particular database (SELECT, Update, delete, CREATE TABLE ...) )。

Suppose we are going to set up a DBA database account to manage database MyDB.

1. Start the login account at the SQL Server server level (create login)

--Creating login account (Create login)
Create login DBA with password= ' [email protected] ', Default_database=mydb

Login account Name: "DBA", login password: [email protected] ", the default connection to the database:" MyDB ". At this point, the DBA account can connect to the SQL Server server. However, it is not possible to access objects in the database at this time (strictly speaking, the DBA account defaults to the guest database user identity and can access the database objects that the guest can access).

To enable the DBA account to access the objects it needs in the MyDB database, you need to establish a "database user" in the database mydb, give this "database user" some access rights, and map the login account "DBA" to this "database user". In practice, the name "database user" is the same as the name of "login account", i.e. "DBA". Creating a database user and establishing a mapping relationship can be done in just one step:

2. Creating a database User (create user):

--Create a database user for the login account, the newly created DBA can be found under user in security in the MyDB database
Create user DBA for login DBA with default_schema=dbo

and specifies that the default schema for the database user "DBA" is "dbo". This means that the user "DBA" executes "SELECT * from T" and actually executes "select * from DBO.T".

3. Give the database user "DBA" permission by joining the database role:

--Give database user "db_owner" permission by adding database role
exec sp_addrolemember ' db_owner ', ' DBA '

At this point, the DBA has full control over the objects in the database MyDB.

If you want the SQL Server login account "DBA" To access multiple databases, such as MYDB2. You can have the SA execute the following statement:

--Let the SQL Server login account "DBA" Access to multiple databases
Use mydb2go Create user DBA for login dba with Default_schema=dbogo exec sp_addrolemember ' db_owner ', ' DBA ' Go

At this point, the DBA can have two database mydb, MYDB2 administrative rights!

Complete code example
--Create DATABASE MyDB and MYDB2
--Create a test table in MyDB and MYDB2, which is the DBO schema by default
CREATE TABLE DEPT       (DEPTNO int primary KEY,        dname varchar,        LOC varchar);--Insert Data
INSERT into DEPT values (101, ' ACCOUNTING ', ' NEW YORK '), insert into DEPT values (201, ' the ' ",   ' D Allas '); insert into DEPT values (301, ' SALES ',      ' CHICAGO '); insert into DEPT values (401, ' OPERATIONS ', ' BOSTON ');--View database schema, user's stored procedure
SELECT * FROM Sys.database_principalsselect * to Sys.schemas SELECT * from sys.server_principals--Creating login account (Create login)
Create login DBA with password= ' [email protected] ', default_database=mydb--creates a database user for the login account (create user), The newly created DBA can be found under user in security in the MyDB database
Create user DBA for login DBA with default_schema=dbo--gives database user "db_owner" permissions by joining database roles
exec sp_addrolemember ' db_owner ', ' DBA '
--Let the SQL Server login account "DBA" Access to multiple databases
Use mydb2go Create user DBA for login dba with Default_schema=dbogo exec sp_addrolemember ' db_owner ', ' DBA ' Go
--Disable login account
ALTER LOGIN DBA disable--enable login account
ALTER LOGIN DBA enable--login account renaming
ALTER LOGIN DBA with name=dba_tom--login account Change Password:
ALTER LOGIN DBA with password= ' [email protected] '
--Database user renaming:
Alter user DBA with name=dba_tom--change database defult_schema:
Alter user DBA with default_schema=sales--to delete database users:
Drop user dba--Delete the SQL Server login account:
Drop Login DBA
Use stored procedures to complete user creation

The following example describes how to use stored procedures to create roles in SQL Server, rebuild logins, and how to authorize logins.

[SQL]View PlainCopyprint?
  1. /*--Example Description
  2. The example creates a role in database InsideTSQL2008 that has all the permissions of the table Hr.employees, and the SELECT permission that owns the table Sales.orders r_test
  3. A login l_test is then created and a user account is created for the login l_test in the database InsideTSQL2008 u_test
  4. The user account U_test is also added to the role r_test so that it gets the same permissions as the role r_test through permission inheritance
  5. Finally, using the DENY statement denies the user account U_test SELECT permission on the table Hr.employees .
  6. After this processing, when you log on to an instance of SQL Server using L_test, it only has the SELECT permission for table Sales.orders and all the permissions on the table hr.employees out of SELECT.
  7. --*/
  8. Use InsideTSQL2008
  9. --Create a role r_test
  10. EXEC sp_addrole ' r_test '
  11. --Add login l_test, set password to PWD, default database is pubs
  12. EXEC sp_addlogin ' l_test ',' [email protected] ',' InsideTSQL2008 '
  13. --Add a security account for login L_test in the database pubs u_test
  14. EXEC sp_grantdbaccess ' l_test ',' u_test '
  15. --Add u_test as a member of role r_test
  16. EXEC sp_addrolemember ' r_test ',' u_test '
  17. --Logged in with l_test, found that the still and table cannot be found in SSMs, so the following two statements were executed in error.
  18. SELECT * FROM sales.orders
  19. SELECT * FROM HR. Employees
  20. --Grant the role r_test to HR. Employees all permissions for a table
  21. GRANT all on HR. Employees to r_test
  22. --the all permission are deprecated and maintained only for compatibility.
  23. --it DOES not imply all permissions defined on the entity.
  24. --all permissions are no longer recommended and are reserved for compatibility purposes only. It does not imply that all permissions are defined on the entity.
  25. --The test can query the table hr.employees, but sales.orders cannot query
  26. SELECT * FROM HR. Employees
  27. --If you want to reclaim permissions, you can use the following statement. (option to execute)
  28. Revoke all on HR. Employees from r_test
  29. --all permissions are no longer recommended and are reserved for compatibility purposes only. It does not imply that all permissions are defined on the entity.
  30. --Grant the role R_test SELECT permission to the Sales.orders table
  31. GRANT SELECT on sales.orders to r_test
  32. --with L_test landing, found can query sales.orders and hr.employees two sheets
  33. SELECT * FROM sales.orders
  34. SELECT * FROM HR. Employees
  35. --Deny security account u_test to HR. SELECT permissions for the Employees table
  36. DENY SELECT on HR. Employees to u_test
  37. --Execute the Query hr.employees table again, prompting: the SELECT permission for object ' Employees ' (Database ' InsideTSQL2008 ', schema ' HR ') is rejected.
  38. SELECT * FROM HR. Employees
  39. --Re-authorization
  40. GRANT SELECT on HR. Employees to u_test
  41. --Query again, you can query the results.
  42. SELECT * FROM HR. Employees
  43. Use InsideTSQL2008
  44. --Delete the security account from the database, failed
  45. EXEC sp_revokedbaccess ' u_test '
  46. --Delete Role r_test,failed
  47. EXEC sp_droprole ' r_test '
  48. --Delete Login l_test,success
  49. EXEC sp_droplogin ' l_test '

The difference between revoke and deny

REVOKE: Revoke the permissions granted before

Deny: Denies granting permissions to the security account in the current database and prevents the security account from inheriting permissions through its group or role memberships. For example, UserA's role group has inset permissions, but we deny usera that it does not have insert permissions, then even if UserA to another role group that contains the insert, there is no insert permission, unless the user is shown authorization.

In simple terms, deny is not allowed in the future, revoke is to take back what has been given.

Instance [SQL]View PlainCopyprint?
  1. GRANT INSERT on TableA to Rolea
  2. GO
  3. EXEC sp_addrolemember Rolea, ' UserA ' --user UserA will have TableA insert permission
  4. GO
  5. REVOKE Insert on TableA from Rolea --user UserA will have no TableA INSERT permission, revoke permissions
  6. GO
  7. GRANT Insert on TableA Torolea --re-Rolea with TableA insert permission
  8. GO
  9. Deny insert on TableA to UserA --although the user UserA Rolea has TableA INSERT permission, UserA itself is denied,  Therefore, user UserA will not have TableA insert permission.
  10. Excerpt from: http://blog.csdn.net/a497785609/article/details/47686659

SQL statement Operations SQL Server database logins, users, and permissions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.