zoukankan      html  css  js  c++  java
  • 创建数据库,表,约束,权限

    --------------创建文件夹---------------
    --打开高级选项
    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:myMyDB.mdf',--主数据文件物理名
        size=3mb,--初始大小
        maxsize=20mb,--最大值
        filegrowth=2mb--增长率,可以用mb为单位,也可以用百分比(如:10%)    
    )
    ,
    (--次数据文件
        name='MyDB_ndf',
        filename='e:myMyDB_ndf.ndf',
        size=1mb,
        maxsize=10mb,
        filegrowth=2mb
    )
    log on--日志文件
    (
        name='MyDB_ldf',
        filename='e:myMyDB_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在禁用权限后,不可以从父类角色中继承相应的权限

  • 相关阅读:
    redis教程(三)-----redis缓存雪崩、缓存穿透、缓存预热
    dubbo入门学习(三)-----dubbo整合springboot
    dubbo入门学习(二)-----dubbo hello world
    spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
    spring cloud深入学习(十三)-----使用Spring Cloud Sleuth和Zipkin进行分布式链路跟踪
    C#关键字params
    C#的lock关键字
    C#的yield关键字
    C#关键字ref和out
    C#的初始化器
  • 原文地址:https://www.cnblogs.com/danmao/p/3870787.html
Copyright © 2011-2022 走看看