- Module1
表是Oracle数据库中数据存储的基本单元。数据按行和列存储。使用表名(如employees)和一组列定义表。为每个列指定一个列名,例如employee_id、last_name和job_id;数据类型,如VARCHAR2、日期或数字;以及宽度。宽度可以由数据类型预先确定,如日期。如果列是数字数据类型,请定义精度和比例而不是宽度。行是对应于单个记录的列信息的集合。
-
可以为表的每列指定规则。这些规则称为完整性约束。一个例子是非空完整性约束。此约束强制列在每行中包含一个值。
例如:create table DEPARTMENTS ( deptno number, name varchar2(50) not null, location varchar2(50), constraint pk_departments primary key (deptno) );
表可以是声明性的,指定表之间的关系,通常称为引用完整性。为了了解其工作原理,我们可以通过在引用DEPARTMENTS表的EMPLOYEES表中包含外键来创建DEPARTMENTS表的“子”表。例如:
create table EMPLOYEES ( empno number, name varchar2(50) not null, job varchar2(50), manager number, hiredate date, salary number(7,2), commission number(7,2), deptno number, constraint pk_employees primary key (empno), constraint fk_employees_deptno foreign key (deptno) references DEPARTMENTS (deptno) );外键必须引用主键,因此要创建“子”表,“父”表必须具有外键要引用的主键。
-
Module2
Creating Triggers触发器是存储在数据库中的过程,在发生某些事情时隐式运行或触发。传统上,触发器支持过程代码的执行,在Oracle中,过程SQL称为PL/SQL块。PL代表过程语言。在表或视图上发生插入、更新或删除时。触发数据库和架构上的支持系统和其他数据事件。
触发器经常用于自动填充表主键,下面的触发器示例显示了一个示例触发器。我们将使用内 置函数获取全局唯一标识符或GUID。
-
create or replace trigger DEPARTMENTS_BIU before insert or update on DEPARTMENTS for each row begin if inserting and :new.deptno is null then :new.deptno := to_number(sys_guid(), 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); end if; end; / create or replace trigger EMPLOYEES_BIU before insert or update on EMPLOYEES for each row begin if inserting and :new.empno is null then :new.empno := to_number(sys_guid(), 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'); end if; end; / -
Module3
Inserting Data现在我们已经创建了表,并且有了自动填充主键的触发器,我们可以向表中添加数据。因为我们有父子关系,以DEPARTMENTS表为父表,EMPLOYEES表为子表,我们将首先在DEPARTMENTS表中插入一行。
insert into departments (name, location) values ('Finance','New York'); insert into departments (name, location) values ('Development','San Jose');让我们通过运行SQL SELECT语句查询表中的所有列和所有行来验证插入是否成功。
select * from departments;
您可以看到,将自动生成一个ID。现在可以在EMPLOYEES表中插入新行,但需要将生成的DEPTID值放入SQL insert语句中。下面的示例展示了如何使用SQL查询来实现这一点,但您可以直接输入部门编号。
-
insert into EMPLOYEES (name, job, salary, deptno) values ('Sam Smith','Programmer', 5000, (select deptno from departments where name = 'Development')); insert into EMPLOYEES (name, job, salary, deptno) values ('Mara Martin','Analyst', 6000, (select deptno from departments where name = 'Finance')); insert into EMPLOYEES (name, job, salary, deptno) values ('Yun Yates','Analyst', 5500, (select deptno from departments where name = 'Development')); -
Module4
Indexing Columns通常,开发人员索引列有三个主要原因:
创建表并指定主键时,将自动创建索引以强制执行主键约束。如果在创建列时为列指定了唯一索引,则也会创建唯一索引。要查看给定表中已经存在的索引,可以运行以下字典查询。select table_name "Table", index_name "Index", column_name "Column", column_position "Position" from user_ind_columns where table_name = 'EMPLOYEES' or table_name = 'DEPARTMENTS' order by table_name, column_name, column_position索引外键通常是一种很好的形式,外键是表中引用另一个表的列。在我们的EMPLOYEES和DEPARTMENTS表示例中,EMPLOYEE表中的DEPTNO列引用DEPARTMENTS表的主键。
create index employee_dept_no_fk_idx on employees (deptno)
我们还可以确定EMPLOYEE表将经常通过NAME列进行搜索。为了提高搜索性能并确保唯一性,我们可以在EMPLOYEE表名称列上创建唯一索引。
create unique index employee_ename_idx on employees (name)
Oracle提供了许多其他索引技术,包括可以索引表达式的基于函数的索引,如上位函数、可以索引自由格式文本的文本索引,以及在数据仓库中有用的位图索引。您还可以创建索引的组织表,可以使用分区索引等。有时,最好使用较少的索引并利用内存中的功能。所有这些主题都超出了本基本介绍的范围。
在列中强制执行唯一值的步骤
为了提高数据访问性能
在更新使用声明性引用完整性的表行时防止锁升级 -
Module5
Querying Data要从单个表中选择数据相当简单,只需使用select…from…WHERe…ORDER BY…语法即可。
select * from employees;
要查询两个相关表中的数据,可以将数据连接起来
select e.name employee, d.name department, e.job, d.location from departments d, employees e where d.deptno = e.deptno(+) order by e.name;作为联接的替代方法,您可以使用内联选择查询数据。
select e.name employee, (select name from departments d where d.deptno = e.deptno) department, e.job from employees e order by e.name; -
Module6
Adding Columns使用ALTER table…add…语法创建表后,可以添加其他列。例如:
alter table EMPLOYEES add country_code varchar2(2);
-
Module7
Querying the Oracle Data Dictionary可以从Oracle数据字典访问表元数据。以下查询显示了如何查询数据字典表。
select table_name, tablespace_name, status from user_tables where table_Name = 'EMPLOYEES'; select column_id, column_name , data_type from user_tab_columns where table_Name = 'EMPLOYEES' order by column_id;
-
Module8
Updating Data您可以使用SQL更新表中的值,为此,我们将使用update子句
update employees set country_code = 'US';
您可以使用SQL更新表中的值,为此,我们将使用更新子句上述查询将更新employee表的所有行,并将国家代码的值设置为我们。您还可以选择性地仅更新特定行。
update employees set commission = 2000 where name = 'Sam Smith';
让我们运行一个查询,看看我们的数据是什么样子
select name, country_code, salary, commission from employees order by name;
-
Module9
Aggregate Queries可以使用聚合函数对表中的数据求和。为了可读性,我们将使用列别名来重命名列,我们还将使用空值函数(NVL)来允许我们对具有空值的列进行正确求和。
select count(*) employee_count, sum(salary) total_salary, sum(commission) total_commission, min(salary + nvl(commission,0)) min_compensation, max(salary + nvl(commission,0)) max_compensation from employees; -
Module10
Compressing Data随着数据库的大小增长到GB或TB以上,请考虑使用表压缩。表压缩节省了磁盘空间并减少了缓冲区缓存中的内存使用。表压缩还可以加快读取期间的查询执行。然而,数据加载和DML的CPU开销是有成本的。表压缩对应用程序完全透明。它在在线分析处理(OLAP)系统中特别有用,因为在该系统中存在冗长的只读操作,但也可以用于在线事务处理(OLTP)系统。您可以使用CREATE table语句的COMPRESS子句指定表压缩。通过在ALTER table语句中使用此子句,可以对现有表启用压缩。在这种情况下,唯一被压缩的数据是启用压缩后插入或更新的数据。类似地,可以使用ALTER table…NOCOMPRESS语句禁用现有压缩表的表压缩。在这种情况下,所有已经压缩的数据都将保持压缩状态,新数据将以未压缩状态插入。
-
要为将来的数据启用压缩,请使用以下语法。
alter table EMPLOYEES compress for oltp; alter table DEPARTMENTS compress for oltp;
-
Module11
Deleting Data可以使用delete语法从表中删除一行或多行。例如,要删除特定行:
delete from employees where name = 'Sam Smith';
-
Module12
Dropping Tables可以使用SQLDROP命令删除表。删除表将删除所有行并删除子对象,包括索引和触发器。以下DROP语句将删除departments和employees表。可选的cascade constraints子句将删除约束,从而允许您以任何顺序删除数据库表。
drop table departments cascade constraints; drop table employees cascade constraints;
-
Module13
Un-dropping Tables如果RECYCLEBIN初始化参数设置为ON(10g中的默认值),则删除此表将其放入回收站。要查看是否可以取消删除表,请运行以下数据字典查询:
select object_name, original_name, type, can_undrop, can_purge from recyclebin;要取消删除表,我们使用flashback命令,例如:
flashback table DEPARTMENTS to before drop; flashback table EMPLOYEES to before drop; select count(*) departments from departments; select count(*) employees from employees;



