sql修仙吧 关注:4贴子:81
  • 9回复贴,共1

Orcle刷题日志2017/11/27

只看楼主收藏回复

create user sonic121 identified by 121;
grant connect,resource to sonic121;
create table categorys(
categoryID number(10,2),
categoryName Varchar2(50) NOT NULL enable,
PRIMARY KEY(categoryID)
);
create table publishers(
PubCode Char(4),
PubName Char(50) NOT NULL enable,
Phone Char(50),
Address Varchar2(100),
PRIMARY KEY(PubCode),
UNIQUE(PubName)
);
create table titles(
TitleCode Char(6),
TitleName Varchar2(80) NOT NULL enable,
Author Char(8) NOT NULL enable,
CategoryID number,
PubCode Char(4),
Price number,
PubDate Date,
PRIMARY KEY(TitleCode),
FOREIGN KEY (CategoryID) REFERENCES categorys(CategoryID),
FOREIGN KEY (PubCode) REFERENCES publishers(PubCode),
CHECK (Price>0)
);


IP属地:广东1楼2017-11-27 09:24回复
    终极第一部分完美整合大部分代码版V1.00
    (自建用户代码)
    create table category
    (
    CategoryID number primary key,
    CategoryName varchar2(50) not null
    )
    create table publishers
    (
    PubCode char(4) primary key,
    PubName char(50) unique,
    Phone char(15),
    Address varchar2(100)
    )
    create table titles
    (
    TitleCode char(6) primary key,
    TitleName varchar2(80) not null,
    Auther char(8) not null,
    CategoryID number constraint FK_CID references category(categoryID),
    PubCode char(4) constraint FK_PC references publishers(PubCode),
    Price number check(Price>0),
    PubDate date
    )
    Alter table category add tele char(20);
    Alter table titles modify author char(15);
    Alter table category drop column tele;
    create sequence cate_seq
    increment by 1
    start with 1;
    create index title_idx on titles(TitleName);
    create synonym title for titles;
    INSERT INTO category VALUES(cate_seq.nextval,'C#基础');
    INSERT INTO category VALUES(cate_seq.nextval,'数据库');
    INSERT INTO category VALUES(cate_seq.nextval,'网站开发');
    INSERT INTO category VALUES(cate_seq.nextval,'计算机基础理论');
    INSERT INTO category VALUES(cate_seq.nextval,'JAVA');
    INSERT INTO category VALUES(cate_seq.nextval,'电子商务');
    INSERT INTO category VALUES(cate_seq.nextval,'移动互联网');
    insert into publishers values('ABYC','白云出版社','020-87056743','广州市市政大街10号');
    insert into publishers values('AXXW','新希望出版社','020-84756554','广州市天府大道22号');
    insert into publishers values('BDDA','东大阿尔出版社','024-73217876','沈阳市铁西路33号');
    insert into publishers values('DDXC','电信出版社','0755-67866755','深圳市深东路120号');
    insert into publishers values('DGZC','贵族出版社','0755-56564367','深圳市深东路1号');
    insert into publishers values('EMDC','牡丹出版社','023-67548956','重庆火锅街10号');
    insert into publishers values('HDJT','大家庭出版社','010-67573321','北京市部外大街3号');
    insert into titles values('T001','C#开发技巧','王江江','1','ABYC','34.50','01-6月-11');
    insert into titles values('T002','网页设计与制作教程','朱丽叶','3','BDDA','35.00','22-3月-09');
    insert into titles values('T003','C#设计模式','刘丽','1','DGZC','50.00','26-12月-10');
    insert into titles values('T004','数据库原理','张田田','2','AXXW','45.30','20-2月-08');
    insert into titles values('T005','Java程序设计基础','麦考林','5','DGZC','32.00','12-3月-10');
    insert into titles values('T006','Oracle数据库初学者指南','斯考特','2','BDDA','65.20','17-5月-09');
    insert into titles values('T007','Android程序设计与开发','张楚贵','7','AXXW','55.00','03-3月-12');
    system用户代码
    create user chrieu identified by 123456;
    grant connect,resource to chrieu;
    grant create session to chrieu;
    grant create sequence to chrieu;
    grant create any index to chrieu;
    grant create synonym to chrieu;


    2楼2017-11-27 10:15
    回复
      2026-03-08 17:52:38
      广告
      不感兴趣
      开通SVIP免广告
      V1.01版更正内容
      ——修改number类型错误
      insert into titles values('T001','C#开发技巧','王江江',1,'ABYC',34.50,'01-6月-11');
      insert into titles values('T002','网页设计与制作教程','朱丽叶',3,'BDDA',35.00,'22-3月-09');
      insert into titles values('T003','C#设计模式','刘丽',1,'DGZC',50.00,'26-12月-10');
      insert into titles values('T004','数据库原理','张田田',2,'AXXW',45.30,'20-2月-08');
      insert into titles values('T005','Java程序设计基础','麦考林',5,'DGZC',32.00,'12-3月-10');
      insert into titles values('T006','Oracle数据库初学者指南','斯考特',2,'BDDA',65.20,'17-5月-09');
      insert into titles values('T007','Android程序设计与开发','张楚贵',7,'AXXW',55.00,'03-3月-12');


      3楼2017-11-27 11:19
      回复
        --(2)修改和删除表数据
        --a
        update publishers set phone = '020-33338888' where pubname='白云出版社';
        update publishers set address = '广州市革新路100号' where pubname='白云出版社';
        --b
        update titles set price = '49' where titlename = '数据库原理';
        --c
        delete from titles where TitleCode = 'T007';
        select * from titles;
        rollback;
        --(3)创建视图
        --a
        create view view1
        as
        select TitleName,Author,Price from titles;


        IP属地:广东4楼2017-11-27 11:19
        回复
          --在System打的
          --创建视图权限
          grant create view to Winko;


          IP属地:广东5楼2017-11-27 11:22
          回复
            --------------创建视图--------------
            --(3)创建视图
            --a
            create view titles_view1
            as
            select TitleName,Author,Price from titles;
            select * from titles_view1;
            --b
            create view titles_view2
            as
            select p.pubname,t.titleName,t.price from publishers p full join
            titles t on p.pubcode = t.pubcode
            where t.pubdate > to_date('20110101','YYYYMMDD')
            select * from titles_view2;


            IP属地:广东6楼2017-11-27 11:30
            回复
              V1.02终极整合版本第一部分代码汇总
              create table category
              (
              CategoryID number primary key,
              CategoryName varchar2(50) not null
              )
              create table publishers
              (
              PubCode char(4) primary key,
              PubName char(50) unique,
              Phone char(15),
              Address varchar2(100)
              )
              create table titles
              (
              TitleCode char(6) primary key,
              TitleName varchar2(80) not null,
              Auther char(8) not null,
              CategoryID number constraint FK_CID references category(categoryID),
              PubCode char(4) constraint FK_PC references publishers(PubCode),
              Price number check(Price>0),
              PubDate date
              )
              Alter table category add tele char(20);
              Alter table titles modify author char(15);
              Alter table category drop column tele;
              create sequence cate_seq
              increment by 1
              start with 1;
              create index title_idx on titles(TitleName);
              create synonym title for titles;
              INSERT INTO category VALUES(cate_seq.nextval,'C#基础');
              INSERT INTO category VALUES(cate_seq.nextval,'数据库');
              INSERT INTO category VALUES(cate_seq.nextval,'网站开发');
              INSERT INTO category VALUES(cate_seq.nextval,'计算机基础理论');
              INSERT INTO category VALUES(cate_seq.nextval,'JAVA');
              INSERT INTO category VALUES(cate_seq.nextval,'电子商务');
              INSERT INTO category VALUES(cate_seq.nextval,'移动互联网');
              insert into publishers values('ABYC','白云出版社','020-87056743','广州市市政大街10号');
              insert into publishers values('AXXW','新希望出版社','020-84756554','广州市天府大道22号');
              insert into publishers values('BDDA','东大阿尔出版社','024-73217876','沈阳市铁西路33号');
              insert into publishers values('DDXC','电信出版社','0755-67866755','深圳市深东路120号');
              insert into publishers values('DGZC','贵族出版社','0755-56564367','深圳市深东路1号');
              insert into publishers values('EMDC','牡丹出版社','023-67548956','重庆火锅街10号');
              insert into publishers values('HDJT','大家庭出版社','010-67573321','北京市部外大街3号');
              insert into titles values('T001','C#开发技巧','王江江',1,'ABYC',34.50,'01-6月-11');
              insert into titles values('T002','网页设计与制作教程','朱丽叶',3,'BDDA',35.00,'22-3月-09');
              insert into titles values('T003','C#设计模式','刘丽',1,'DGZC',50.00,'26-12月-10');
              insert into titles values('T004','数据库原理','张田田',2,'AXXW',45.30,'20-2月-08');
              insert into titles values('T005','Java程序设计基础','麦考林',5,'DGZC',32.00,'12-3月-10');
              insert into titles values('T006','Oracle数据库初学者指南','斯考特',2,'BDDA',65.20,'17-5月-09');
              insert into titles values('T007','Android程序设计与开发','张楚贵',7,'AXXW',55.00,'03-3月-12');
              update publishers set phone='00-33338888' where pubname='白云出版社';
              update publishers set address='广州市革新路100号' where pubname='白云出版社';
              update titles set price='49' where titlename='数据库原理';
              delete from titles where TitleCode='T007';
              rollback
              create view titles_view1
              as
              select TitleName,Author,Price from titles;
              select * from titles_view1;
              create view titles_view2
              as
              select p.pubname,t.titleName,t.price from publishers p full join
              titles t on p.pubcode = t.pubcode
              where t.pubdate > to_date('20110101','YYYYMMDD')
              select * from titles_view2;


              7楼2017-11-27 11:41
              回复
                21题
                select CategoryID,CategoryName from category where CategoryID = '2' or CategoryID='4' or CategoryID='6';


                IP属地:广东10楼2017-11-28 16:06
                回复
                  2026-03-08 17:46:38
                  广告
                  不感兴趣
                  开通SVIP免广告
                  1)查看系统当前日期和时间
                  Select to_char(sysdate,'yyyy-mm-dd hh12:mi:ss') from dual;
                  2)分别显示系统当前日期的年、月和日部分。
                  select to_char(sysdate,'yyyy'),to_char(sysdate,'mm'),to_char(sysdate,'dd')from dual;
                  3)去年的10月1日距离现在过去了多少天?
                  select round(months_between(sysdate,'01-10月-16') * 30) from dual;
                  4)查询titles表中出版日期在3月至6月的出版物名称和对应日期,并按出版的月份升序排序
                  selectTitleName,PubDate from titles where to_char(PubDate,'mm')between '03' and '06'order by PubDate asc;
                  5)查询titles表中价格最高的出版物的名称、作者和出版日期
                  selectTitleName,Author,PubDate, price from titles where Price=(select max(Price)fromtitles);
                  6)查询titles表中各类别出版物的平均价格
                  selectCategoryID, avg(price) from titles group by CategoryID;
                  7)查询titles表中各出版社出版图书的数量
                  selectPubCode,count(PubCode) from titles group by pubcode;
                  8)查询titles表中出版日期在2009年至2010年的出版记录,并按出版日期升序排序。
                  select * fromtitles where to_char(Pubdate,'yyyy') between '2009' and '2010' order byPubdate;
                  9)查询titles表中出版物名称包含“数据库”的出版信息
                  select * fromtitles where TitleName like '%数据库%';
                  10)查询titles表中出版社代码第二位是“X”的出版物名称、作者和出版社代码
                  selectTitleName,Author,TitleCode from titles where pubcode like '_X%';
                  11)查询titles表的价格超过35的出版物名称、价格和85折后的价格
                  select TitleName,Price, 0.85 * Price D from titles where price > 35;
                  12)查询titles表的出版物名称长度大于7的出版物代码和作者。
                  select TitleCode,Author from titles where length(TitleName) > 7;
                  13) 查询类别是“C#基础”的出版物的名称、作者和价格。
                  selectTitleName,Author,Price from titles where CategoryID = '1';
                  14)查询“新希望出版社”出版的,类别是“数据库”的出版物的名称、类别ID和出版日期。
                  selectt.titlename , t.categoryid , t.pubdate from titles t inner join publishers p ONt.pubcode = p.pubcode where categoryid ='2' and pubname = '新希望出版社' ;
                  15) 查询各类别出版物平均价格高于48的出版物名称和平均价格,并按平均价格降序排列。
                  selectcategoryname, avg(price) from titles t inner join category c ON t.categoryid = c.categoryid
                  group bycategoryname
                  having avg(price)>48
                  order byavg(price) DESC;
                  16)查询出版日期满5年的出版物信息,包括出版物名称、作者、价格和出版日期。
                  select titlename, author, price , pubdate from titles where to_char(sysdate - pubdate) >5;
                  17)查询titles表出版社代码不含’B’的出版物的详细信息(指所有字段,以下同)。
                  select * fromtitles where pubcode not like '%B%';
                  18) 查询titles表价格高于出版社ID为’AXXW’的任意图书的出版物的名称、价格和出版社ID。
                  select titlename, price , pubcode from titles where price > (select max(price) from titleswhere pubcode = 'AXXW');
                  19)查询titles表中类别id是1,3,5的出版物详细信息。
                  select * fromtitles where categoryid = '1' or categoryid = '3' or categoryid = '5' ;
                  20)查询titles表中价格高于出版物名称为“C#设计模式”的出版物的名称、作者和出版日期。
                  selectTitleName,Author,PubDate from titles where price > all(select price fromtitles where titlename = 'C#设计模式');
                  21)查询类别表category中类别ID是2,4,6的类别ID和类别名称。
                  selectCategoryID,CategoryName from category where CategoryID =
                  all(selectCategoryID from titles where CategoryID='2' and CategoryID='4');
                  22)查询出版社表publishers中出版社地址包含’广州’的出版社名称和地址。
                  select PubName, Addressfrom publishers where Address like '%广州%';
                  23) 查询出版社表publishers中出版社名称中“出版社”前面的内容和出版社地址。
                  selectsubstr(PubName,1,instr(PubName,'出版社') - 1),address from publishers;
                  24)查询类别id和类别名称以及各类别出版物的数量。
                  Selectc.categoryid,c.categoryname,count(t.price) from category c inner join titles ton
                  c.categoryid = t.categoryid
                  Group by c.categoryid,c.categoryname
                  Order by c.categoryid;


                  11楼2017-11-30 08:43
                  回复
                    1, 创建新用户,使用系统用户授权
                    createuser chrieu identified by 123456;
                    grantconnect,resource to chrieu;
                    grantcreate session to chrieu;
                    grantcreate sequence to chrieu;
                    grantcreate any index to chrieu;
                    grantcreate synonym to chrieu;
                    grantcreate view to chrieu;
                    2, 使用新建的用户登录,并创建表、视图、序列、索引和同义词,修改表结构
                    这是一个针对某出版社的出版物管理的3张表,分别为类别表、出版社表以及出版物表,
                    表名分别是:category 、publishers、titles,各表的字段名、字段的数据类型和描述如下:
                    表1 类别表:category
                    列名 描述 数据类型 备注
                    CategoryID 类别ID number 主键
                    CategoryName 类别名称 Varchar2(50) 非空
                    表2 出版社表:publishers
                    列名 描述 数据类型 备注
                    PubCode 出版社代码 Char(4) 主键
                    PubName 出版社名称 Char(50) 唯一约束
                    Phone 联系电话 Char(15)
                    Address 地址 Varchar2(100)
                    表3 出版物表:titles
                    列名 描述 数据类型 备注
                    TitleCode 出版物代码 Char(6) 主键
                    TitleName 出版物名称 Varchar2(80) 非空
                    Author 作者 Char(8) 非空
                    CategoryID 类别ID number 外键(自category)
                    PubCode 出版社代码 Char(4) 外键(自publishers)
                    Price 价格 number >0
                    PubDate 出版日期 Date
                    createtable category
                    (
                    CategoryID number primary key,
                    CategoryName varchar2(50) not null
                    )
                    create table publishers
                    (
                    PubCode char(4) primary key,
                    PubName char(50) unique,
                    Phone char(15),
                    Address varchar2(100)
                    )
                    create table titles
                    (
                    TitleCode char(6) primary key,
                    TitleName varchar2(80) not null,
                    Auther char(8) not null,
                    CategoryID number constraint FK_CIDreferences category(categoryID),
                    PubCode char(4) constraint FK_PC references publishers(PubCode),
                    Price number check(Price>0),
                    PubDate date
                    )
                    使用新用户登录,并完成如下操作:
                    1)在新用户用中按要求创建以上3张数据表
                    2)修改表结构
                    a,修改表category,增加一列tele 字符类型 长度是20 ,命令执行后查看表结构。
                    Alter table category add tele char(20);
                    b,修改表titles,修改author字段,使其长度为15个字符,然后查看表结构
                    Alter table titles modify auther char(15);
                    c,修改表publishers,修改Phone字段,使其长度为20个 字符,查看表结构。
                    Alter table publishers modify Phonechar(20);
                    d,删除category表的tele列,查看表结构。
                    Alter table category drop column tele;
                    3) 创建序列、索引和同义词
                    a,创建序列cate_seq,初始值为1,增量为1,其他参数默认(即可以不指定)
                    create sequence cate_seq
                    increment by 1
                    start with 1;
                    b,在titles表的TitleName列上创建索引title_idx.
                    create index title_idx on titles(TitleName);
                    c,给titles表创建私有同义词title
                    create synonym title for titles;
                    d,使用数据字典查看所创建的表、序列、索引和同义词
                    select object_name,object_type fromuser_objects;
                    4, 数据操作(增加、修改和删除数据)
                    1) 增加数据:在3个表上分别插入以下数据:
                    category表:使用上题创建的序列在category表插入数据
                    INSERT INTOcategory VALUES(cate_seq.nextval,'C#基础');
                    INSERT INTOcategory VALUES(cate_seq.nextval,'数据库');
                    INSERT INTOcategory VALUES(cate_seq.nextval,'网站开发');
                    INSERT INTOcategory VALUES(cate_seq.nextval,'计算机基础理论');
                    INSERT INTOcategory VALUES(cate_seq.nextval,'JAVA');
                    INSERT INTOcategory VALUES(cate_seq.nextval,'电子商务');
                    INSERT INTOcategory VALUES(cate_seq.nextval,'移动互联网');
                    publishers表:
                    insert into publishers values('ABYC','白云出版社','020-87056743','广州市市政大街10号');
                    insert into publishers values('AXXW','新希望出版社','020-84756554','广州市天府大道22号');
                    insert into publishers values('BDDA','东大阿尔出版社','024-73217876','沈阳市铁西路33号');
                    insert into publishers values('DDXC','电信出版社','0755-67866755','深圳市深东路120号');
                    insert into publishers values('DGZC','贵族出版社','0755-56564367','深圳市深东路1号');
                    insert into publishers values('EMDC','牡丹出版社','023-67548956','重庆火锅街10号');
                    insert into publishers values('HDJT','大家庭出版社','010-67573321','北京市部外大街3号');
                    Titles表
                    insert into titles values('T001','C#开发技巧','王江江',1,'ABYC',34.50,'01-6月-11');
                    insert into titles values('T002','网页设计与制作教程','朱丽叶',3,'BDDA',35.00,'22-3月-09');
                    insert into titles values('T003','C#设计模式','刘丽',1,'DGZC',50.00,'26-12月-10');
                    insert into titles values('T004','数据库原理','张田田',2,'AXXW',45.30,'20-2月-08');
                    insert into titles values('T005','Java程序设计基础','麦考林',5,'DGZC',32.00,'12-3月-10');
                    insert into titles values('T006','Oracle数据库初学者指南','斯考特',2,'BDDA',65.20,'17-5月-09');
                    insert into titles values('T007','Android程序设计与开发','张楚贵',7,'AXXW',55.00,'03-3月-12');
                    注意:日期数据可以和图中显示的不同
                    2) 修改和删除表数据:
                    a, 修改publishers数据:修改“白云出版社”的电话为020-33338888,地址为广州市革新路100号,提交。
                    update publishersset phone='00-33338888' where pubname='白云出版社';
                    b, 修改titles表数据:将“数据库原理”一书价格从45.30调到49元,提交
                    update titlesset price='49' where titlename='数据库原理';
                    c,删除titles表数据:删除TitleID是T008和 T007的出版
                    物数据,使用select查看后回滚。
                    deletefrom titles where TitleCode='T007';
                    3) 创建视图
                    a, 创建视图view1,查看出版物表的出版物名称、作者和价格。
                    create view titles_view1
                    as
                    select TitleName,Auther,Price from titles;
                    select * from titles_view1;
                    b, 创建视图view2,查看2011年以后出版的出版物的名字、出版社名称和出版日期。
                    createview titles_view2
                    as
                    select p.pubname,t.titleName,t.price frompublishers p full join
                    titles t on p.pubcode = t.pubcode
                    where t.pubdate > to_date('20110101','YYYYMMDD')


                    12楼2017-11-30 08:45
                    回复