您的位置:首页 > 数据库

mssql常用语句,平时用来学习sql的

2009-02-25 12:45 399 查看
  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     WrittenExam float,

 48     LabExam float,

 49     StuNo int

 50 )

 51 alter table ScoreInfo

 52     drop constraint CK_WrittenExam,CK_LabExam

 53 go

 54 alter table ScoreInfo

 55     alter column WrittenExam numeric(5,2)

 56 alter table ScoreInfo

 57     alter column LabExam numeric(5,2)

 58 go

 59 --约束

 60 alter table StuInfo

 61     add constraint PK_StuNo primary key(StuNo)

 62 alter table ScoreInfo

 63     add constraint CK_WrittenExam check(WrittenExam>0 and WrittenExam<100)

 64 alter table ScoreInfo

 65     add constraint CK_LabExam check(LabExam>0 and LabExam<100)

 66 alter table ScoreInfo

 67     add constraint FK_StuNo foreign key(StuNo) references StuInfo(StuNo)

 68 go

 69 

 70 --授权windows用户访问数据库

 71 

 72     exec sp_grantlogin 'lab-04\administrator'--即将过期的方式

 73     create login [lab-04\administrator] from windows----推荐方式

 74 

 75 drop login [lab-04\administrator]--删除登录

 76 

 77 create login t0811 with password='t0811'--创建新sql登录

 78 

 79 --创建新数据库用户,以前用sp_grantdbaccess,以后用

 80 use StuDb

 81 go

 82 create user t0811InStuDb for login t0811

 83 --授权访问表

 84 grant select,delete,update,insert on StuInfo to t0811InStuDb

 85 --取消权限

 86 revoke delete on StuInfo to t0811InStuDb

 87 --将t0811这个登录加入到sysadmin这个服务器级别角色中

 88 --exec sp_addsrvrolemember 't0811','sysadmin'

 89 

 90 --将t0811InStuDb这个数据库用户加入到db_owner这个数据库级别角色中

 91 exec sp_addrolemember 't0811InStuDb','db_owner'

 92 --拒绝某个用户的某个权限

 93 deny delete on StuInfo to t0811InStuDb

 94 

 95 

 96 

 97 --------添加测试数据

 98 --学员信息

 99 declare @n int

 set @n=1

 while @n<10

 begin

 Insert StuInfo (StuName)values('张'+convert(varchar(2),@n))

 set @n=@n+1

 end

 

 --成绩信息

 declare @n int

 set @n=1

 while @n<100

 begin

 Insert ScoreInfo (StuNo,WrittenExam,LabExam)

 values(convert(int, rand()*10),rand()*101,rand()*101)

 set @n=@n+1

 end

 

 

 select * from StuInfo

 select * from ScoreInfo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: