--------------創(chuàng)建文件夾---------------
--打開(kāi)高級(jí)選項(xiàng)
exec sp_configure 'show advanced options',1
reconfigure--重啟配置
--開(kāi)啟xp_cmdshell功能(開(kāi)啟后能使用dos命令)
exec sp_configure 'xp_cmdshell',1
reconfigure
--使用xp_cmdshell功能
exec xp_cmdshell 'md e:\my'
--注意:sp_開(kāi)頭是系統(tǒng)存儲(chǔ)過(guò)程,xp_開(kāi)頭是擴(kuò)展存儲(chǔ)過(guò)程
-----------------創(chuàng)庫(kù)----------------------
--判斷MyDB數(shù)據(jù)庫(kù)是否存在,如果存在就刪除
--方法一
--if exists(select * from sysdatabases where name='MyDB')
?? ?--drop database MyDB
--方法二
if DB_ID('MyDB') is not null
?? ?drop database MyDB?? ?
--創(chuàng)建MyDB數(shù)據(jù)庫(kù)
create database MyDB
on primary--主數(shù)據(jù)文件,on后面接的是文件組名稱(chēng),可省略
(
?? ?name='MyDB',--主數(shù)據(jù)文件邏輯名
?? ?filename='e:\my\MyDB.mdf',--主數(shù)據(jù)文件物理名
?? ?size=3mb,--初始大小
?? ?maxsize=20mb,--最大值
?? ?filegrowth=2mb--增長(zhǎng)率,可以用mb為單位,也可以用百分比(如:10%)?? ?
)
,
(--次數(shù)據(jù)文件
?? ?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
--------------創(chuàng)表---------------
use MyDB
--判斷表是否存在
--方法一
--if exists(select * from sysobjects where name='stuinfo')
?? ?--drop table stuinfo
--方法二
if OBJECT_ID('stuinfo') is not null
?? ?drop table stuinfo
?? ?
--創(chuàng)建表
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),--標(biāo)識(shí)列
?? ?stuNo int not null,
?? ?score float not null
)
---------------添加約束------------------
--主鍵約束
alter table stuinfo
?? ?add constraint PK_stuNo primary key(stuNo)
--默認(rèn)約束
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、創(chuàng)建登錄名
--方法一:
create login T1346 with password='sasa'
--方法二:
exec sp_addlogin 'T1346','sasa'
--2、根據(jù)登錄名創(chuàng)建用戶
--注意:先要確定數(shù)據(jù)庫(kù)(即給哪個(gè)數(shù)據(jù)庫(kù)添加的用戶)
use MyDB
--方法一:
create user T1346_user for login T1346
--方法二:
exec sp_grantdbaccess 'T1346','T1346_user'
--3、授權(quán)(分配權(quán)限)
--添加用戶T1346_user對(duì)stuinfo表的操作權(quán)限,如果是對(duì)所有表都添加權(quán)限可以把on stuinfo去掉
grant select,insert on stuinfo to T1346_user
--收回權(quán)限
revoke insert on stuinfo to T1346_user
--revoke與deny的區(qū)別
--1、revoke收回權(quán)限后,還可以從父類(lèi)角色中繼承相應(yīng)的權(quán)限
--2、deny在禁用權(quán)限后,不可以從父類(lèi)角色中繼承相應(yīng)的權(quán)限
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
