益哥哥

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

数据库中的重要对象:表、视图、序列、函数、存储过程、索引、同义词
1、表:
用PL/SQL Developer 软件打开 Scott的DEPT表
查看SQL,可以看见DEPT表创建的脚本
-- Create table
create table DEPT
(
deptno NUMBER(2) not null,
dname VARCHAR2(14),
loc VARCHAR2(13)
)
tablespace USERS
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
--主键约束
alter table DEPT
add constraint PK_DEPT primary key (DEPTNO)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
-- Grant/Revoke object privileges
grant select on DEPT to HR;

2、视图
用PL/SQL Developer 软件打开 Scott的Views对象
选中要查看的视图,右键查看
就可以看到相应的视图语句
create or replace view avg_sal as
select b.dname as 部门,count(*) as 人数,round(avg(a.sal),2) as 平均工资 from emp a
join dept b on a.deptno =b.deptno
group by b.dname;

3、序列
用PL/SQL Developer 软件打开 Scott的SEQUENCE对象
选中要查看的视图,右键查看
create sequence computers_cmpno_seq
start with 1001
increment by 1
nomaxvalue
nocycle
nocache;
查看当前序列的值:
select computers_cmpno_seq.currval from dual;
下一个值:
select computers_cmpno_seq.nextval from dual;
调用序列的时候一般用.nextval;
insert into computers (COMPNO,COMPMODEL,buytime,price) values(computers_cmpno_seq.nextval,'Apple',sysdate,8888);

查看所拥有的序列
select * from USER_SEQUENCES;

4、函数
创建函数:(根据输入的员工号取得其对应的工资)
create or replace function get_empsal (emp_no number)
return number
is emp_sal number(7,2);
begin
select sal into emp_sal from emp
where empno=emp_no;
return (emp_sal);
end;
创建以后,可以用PL/SQL Developer 软件打开 Scott的Function对象
选中要查看的视图,右键查看以及测试
测试的时候:
begin
-- Call the function
:result := get_empsal(emp_no => :emp_no);
end;

输入值即可得到结果
也可以直接在数据库用语句测试:
select get_empsal(7566) from dual;

5、存储过程
create table COMPUTERS
(
compno NUMBER(4) not null,
compmodel VARCHAR2(64),
buytime DATE,
price NUMBER(7,2)
)
insert into Computers (COMPNO,compmodel,buytime,price) values (compNo_seq.nextval,'Think T60',sysdate,6666);
insert into Computers (COMPNO,compmodel,buytime,price) values (compNo_seq.nextval,'Accer 4750',sysdate,6666);
insert into Computers (COMPNO,compmodel,buytime,price) values (compNo_seq.nextval,'Think T80',sysdate,8888);
insert into Computers (COMPNO,compmodel,buytime,price) values (compNo_seq.nextval,'Accer 4750',sysdate,6666);
创建存储过程:
create or replace procedure DelComputer(cmp_no in number)
is
begin
delete from computers where computers.cmpno=cmp_no;
commit;
end;

创建以后,可以用PL/SQL Developer 软件打开 Scott的Procedure对象
选中要查看的视图,右键查看以及测试
测试的时候:
begin
-- Call the procedure
delcomputers(cmp_no => :cmp_no);
end;

也可以直接在数据库用语句测试:

execute DELCOMPUTERS(1002);

6、索引
对数据库表中的一列或多列的值进行排序的一种结构
创建一个测试的表:
create table TEST_INDEX
(
id NUMBER,
name VARCHAR2(200)
)
创建一个存储过程插入1千万条数据
create or replace procedure insert_data
is
temp varchar2(20) :='Test data';
begin
for i in 1..10000000 loop
insert into test_index(id,name) values (i,temp);
end loop;
commit;
end;
查询数据条数
select count(*) from test_index;
随机查询一条数据
select * from test_index where id='9943432';
在没有建索引的时候,查询一条数据耗时很久。

创建索引:
create index id_text_index on test_index(id);
再查询
select * from test_index where id='9943432';
查出结果时间大幅减少

删除索引、清空表数据、删除表
drop index id_text_index;
truncate table test_index;
drop table test_index;

7、同义词
现有对象的一个别名,分为:私有同义词和公有同义词
查看同义词的权限:
select * from session_privs where privilege like '%SYNONYM%';
没有该权限 则通过sys账号授权
grant create any synonym to scott;
创建同义词:
create synonym sg for salgrade;
sg即为salgrade的同义词
select * from sg;
创建公共同义词
首先需要用sys授权public synonym的权限
grant create public synonym to scott;
创建scott的emp表的公共同义词为emp
create public synonym emp for scott.emp;
其他能访问到scott的emp表的用户即可以直接访问emp
sys账号
select * from scott.emp;
select * from emp;查询结果一样。

删除同义词:
drop synonym sg;
drop public synonym emp;

 

posted on 2017-07-06 09:29  益哥哥  阅读(252)  评论(0编辑  收藏  举报