Sams Teach Yourself SQL in 24 Hours

Sams Teach Yourself SQL in 24 Hours

By Ron Plew and Ryan Stephens

Controlling User Access

User access is primarily controlled by a user account and password, but that is not enough to access the database in most major implementations. The creation of a user account is only the first step in allowing access to the database, as well as controlling that access.

After the user account has been created, the database administrator, security officer, or designated individual must be able to assign appropriate system-level privileges to a user for that user to be allowed to perform actual functions within the database, such as creating tables or selecting from tables. Furthermore, the schema owner usually needs to grant database users access to objects in the schema so that the user can do his or her job.

There are two commands in SQL that allow database access control involving the assignment of privileges and the revocation of privileges. The following are the two commands used to distribute both system and object privileges in a relational database:

The GRANT Command

The GRANT command is used to grant both system-level and object-level privileges to an existing database user account.

The syntax is as follows:

   syntax_icon.gif
GRANT PRIVILEGE1 [, PRIVILEGE2 ][ ON OBJECT ]
TO USERNAME [ WITH GRANT OPTION | ADMIN OPTION]

Granting one privilege to a user is as follows:

   input_icon.gif

   GRANT SELECT ON EMPLOYEE_TBL TO USER1;

   output_icon.gif
Grant succeeded.

Granting multiple privileges to a user is as follows:

   input_icon.gif

   GRANT SELECT, INSERT ON EMPLOYEE_TBL TO USER1;

   output_icon.gif
Grant succeeded.

Notice that when granting multiple privileges to a user in a single statement, each privilege is separated by a comma.

Granting privileges to multiple users is as follows:

   input_icon.gif

   GRANT SELECT, INSERT ON EMPLOYEE_TBL TO USER1, USER2;

   output_icon.gif
Grant succeeded.

The syntax to create a user in MySQL is as follows:

   syntax_icon.gif
GRANT USAGE ON DATABASE.TABLES
TO USERNAME@HOST IDENTIFIED BY 'PASSWORD';

GRANT OPTION

GRANT OPTION is a very powerful GRANT command option. When an object's owner grants privileges on an object to another user with GRANT OPTION, the new user can also grant privileges on that object to other users, even though the user does not actually own the object. An example follows:

   input_icon.gif

   GRANT SELECT ON EMPLOYEE_TBL TO USER1 WITH GRANT OPTION;

   output_icon.gif
Grant succeeded.

ADMIN OPTION

ADMIN OPTION is similar to GRANT OPTION in that the user that has been granted the privileges also inherits the ability to grant those privileges to another user. GRANT OPTION is used for object-level privileges, whereas ADMIN OPTION is used for system-level privileges. When a user grants system privileges to another user with ADMIN OPTION, the new user can also grant the system-level privileges to any other user. An example follows:

   mysql_icon.gif
   input_icon.gif

   GRANT CREATE TABLE TO USER1 WITH ADMIN OPTION;

   output_icon.gif
Grant succeeded.

The REVOKE Command

The REVOKE command removes privileges that have been granted to database users. The REVOKE command has two options: RESTRICT and CASCADE. When the RESTRICT option is used, REVOKE succeeds only if the privileges specified explicitly in the REVOKE statement leave no other users with abandoned privileges. The CASCADE option revokes any privileges that would otherwise be left with other users. In other words, if the owner of an object granted USER1 privileges with GRANT OPTION, USER1 granted USER2 privileges with GRANT OPTION, and then the owner revokes USER1's privileges, CASCADE also removes the privileges from USER2.

newterm_icon.gif

Abandoned privileges are privileges that are left with a user who was granted privileges with the GRANT OPTION from a user who has been dropped from the database or had his/her privileges revoked.

The syntax is as follows:

   syntax_icon.gif
REVOKE PRIVILEGE1 [, PRIVILEGE2 ] [ GRANT OPTION FOR ] ON OBJECT
FROM USER { RESTRICT | CASCADE }

The following is an example:

   input_icon.gif

   REVOKE INSERT ON EMPLOYEE_TBL FROM USER1;

   output_icon.gif
Revoke succeeded.

Controlling Access on Individual Columns

Instead of granting object privileges (INSERT, UPDATE, or DELETE) on a table as a whole, you can grant privileges on specific columns in the table to restrict user access, as shown in the following example example:

   input_icon.gif

   GRANT UPDATE (NAME) ON EMPLOYEES TO PUBLIC;

   output_icon.gif
Grant succeeded.

The PUBLIC Database Account

The PUBLIC database user account is a database account that represents all users in the database. All users are part of the PUBLIC account. If a privilege is granted to the PUBLIC account, all database users have the privilege. Likewise, if a privilege is revoked from the PUBLIC account, the privilege is revoked from all database users, unless that privilege was explicitly granted to a specific user. The following is an example:

   input_icon.gif

   GRANT SELECT ON EMPLOYEE_TBL TO PUBLIC;

   output_icon.gif
Grant succeeded.

Groups of Privileges

Some implementations have groups of privileges in the database. These groups of permissions are referred to with different names. Having a group of privileges allows simplicity for granting and revoking common privileges to and from users. For example, if a group consists of ten privileges, the group can be granted to a user instead of all ten privileges.

newterm_icon.gif

SQLBase has groups of privileges called authority levels, whereas these groups of privileges in Oracle are called roles. SQLBase and Oracle both include the following groups of privileges with their implementations:

The CONNECT group allows a user to connect to the database and perform operations on any database objects to which the user has access.

The RESOURCE group allows a user to create objects, drop objects he or she owns, grant privileges to objects he or she owns, and so on.

The DBA group allows a user to perform any function within the database. The user can access any database object and perform any operation with this group.

An example for granting a group of privileges to a user follows:

   mysql_icon.gif
   input_icon.gif

   GRANT DBA TO USER1;

   output_icon.gif
Grant succeeded.

Share ThisShare This

Informit Network