sql server 2005 基本操作语句
--创建数据库
create database abc
--重命名数据库,第一个是老数据库名称,第二个是新数据库的名称
sp_renamedb abc,zhou
--使用数据库
use abc
--查询数据库信息
sp_helpdb abc
--删除数据库
drop database abc
--创建数据库
--一个数据文件mdf,一个日志文件ldf
create database zhou
on
(
name = abc_dat,
filename = 'd:\\job\abc_dat.mdf',
size = 10,
maxsize = 50,
filegrowth = 5
)
log on
(
name =abc_log,
filename = 'd:\\job\abc_log.ldf',
size = 5,
maxsize = 25,
filegrowth = 5
)
--创建架构
create schema zhou
--创建表
create table zhou.infor
(
sno char(4),
sname char(8),
sex char(2),
birthday datetime,
age int,
telephone char(11) unique--唯一约束,不能出现相同号码,否则无效,
address varchar(100),
)
--增加记录,值与字段必须是一一对应的
insert into zhou.infor
values
('0001','周亚朋','男','2010-05-24','0','0797968689','长沙民政职业技术学院')
查询结果
select*from zhou.infor
sno sname sex birthday age telephone address
0001 周亚朋 男 2010-05-24 00:00:00.000 0 0797968689 长沙民政职业技术学院
主键约束
sno char(4) primary key,--表示主键,不会重复
--增加一个约束,只能是男或女,默认是女
sex char(2)
constraint conSex--约束名称
check(sex in ('男','女'))--检查输入的信息是否符合
constraint conDESex default '女',
外键约束
create table zhou.stu
(
sno char(4)
constraint a--定义外键名
foreign key references--外键约束,zhou.stu中sno必须符合zhou.infor(sno),否则无效
zhou.infor(sno),
stusubject char(20),
score dec(3,1)
)
唯一约束
telephone char(11) unique--唯一约束,不能出现相同号码,否则无效
三
create schema student
create database SM
--创建规则
create rule rulTelephone
as @stelephone like
'[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
--绑定规则
sp_bindrule 'rulTelephone','student.stuInfor.stelephone'
-- 解除规则
sp_unbindrule 'student.stuInfor.stelephone'
create table student.stuInfor
(
sno char(4) constraint b--定义主键名
primary key,--表示主键
sname char(8) unique,
--增加一个约束,只能是男或者是女,默认是女
ssex char(2)
constraint conSex
check(ssex in ('男','女'))
constraint conDeSex default'女',
sbirthday datetime,
stelephone char(11),-- constraint chkType
-- check(stelephone like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),
saddress varchar(100),
)
--对表重命名
sp_rename 'student.test','stuInfor'
--删除表
drop table student.stuInfor
--增加记录,值与字段必须是一一对应的
insert into student.stuInfor
values
('004','tzs','女','2010-05-24','1234567b','长沙民政职业技术学院')
--删除表
drop table student.stuInfor
--查询表
select*from student.stuInfor
--创建数据类型
create type sectype
from char(20) not null;
--创建一个成绩表
create table student.stuScore
(
Snumber int identity(1,1) primary key,
sno char(4) foreign key references
student.stuInfor(sno),
stuSubject char(20),
score dec(4,1) constraint chkRang
check(score between 0 and 100),
sec sectype,
)
--删除一列
alter table student.stuScore
drop COLUMN sec
--删除约束
alter table student.stuScore
drop constraint chkRang
--增加约束
alter table student.stuScore
add constraint chkRang
check(score between 0 and 100)
--增加一列
alter table student.stuScore
add bak varchar(100)
--修改列
alter table student.stuScore
alter column bak varchar(200)
drop table student.stuScore
insert into student.stuScore
values
('004','computer',89,'第一学期')
select * from student.stuScore
delete from student.stuScore where
Snumber>5
1万+




被折叠的 条评论
为什么被折叠?



