首页
IT
登录
6mi
u
盘
搜
搜 索
IT
使用SQL创建库、表、约束
使用SQL创建库、表、约束
xiaoxiao
2021-12-14
21
一、创建数据库
create
database stuDB
on
primary
-- 默认就属于
primary
文件组,可省略
(
/*--数据文件的具体描述--*/
name=
'stuDB_data'
,
-- 主数据文件的逻辑名称
filename=
'D:\stuDB_data.mdf'
,
-- 主数据文件的物理名称
size=
5
mb,
--主数据文件的初始大小
maxsize=
100
mb,
-- 主数据文件增长的最大值
filegrowth=
15
%
--主数据文件的增长率
)
log
on
(
/*--日志文件的具体描述,各参数含义同上--*/
name=
'stuDB_log'
, filename=
'D:\stuDB_log.ldf'
, size=
2
mb, filegrowth=
1
mb )
二、删除数据库
use master
-- 设置当前数据库为master,以便访问sysdatabases表
go if exists(
select
*
from
sysdatabases
where
name=
'stuDB'
)
drop
database
stuDB
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
转载请注明原文地址: https://ju.6miu.com/read-970034.html
专利
最新回复
(
0
)