实验环境;
1 创建一个schema
Schema
CREATE SCHEMA [Sales] AUTHORIZATION [dbo]
CREATE SCHEMA [Sales] AUTHORIZATION [dbo]
2 创建两个表,一个视图,一个存储过程
[Sales].[Customer]
CREATE TABLE [Sales].[Customer](
[Customer_ID] [int] NOT NULL,
[Customer_Name] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
CONSTRAINT [PK_Sales.Customer] PRIMARY KEY CLUSTERED
(
[Customer_ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [Sales].[Customer](
[Customer_ID] [int] NOT NULL,
[Customer_Name] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
CONSTRAINT [PK_Sales.Customer] PRIMARY KEY CLUSTERED
(
[Customer_ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
[Sales].[CustomerContacts]
CREATE TABLE [Sales].[CustomerContacts](
[Customer_ID] [int] NOT NULL,
[Customer_Address] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [Sales].[CustomerContacts] WITH CHECK ADD CONSTRAINT [FK_CustomerContacts_Customer] FOREIGN KEY([Customer_ID])
REFERENCES [Sales].[Customer] ([Customer_ID])
CREATE TABLE [Sales].[CustomerContacts](
[Customer_ID] [int] NOT NULL,
[Customer_Address] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [Sales].[CustomerContacts] WITH CHECK ADD CONSTRAINT [FK_CustomerContacts_Customer] FOREIGN KEY([Customer_ID])
REFERENCES [Sales].[Customer] ([Customer_ID])
[Sales].[ContactList]
CREATE VIEW [Sales].[ContactList]
AS
SELECT Sales.Customer.Customer_ID, Sales.Customer.Customer_Name, Sales.CustomerContacts.Customer_Address
FROM Sales.Customer INNER JOIN
Sales.CustomerContacts ON Sales.Customer.Customer_ID = Sales.CustomerContacts.Customer_ID
CREATE VIEW [Sales].[ContactList]
AS
SELECT Sales.Customer.Customer_ID, Sales.Customer.Customer_Name, Sales.CustomerContacts.Customer_Address
FROM Sales.Customer INNER JOIN
Sales.CustomerContacts ON Sales.Customer.Customer_ID = Sales.CustomerContacts.Customer_ID
[Sales].[GetContact]
CREATE PROCEDURE [Sales].[GetContact]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT * from Sales.ContactList
END
CREATE PROCEDURE [Sales].[GetContact]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT * from Sales.ContactList
END
3 创建一个login
Login
这个时候使用SaleA登录MyDB是没有任何权限的。执行下列命令会得到4个错误
T-SQL
select * from Sales.Customer
select * from Sales.CustomerContacts
select * from Sales.ContactList
exec Sales.GetContact
select * from Sales.Customer
select * from Sales.CustomerContacts
select * from Sales.ContactList
exec Sales.GetContact
错误信息:
Msg 229, Level 14, State 5, Line 1
SELECT permission denied on object 'Customer', database 'MyDB', schema 'Sales'.
Msg 229, Level 14, State 5, Line 2
SELECT permission denied on object 'CustomerContacts', database 'MyDB', schema 'Sales'.
Msg 229, Level 14, State 5, Line 3
SELECT permission denied on object 'ContactList', database 'MyDB', schema 'Sales'.
Msg 229, Level 14, State 5, Procedure GetContact, Line 1
EXECUTE permission denied on object 'GetContact', database 'MyDB', schema 'Sales'.
但是你如果赋予SaleA执行存贮过程的权限,你就可以得到访问到sp所对应的视图,视图所对应的表的内容了。
我开始是以为SaleA的权限应该包含试图的select权限的。唉,真是实践一下,才能知道不是这个样子的。
这样的设计应该是为了了管理的方便,如果需要级联权限,肯定会晕掉的,这样的简洁明了的方式之值得我们学习和应用到其他的设计中去的