oracle对表常用的操作

        最近业务需要给表添加索引,因为数据量很大时,查询效率很低;老大建议使用索引; 之前总结的时候将索引没有记录,当然啦,也怪笔者基础薄弱,不管了,慢慢进步嘛,好了进入正题吧!

       首先准备工作,先建两个临时表,后边操作会用到;

--建临时学生表
create table temp_student(
 id number,
 name varchar2(20),
 sex char(2),
 age number,
 class varchar2(20),
 userId number
);
comment on  table  temp_student       is '临时学生表';
comment on  column temp_student.id    is 'ID';
comment on  column temp_student.name  is '姓名';
comment on  column temp_student.sex   is '性别';
comment on  column temp_student.age   is '年龄';
comment on  column temp_student.class is '班级';
comment on  column temp_student.userId is '临时人员ID';
--建临时人员表
create table temp_user(
 id   number,
 name varchar2(20),
 age number,
 sex char(2)
);
comment on  table  temp_user       is '临时人员表';
comment on  column temp_user.id    is 'ID';
comment on  column temp_user.name  is '姓名';
comment on  column temp_user.age   is '年龄';
comment on  column temp_user.sex   is '性别';

      下边是常用的操作,这里我暂时总结八点吧,吉利数字,嘎嘎!后边有需要继续总结,在这里将表数据的增删改没有总结,因为太简单了,在这里笔者认为是占地方,所以就免了,如果大家对表数据的增删改都不熟悉,那这篇文章对您暂时还不太适合,当以后熟悉了再来看看;

--1
--查询表(pl/sql中可修改)
select * from temp_student for update; 
select * from temp_user for update;

--2
--删除表
drop table temp_student;
drop table temp_user;

--3
--添加字段
alter table temp_student add  join_date date;
comment on column temp_student.join_date is '入学时间'; 
--修改字段
alter table temp_student modify(class varchar2(200));
--重命名字段
alter table temp_student rename column  join_date to join;
--重命名表
rename temp_student to t_student;
--删除字段
alter table t_student drop column join;

--4
--建主键
alter table temp_student add primary key (id);
alter table temp_user add primary key(id);
--建外键
alter table temp_student add constraint  userKey foreign key(userId) references temp_user(id);
--撤销主键
alter table temp_student drop primary key;
--撤销外键
alter table temp_student drop constraint userKey;

--5
--给表建立公共别名
create public synonym t_student for temp_student;
create public synonym t_user for temp_user;

--6
--授权给指定角色权限
grant select,insert,update on sys.temp_student to bepcde,bepopr;
--收回给指定角色权限
revoke select,insert,update on sys.temp_student from bepcde,bepopr;
 
--7 
--建序列
create sequence t_student_seq 
  minvalue 1 --初始序号为1
  maxvalue 9999999999999999  --最大序号,这里也可以设置
  start with 1 --从1开始计算
  increment by 1 --每次增1
  cache 30  --缓存20个
  cycle; --//循环
--删除序列
drop sequence t_student_seq;
--查询下一个序列号
select t_student_seq.nextval from dual; --1

--8
--创建索引
create index id_a on t_student(name);
--删除索引
drop index id_a;

      如果朋友们发现那里有疑问或者问题,请指出来,谢谢!

原文地址:https://www.cnblogs.com/lidelin/p/9996819.html