一. 表
<1> 创建表
1. 普通建表
create table department (
dep_no number( 2 ) primary key comment '主键' ,
dep_name varchar2( 20 ) not null ,
location varchar2( 40 ) default 'QingDao' not null
) ;
2. 子查询建表
create table emp_new (
name,
job,
salary,
hiredate )
as ( select
name,
job,
salsry,
hiredate
from
emp
where
dep_no = 30 ) ;
3. 创建临时表
create global temporary table employee_temp (
temp_no number( 3 )
) ;
<2> 操作表
1. 修改表
rename old_table to new_table;
2. 删除表
(1) 删除表结构和内容
drop table temp_employee;
(2) 仅删除表内容
truncate table temp_employee;
<3> 操作列
1 添加列
alter table department add (
sex char ( 4 ) default '无'
) ;
2 修改列
alter table department modify (
dep_no number( 3 ) primary key
) ;
3 删除列
alter table department drop (
location
) ;
<4> 操作注释
comment on table
employee is '雇员表' ;
comment on column
emmployee. emp_name is '雇员姓名' ;
comment on table employee is ' ' ;
二. 索引
<1> 创建索引
1. 单列索引
create index index_dep_no on department( dep_no ) ;
2. 复合索引
create index
index_dep_no_and_dep_name on department( dep_no, dep_name ) ;
3. 唯一索引
create unique
index_dep_no on department( dep_bo ) ;
<2> 维护索引
1.重建索引
create index
index_dep_no
rebuild;
2.删除索引
drop index index_dep_no;
三. 约束
<1> 创建约束
先区分-> 表级约束 和 列级约束
constraint
constraint_age check ( age between 1 and 18 ) ;
alter table
employee
add constraint
constraint_pone check ( phone_no like '1%' ) ;
1. not null 非空约束
2. unique 唯一约束
3. primary key 主键约束
4. foreign key 外键约束
create table employee (
emp_no number( 2 ) primary key ,
emp_name varchar ( 12 ) not null ,
age number( 2 ) not null ,
salary number( 7 ) not null ,
phone_no number( 16 ) ,
dep_no number( 2 ) constraint frk_dep_no references department( dep_no) ) ;
constraint
emp_primary
foreign key ( dep_no ) references department( dep_no ) ;
5. check 条件约束
check ( sex in ( '男' , '女' ) )
check ( sex = '男' or sex = '女' )
<2> 维护约束
1. 修改约束名
alter table
department
rename constraint
constraint_old
to
constraint_new;
2. 激活/禁止约束
alter table
department
disable
constraint_department;
alter table
department
enable constraint_department;
3. 删除约束
alter table
department
drop constraint
constraint_department;
alter table
department
drop primary key ;
alter table
department
drop primary key cascade ;