關於登錄名,用戶名,角色,權限的學習筆記

select a.name,a.xtype,b.name,case action when 26 then 'references' when 178 then 'create function' when 193 then 'select'
when 195 then 'insert' when 196 then 'delete' when 197 then 'update' when 198  then 'create table' when 203 then 'create database'
when 207 then 'create view' when 222 then 'create procedudre' when 224 then 'execute' when 228 then 'backup database'
when 233 then 'create default' when 235 then 'backup log' when 236 then 'create rule'end
from sysobjects a inner join sysprotects c on a.id=c.id inner join sysusers b on c.uid=b.uid 
where b.name='user1'--在此輸入角色或用戶名,查看其權限

create table tb(id int,num int)--新建測試表
insert into tb
select 1,1

--新建一個登陸,登陸名:qiankun ,密碼:空,默認數據庫:test
sp_addlogin 'qiankun','','test'
--爲登錄qiankun在數據庫test中新建一個用戶user1. 如果一個登陸沒有對應的用戶,則無法登陸
use test
go
sp_adduser 'qiankun','user1'
--查看登錄
sp_helplogins 'qiankun'
--查看該數據庫裏的用戶,此時用戶user1默認屬於public組,擁有public的權限,此時user1對錶tb沒有任何權限
sp_helpuser
--新建一個角色role1
sp_addrole 'role1'
--給角色賦予select 表tb的權限
grant select on tb to role1
--把用戶user1添加進role1中,此時user1可以select 表tb
sp_addrolemember 'role1','user1'
--直接給用戶user1賦予修改num字段的權限
grant update on tb(num) to user1
--給角色role1賦予新建表的權限,用戶對自己新建的對象擁有全部權限
grant create table to role1
--給角色授予表tb的所有權限
grant all  on tb to role1
--給角色role1授予所有語句權限,不能創建數據庫,不能執行master..xp_cmdshell,待續!!!!!!!!!!!!!1
grant all to role1

------------------------------------------------------------------------------------------------------------
--此時登錄qiankun只能使用數據庫test,不能使用其他的數據庫。
--通過在目的數據庫上新建一個用戶映射,登錄qiankun可以訪問多個數據庫
--方法1
use wsd
go
sp_grantdbaccess 'qiankun','user2'
--收回訪問數據庫權限
use wsd
go
sp_revokedbaccess 'user2'
--方法2
use wsd
go
sp_adduser 'qiankun','user2'
--收回訪問數據庫權限
use wsd
go
sp_dropuser 'user2'
--用方法1添加的用戶可以用方法2來刪除,方法2添加的用戶可以用方法1刪除,兩者的區別?!!!!!
--
--對於一個登陸名,如果想訪問某一個數據庫,必須在這個數據庫上爲該登錄名添加一個用戶,並且只能添加一個。
--不同數據庫中的用戶名字可以相同,但是它們是不同的用戶,權限不一樣。

發佈了34 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章