创建数据库,表,约束,权限

系统 1571 0

--------------创建文件夹---------------
--打开高级选项
exec sp_configure 'show advanced options',1
reconfigure--重启配置
--开启xp_cmdshell功能(开启后能使用dos命令)
exec sp_configure 'xp_cmdshell',1
reconfigure
--使用xp_cmdshell功能
exec xp_cmdshell 'md e:\my'
--注意:sp_开头是系统存储过程,xp_开头是扩展存储过程

-----------------创库----------------------
--判断MyDB数据库是否存在,如果存在就删除
--方法一
--if exists(select * from sysdatabases where name='MyDB')
    --drop database MyDB
--方法二
if DB_ID('MyDB') is not null
    drop database MyDB    

--创建MyDB数据库
create database MyDB
on primary--主数据文件,on后面接的是文件组名称,可省略
(
    name='MyDB',--主数据文件逻辑名
    filename='e:\my\MyDB.mdf',--主数据文件物理名
    size=3mb,--初始大小
    maxsize=20mb,--最大值
    filegrowth=2mb--增长率,可以用mb为单位,也可以用百分比(如:10%)    
)
,
(--次数据文件
    name='MyDB_ndf',
    filename='e:\my\MyDB_ndf.ndf',
    size=1mb,
    maxsize=10mb,
    filegrowth=2mb
)
log on--日志文件
(
    name='MyDB_ldf',
    filename='e:\my\MyDB_ldf.ldf',
    size=1mb,
    maxsize=10mb,
    filegrowth=2mb
)
go


--------------创表---------------
use MyDB
--判断表是否存在
--方法一
--if exists(select * from sysobjects where name='stuinfo')
    --drop table stuinfo
--方法二
if OBJECT_ID('stuinfo') is not null
    drop table stuinfo
    
--创建表
create table stuinfo
(
    stuNo int not null,
    stuAge int not null
)
go
--添加列(注意:add后面不能加column)
alter table stuinfo
    add stuName nvarchar(20) not null
alter table stuinfo
    add stuSex nvarchar(2) not null
--删除列
alter table stuinfo
    drop column stuName

create table stuscore
(
    ID int identity(1,1),--标识列
    stuNo int not null,
    score float not null
)

---------------添加约束------------------
--主键约束
alter table stuinfo
    add constraint PK_stuNo primary key(stuNo)
--默认约束
alter table stuinfo
    add constraint DF_stuSex default('男') for stuSex
--唯一约束
alter table stuinfo
    add constraint UQ_stuName unique(stuName)
--检查约束
alter table stuinfo
    add constraint CK_stuAge check(stuAge>0 and stuAge<100)
--外键约束
alter table stuscore
    add constraint FK_stuNo foreign key(stuNo) references stuinfo(stuNo)

--删除约束
alter table stuinfo
    drop constraint UQ_stuName
    

------------------安全管理-------------------------
--1、创建登录名
--方法一:
create login T1346 with password='sasa'
--方法二:
exec sp_addlogin 'T1346','sasa'

--2、根据登录名创建用户
--注意:先要确定数据库(即给哪个数据库添加的用户)
use MyDB
--方法一:
create user T1346_user for login T1346
--方法二:
exec sp_grantdbaccess 'T1346','T1346_user'

--3、授权(分配权限)
--添加用户T1346_user对stuinfo表的操作权限,如果是对所有表都添加权限可以把on stuinfo去掉
grant select,insert on stuinfo to T1346_user

--收回权限
revoke insert on stuinfo to T1346_user

--revoke与deny的区别
--1、revoke收回权限后,还可以从父类角色中继承相应的权限
--2、deny在禁用权限后,不可以从父类角色中继承相应的权限

创建数据库,表,约束,权限


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论