sql 2005数据库安全性和参照完整性
作者:[网上资料 ]
--创建登录账号
create login username with password='password'
--创建数据库用户
create user username for login username
---授予该用户对Student表的查询权限,同时授予其进一步授权其他用户的权利。
grant select
on student
to user_tom
with grant option
-----
create login marry with password='marry001'
create user user_marry for login marry
--创建角色
create role dbnormuser
---对角色授权
grant select
on student
to dbnormuser
--指定固定的服务器角色(登录名)
sp_addsrvrolemember marry,sysadmin
删除
sp_dropsrvrolemember marry,sysadmin
--查看用户
sp_helpuser
---新建外键约束
ALTER TABLE test_sub ADD CONSTRAINT main_id_cons FOREIGN KEY (main_id) REFERENCES test_main;
---删除外键
ALTER TABLE test_sub drop CONSTRAINT main_id_cons
--创建级联删除外键
ALTER TABLE test_sub ADD CONSTRAINT main_id_cons FOREIGN KEY (main_id) REFERENCES test_main on delete
cascade
--创建级联更新外键
ALTER TABLE test_sub ADD CONSTRAINT main_id_cons FOREIGN KEY (main_id) REFERENCES test_main on update
cascade
--创建删除被参照主键是 键参照关系的外键设置为null
ALTER TABLE test_sub ADD CONSTRAINT main_id_cons FOREIGN KEY (main_id) REFERENCES test_main on update
set null
----参照完整性
不能删除 带有外键的记录
|
create login username with password='password'
--创建数据库用户
create user username for login username
---授予该用户对Student表的查询权限,同时授予其进一步授权其他用户的权利。
grant select
on student
to user_tom
with grant option
-----
create login marry with password='marry001'
create user user_marry for login marry
--创建角色
create role dbnormuser
---对角色授权
grant select
on student
to dbnormuser
--指定固定的服务器角色(登录名)
sp_addsrvrolemember marry,sysadmin
删除
sp_dropsrvrolemember marry,sysadmin
--查看用户
sp_helpuser
---新建外键约束
ALTER TABLE test_sub ADD CONSTRAINT main_id_cons FOREIGN KEY (main_id) REFERENCES test_main;
---删除外键
ALTER TABLE test_sub drop CONSTRAINT main_id_cons
--创建级联删除外键
ALTER TABLE test_sub ADD CONSTRAINT main_id_cons FOREIGN KEY (main_id) REFERENCES test_main on delete
cascade
--创建级联更新外键
ALTER TABLE test_sub ADD CONSTRAINT main_id_cons FOREIGN KEY (main_id) REFERENCES test_main on update
cascade
--创建删除被参照主键是 键参照关系的外键设置为null
ALTER TABLE test_sub ADD CONSTRAINT main_id_cons FOREIGN KEY (main_id) REFERENCES test_main on update
set null
----参照完整性
不能删除 带有外键的记录
|