--------------創建文件夾---------------
--打開高級選項
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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
