您的位置:首页 > 数据库

(Sql)考试前做的练习题...

2005-07-05 18:13 288 查看
  
--1.创建一个名为school数据库、要求如下:
--有一个primary文件组和两个user_defined文件组;
--第一个user_defined文件组设置为default文件组;
/*
use pubs
drop database school
*/
if exists (select * from master..sysdatabases where name = 'school') drop database school
go
 
create database school
on primary
(
       name             = 'school_db_data',
       filename       ='f:/clin003/school_db_data.mdf',--here
       size         =5,
       filegrowth        =2
 
)
 
log on
(
       name              ='school_db_log',
       filename       ='f:/clin003/school_db_log.ldf',
       filegrowth       =1
)
go-----------------------------------------------
--查询表authors中au_fname列值中以字母a开头的记录(尽可能列出所有方法)use pubs
 
select * from authors
go
select * from authors where au_fname like 'a%'
go
select * from authors where au_fname in  ('a%','A','abraham')
go
select * from authors where au_fname between 'a'and 'b'
 
select * from authors where au_fname >= 'a' and au_fname <'b'
go
 
 
--查询表titles中type类型中后四个字母为cook的所有记录
select * from titles  where type like '%cook'
go
 
--查询表titles中的图书的类型类型种类
select     type from titles group by type
go
--区别 order by 效果
--select   type from titles order by type
 
--(4)列出表titles中图书的出版时间。
select titles.pubdate from titles
 
--(5)按类型和出版社分组统计价格中的最大值和平均价格
--(分别使用group by 和compute by子句)
 
--按类型分组统计价格中的最大值和平均价格(group by)
select        max(titles.price) as 价格中的最大值,-- min(titles.price),
       avg(titles.price) as 价格中的平均价格
              from titles 
              group by type
go
--按出版社分组统计价格中的最大值和平均价格(group by)
select        max(titles.price)       as 价格中的最大值,-- min(titles.price),
       avg(titles.price)        as 价格中的平均价格 ,
        publishers.pub_name        as 出版社
                            from titles , publishers
                            where titles.pub_id = publishers.pub_id
                            group by publishers.pub_name
go
select * from titles--publishers
 
--按类型分组统计价格中的最大值和平均价格(compute by)
use pubs
select type , price ,advance from titles order by type
compute max(price) ,avg(price) by type
go
--按出版社分组统计价格中的最大值和平均价格(compute by)
use pubs
select titles.type, titles.price , publishers.pub_name
                            from titles ,publishers
                            where titles.pub_id = publishers.pub_id
                            order by publishers.pub_name
compute max(titles.price) ,avg(titles.price)   by publishers.pub_name
                            --用by将分条显示汇总信息
go
                     --------------------------------------------------------------------------------------------------------- 
use school
--表名:Products
create table Products
(
       productid            int                  not null,
       productname          varchar(32)          not null,
       descrip              varchar(128)  null  ,
       price                smallmoney          not null ,
       instock              bit                  null  ,
 
)
go
--表名:Sales
create table sales
(
       orderid              int    not null,
       customerid           int     not null,
       ordertotal           money   not null,
       shipmethodvar char(32)not null,
       shipped              bit     null,
 
)
go
 
--表名:SalesDetail
create table SalesDetail
(
       orderid       int           not null,
       productid     int           not null,
       quantity      smallint      not null,
       extendedprice smallmoney    not null
 
)
go
--查看创建成功妹妹有
select   '创建情况:Products' 创建情况
go
sp_help Products
go
select '创建情况:sales' 创建情况
go
 
sp_help sales
go
select '创建情况:SalesDetail' 创建情况
go
 
sp_help SalesDetail
go--------------------------------------------------------------------------------4、创建表xue_sheng
/*
(学号,         姓名, 性别,出生日期,政治面貌,           籍贯,    电话      入学成绩,系别,班级)
j2002010101        张华       女       1982-12-18       团员       武汉       13845678912    879.50
*/
 
use school
create table xue_sheng
(
       学号 char(12) not null,
       姓名 char(8) not null,
       性别 char(2) not null,
       出生日期 smalldatetime not null,
       政治面貌 char(4) not null,
       籍贯       char(8) not null,
       电话   char(12) null ,
       入学成绩 int null,   
)
go
select * from sheet1$
go
select * from xue_sheng
go
--为多行记录填充数据
insert into xue_sheng --values
select * from sheet1$
go
 
 
--创建表cheng_ji
/* 学号 ,          数据库, 数据结构 , 网络技术*/
create table cheng_ji
(     学号 char(12) not null,
       数据库 integer not null,
       数据结构 integer not null,
       网络技术 integer not null,
       总成绩    as 数据库+数据结构+网络技术
)
go
 
select * from cheng_ji
go
drop table cheng_ji
select * from sheet1$test
go
--为多行记录填充数据
insert into cheng_ji select * from sheet1$test
go
--利用excel 导入数据
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息