4.18日数据库上机

CREATE DATABASE jxgl
GO
USE jxgl
Go
Create Table Student
(Sno CHAR(5) NOT NULL PRIMARY KEY(Sno),
 Sname VARCHAR(20),
 Sage SMALLINT CHECK(Sage>=15 AND Sage<=45),
 Ssex CHAR(2) DEFAULT'男' CHECK(Ssex='男' OR Ssex='女'),
 Sdept CHAR(2));
Create Table Course
(Cno CHAR(2) NOT NULL PRIMARY KEY(Cno),
 Cname VARCHAR(20),
 Cpno CHAR(2),
 Ccredit SMALLINT);
 create table sc
(sno char(5) not null constraint s_f foreign key references student(sno),
 cno char(2) not null,
 grade smallint check((grade is null)or(grade between 0 and 100)),
 primary key(sno,cno),
 foreign key (cno) references course(cno));
 insert into student values('98001','钱横',18,'男','CS');
 insert into student values('98002','王林',19,'女','CS');
 insert into student values('98003','李民',20,'男','IS');
 insert into student values('98004','赵三',16,'女','MA');
 insert into Course values('1','数据库系统','5',4);
 insert into Course values('2','数学分析',null,2);
 insert into Course values('3','信息系统导论','1',3);
 insert into Course values('4','操作系统_原理','6',3);
 insert into Course values('5','数据结构','7',4);
 insert into Course values('6','数据处理基础',null,4);
 insert into Course values('7','C语言','6',3);
 insert into sc values('98001','1',87);
 insert into sc values('98001','2',67);
 insert into sc values('98001','3',90);
 insert into sc values('98002','2',95);
 insert into sc values('98002','3',88);

 insert into Student values('98011','张静',27,'女','CS');
  insert into Student values('99201','石科',21,'男','CS')
  insert into Student values('99202','宋笑',19,'女','CS')
  insert into Student values('99203','王欢',20,'女','IS')
  insert into Student values('99204','彭来',18,'男','MA')
  insert into Student values('99205','李晓',22,'女','CS')

 insert into Student(Sno,Sname,Sage) values('98012','李四',16);

 insert into Student values('99010','赵青江',18,'男','CS')
 insert into Student values('99011','张丽萍',19,'女','CH')
 insert into Student values('99012','陈景欢',20,'男','IS')
 insert into Student values('99013','陈婷婷',16,'女','PH')
 insert into Student values('99014','李军',16,'女','EH')


insert into SC
    select sno,cno,null
    from Student,Course
    where Sdept = 'CS' and cno='5';

insert into Student
    select cast(cast(sno as integer)+1 as char(5)),sname+'2',sage,ssex,sdept
    from Student where Sname='赵三';
    go
    select *
    from Student;

    select *from Student

    update Student set sname='李明',Sage=23 where sno='98003'

    select *from Student

    update student set student.Sage = Student.Sage+1
    from (select top(3) * from student order by sno) as stu3
    where stu3.sno=Student.sno;


    select *from Student
    update top(3) percent Student set student.Sage=Student.Sage+1;
    select *from Student

    select *from sc
    update sc 
    set grade=(select avg(grade) from sc where cno='3')
    where sno = '98001' and cno='3'
    select *from sc
    select *from sc
    update sc set grade=0
    where cno='2' and sno in(select sno from Student where sname = '王林')
    select *from sc


    select * into TSC from sc

    select * from TSC


    select *from sc
    delete from sc
    where 'CS'=(select sdept from Student
                where Student.Sno=sc.sno)

    select *from sc

    insert into sc select * from TSC

    delete from sc

    truncate table sc

insert into sc values('99010','1',87)
insert into sc values('99010','3',80)
insert into sc values('99010','4',87)
insert into sc values('99010','6',85)
insert into sc values('99011','1',52)
insert into sc values('99011','2',47)
insert into sc values('99011','3',53)

insert into sc values('99011','5',45)
insert into sc values('99012','1',84)
insert into sc values('99012','4',67)
insert into sc values('99012','5',81)

insert into sc(sno,cno)values('99010','2')
insert into sc(sno,cno)values('99012','3')

select * from sc
--2
select * into TS from Student

