您的位置:首页 > 数据库 > Oracle

oracle的学习笔记

2017-01-13 11:24 267 查看

2017.01.09

数据库连接

内连接,外连接,自然连接,自连接,交叉连接

内连接:table1 inner join或join table2 [on condition]

只选择符合条件的行

外连接 包括左联接,右连接,完全连接

左联接:table1 left out join或left join table2 on condition

显示左表所有行和右表符合条件的行,不符合的进行补空

右连接:table1 right out join或right join table2 on condition

显示右表所有行和左表符合条件的行,不符合的进行补空

完全连接:table1 full out join或full join table2 on condition

对两表进行左联接和右连接操作再合并两表并去除重复的行

自然连接:table1 natural join table2 on condtion

要求两表有相同的列,并且不能再列前加表名否则会报错

交叉连接:table1 cross join table2

查询结果是一个笛卡尔积

 

函数

Instr (str1,str2,int1,int2) 获取str2在str1中第int2次出现的位置从int1位置查找

select instr('abcdefghijklmnopqrstuvwxyzz','z',1 ,1) as abc
from dual;--26

initcap(str) 首字母大写

select initcap('this
is my sql,my?') from dual;--This
Is My Sql,My?

Ascii()和chr()字符和ASCII码的转换

select chr(56) from dual;--8

select ascii(8) from dual;--56

lower(str)和upper(str)
大小写转换

select lower('This
is My SQL?') from dual;--this
is my sql?

select upper('This
is My SQL?') from dual;--THIS
IS MY SQL?

rtrim(str1,str2),ltrim(str1,str2)和trim(str2 from str1)
从str1中删除对应位置的str2,省略str2
的情况时删除对应位置的空格。

select ltrim('****abcd******','*') from dual;--abcd******

select rtrim('****abcd******','*') from dual;--****abcd

select trim('*' from '**88abcd*****') from dual;--88abcd

select ltrim('
   abcd    ') from dual;--abcd
   

substr(str,int1,int2)在str从int1位置截取长度为int2的字符串

select substr('messagebox',8,3) from dual;--box
  

2017.01.10

事务:一系列语句构成的逻辑单元。

重要属性:原子性,隔离性,一致性,持久性

提交事务:commit

回滚事务:rollback

 

2017.01.12

数据表对象

创建表:create table [mode_name.]table_name(…);

create table scott.student(

stuname varchar2(31),

stuno number(10),

sex char(2)

)

 

--创建表的备份:

create table student_2 as select * from student

增添和删除字段:

alter table student
add(province
varchar2(10));--添加省字段

alter table student
drop column province;--删除省份字段

alter table student
drop (sex,age) --删除多个字段

修改字段:alter table table_name modify column_name colunm_property

alter table student
modify age
varchar2(31);--修改student表的age字段类型为varchar2(31)

重命名表:alter table table_old_name rename to table_new_name

alter table student
rename to students;--修改student表的名称为students

删除表:drop table table_name[cascade constraints];

如果该表存在约束,关联的视图和触发器等,则必须使用”cascade constraints”这个可选子句才能将其删除。

一般情况下,当某个表被删除后,实际上它并没有被彻底删除(仅仅是在数据字典中被除名),而是把表放到了回收站中(依然占用内存)。可以使用flashback table语句进行还原。

flashback table student
before drop;--还原student表

修改表状态:alter table table_name read [only|write];

alter table student
read only;--修改student表为只读

 

完整性约束

非空约束(not null):限制必须为某个列提供值。

alter table student
modify age
not null;--为age添加非空约束

alter table student
modify age
null;--删除age的非空约束

主键约束(primary key):用于唯一的标识表中的每一行记录。

alter table student
add constraint Stu_PK
primary key(StuName);--为student表添加主键Stu_PK

alter table student
add primary key(StuName);--为student表添加系统自动分配名称的主键

alter table student
drop constraint Stu_PK;--删除主键

唯一性约束(unique):强调所在列不允许有相同的值,但是可以允许为空值。

类似于主键约束。

外键约束(foreign key):在”另外一张表中”,被引用的列中不存在的数据不能出现在“当前表,”对应列中。

alter table student
add constraint temp_departid_fk

foreign key(age) reference students(age);  --创建外键约束

禁用和激活约束:alter table table_name [enable|disable] constraint con_name;

删除约束:alter table table_name drop constraint con_name;

 

2017.01.13

索引对象

减少执行查询操作时的系统开销。

创建索引:oracle首先对将要建立索引的字段进行排序,然后将排序后的字段值和对应记录的rowid存储在索引字段中。

B树索引:最常用的索引类型(默认类型),以B树结构组织存放数据,默认以升序排列。通常由根块,分支块和页块组成,主要数据都集中在叶子节点。

creat index stu_index
on student(StuName)

pctfree 25 --指定预留空间

tablespace users;--指定表空间

位图索引:用于列的基数很低的时候

反向建索引:将键值反向处理的B树索引

函数索引:存放经过函数处理的表中数据的的B树索引

 

Like与索引:

1. like %keyword
索引失效,使用全表扫描。但可以通过翻转函数+like前模糊查询+建立翻转函数索引=走翻转函数索引,不走全表扫描。
2. like keyword%
索引有效。
3. like %keyword%
索引失效,也无法使用反向索引。
 

视图对象

Creat [or replace] view <view_name> [alias[,alias]…]]

As <subquery>

[with check option][constraint constraint_name]

[with read only]

数据库并不存储视图中的值,而是存储视图的定义信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle 数据库 合并