• Tsql


     

      1  
      2 --1,创建登录,可以有两种方式:
      3 --
    1)以SQL验证的方式新建一个新的登录:
      4
        exec sp_addlogin 'allnen','all123'--创建登录,登录名和密码
      5

      6 --2)以windows验证的方式将windows中的某个用户授予登录数据库服务的权限:
      7
        exec sp_grantlogin 'PC-200901051757\allnen'
      8 --或者
      9
        --create login [PC-200901051757\allnen] from windows
     10

     11 --如果要删除某个登录,可以用如下方式:
     12
        --drop login [PC-200901051757\allnen] 
     13

     14 --2,现在就可以用allnen这个帐号登录,但这里先不要登录,我们先给这个帐号授予创建数据库和表的权限
     15

     16     exec sp_grantdbaccess @loginame='allnen',@name_in_db='allnenDbUser'--将登录名映射到当前数据库
     17

     18     --exec sp_revokedbaccess 'allnen'--从当前数据库中删除对应的登录用户映射,也就是去除某个用户访问此数据库的权限
     19

     20     grant create database,create table to allnen--授予当前用户创建数据库的权限
     21 --
    如果要去除某个用户的权限,用revoke,如果要拒绝某个用户的相关权限,用denyrevokedeny的区别,请查阅其他相关文章
     22
        --deny create database to allnenDbUser
     23

     24 --3,现在就可以用allnen这个帐号登录,然后创建数据库和表了
     25 --
    有时候如果要创建的数据库已经存在 ,那创建数据库的时候就会出错,
     26 --
    所以我们一般在创建数据库前都会先判断要创建的数据库是否已经存在,
     27 --
    如果存在,就先将存在的数据库删除
     28
        IF EXISTS(SELECT * FROM sysdatabases WHERE name='stuDB')
     29         drop database stuDB
     30     GO
     31 --当然,也可以用db_id(数据库名)这个函数来判断
     32

     33 --创建数据库的时候,有可能数据库文件已经存在,或者文件目录不存在等,所以,我们还要先在硬盘中创建对应的文件夹
     34

     35     exec sp_configure 'show advanced options'1--显示高级选项,然后才可以配置xp_cmdshell
     36
        reconfigure--从新应用配置,让配置生效
     37
        exec sp_configure 'xp_cmdshell',1--启用xp_cmdshell功能
     38
        reconfigure
     39     exec xp_cmdshell 'mkdir D:\project\'--xp_cmdshell创建目录
     40
        GO
     41 --开始创建数据库
     42
        CREATE DATABASE stuDB
     43        ON  PRIMARY  --默认就属于PRIMARY主文件组,可省略
     44
        (
     45      NAME='stuDB_data',  --主数据文件的逻辑名
     46
         FILENAME='D:\project\stuDB_data.mdf',  --主数据文件的物理名
     47
         SIZE=5mb,  --主数据文件初始大小
     48
         MAXSIZE=100mb,  --主数据文件增长的最大值
     49
         FILEGROWTH=15%   --主数据文件的增长率
     50
        )
     51     LOG ON
     52     (
     53       NAME='stuDB_log',
     54       FILENAME='D:\project\stuDB_log.ldf',
     55       SIZE=2mb,
     56       FILEGROWTH=1MB
     57     )
     58     GO 
     59 --如果要创建多个数据库文件或者多个日志文件,则可以用这样的方式
     60
        CREATE  DATABASE  employees
     61       ON 
     62        (
     63        /*-主数据文件的具体描述-*/
     64        NAME = 'employee1'
     65        FILENAME = 'D:\project\employee1_Data.mdf' , 
     66        SIZE = 10
     67        FILEGROWTH = 10%
     68       ), 
     69       (
     70        /*-次要数据文件的具体描述-*/
     71        NAME = 'employee2'
     72        FILENAME = 'D:\project\employee2_Data.ndf' , 
     73        SIZE = 20
     74        MAXSIZE = 100
     75        FILEGROWTH = 1
     76       ) 
     77      LOG ON 
     78       (
     79        /*-日志文件1的具体描述-*/
     80        NAME = 'employeelog1'
     81        FILENAME = 'D:\project\employeelog1_Log.ldf' , 
     82        SIZE = 10
     83        FILEGROWTH = 1
     84        ), 
     85       (
     86        /*-日志文件2的具体描述-*/
     87        NAME = 'employeelog2'
     88        FILENAME = 'D:\project\employeelog2_Log.ldf' , 
     89        SIZE = 10
     90        MAXSIZE = 50
     91        FILEGROWTH = 1
     92       )
     93     GO 
     94
     95
     96 --
     97

     98 --4,创建完数据库后,我们就可以开始创建表了
     99
        USE stuDB   --将当前数据库设置为stuDB 
    100
        GO
    101 --同样,在创建表的时候,表也有可能已经存在,所以我们要先将已存在的表删除
    102
        IF EXISTS(SELECT * FROM sysobjects  WHERE name='stuInfo')
    103         drop table stuInfo
    104     GO
    105     CREATE  TABLE  stuInfo    /*-创建学员信息表-*/
    106     (
    107      stuName  VARCHAR(20)  NOT  NULL ,  --姓名,非空(必填)
    108
         stuNo   CHAR(6)  NOT  NULL,   --学号,非空(必填)
    109
         stuAge  INT  NOT  NULL,  --年龄,INT类型默认为4个字节
    110
         stuID  NUMERIC(18,0),     --身份证号
    111
         stuSeat   SMALLINT  IDENTITY (1,1),   --座位号,自动编号
    112
         stuAddress   TEXT   --住址,允许为空,即可选输入
    113
        ) 
    114     GO
    115
    116     IF EXISTS(SELECT * FROM sysobjects WHERE name='scoreInfo')
    117         DROP TABLE scoreInfo
    118     GO
    119     CREATE TABLE scoreInfo
    120     (
    121         scoreInfoId int identity(1,1primary key,
    122         score int,
    123         stuNo CHAR(6)
    124     )
    125     GO
    126
    127 --5,创建完表之后,要做的事情就算给表建立约束
    128
        alter table stuInfo
    129         add constraint PK_stuNo primary key(stuNo)--主键约束
    130
        go
    131     alter table stuInfo
    132         add constraint UQ_stuID unique(stuID)--唯一约束
    133
        go
    134     alter table stuInfo
    135         add constraint DF_stuAddress default('地址不详'for stuAddress--默认约束
    136
        go
    137     alter table stuInfo
    138         add constraint CK_stuAge check(stuAge >20 and stuAge<40)--检查约束
    139
        go
    140     alter table scoreInfo
    141         add constraint FK_stuNo foreign key(stuNo) references stuInfo(stuNo) --外键约束
    142
        go

     1 --1,用管理员登录
     2
     --2,用管理员创建新数据库
     3
     --3,用管理员创建新登录
     4
     --4,授权新登录名访问新数据库的权限
     5
     use master
     6 go
     7 exec sp_configure 'show advanced options',1
     8 reconfigure
     9 exec sp_configure 'xp_cmdshell',1
    10 reconfigure
    11 exec xp_cmdshell 'mkdir d:\Data\'
    12 
    13 
    14 if exists(select * from sysdatabases where name='StuDb')
    15 drop database StuDb
    16 create database StuDb on primary 
    17 (
    18     name='StuDb',
    19     filename='D:\Data\StuDb.mdf',
    20     size=5MB,
    21     filegrowth=15%
    22 )
    23 log on
    24 (
    25     name='StuDb_log',
    26     filename='D:\Data\StuDb.ldf',
    27     size=3MB,
    28     maxsize=10MB,
    29     filegrowth=10%
    30 )
    31 go
    32 use StuDb
    33 go
    34 if exists(select *from sysobjects where name='StuInfo')
    35     drop table StuInfo
    36 go
    37 create table StuInfo(
    38     StuNo int identity(1,1),
    39     StuName nvarchar(10)
    40 )
    41 go
    42 if exists(select *from sysobjects where name='ScoreInfo')
    43     drop table ScoreInfo
    44 go
    45 create table ScoreInfo(
    46     ScoreInfoId int identity(1,1),
    47     ExamScore float,
    48     LabScore float,
    49     StuNo int
    50 )
    51 --删除约束
    52
     alter table ScoreInfo
    53     drop constraint CK_ExamScore,CK_LabScore
    54 go
    55 alter table ScoreInfo
    56     alter column ExamScore numeric(5,2)
    57 alter table ScoreInfo
    58     alter column LabScore numeric(5,2)
    59 go
    60 --约束
    61
     alter table StuInfo
    62     add constraint PK_StuNo primary key(StuNo)
    63 alter table ScoreInfo
    64     add constraint CK_ExamScore check(ExamScore>0 and ExamScore<100)
    65 alter table ScoreInfo
    66     add constraint CK_LabScore check(LabScore>0 and LabScore<100)
    67 alter table ScoreInfo
    68     add constraint FK_StuNo foreign key(StuNo) references StuInfo(StuNo)
    69 go
    70 
    71 --授权windows用户访问数据库
    72
     
    73     exec sp_grantlogin 'lab-04\administrator'--即将过期的方式
    74
         create login [lab-04\administrator] from windows----推荐方式
    75
     
    76 drop login [lab-04\administrator]--删除登录
    77
     
    78 create login t0811 with password='t0811'--创建新sql登录
    79
     
    80 --创建新数据库用户,以前用sp_grantdbaccess,以后用
    81
     use StuDb
    82 go
    83 create user t0811InStuDb for login t0811
    84 --授权访问表
    85
     grant select,delete,update,insert on StuInfo to t0811InStuDb
    86 --取消权限
    87
     revoke delete on StuInfo to t0811InStuDb
    88 --t0811这个登录加入到sysadmin这个服务器级别角色中
    89
     --exec sp_addsrvrolemember 't0811','sysadmin'
    90
     
    91 --t0811InStuDb这个数据库用户加入到db_owner这个数据库级别角色中
    92
     exec sp_addrolemember 't0811InStuDb','db_owner'
    93 --拒绝某个用户的某个权限
    94
     deny delete on StuInfo to t0811InStuDb
    95 

  • 相关阅读:
    非阻塞式线程安全列表-ConcurrentLinkedDeque
    计数器
    Linux 查看服务器内存使用情况
    Oracle to_date, to_timestamp
    Hibernate session.flush() 使用
    阿里规约认证(题库附答案)
    多态性
    Java数据类型
    String类特点分析
    数组的定义
  • 原文地址:https://www.cnblogs.com/xiaogelove/p/2342638.html
Copyright © 2020-2023  润新知