1. 用sql通配符写一个格式验证某标识符的合法性:
   1)该标识符长度不限。
   2)不能以数字作为首字符。
   3)中部含一个"-"作为分隔符。
   4)"-"后第二个字符为大写英文字母,以字符"$"结尾。
create table isidentity
(id varchar(20)check(id like'[^0-9]%-[A-Z]%$'))
insert into isidentity(id)values('a-A$')
insert into isidentity(id)values('1a-3$')
--INSERT 语句与 COLUMN CHECK 约束 'CK__isidentity__id__2B3F6F97' 冲突。该冲突发生于数据库 'a',表 'isidentity', column 'id'。
2. 现有三张表,表结构如下。
   请编写sql脚本查询出至少选了2门课的学生姓名。
 
 学生表 XUESHENG
 字段:
  s_id int ,
  s_name varchar

   课程表 KECHENG
 字段:
  c_id int
  c_name varchar
 
 选课表 XUANKE
 字段: 
  id int
  s_id int, c_id int
select * from XUANKE
select * from KECHENG
select * from XUESHENG
select a.s_name from xuesheng as a ,XUANKE as b where a.s_id=b.s_id group by a.s_name having count(a.s_name)>=2
3. 请编写查看本周第一天日期的脚本。
SELECT datename(dw,DATEADD(wk,DATEDIFF(wk,0,getdate()),0))
4. 现有学员信息表,结果及记录如下,
 STUNO STUNAME STUBIRTHDAY
 1, 李, 2000-01-01 00:00:00
 2, 海, 2000-01-01 00:00:00
 3, 西, 1989-01-01 00:00:00
 5, x, 1999-01-01 00:00:00
   1)请检索生日在1999年及以后的学员。
   2)请检索生日在2000年的学员。
create table ccc
(stuno int identity(1,1),
 stuname varchar(10),
 stubirthday datetime
)
insert into ccc(stuname,stubirthday)
select '李','2000-01-01 00:00:00'union
select '海','2000-01-01 00:00:00'union
select '西','1989-01-01 00:00:00'union
select 'x','1999-01-01 00:00:00'
select stuno,stuname,stubirthday from ccc where datepart(yy,stubirthday)>=1999
select stuno,stuname,stubirthday from ccc where datepart(yy,stubirthday)=2000
5. 现有CARD表,由一个字段构成,字段名:ID
   请将ID字段值的所有小写字母替换成大写,并将所有'0'替换成'零'。
 例如,a0 -> A零
create table card
(id varchar(20))
insert into card (id)
select 'dfdfo'union
select 'fgfg0ll'union
select 'hhjh0ghg'union
select 'aa00'union
select 'o0ofg'
update card
set id=upper(replace(id,'0','零'))
select id from card
6. 在数据库表中有以下字符数据,如:
 13-a、1-d、133-c、15-ab、1-c、13-E
   请编写SQL语句对该字段进行排序,先按前半部分的数字顺序排列,再按后半部分的字母倒序排。
   排列成功后结果示例如下:
 1-d、1-c、13-E、13-a、15-ab、133-c

   数据库表名:order;字段名:id
create table order1
(id varchar(10))
insert into order1(id)
select '13-a'union
select '1-d'union
select '133-c'union
select '15-ab'union
select '1-c'union
select '13-E'
select id from order1 order by Convert(int,left(id,charindex('-',id)-1)),(ASCII(right(id,len(id)-charindex('-',id)))) desc
--select  left(id,charindex('-',id)-1)from order1
--select  ASCII(right(id,len(id)-charindex('-',id)))from order1
--结果:
1-d
1-c
13-a
13-E
15-ab
133-c
7. 现有salary表,表结构如下。该表记录了公司内所有员工的薪水发放记录。
   请编写脚本,检索月薪介于500-800的员工姓名。

 SALARY表结构如下:
   FID int  PRIMARY KEY, --记录id
   FName varchar(10), --员工姓名
   FDate datetime,  --日期
   FSalary float  --金额
