数据库上课笔记
- 格式:docx
- 大小:19.44 KB
- 文档页数:7
oracle数据库基本单元
表空间
用户
权限
表设计列
数据的完整性(准确安全)域完整,实体完整,引用完整
数据的冗余
sqlplus/nolog
conn/as sysdba
alter user scott account unlock (解锁)
alter user scott indentified by 123; (更改密码)
sql语句分类
DDL:数据定义语言
Create table
Alter table
Drop table
Sequence(序列)
DCL:数据控制语言
Grant revoke
DML:数据操作语言
Commit(直接提交)9.9
DQL:数据查询语言0
Select 列明(空格)别名,列明
Where 列明<>(不等于)
Where 列明in(1,2,3)数字日期字符
Between -- 包含边界数字日期
Where 列名字like’a%’‘%a’‘%a%’(like类似的意模糊查询只能用于文字%代表任意字符可以是0)
Where 列名字like’_a’(_代表一个任意字符) __a__ blake
Where 列名字is null (选出空的)is not null 非空
Order by 列明排序方式asc(升序可省略)desc(降序)
函数
1基本函数
2聚合函数(汇总数据)
Count(汇总)
Sum(求和)
Avg
Min
Max
select ename,sal from emp
where hiredate between
to_date('1982/1/1','yyyy/mm/dd') and
to_date('1987/12/30','yyyy/mm/dd')
and ename like'%A%'
order by sal desc;
--显示1982-1987之间名字中含有A的姓名和收入,以收入由高到低排序
select ename,sal,hiredate from emp
where deptno in(20,30) and sal >=2000
order by hiredate desc
--显示deptno在20和30的,工资2000以上的姓名,工资和雇佣时间,以雇佣时间从近到远
select deptno,count(sal),avg(sal)from emp where deptno in(10,30)
group by deptno
select deptno,count(sal),avg(sal) from emp group by deptno
having avg(sal)>=2000
order by avg(sal) desc
select ename,sum(sal),sum(comm) from emp group by ename
order by sum(sal)
111
select ename,sal from emp
where hiredate between
to_date('1982/1/1','yyyy/mm/dd') and
to_date('1987/12/30','yyyy/mm/dd')
and ename like'%A%'
order by sal desc;
--显示1982-1987之间名字中含有A的姓名和收入,以收入由高到低排序
select ename,sal,hiredate from emp
where deptno in(20,30) and sal >=2000
order by hiredate desc
--显示deptno在20和30的,工资2000以上的姓名,工资和雇佣时间,以雇佣时间从近到远
select
to_char(hiredate,'yyyy'),deptno,count(sal),sum (sal),avg(sal)from emp
where deptno=20
group by to_char(hiredate,'yyyy'),deptno
having count(sal)>1
order by to_char(hiredate,'yyyy')
drop table student;
create table student(
studentid number not null,
sname varchar(20) not null,
genden nchar(4) not null,
weight number
);
alter table student
add born date;
alter table student
add constraint pk_student_studentid
primary key(studentid);
alter table student
add constraint ck_student_weight
check(weight>=0);
create sequence seq_student
start with1
increment by1
cache10;
insert into student
(studentid,sname,genden,weight,born) values
(1,'张三','男
',78,to_date('1997/02/21','yyyy/mm/dd'))
select *from student
前提列数相同
union (并集去重复)
union all (并集不去重复)
minus (减法)1-2 1为基准
intersect (交集)
子查询
条件子查询
查询结果作为列
Select ename,sal-(select avg(sal)from emp)frpm emp
查询结果作为表
Select*from emp e
Inner join
(select deptno,avg(sal) avgsal from emp group bu deptno)t on t. deptno=e.deptno
Inner join dept on dept.deptno=e.deptno
Exists
Select * from dept
Where not exists (select* from emp
Where emp .deptno=dept.deptno)。