完成下列操作,寫出相應(yīng)的SQL語句
創(chuàng)建表空間neuspace,數(shù)據(jù)文件命名為neudata.dbf,存放在d:\data目錄下,文件大小為200MB,設(shè)為自動增長,增量5MB,文件最大為500MB。(8分)
答:create tablespace neuspace datafile ‘d:\data\neudata.dbf’ size 200m auto extend on next 5m maxsize 500m;
2. 假設(shè)表空間neuspace已用盡500MB空間,現(xiàn)要求增加一個數(shù)據(jù)文件,存放在e:\appdata目錄下,文件名為appneudata,大小為500MB,不自動增長。(5分)
答:alter tablespace neuspace add datafile ‘e:\appdata\appneudata.dbf’ size 500m;
3. 以系統(tǒng)管理員身份登錄,創(chuàng)建賬號tom,設(shè)置tom的默認表空間為neuspace。為tom分配connect和resource系統(tǒng)角色,獲取基本的系統(tǒng)權(quán)限。然后為tom分配對用戶scott的表emp的select權(quán)限和對SALARY, MGR屬性的update權(quán)限。(8分)
答:create user tom identified by jack default tablespace neuspace;
Grant connect, resource to tom;
Grant select, update(salary, mgr) on scott.emp to tom;
4. 按如下要求創(chuàng)建表class和student。(15分)
屬性
類型(長度)
默認值
約束
含義
CLASSNO 數(shù)值 (2) 無 主鍵 班級編號
CNAME 變長字符 (10) 無 非空 班級名稱
屬性
類型(長度)
默認值
約束
含義
STUNO 數(shù)值 (8) 無 主鍵 學(xué)號
SNAME 變長字符 (12) 無 非空 姓名
SEX 字符 (2) 男 無 性別
BIRTHDAY 日期 無 無 生日
EMAIL 變長字符 (20) 無 唯一 電子郵件
SCORE 數(shù)值 (5, 2) 無 檢查 成績
CLASSNO 數(shù)值 (2) 無 外鍵,關(guān)聯(lián)到表CLASS的CLASSNO主鍵 班級編號
答:create table class
(classno number(2) constraint class_classno_pk primary key,
cname varchar2(10) not null);
create table student
(stuno number(8) constraint student_stuno_pk primary key,
sname varchar2(12) not null,
sex char(2) default ‘男’,
birthday date,
email varchar2(20) constraint student_email_uk unique,
score number(5,2) constraint student_score_ck check(score>=0 and score<=100),
classno number(2) constraint student_classno_fk references class(classno)
);
5. 在表student的SNAME屬性上創(chuàng)建索引student_sname_idx(5分)
答:create index student_sname_idx on student(sname);
6. 創(chuàng)建序列stuseq,要求初值為20050001,增量為1,最大值為20059999。(6分)
答:create sequence stuseq increment by 1 start with 20050001 maxvalue 20059999 nocache nocycle;
7. 向表student中插入如下2行。(5分)
STUNO SNAME SEX BIRTHDAY EMAIL SCORE CLASSNO
從stuseq取值 tom 男 1979-2-3 14:30:25 tom@163.net 89.50 1
從stuseq取值 jerry 默認值 空 空 空 2
答:insert into student values(stuseq.nextval, ’tom’, ’男’, to_date(‘1979-2-3
14:30:25’, ’yyyy-mm-dd fmhh24:mi:ss’), ’tom@163.net’, 89.50, 1);
insert into student (stuno, sname, classno) values(stuseq.nextval, ’jerry’, 2);
8. 修改表student的數(shù)據(jù),將所有一班的學(xué)生成績加10分。(4分)
答:update student set score=score+10 where classno=1;
9. 刪除表student的數(shù)據(jù),將所有3班出生日期小于1981年5月12日的記錄刪除。(4分)
答:delete from student where classno=3 and birthday > ’12-5月-81’;
10. 完成以下SQL語句。(40分)
(1) 按班級升序排序,成績降序排序,查詢student表的所有記錄。
答:select * from student order by classno, score desc;
(2) 查詢student表中所有二班的成績大于85.50分且出生日期大于1982-10-31日的男生的記錄。
答:select * from student where classno=2 and score>85.50 and birthday < ’31-10月-82’ and sex=’男’;
(3) 查詢student表中所有三班成績?yōu)榭盏膶W(xué)生記錄。
答:select * from student where classno=3 and score is null;
(4) 表student與class聯(lián)合查詢,要求查詢所有學(xué)生的學(xué)號,姓名,成績,班級名稱。(使用oracle與SQL 99兩種格式)
答:select s.stuno, s.sname, s.score, c.cname from student s, class c where s.classno=c.classno;
(5) 按班級編號分組統(tǒng)計每個班的人數(shù),最高分,最低分,平均分,并按平均分降序排序。
答:select classno, count(*), max(score), min(score), avg(score) from student group by classno order by avg(score) desc;
(6) 查詢一班學(xué)生記錄中所有成績高于本班學(xué)生平均分的記錄。
答:select * from student where classno=1 and score > (select avg(score) from student where classno=1);
(7) 統(tǒng)計二班學(xué)生中所有成績大于所有班級平均分的人數(shù)。
答:select count(*) from student where classno=2 and score > all (select avg(socre) from student group by classno);
(8) 查詢平均分最高的班級編號與分數(shù)。
答:select classno, avg(score) from student group by classno having avg(score) = (select max(avg(score)) from student group by classno);
(9) 查詢所有學(xué)生記錄中成績前十名的學(xué)生的學(xué)號、姓名、成績、班級編號。
答:select stuno, sname, score, classno from (select * from student order by score desc) where rownum<=10;
(10) 創(chuàng)建視圖stuvu,要求視圖中包含student表中所有一班學(xué)生的stuno, sname, score, classno四個屬性,并具有with check option限制。
答:create view stuvu
as
select stuno, sname,score,classno from student where classno=1 with check option;
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
