一、creating a table
--创建表
create table computer_systems(
computer_system_id number(38, 0) not null,
agent_uuid varchar2(256),
operating_system_id number(19, 0) not null,
hardware_model varchar2(50),
create_dtt date default sysdate not null,
update_dtt date,
constraint computer_systems_pk primary key (computer_system_id)
using index tablespace inv_mgmt_index
) tablespace inv_mgmt_data;
--列注释
comment on column computer_systems.computer_system_id is
'Surrogate key generated via an Oracle sequence.';
--创建唯一索引
create unique index computer_system_uk1 on computer_systems(agent_uuid)
tablespace inv_mgmt_index;
--增加参考约束
alter table computer_systems add constraint computer_systems_fk1
foreign key (operating_system_id)
references operating_systems(operating_system_id);
二、创建临时表
--commit时保存数据。
create global temporary table today_regs
on commit preserve rows
as select * from f_registrations
where create_dtt > sysdate - 1;
--commit时删除数据。
create global temporary table temp_output(temp_row varchar2(30))
on commit delete rows;
三、moving a table
--移动表空间
alter table parties move tablespace mts;
--移动表空间后数据存储位置发放了变化,需要rebuild索引。
select dbms_rowid.rowid_relative_fno(rowid), emp_id from emp;
四、renaming objects
--重命名表名
alter table products rename to products_old;
drop table products_old;
--重命名列名
alter table products rename column product_id to product_no;
--重命名索引名字
alter index prodid rename to prod_idx1;
--重命名约束名字
alter table details rename constraint mast_con to mast_fk1;
--重命名触发器名字
alter trigger f_shipments_bu_tr1 rename to f_au_tr2;
五、dropping a table
--删除表
drop table inventory;
--删除表的同时删除外键约束,否则会报错
drop table inventory cascade constraints;
--清空回收站。当你删除一个对象的时候,oracle只是逻辑删除,真实的是rename了一下,所占用的表空间并不释放,以备以后恢复。如果不需要恢复,可以清空回收站。
purge recyclebin;
--删除的同时清空回收站,以后也不能通过闪回操作恢复。
drop table inventory purge;
六、undropping a table
SQL> create table yxl(id number);
Table created
SQL> drop table yxl;
Table dropped
SQL> select OBJECT_NAME,original_name from user_recyclebin where ORIGINAL_NAME='YXL';
OBJECT_NAME ORIGINAL_NAME
------------------------------ --------------------------------
BIN$ekJvLBZI5FPgQAB/AQBIcQ==$0 YXL
SQL> flashback table yxl to before drop;
Done
SQL> desc yxl;
Name Type Nullable Default Comments
---- ------ -------- ------- --------
ID NUMBER Y
注:在10g或更高的版本中,删除表时,不真正删除,而是重命名为以BIN$开头的表,并放到回收站里。
七、creating an index
--常用操作
create index d_sources_idx1 on d_sources(d_source_id);
create index d_sources_idx1 on d_sources(d_source_id) tablespace dim_index;
select table_name, index_name, index_type, tablespace_name, status
from user_indexes
order by table_name;
--自动生成rebuild脚本
SPO ind_build_dyn.sql
SET HEAD OFF PAGESIZE 0 FEEDBACK OFF;
SELECT 'ALTER INDEX ' || index_name || ' REBUILD;' FROM user_indexes;
SPO OFF;
SET FEEDBACK ON;
八、创建基于函数的索引
create index user_upper_idx on emp(upper(emp_name));
九、创建bitmap索引
create bitmap index f_down_date_fk1 on f_downloads(d_date_id);
--对分区表创建local索引
create bitmap index f_down_date_fk1 on f_downloads(d_date_id) local;
create bitmap index f_down_date_fk1 on f_downloads(d_date_id) local nologging;
十、创建索引组织表
create table prod_sku
(prod_sku_id number,
sku varchar2(256),
create_dtt timestamp(5),
constraint prod_sku_pk primary key(prod_sku_id)
)
organization index
including sku
pctthreshold 30
tablespace inv_mgmt_data
overflow
tablespace mts;
--查看索引
select index_name,table_name,index_type from user_indexes;
十一、creating a view创建视图
create or replace view sales_rockies as
select sales_id, amnt, state
from sales
where state in ('CO','UT','WY','ID','AZ');
--with check option操作。
create or replace view sales_rockies as
select sales_id, amnt, state
from sales
where state in ('CO','UT','WY','ID','AZ')
with check option;
十二、创建同义词
create or replace synonym inv for inv_mgmt.inv;
grant all on books to public;
create public synonym books for inv_mgmt.books;
十三、保证表中行数唯一性。
--主键约束
create table dept(dept_id number primary key, dept_desc varchar2(30));
create table dept( dept_id number constraint dept_pk primary key using index tablespace users, dept_desc varchar2(30));
create table dept(
dept_id number,
dept_desc varchar2(30),
constraint dept_pk primary key (dept_id)
using index tablespace prod_index);
--创建表后添加主键约束
create index dept_pk on dept(dept_id);
alter table dept add constraint dept_pk primary key (dept_id);
--unique约束
create table dept(dept_id number, dept_desc varchar2(30) constraint dept_desc_uk1 unique using index tablespace prod_index);
create index dept_desc_uk1 on dept(dept_desc);
alter table dept add constraint dept_desc_uk1 unique(dept_desc);
十四、外键约束
create table emp(
emp_id number,
name varchar2(30),
dept_id number constraint emp_dept_fk references dept(dept_id));
--添加外键约束
alter table emp add constraint emp_dept_fk foreign key (dept_id) references dept(dept_id);
十五、check约束
create table emp(
emp_id number,
emp_name varchar2(30),
st_flg number(1) CHECK (st_flg in (0,1))
);
create table emp(
emp_id number,
emp_name varchar2(30),
st_flg number(1) constraint st_flg_chk CHECK (st_flg in (0,1))
);
CREATE table emp(
emp_id number,
emp_name varchar2(30),
st_flg number(1) constraint "st_flg must be 0 or 1" check (st_flg in (0,1))
);
十六、数据库之间的连接(数据库链)
create database link mss connect to e123 identified by e123 using 'oss.east:1521/BRDSTN';
十七、创建序列
create sequence inv2 start with 10000 maxvalue 1000000;
drop sequence inv2;
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7204674/viewspace-622213/
,如需转载,请注明出处,否则将追究法律责任。