Home > SQL Server Tips > Database Management and Administration > Granting permissions in SQL Server 2005
SQL Server Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

DATABASE MANAGEMENT AND ADMINISTRATION

Granting permissions in SQL Server 2005


Adam Machanic, Contributor
09.22.2005
Rating: -4.19- (out of 5)


Expert advice on database administration
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


Imagine for a moment that you're a SQL Server 2000 production DBA (unless, of course, you are). You arrive at the office one morning, get a hot drink and check your e-mail. There are two new co-worker requests in your inbox: Bob in purchasing needs read-only access to all of the sales data so he can run reports to predict upcoming material acquisition volumes; John in Web development has a problem with a production Web application and needs access in order to run a Profiler trace to identify the problem. These seem like easy requests to fulfill -- or are they?

In the first case, sales data is stored in over 120 of the 400 tables in the production database. You have a role established for members of the sales team that grants both SELECT and UPDATE permissions, but Bob only needs permission to read data. This leaves you with only one option: Grant Bob SELECT access to each table, one by one.

Feel the carpal tunnel syndrome setting in yet?

As for the access request to run Profiler traces -- no can do. In SQL Server 2000, access to run a trace is granted only to members of the Systems Administrators (sysadmin) fixed server role. Assigning John to that role would give him complete access to everything on the server -- probably not a good idea. This leaves you with only one option: Work with John and run the trace yourself, tacking more tasks onto your already tight schedule.

Neither scenario is alien to any regular SQL Server 2000 DBA, but take solace -- a break is coming. SQL Server 2005 will not only offer various programmatic enhancements, it will also sport various security model improvements. A couple of these solve both Bob's and John's dilemmas: user-schema separation and granular server permissions.

TABLE OF CONTENTS
  [IMAGE] New feature: User-schema separation
  [IMAGE] New feature: Granular server permissions...


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google



RELATED CONTENT
Database Management and Administration
Password cracking tools for SQL Server
Using traces in SQL Server Profiler
Meet compliance requirements with improved database security practices
Hardening the network and OS for SQL Server security
Securing the server and database in SQL Server
How SQL Server 2008 components impact SharePoint implementations
Troubleshooting Distributed Transaction Coordinator errors in SQL Server
Achieving high availability and disaster recovery with SharePoint databases
Clearing the Windows page file and its effect on server performance
Deploying a SQL Server virtual appliance for Microsoft Hyper-V

Microsoft SQL Server 2005
End of life comes for SQL Server 2005 SP2, 2008
SQL Server Reporting Services Fast Guide
SQL Server Service Broker Tutorial and Reference Guide
Tips for tuning SQL Server 2005 to improve reporting performance
SQL Server consolidation: Why it's an optimization technique
Parent-child dimensions in SQL Server 2005 with Analysis Services MDX
Enforcing data integrity in a SQL Server database
SSIS error message due to installation problem on SQL Server 2005
Should you upgrade to SQL Server 2005 or SQL Server 2008?
Basics for working with DATETIME and SMALLDATETIME in SQL Server 2005
Microsoft SQL Server 2005 Research

