Oracle表的建立與管理
阿新 • • 發佈:2019-02-08
--表的建立與管理
--表的建立
create table member(
mid number(5),
name varchar2(50) default '無名氏',
age number(3),
birthday date default sysdate,
note clob
);
select * from tab;
desc member;
insert into member(mid,name,age,birthday,note)
values (1,'陳飛鵬',30,to_date('1992-09-12','yyyy-mm-dd'),'總公司活動提倡者');
insert into member(mid,name,age,birthday,note)
values (2,'董鳴楠',29,to_date('1980-08-13','yyyy-mm-dd'),'積極響應者');
select * from member;
--表的複製
drop table myemp;
create table myemp as select * from emp;
select * from myemp;
create table myemp10
as
select * from emp where deptno=10;
select * from myemp10;
create table employee1
as
select * from emp where 1=2;
select * from employee1;
desc employee1;
create table department1
as
select d.deptno,d.dname,d.loc,
count(e.empno) count,sum(e.sal+nvl(e.comm,0)) sum,
round(avg(e.sal+nvl(e.comm,0)),2) avg,max(e.sal) max,min(e.sal) min
from emp e,dept d
where e.deptno(+)=d.deptno
group by d.deptno,d.dname,d.loc
order by d.deptno;
--資料表的重新命名
select * from department1;
select * from user_tables;
rename member to mldnuser;
select * from user_tables;
--截斷表 desc
truncate table mldnuser;
select * from mldnuser;
rollback;
drop table mldnuser;
drop table myemp;
drop table myemp10;
drop table employee1;
drop table department1;
select * from tab;
flashback table myemp to before drop;
select object_name,original_name,operation,type from recyclebin;
select * from myemp10;
drop table myemp purge;
drop table myemp10 purge;
purge table myemp;
purge recyclebin;
--修改表結構
drop table member purge;
create table member(
mid number,
name varchar2(50) default'無名氏'
);
insert into member(mid,name)values(1,'李興華');
insert into member(mid,name)values(2,'董鳴楠');
insert into member(mid,name)values(3,'王月清');
commit;
select * from member;
alter table member add(age number(3));
alter table member add(sex varchar(10) default'男');
alter table member add(photo varchar2(100) default'nophoto.jpg');
desc member;
alter table member modify(name varchar(30));
alter table member modify(sex varchar2(3) default'女');
alter table member drop column photo;
alter table member drop column age;
alter table member set unused(sex);
alter table member set unused column name;
select * from member;
alter table member drop unused columns;
--添加註釋
drop table member purge;
create table member(
mid number,
name varchar2(50) default'無名氏',
age number(3),
birthday date
);
commit;
select * from user_tab_comments where table_name='MEMBER';
comment on table member is '用於記錄參加活動的成員資訊';
select * from user_col_comments where table_name='MEMBER';
comment on column member.mid is '參加活動的成員編號';
--表的建立
create table member(
mid number(5),
name varchar2(50) default '無名氏',
age number(3),
birthday date default sysdate,
note clob
);
select * from tab;
desc member;
insert into member(mid,name,age,birthday,note)
values (1,'陳飛鵬',30,to_date('1992-09-12','yyyy-mm-dd'),'總公司活動提倡者');
insert into member(mid,name,age,birthday,note)
values (2,'董鳴楠',29,to_date('1980-08-13','yyyy-mm-dd'),'積極響應者');
select * from member;
--表的複製
drop table myemp;
create table myemp as select * from emp;
select * from myemp;
create table myemp10
as
select * from emp where deptno=10;
select * from myemp10;
create table employee1
as
select * from emp where 1=2;
select * from employee1;
desc employee1;
create table department1
as
select d.deptno,d.dname,d.loc,
count(e.empno) count,sum(e.sal+nvl(e.comm,0)) sum,
round(avg(e.sal+nvl(e.comm,0)),2) avg,max(e.sal) max,min(e.sal) min
from emp e,dept d
where e.deptno(+)=d.deptno
group by d.deptno,d.dname,d.loc
order by d.deptno;
--資料表的重新命名
select * from department1;
select * from user_tables;
rename member to mldnuser;
select * from user_tables;
--截斷表 desc
truncate table mldnuser;
select * from mldnuser;
rollback;
drop table mldnuser;
drop table myemp;
drop table myemp10;
drop table employee1;
drop table department1;
select * from tab;
flashback table myemp to before drop;
select object_name,original_name,operation,type from recyclebin;
select * from myemp10;
drop table myemp purge;
drop table myemp10 purge;
purge table myemp;
purge recyclebin;
--修改表結構
drop table member purge;
create table member(
mid number,
name varchar2(50) default'無名氏'
);
insert into member(mid,name)values(1,'李興華');
insert into member(mid,name)values(2,'董鳴楠');
insert into member(mid,name)values(3,'王月清');
commit;
select * from member;
alter table member add(age number(3));
alter table member add(sex varchar(10) default'男');
alter table member add(photo varchar2(100) default'nophoto.jpg');
desc member;
alter table member modify(name varchar(30));
alter table member modify(sex varchar2(3) default'女');
alter table member drop column photo;
alter table member drop column age;
alter table member set unused(sex);
alter table member set unused column name;
select * from member;
alter table member drop unused columns;
--添加註釋
drop table member purge;
create table member(
mid number,
name varchar2(50) default'無名氏',
age number(3),
birthday date
);
commit;
select * from user_tab_comments where table_name='MEMBER';
comment on table member is '用於記錄參加活動的成員資訊';
select * from user_col_comments where table_name='MEMBER';
comment on column member.mid is '參加活動的成員編號';