create table salary
(
fid int primary key,
fname varchar(10),
Fdate datetime,
fsalary float
)
insert into salary (fid,fname,fdate,fsalary)
select 1,'a','2008-11-1',100 union
select 2,'a','2008-11-15',300 union
select 3,'a','2008-11-30',100 union
select 4,'b','2008-11-1',300 union
select 5,'b','2008-11-1',400 union
select 6,'c','2008-11-1',100 union
select 7,'c','2008-11-1',600 union
select 8,'c','2008-11-1',200 union
select 9,'d','2008-11-1',1000 union
select 10,'d','2008-11-1',100 union
select 11,'e','2008-11-1',900 union
select 12,'e','2008-11-1',100
请编写脚本,检索月薪介于500-800的员工姓名。
select * from salary
select sum(fsalary) from salary group by fname
500.0
700.0
900.0
1100.0
1000.0
select fname from salary group by fname,datepart(yy,fdate),datepart(mm,fdate)having sum(fsalary)>=500 and sum(fsalary)<=800
8. 现有车票预售系统数据库的三张表,如下:
 STATION,存储车站信息
    TRAIN,存储火车信息
 PASS_STATION,存储途经车站信息
   建表脚本见text.sql。

   请从上述三张中,统计出下面的信息,存放到一张输出表中。
  
 车次, 总里程, 发车时间, 到站时间, 起始站, 终止站
select distinct t1.train_name as 车次,leave_time as 发车时间,arrival_time as 到站时间 ,distance as 总里程, t1.station_name as 起始站,t2.station_name as 终止站 from(select train_id,leave_time,distance,station_id from pass_station where pass_xuh=0)a1,
(select train_id,arrival_time,station_id from pass_station where pass_xuh=100)a2
,(select station_name,train_name,station_id,train_id  from station as b1
,(select train_name,start_sta_id,train_id from train)a3 where a3.start_sta_id=b1.station_id) t1
,(select station_name ,station_id from station as b2
,(select end_sta_id from train)a4 where a4.end_sta_id=b2.station_id) t2
where a1.train_id=a2.train_id and t1.train_id=a2.train_id and t1.station_id=a1.station_id  and a2.station_id=t2.station_id
9. 现有两张表,表AAA和表BBB,结构及记录如下。
 表一:AAA
 商品名称mc   商品总量sl
    A          100
    B          120

 表二:BBB
 商品名称mc   出库数量sl
     A          10
     A          20
     B          10
     B          20
     B          30

   请用一条Transact-SQL语句算出商品A,B目前还剩多少?
select b.mc,(sl- bb) as '目前还剩多少?' from aaa ,(select sum(sl)as bb,mc from bbb group by mc)as b where aaa.mc=b.mc
10. (可选)现有员工信息表(employee),结构如下
 员工号(ID),姓名(name),年龄(age),文化程度(wh)
    包括四种情况(本科以上,大专,高中,初中以下)。
    请根据年龄字段查询统计:
    表中文化程度为本科以上,大专,高中,初中以下,各有多少人?
    占总人数多少?

 学历       年龄      人数        百分比
 本科以上   20        34           14
 大专       20        33           13
 高中       20        33           13
 初中以下   20        100          40
 初中以下   21        50           20
insert into employee(name,age ,wh)
select 'zzsshg',40,'大专'union
select 'zzsa',34,'本科'
select * from employee
select distinct aaa.wh as 学历,c as 年龄,a as 人数,Convert(varchar(10),a*100/bb.b)+'%' as '百分比'
from employee as aaa ,
(select wh,count(*)as a ,age as c from  employee where age>=20 and age<=21 group by wh,age)as aa
,(select count(*)as b from employee)as bb where aa.wh=aaa.wh group by aa.a,aaa.wh,bb.b,c