創(chuàng)建和刪除數(shù)據(jù)庫sql及表的語句
創(chuàng)建和刪除數(shù)據(jù)庫sql及表的語句
一、創(chuàng)建數(shù)據(jù)庫的SQL語句:
create database stuDB?
on ?primary ? ? ? ? ? ? ? ? ?-- 默認(rèn)就屬于primary文件組,可省略
(
/*--數(shù)據(jù)文件的具體描述--*/
? ? name='stuDB_data', ?-- 主數(shù)據(jù)文件的邏輯名稱
? ? filename='D:stuDB_data.mdf', -- 主數(shù)據(jù)文件的物理名稱
? ? size=5mb, ? ? ? ? ? ? ??--主數(shù)據(jù)文件的初始大小
? ? maxsize=100mb, ?? -- 主數(shù)據(jù)文件增長的最大值
? ? filegrowth=15% ? ? ?--主數(shù)據(jù)文件的增長率
)
log on
(
/*--日志文件的具體描述,各參數(shù)含義同上--*/
? ? name='stuDB_log',
? ? filename='D:stuDB_log.ldf',
? ? size=2mb,
? ? filegrowth=1mb
)
二、?刪除數(shù)據(jù)庫:SQL Server將數(shù)據(jù)庫的清單存放在master系統(tǒng)數(shù)據(jù)庫的sysdatabases表中,只需要查看該表
是否存在于該數(shù)據(jù)庫中就可以了,語句如下
use?master?--?設(shè)置當(dāng)前數(shù)據(jù)庫為master,以便訪問sysdatabases表 go if?exists(select?*?from?sysdatabases?where?name='stuDB') drop?database?stuDB go
三、創(chuàng)建表和刪除表的SQL語句如下:
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"列為自動編號, 也稱為標(biāo)識列?
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數(shù)據(jù)庫中添加兩個用戶(必須存在)--*/
use stuDB
go
? exec sp_grantdbaccess 'xie','123456'
go
-- 提示:SQL Server 中的dbo用戶是具有在數(shù)據(jù)庫中執(zhí)行所有活動權(quán)限的用戶,表示數(shù)據(jù)庫的所有者(owner),一般來說,
-- 如果創(chuàng)建了某個數(shù)據(jù)庫,就是該數(shù)據(jù)庫的所有者,即dbo用戶,dbo用戶是一個比較特殊的數(shù)據(jù)庫用戶,無法刪除,且此用
-- 戶始終出現(xiàn)在每個數(shù)據(jù)庫中
/* --給數(shù)據(jù)庫用戶授權(quán)-- */
-- 授權(quán)的語法如下
-- grant 權(quán)限 [on 表名] to 數(shù)據(jù)庫用戶
use stuDB
go
? grant select,update,insert on stuMarks to xie
? grant create table to xie
go