题目要求:
首先建立一个表格,并且一个记录由姓名(该姓名由4个字母组成,第一个字母大写A-Z;后三个字母均为a-z)、性别(男、女)、出生年月日、身高(规定150-185之间的随机数)、体重((身高-100)*2)。创建1000个这样的记录。之后查询出生日为今天人数的姓名,并且查询出姓名中间两个字母为'ab'的姓名。
实现代码如下:
use DBTEST
--创建表的基本语法
/*
create table 表名
(
字段名1 数据类型,
字段名1 数据类型,
字段名1 数据类型
)
*/
--判断表是否存在,如果存在的话,进行删除
if exists(select * from sys.objects where name='tb1' and type='U')
drop table tb1
create table tb1
(
nm varchar(4),
gd varchar(1),
br date,
hg int,
wt int,
ph varchar(20)
)
declare @nm varchar(4),@gd varchar(1) ,@br date,@hg int ,@wt int ,@ph varchar(20)
declare @j int,@k int
set @j=1
while @j<=1000
begin
set @nm =char(rand()*26+65)+char(rand()*26+97)+char(rand()*26+97)+char(rand()*26+97)
set @k=rand()*2+1
set @gd =substring('MF',@k,1)
set @br =dateadd(day,rand()*9495,'1990-1-1')
set @hg =rand()*35+150
set @wt =(@hg-100)*2
set @ph = 'd;\photo\ph'+@nm
insert into tb1 values(@nm ,@gd ,@br,@hg ,@wt ,@ph )
set @j=@j+1
end
select * from tb1
select nm from tb1 where month(br) = month(getdate()) and day(br) = day(getdate())
select nm from tb1 where substring(nm,2,2)='ab'