zoukankan      html  css  js  c++  java
  • sql server 2005 基础实验

    use master
    go
    if exists(select name from sys.databases where name=N'Studentselect')
    drop database Studentselect
    go

    create database Studentselect
    on primary
    (
     name=Studentselect,
     filename='E:\SQL Server 2005\Studentselect_data.mdf',
     size=4mb,
     maxsize=10MB,
     filegrowth=1MB
    )
    log on
    (
     name=Studentselect_log,
     filename='E:\SQL Server 2005\Studentselect_log.ldf',
     size=1MB,
     maxsize=6MB,
     filegrowth=1%

    )

    go
    create table student
    (
     sno char(8) primary key,
     sname char(8) not null,
     age int,
     sex char(2),
     dept varchar(50)

    )
    create table course
    (
     cno char(4) primary key,
     cname char(20) not null,
     credit float,
     pcno varchar(20),
     describe varchar(100)
    )


    create table SC
    (
     sno char(8),
     cno char(4),
     grade float,
     primary key(sno,cno),
     foreign key (sno) references student(sno),
     foreign key (cno) references course(cno)
    )

    create table  SCC
    (
     sno char(8),
     cno char(4),
     grade float,
     unique(sno,cno),
     foreign key (sno) references student(sno),
     foreign key (cno) references course(cno)

    )

    create table  SCCCCCC
    (
     sno char(8),
     cno char(4),
     grade float,
     
     foreign key (sno) references student(sno),
     foreign key (cno) references course(cno)

    )

    insert into student(sno,sname,age,sex,dept)
    values('09001101','张林',18,'男','计算机系')
    insert into student(sno,sname,age,sex,dept)
    values('09001102','程明',18,'男','计算机系')
    insert into student(sno,sname,age,sex,dept)
    values('09001103','王艳',19,'女','计算机系')
    insert into student(sno,sname,age,sex,dept)
    values('09001104','严平平',20,'女','计算机系')
    insert into student(sno,sname,age,sex,dept)
    values('09001105','王洪敏',19,'女','信息管理系')
    insert into student(sno,sname,age,sex,dept)
    values('09001106','孙祥新',18,'男','信息管理系')
    insert into student(sno,sname,age,sex,dept)
    values('09001107','吕占英',19,'女','信息管理系')
    insert into student(sno,sname,age,sex,dept)
    values('09001108','李义',19,'男','机械工程系')
    insert into student(sno,sname,age,sex,dept)
    values('09001109','牟万里',18,'男','机械工程系')
    insert into student(sno,sname,age,sex,dept)
    values('09001110','刘丽霞',20,'女','机械工程系')


    insert into course(cno,cname,credit,pcno,describe )
    values ('0101','计算机基础',5,0101,'可自学')
    insert into course(cno,cname,credit,pcno,describe )
    values ('0102','C++程序设计',4,null,'可自学')
    insert into course(cno,cname,credit,pcno,describe )
    values ('0206','离散数学',4,0102,'可自学')
    insert into course(cno,cname,credit,pcno,describe )
    values ('0208','数据结构',4,0101,'可自学')
    insert into course(cno,cname,credit,pcno,describe )
    values ('0209','操作系统',4,0101,'可自学')
    insert into course(cno,cname,credit,pcno,describe )
    values ('0210','微机原理',5,0101,'可自学')
    insert into course(cno,cname,credit,pcno,describe )
    values ('0211','图形学',3,0102,'可自学')
    insert into course(cno,cname,credit,pcno,describe )
    values ('0212','数据库原理',4,0102,'可自学')
    insert into course(cno,cname,credit,pcno,describe )
    values ('0301','计算机网络',3,0102,'可自学')
    insert into course(cno,cname,credit,pcno,describe )
    values ('0302','软件工程',2,0102,'可自学')


    insert into SC(sno,cno,grade)
    values ('09001101','0101',68)
    insert into SC(sno,cno,grade)
    values ('09001101','0206',76)
    insert into SC(sno,cno,grade)
    values ('09001103','0101',62)
    insert into SC(sno,cno,grade)
    values ('09001106','0209',75)
    insert into SC(sno,cno,grade)
    values ('09001106','0210',77)
    insert into SC(sno,cno,grade)
    values ('09001106','0212',75)
    insert into SC(sno,cno,grade)
    values ('09001106','0301',87)
    insert into SC(sno,cno,grade)
    values ('09001108','0101',68)
    insert into SC(sno,cno,grade)
    values ('09001109','0212',88)
    insert into SC(sno,cno,grade)
    values ('09001109','0302',76)
    insert into SC(sno,cno,grade)
    values ('09001110','0101',66)
    insert into SC(sno,cno,grade)
    values('09001109','0101',88)

    alter table SCCCCCC
    ADD constraint sxx
    unique(sno,cno)

    alter table SCCCCCC
    add  constraint unique_cno
    unique nonclustered(sno,cno)

    alter table SC
    add constraint sccc foreign
     key (sno) references student(sno)
    on delete cascade

    alter table student
    add constraint
     age check(age>10)
    alter table course
    add constraint credit check(credit<8)

    alter table student
    add constraint sx check(sex
     in('男','女'))
     
     create default department_default as 'student'
     sp_bindefault department_default, 'student.dept'
     sp_unbindefault 'student.dept'
     drop default department_default
     
     alter table student
     add class varchar(50)
     alter table student
     alter column class varchar(20)
     alter table student
     drop column class
     alter table student
     drop column sno
     select cno from course where cname='离散数学'
     select sno from SC where cno='0206'
     select sno,sname from student where sno='09001101'
     select student.sno,student.sname from student
    join
    SC
    on SC.sno=student.sno
    join
    course
    on course.cno=SC.cno
    where course.cname='离散数学'
    select student.sno,student.sname from student
    join
    SC
    on SC.sno=student.sno and SC.cno='0101'
    where
     SC.grade>(select grade from SC join student on SC.sno=student.sno and SC.cno='0101'
      where student.sname='张林')
    select student.sno,student.sname,student.age,student.dept from student
    where student.age<(select min(student.age) from student where dept='计算机系')
    and
    student.dept not like
     '计算机系'

    select student.sno,student.sname from student
    join
    SC
    on SC.sno=student.sno
    join
    course
    on course.cno=SC.cno and course.cname='数据库原理'
    where SC.grade=(select SC.grade from SC join student on student.sno=SC.sno and student.sname='王洪敏' join course on course.cno=SC.cno and course.cname='数据库原理')


    select student.sno,student.sname from student
    join
    SC
    on SC.sno=student.sno and
     SC.cno not like '0206'
     
     select student.sno,student.sname from student
    join
    SC
    on SC.sno=student.sno and SC.cno in(select cno from course)

    select student.sno,student.sname from student where student.sno in(
     )
    select SC.cno from SC
    group by cno
    select SC.sno from SC
    group by sno
    select sno,count(cno) as cout from SC group by sno
    select count(cno) from course
    /*
    select student.sno,student.sname from student
    join
    (select sno,count(cno) as cout from SC group by sno)
    on sc.sno=student.sno
    join
    (select count(cno) as cout from course)
    on cs.cout=sc.cout
    */
    --这个例子应该说明,你从一个表中查询或者其他操作,得到一个表后,这个表就是另外一个表 用as重新命名这个表名
    select student.sno,student.sname from student
    join
    (select sno,count(cno) as cout from SC group by sno) as sc
    on sc.sno=student.sno
    join
    (select count(cno) as cout from course) as cs
    on cs.cout=sc.cout
    select student.sno,student.sname,SC.grade from student,SC
    where student.sno=SC.sno and SC.cno=(select cno from course where cname='计算机基础') and SC.grade>(select avg(grade) from SC where SC.cno=(select cno from course where cname='计算机基础'))
    -- 如何优化这个查询呢,让其简洁
    select student.sno,student.sname,SC.grade from student
    join
    (select sno,cno,grade from SC where cno=(select cno from course where cname='计算机基础')) as sc
    on sc.sno=student.sno and sc.grade>(select avg(sc.grade) from sc)


    select SC.sno,SC.cno,SC.grade from SC where SC.cno=(select cno from course where cname='计算机基础')

    select dept,count(sno) as cout from student
    group by dept
    order by cout desc


    create view vstu_grade
    as
    select student.sno,student.sname,course.cname,SC.grade
    from student,course,SC
    where student.sno=SC.sno and SC.cno=course.cno

    --过程'vstu_grade' 的请求失败,因为 'vstu_grade' 是 视图 对象。

    --exec vstu_grade

    create index X_dept on student(dept)

    exec sp_helpindex student

    drop index X_dept on student--猜的,书上P32有问题?

    alter table student --不能这样写
    drop index X_dept

    alter table student
    drop index PK_primary key_1(PK_student_7C8480AE)--删除主键有问题

    --游标的作用

    declare cs_cursor scroll cursor
    for
    select sno,sname,age,sex,dept from student order by sno
    for read only
    open cs_cursor
    fetch from cs_cursor--会一条一条进行下去
    fetch last from cs_cursor--表中最后一条记录
    fetch first from cs_cursor--第一条记录
    fetch prior from cs_cursor--当前行前面一行
    fetch absolute 2 from cs_cursor--第二行
    fetch relative 2 from cs_cursor--当前行的第二行,是一个动态的往下读取的游标

    close cs_cursor
    deallocate cs_cursor
    select * from SC
    exec proCharg '09001101'

    sp_addlogin 'abc' '123'

    backup database Studentselect
    to disk='d:\1002\studentselect.bak'
    with format

    restore database Studentselect
    from disk='d:\1002\studentselect.bak'


    select student.sno,student.sname from student
    join SC
    ON SC.sno=student.sno and student.sname not like '王洪敏'
    join course
    on course.cno=SC.cno
    where course.cname='数据库原理' and SC.grade=(select grade from SC where course.cno=SC.cno and SC.sno=(select sno from student where sname='王洪敏'))

  • 相关阅读:
    Selenium(十五)---JavaScript的调用
    Selenium(十三)---文件下载
    Selenium(十四)----上传文件
    Selenium(十二)---警告框处理
    Selenium(十一)---获取url与title
    Selenium(十)---模拟键盘操作
    Selenium(九)---模拟鼠标操作
    iOS关于RunLoop和Timer
    iOS关于应用内分享
    iOS内存错误EXC_BAD_ACCESS的解决方法(message sent to deallocated instance)
  • 原文地址:https://www.cnblogs.com/woshare/p/2776887.html
Copyright © 2011-2022 走看看