delete from TS

select * from TS

--3
insert into SC
    select sno,cno,60
    from Student
    where Sdept = 'IS' and cno='7';
    // 如果from student course 会进行广义笛卡尔积,会导致重复出现;或者使用distinct 来约束
--4 
select * from SC
select * from Student
insert into TS 
        select * 
        from Student
        where Ssex='女' and Sage<=16;


--5 
INSERT INTO TS
SELECT *
FROM STUDENT
WHERE SNO IN
        (SELECT SNO
        FROM SC
        GROUP BY SNO
        HAVING MAX(GRADE)<60
        )
--6
update Student set sname='刘华',Sage=sage+1 where sno='99011'


select * from SC
--7
update sc
set grade =null
where grade<60 and cno in(select cno
                            from Course
                            where Cname='数据库系统')


select * from SC


    --8
    update student set student.Sage = Student.Sage+1
    from (select top(4) * from student order by sno) as stu3
    where stu3.sno=Student.sno;
--9
    update sc set grade=null
    where cno='3' and sno in(select sno from Student where sname = '王林')
    select *from sc
--10
    update sc 
    set grade=grade*1.05
    where grade<(select avg(grade) from sc) and sno in(select sno from Student where ssex = '女')


    select *from sc
--11
    update sc
    set grade = grade*0.98
    where grade<=80 and cno='2'

    update sc
    set grade = grade * 0.99
    where grade >80 and cno='2'
    --12
    select * into t11 from Student
    select * into t12 from Course
    select * into t13 from SC

    select * from sc
    --13
    delete from sc
    where grade IS null

    select * from sc

--14
delete from sc
where sno in (select sno
              from Student
              where Sname='钱横')
--15
delete from sc
where sno ='98005'

delete 
from Student
where sno='98005'

--16
delete from sc
where sno in(select sno
            from Student
            where Sname like '张%')

select * from Student
delete from Student
where Sname like '张%'

--17
delete from student
delete from course

--18
insert into student  select * from t1
insert into sc select * from t2
insert into course  select * from t3
基于MySQL,设计并实现一个简单的旅行预订统。该统涉及的信息有航班、大巴班车、宾馆房间和客户数据等信息。其关模式如下: FLIGHTS (String flightNum, int price, int numSeats, int numAvail, String FromCity, String ArivCity); HOTELS(String location, int price, int numRooms, int numAvail); BUS(String location, int price, int numBus, int numAvail); CUSTOMERS(String custName,custID); RESERVATIONS(String custName, int resvType, String resvKey) 为简单起见,对所实现的应用统作下列假设: 1. 在给定的一个班机上,所有的座位价格也一样;flightNum是表FLIGHTS的一个主码(primary key)。 2. 在同一个地方的所有的宾馆房间价格也一样;location是表HOTELS的一个主码。 3. 在同一个地方的所有大巴车价格一样;location是表 BUS的一个主码。 4. custName是表CUSTOMERS的一个主码。 5. 表RESERVATIONS包含着那些和客户预订的航班、大巴车或宾馆房间相应的条目,具体的说,resvType指出预订的类型(1为预订航班,2为预订宾馆房间,3为预订大巴车),而resvKey是表RESERVATIONS的一个主码。 6. 在表FLIGHTS中,numAvail表示指定航班上的还可以被预订的座位数。对于一个给定的航班(flightNum),数据库一致性的条件之一是,表RESERVATIONS中所有预订该航班的条目数加上该航班的剩余座位数必须等于该航班上总的座位数。这个条件对于表BUS和表HOTELS同样适用。 应用统应完成如下基本功能: 1. 航班,大巴车,宾馆房间和客户基础数据的入库,更(表中的属性也可以根据你的需要添加)。 2. 预定航班,大巴车,宾馆房间。 3. 查询航班,大巴车,宾馆房间,客户和预订信息4. 查询某个客户的旅行线路。 5. 检查预定线路的完整性。 6. 其他任意你愿意加上的功能。 作业检查: 1. 提交源程序,可执行程序,以及程序运行说明。 2. 统分析、设计与实现报告。 3. 考试前检查完毕,延迟拒收。 4. 提交word文件,形式为:学号_姓名
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值