create database test
on primary -- 默認就屬於primary文件組,可省略
(
/*--數據文件的具體描述--*/
name='test', -- 主數據文件的邏輯名稱
filename='D:\項目文件\淮礦\高科\test.mdf', -- 主數據文件的物理名稱
size=5mb, --主數據文件的初始大小
maxsize=100mb, -- 主數據文件增長的最大值
filegrowth=15%--主數據文件的增長率
)
log on
(
/*--日志文件的具體描述,各參數含義同上--*/
name='stuDB_log',
filename='D:\項目文件\淮礦\高科\test_log.ldf',
size=2mb,
filegrowth=1mb
)
==========================================================
use master -- 設置當前數據庫為master,以便訪問sysdatabases表================刪除數據庫
go
if exists(select * from sysdatabases where name='test')--查詢該庫是否存在
drop database test
go
==========================================================
use StuDB
go
if exists(select * from sysobjects where name='stuMarks')
drop table stuMarks
create table stuMarks
(
ExamNo int identity(1,1) primary key,
stuNo char(6) not null,
writtenExam int not null,
LabExam int not null
)
go
-- 其中,列屬性"identity(起始值,遞增量)" 表示"ExamNo"列為自動編號, 也稱為標識列
alter table 表名
add constraint 約束名 約束類型 具體的約束說明
alter table 表名
drop constraint 約束名
alter table stuMarks
add constraint UQ_stuNo Unique(stuNo)
alter table stuMarks
drop constraint UQ_stuNo
/*--添加SQL登錄賬戶--*/
exec sp_addlogin 'xie', '123456' -- 賬戶名為xie,密碼為123456
--刪除xie賬戶名
exec sp_droplogin 'xie'
/*--在stuDB數據庫中添加兩個用戶(必須存在)--*/
use stuDB
go
exec sp_grantdbaccess 'xie','123456'
go
-- 提示:SQL Server 中的dbo用戶是具有在數據庫中執行所有活動權限的用戶,表示數據庫的所有者(owner),一般來說,
-- 如果創建了某個數據庫,就是該數據庫的所有者,即dbo用戶,dbo用戶是一個比較特殊的數據庫用戶,無法刪除,且此用
-- 戶始終出現在每個數據庫中
/* --給數據庫用戶授權-- */
-- 授權的語法如下
-- grant 權限 [on 表名] to 數據庫用戶
use stuDB
go
grant select,update,insert on stuMarks to xie
grant create table to xie
go