oracle的学习笔记

来源:互联网 发布:做微信表情的软件 编辑:程序博客网 时间:2024/05/01 23:56

2017.01.09

数据库连接

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

内连接:table1 inner joinjoin table2 [on condition]

只选择符合条件的行

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

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

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

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

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

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

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

自然连接:table1 natural join table2 on condtion

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

交叉连接:table1 cross join table2

查询结果是一个笛卡尔积

 

函数

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

select instr('abcdefghijklmnopqrstuvwxyzz','z',1 ,1) as abcfrom 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)strint1位置截取长度为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 studentadd(provincevarchar2(10));--添加省字段

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

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

修改字段:alter table table_name modify column_name colunm_property

alter table studentmodify agevarchar2(31);--修改student表的age字段类型为varchar231

重命名表:alter table table_old_name rename to table_new_name

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

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

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

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

flashback table studentbefore drop;--还原student

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

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

 

完整性约束

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

alter table studentmodify agenot null;--age添加非空约束

alter table studentmodify agenull;--删除age的非空约束

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

alter table studentadd constraint Stu_PKprimary key(StuName);--student表添加主键Stu_PK

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

alter table studentdrop constraint Stu_PK;--删除主键

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

类似于主键约束。

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

alter table studentadd 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_indexon 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]

数据库并不存储视图中的值,而是存储视图的定义信息。

0 0