Oracle学习系列6
Oracle学习系列6
************************************************************************************
删除约束(重点):
格式:
alter table person drop constraint constraint_name ;
创建person表:
create table person(
UID varchar()
name varchar() not null, //非空约束
age number() not null, //非空约束
birthday date,
sex varchar() default '男',
) ;
/**
主键约束:主键字段_PK
唯一约束:字段_UK
检查约束:字段_CK
外键约束:父字段_子字段_FK
*/
ex:为表添加若干个约束,
格式:
alter table tab_name add constraint constraint_name constraint_type(constrain_colx) ;
ex:添加约束
alter table person add constraint person_uid_PK primary key(uid);//增加主键约束
alter talbe person add constraint person_name_UK unique(uid); //增加唯一约束
alter table person add constraint person_age_CK checke(age between and ) ;
alter table person add constraint person_sex_CK check(sex in ('男','女','中')) ;
ex:删除约束
alter table person drop constraint person_uid_PK ;//删除主键约束
alter talbe person drop constraint person_name_UK ; //删除唯一约束
alter table person drop constraint person_age_CK ; //删除检查约束
alter table person drop constraint person_sex_CK ; //删除检查约束
************************************************************************************
rownum(重点):
rownum: 表示行号,实际上是一个伪列,可在每张表中出现
select rownum, empno, ename ,job,sal,hiredate
from emp ; // ROWNUM 采用自动编号的方式呈现
ex:
select rownum, empno, ename, job, sal hiredate
from emp
;
************************************************************************************
建表、约束、查询综合练习:
题目背景:
有某个学生运动会比赛信息的数据库,保存了如下的表:
运动员sporter(运动员编号sporterid, 运动员姓名name, 运动员性别sex, 所属系好department)
项目item (项目编号itemid, 项目名称itemname, 项目比赛地点location)
成绩grade(运动员编号sportid,项目编号itemid, 积分mark)
1建表要求:
a,定义各个表的主键外码约束
b,运动员的姓名和所属系别不能为空值
c,积分要么为null,要么为6,,,,分别代表第一二三和其他名词的积分
create table sporter(
sporterid nummber() primary key not null,
name varchar2() not null,
sex varchar2() not null,
department varchar2() not null,
constraint sporter_sex_CK check(sex in('男','女'))
);
create table item(
itemid varchar2() primary key not null,
itemname varchar2() not null,
location varchar2() not null
);
create table grade(
sporterid number(),
itemid varchar2(),
mark number(),
constraint sporter_grade_sporterid_FK foreign key(sporterid)
references sporter(sporterid) on delete cascade
constraint item_grade_itemid_FK foreign key(itemid)
references item(itemid) on delete cascade
constraint grade_mark_CK check(mark ,,,))
);
测试数据:
insert into sporter(sportid, name, sex, department )
values(,'黎明','男','计算机系');
insert into sporter(sportid, name, sex, department )
values(,'张三','男','数学系系');
insert into sporter(sportid, name, sex, department )
values(,'李四','男','计算机系');
insert into sporter(sportid, name, sex, department )
values(,'王五','男','物理系');
insert into sporter(sportid, name, sex, department )
values(,'李楠','女','心理系');
insert into sporter(sportid, name, sex, department )
values(,'孙俪','女','艺术系');
---------------------------------------------------------
insert into item(itemid ,itemname, location)
values('x001','男子五千米','一操场') ;
insert into item(itemid ,itemname, location)
values('x002','男子标枪','一操场') ;
insert into item(itemid ,itemname, location)
values('x003','男子跳远','二操场') ;
insert into item(itemid ,itemname, location)
values('x004','女子跳高','二操场') ;
insert into item(itemid ,itemname, location)
values('x005','女子三千米','三操场') ;
---------------------------------------------------------
insert into grade(sporterid, itemid,mark)
values(,);
insert into grade(sporterid, itemid,mark)
values(,);
insert into grade(sporterid, itemid,mark)
values(,);
insert into grade(sporterid, itemid,mark)
values(,);
insert into grade(sporterid, itemid,mark)
values(,);
insert into grade(sporterid, itemid,mark)
values(,);
insert into grade(sporterid, itemid,mark)
values(,);
insert into grade(sporterid, itemid,mark)
values(,);
insert into grade(sporterid, itemid,mark)
values(,);
要求:
求出目前总积分最高的系名,及其积分:
select * from (
select s.department , sum(g.mark) sum
from sporter s, grade g
where s.sporterid=g.sporterid
group by s.department
order by sum desc
)
找出在一操场进行比赛的各项目名称及其冠军的姓名:
select i.itemname, s.name, g.mark
from item i, grade g,sporter s
where i.location='一操场'
and i.itemid=g.itemid
and s.sporterid=g.sporterid
and g.mark= ;
找出参加了张三所参加过的项目的其他同学的姓名:
select distinct s.name
from sporter s, grade g
where s.sporterid=g.sporterid and s.name <>'张三'
and g.itemid IN (
select g.itemid from sporter s ,grade g
where s.sporterid=g.sporterid
and s.name='张三'
) ;
经查张三使用了违禁药品,其成绩都记0分,请在数据库中做出相依修改:
update grade
where sporterid =(
select sportid from sporter where name ='张三'
) ;
经组委会协商,需要删除女子跳高比赛项目:
delete from item
where itemname='女子跳高' ;
------------------------------------------------------------
删除顺序: //先删子表,再删主表
drop table grade;
drop table sporter;
drop table item ;
************************************************************************************
集合操作:
分类:
并(UNION) :将多个查询的结果组合到一个查询结果中,无重复值 //UNIONALL:包含重复值
交(INTERSECT):返回多个查询结果相同的部分
差(MINUS) :返回两个结果的差集
复制emp表,将部门20的雇员信息取出来:
create table emp20
as select * from emp
;
验证UNION:
select * from emp
union
select * from emp20 ;
验证UNIONALL:
select * from emp
unionall
select * from emp20 ;
验证INTERSECT:
select * from emp
INTERSECT
select * from emp20 ;
验证MINUS:
select * from emp
minus
select * from emp20 ;
----------------------------------------------------------------------------------
SQL查询最终格式:
////////////////////////////////////////////////////////////////////////////////////////////
select { distinct } * | col1 别名1 col2 别名2 ...
from tab1 别名1 , tab2 别名2 ,
(
select { distinct } * | col1 别名1 col2 别名2 ...
from tab1 别名1 , tab2 别名2 , tab3 别名3 ,...
{where 条件s }
{group by 分组条件 { having 分组条件 } }
{ order by col1 ASC | DESC , col2 ASC | DESC, ...} ;
)别名x tab3 别名3 ,...
{where 条件s
(
select { distinct } * | col1 别名1 col2 别名2 ...
from tab1 别名1 , tab2 别名2 , tab3 别名3 ,...
{where 条件s }
{group by 分组条件 { having 分组条件 } }
{ order by col1 ASC | DESC , col2 ASC | DESC, ...} ;
)
}
{group by 分组条件 { having 分组条件 } }
{ order by col1 ASC | DESC , col2 ASC | DESC, ...} ;
{UNION | INTERSECT |MINUS}
select { distinct } * | col1 别名1 col2 别名2 ...
from tab1 别名1 , tab2 别名2 ,
(
select { distinct } * | col1 别名1 col2 别名2 ...
from tab1 别名1 , tab2 别名2 , tab3 别名3 ,...
{where 条件s }
{group by 分组条件 { having 分组条件 } }
{ order by col1 ASC | DESC , col2 ASC | DESC, ...} ;
)别名x tab3 别名3 ,...
{where 条件s
(
select { distinct } * | col1 别名1 col2 别名2 ...
from tab1 别名1 , tab2 别名2 , tab3 别名3 ,...
{where 条件s }
{group by 分组条件 { having 分组条件 } }
{ order by col1 ASC | DESC , col2 ASC | DESC, ...} ;
)
}
{group by 分组条件 { having 分组条件 } }
{ order by col1 ASC | DESC , col2 ASC | DESC, ...} ;
////////////////////////////////////////////////////////////////////////////////////////////
Oracle学习系列6的更多相关文章
- Oracle学习系列1-7
Oracle学习系列1 两个服务必须启动: OracleOraDb10g*TNListener 和 OracleService*** 使用sqlplusw先进行环境的设置 set linesize 3 ...
- Oracle学习系列7
Oracle学习系列7 ************************************************************************************ 关联表 ...
- Oracle学习系列5
Oracle学习系列5 ************************************************************************************ ,掌握 ...
- Oracle学习系列4
Oracle学习系列4 ************************************************************************************ 数据库 ...
- Oracle学习系列3
Oracle学习系列3 ************************************************************************************ 多表查 ...
- Oracle学习系列1
两个服务必须启动: OracleOraDb10g*TNListener 和 OracleService*** 使用sqlplusw先进行环境的设置 set linesize 300 ; set pag ...
- oracle学习系列之四 (视图)
视图视图是数据库中特有的对象.视图用于存储查询,但不会存储数据(物化视图除外).这是视图和数据表的重要区别.可以利用视图进行查询,插入,更新和删除数据.Oracle有如下四种视图(关系视图,内嵌视图, ...
- oracle学习系列之三 (约束)
主键约束:外键约束:唯一性约束:检查约束:默认值约束 -——————五大约束 一. 主键约束: --创建表的主键约束 create table student (student_id number ...
- Oracle 学习系列之二(会话与事务级临时表和dual表 )
一. 会话临时表 --创建会话临时表create global temporary table tmp_user_session(user_id int, user_name varchar2(20) ...
随机推荐
- python打怪之路【第一篇】:99乘法表
需求:实现99乘法表 代码: #!/usr/bin/env python # -*- coding:utf-8 -*- #author chenjing for i in range(10): for ...
- js、jquery对于html内容的转义
-------2016-7-27 14:23:34-- source:[1]js转义html
- Java Performance - 如何调查解决 CPU 问题
随着硬件的发展,往往服务器会配置足够的 CPUs, Java Server/服务器不太有 CPU 问题:但是偶尔因为 代码海量循环 或者 线程安全性(thread safe), 还是会带来 CPU 问 ...
- Openvpn 本地密码验证
1.修改配置文件.(添加下列配置) auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env #开启用户密码脚本 client-cert-not-r ...
- Android Gradle的使用
说明: 在Android Studio中的Terminal可以直接使用Gradle命令行,不需要配置环境,而想直接在电脑的命令行使用Gradle命令行, 则需要配置环境,Window下配置Gradle ...
- 20145224&20145238《信息安全系统设计基础》实验三
20145224陈颢文20145238荆玉茗 <信息安全系统设计基础>第五次实验报告 课程:信息安全系统设计基础 班级: 1452 姓名:荆玉茗 陈颢文 学号:20145238 20145 ...
- Android开发--FrameLayout的应用
1.简介 frameLayout为框架布局,该布局的特点为层层覆盖,即最先放置的部件位于最下层,最后放置的部件位于最上层. 2.构建 如图所示,该视图中有五个TextView.其中,tv1放置在最底层 ...
- [转]不正当使用HashMap导致cpu 100%的问题追究
以前项目中遇到类似业务,但使用的是CurrentHashMap,看到这篇文章,转载记录,警示自己. 以下内容转自: 转载自并发编程网 – ifeve.com(http://ifeve.com/hash ...
- c#连接各种数据库
1.C#连接连接Access程序代码: ------------------------------------------------------------------------------- ...
- Grunt完成对LESS实时编译
安装 安装grunt需要先安装node.js. 之后需要借助npm来安装grunt-cli,在cmd中npm install -g grunt-cli.(测试grunt --version看是否正确显 ...