SQL Server Security
Password cracking tools for SQL Server
Meet compliance requirements with improved database security practices
Hardening the network and OS for SQL Server security
Securing the server and database in SQL Server
SQL Server security made simple and sensible
Blog: Protect your databases from the internal threat
Setting up SQL Server Service Broker for secure communication
The keys to database backup protection for SQL Server
Understanding transparent data encryption in SQL Server 2008
The fine line between not encrypting your databases and breach notification

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
application server  (SearchSQLServer.com)
Yukon  (SearchSQLServer.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



  [IMAGE] New function: View available permissions

[IMAGE][IMAGE]  User-schema separation[IMAGE] Return to Table of Contents

SQL Server 2005 adds support for schemas, as described in the ANSI SQL-92 specification. Think of schemas as logical containers for database objects. These containers replace the way that many DBAs used object owners in previous versions of SQL Server. In SQL Server 2005, objects no longer have an owner. Instead, objects are members of a schema, and are accessed by schema name.

Let me compare. In previous versions of SQL Server, the following three-part naming system applied:

    database_name.owner_name.object_name

In SQL Server 2005, this system is used:

    database_name.schema_name.object_name

In SQL Server 2005, instead of granting permissions to objects directly, DBAs can choose to grant permissions to schemas. Granting permissions to a schema propagates permissions to every object currently contained in the schema and any objects added to the schema in the future, meaning that the DBA does not need to update permissions for every user any time a new object is added.

This level of indirection between users and objects has these key advantages:

    1. Since objects are no longer owned by users, users can be dropped without dropping or reassigning any objects they own. This saves you a headache when employees leave, for instance.

    2. Objects in the same database can now be grouped logically based on business function. For instance, you may wish to populate a client management database with both client contact information and sales data. The tables that make up each function are independent for the most part. In previous versions of SQL Server, you may have had to create two databases. While that logically groups the objects, it makes them impossible to relate using foreign key constraints, which, consequently makes referential integrity difficult.

Schemas are created using the CREATE SCHEMA T-SQL statement. For instance, a schema called Sales can be created using the following T-SQL:

    CREATE SCHEMA Sales

Once a schema is created, you can create objects within it by simply prefixing with the schema name. For instance, to create a table in the Sales schema called MySalesData, the following T-SQL could be used:

Objects can also be transferred among schemas using the TRANSFER option of the ALTER SCHEMA statement. The MySalesData table could be moved to a schema called OtherSchema using the following T-SQL:

Once schemas are created and populated, users can be assigned permissions to every object in the schema simultaneously. For instance, to assign a user called Bob SELECT access to all objects in the Sales schema, the following T-SQL could be used:

    GRANT SELECT ON Sales TO BOB

This is clearly more efficient than the equivalent method in previous versions of SQL Server -- having to assign permissions to every object one by one.

[IMAGE][IMAGE]  New feature: Granular server permissions[IMAGE] Return to Table of Contents

Using the user-schema separation feature makes assigning permissions to database objects en masse quite simple. What about the assignment of server-level permissions?

SQL Server versions up to and including SQL Server 2005 ship with fixed-server roles, collections of permissions to various server resources. If you want to grant login permissions to create, alter or drop databases, you can make that login a member of the "dbcreator" server role. But, if you want to grant login access only to create databases, you would not have been able to in previous versions of SQL Server. In SQL Server 2005, the fixed roles have been broken down into their component parts, which can be granted via the GRANT statement. For instance, to grant a login called Bob access to create databases on the server, the following T-SQL can be used:

    GRANT CREATE ANY DATABASE TO Bob

There are many other server-level permissions available, all of which are listed in the SQL Server 2005 Books Online topic, GRANT Server Permissions. By far the most exciting permission that can be granted is the ability to run traces. Remember John, the Web developer who needed access to run traces on the production server? In SQL Server 2000, this would have required access to the Server Administrators' fixed-server role. In SQL Server 2005, the following T-SQL will do the trick:

    GRANT ALTER TRACE TO John

This feature alone should put a grin on the face of most production DBAs. There is no more need to add developers to the Systems Administrator role just so they can run traces -- and there's no need to do work for them since they shouldn't be added to that role to begin with.

[IMAGE][IMAGE]  New function: View available permissions[IMAGE] Return to Table of Contents

All of these new permissions and methods for assigning permissions can make things confusing. Users may not understand what permissions they do or do not actually have. Luckily, Microsoft has provided a new function to deal with these issues: fn_my_permissions(). This function returns information about which permissions are available to the user currently logged in or impersonated.

The fn_my_permissions() function has two parameters: a target name and target type described by the name. For instance, if the target is the schema called Sales, the target name is SCHEMA and the following T-SQL would be used to return a table of permissions:

To drill down further and request information on permissions to the MySalesData table, this T-SQL would be used:

Virtually every object type can be specified as a target. To request server-level permissions, use NULL for both parameters, as follows:

Live the easy life

So let's recap. The new permissions features in SQL Server 2005 give you more control over resources and less to worry about on a day-to-day basis. User-schema separation makes managing large databases much simpler. The ability to grant permissions on a more granular basis means that you will be able to let users do some of their own tasks, thereby offloading responsibility. Both features should make your life -- and users' lives -- much simpler. And isn't that everyone's definition of the Easy Life?

About the author: Adam Machanic is a database-focused software engineer, writer and speaker based in Boston, Mass. He has implemented SQL Server for a variety of high-availability OLTP and large-scale data warehouse applications, and also specializes in .NET data access layer performance optimization. He is a Microsoft Most Valuable Professional (MVP) for SQL Server and a Microsoft Certified Professional. Machanic is co-author of Pro SQL Server 2005, published by Apress.


More information from SearchSQLServer.com

  • Book excerpt: Read up on assembly permissions in SQL Server 2005
  • Book excerpt: Get up to speed on new security features in SQL Server 2005
  • Ask the Expert: Ask Adam Machanic a question about SQL Server 2005


  • Rate this Tip
    To rate tips, you must be a member of SearchSQLServer.com.
    Register now to start rating these tips. Log in if you are already a member.


    Submit a Tip




    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



    SQL Server Development - .NET, C#, T-SQL, Visual Basic
    HomeNewsTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersIT Downloads
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts