Managing Access to Tables and
|
|
Permission |
Description |
|---|---|
|
ALTER |
Can modify table properties |
|
CONTROL |
Provides ownership-like permissions |
|
DELETE |
Can delete rows from the table |
|
INSERT |
Can insert rows in the table |
|
REFERENCES |
Can reference the table from a foreign key |
|
SELECT |
Can select rows from the table |
|
TAKE OWNERSHIP |
Can take ownership of the table |
|
UPDATE |
Can update rows on the table |
|
VIEW DEFINITION |
Can access the the table's metadata |
You can provide access to database users and roles by using the GRANT statement. The following sample grants SELECT, INSERT, and UPDATE permissions to Sara on the Sales.Customer table. (The code in this and
-- Change the connection context to the database AdventureWorks. USE AdventureWorks; GO -- Grant some permissions to Sara on the Sales.Customer table. GRANT SELECT,INSERT,UPDATE ON Sales.Customer TO Sara;
When you need to prevent a user from accessing a table, you might face one of two scenarios. If you have granted permission to the user on the table before, you should use the REVOKE statement to clear the previously granted permissions. For example:
-- Change the connection context to the database AdventureWorks. USE AdventureWorks; GO -- Revoke SELECT permissions from Sara on the Sales.Customer table REVOKE SELECT ON Sales.Customer TO Sara;
However, the user might maintain the
-- Change the connection context to the database AdventureWorks. USE AdventureWorks; GO -- Deny DELETE permission to Sara on the Sales.Customer table, -- regardless of what permissions this user might -- inherit from roles. DENY DELETE ON Sales.Customer TO Sara;
SQL Server 2005 provides you the option to grant or refuse access to columns individually instead of working with tables. This feature gives you the flexibility to
|
Permission |
Description |
|---|---|
|
SELECT |
Can select the column |
|
UPDATE |
Can update the column |
|
REFERENCE |
Can reference the column from a foreign key |
You can grant access to columns individually by using the GRANT option. The following sample grants SELECT and UPDATE permissions to Sara on the Demographics and ModifiedDate columns for the Sales.Individual table. (The code in this and subsequent sections for managing access to columns is included in the sample files as ManagingAccessToColumns. sql.)
-- Change the connection context to the database AdventureWorks. USE AdventureWorks; GO -- Grant SELECT and UPDATE permissions to Sara -- on some specific columns of the Sales.Individual table GRANT SELECT,UPDATE ( Demographics, ModifiedDate) ON Sales.Individual TO Sara;
Similarly, you can revoke access to columns individually using the REVOKE statement. Remember that if you want to prevent a user from gaining permission, you need to use the DENY statement.
-- Change the connection context to the database AdventureWorks. USE AdventureWorks; GO -- Revoke previosly granted or denied permissions -- from Sara on the Demographics column. REVOKE UPDATE (Demographics) ON Sales.Individual TO Sara;