本次考核涉及到:
总结:
任务1:
建立一个数据库名字为:compterschool/*
--要求:
建立一个数据库名字为:compterschool
建立一个表:你姓名的全拼
字段有:
st_id
st_name
st_gender
st_birth
st_tel
st_addr
执行select * from
*/
--创建数据库
go
create database ComputerSchool
go
--创建数据表
go
use ComputerSchool
create table YUZHIWEI
(
st_id uniqueidentifier,
st_name varchar(20),
st_gender varchar(6),
st_birth varchar(10),
st_tel varchar(14),
st_addr varchar(50)
)
go
执行查询操作
/*
--要求:
修改st_name字段和st_tel字段为:nm_st和st_telephone
并插入六条记录
执行查询操作
*/
use ComputerSchool
--修改字段
go
exec sp_rename 'YUZHIWEI.st_name','nm_st','column'
exec sp_rename 'YUZHIWEI.st_tel','st_telephone','column'
go
--插入六条记录
go
insert into YUZHIWEI
values
('001','zhangsan','male','19961025','13718467856','jiaozuo'),
('002','lisi','female','19941024','13718467856','zhengzhou'),
('003','wangwu','male','19951125','13718467856','xinxiang'),
('004','wanger','female','19950125','13718467856','kaifeng'),
('005','erhuo','male','19950205','13718467856','xinyang'),
('006','shabi','female','19960515','13718467856','zhumadian')
go
go
select * from YUZHIWEI
go
查询所有插入的数据
/*
要求:
删除字段st_addr
清空数据表,然后插入10万条记录,其中每条记录要求如下:
st_id 由newid()函数生成
nm_st列为两个大写字母
st_gender列为'F'或'M'
st_birth列位于1990.01.01-2010.12.31之间
查询所有插入的数据
*/
use ComputerSchool
--函数名称:Chose
--函数功能:从F和M中随机选择一个
go
create function SelectGender(@num int)
returns varchar
as
begin
if @num%2=0
return 'F'
return 'M'
end
go
--函数名称:getDateBetween()
--函数功能:返回两个日期之间的日期
go
create function getDateBetween(@begindate date,@enddate date,@num float)
returns date
as
begin
declare @d int
declare @incr int
set @d=datediff(day,convert(date,@begindate),convert(date,@enddate))
set @incr=ceiling(@num*@d)
return dateadd(day,@incr,convert(date,@begindate))
end
go
--删除st_addr列
go
alter table YUZHIWEI
drop column st_addr
go
--删除所有记录
go
delete from YUZHIWEI
go
/*
--要求:插入100000条记录
每条记录要求如下:
st_id 由newid()函数生成
nm_st列为两个大写字母
st_gender列为'F'或'M'
st_birth列位于1990.01.01-2010.12.31之间
*/
go
declare @count int
select @count=0
while @count<100000
begin
insert into YUZHIWEI(st_id,nm_st,st_gender,st_birth)
values(
newid(),
char(65+ceiling(Rand()*25))+char(65+ceiling(Rand()*25)),
dbo.SelectGender(ceiling(rand()*10)),
dbo.getDateBetween('1990-01-01','2010-12-31',rand())
)
set @count=@count+1
end
go
go
select *
from YUZHIWEI
go