目录
一、表空间操作
Create tablespace tablespace_name
Datafile ‘数据文件保存目录/数据文件名.dbf’
Size autoextent on next maxsize;
注释:
size 大小,autoextent on next 增量,maxsize 最大值
例题:创建表空间neuspace,数据文件命名为neudata.dbf,存放在d:\data目录下,文件大小为200MB,设为自动增长,增量5MB,文件最大为500MB。(8分)
create tablespace neuspace
datafile 'D:/DATE/neudata.dbf'
size 200M autoextend on next 5M maxsize 500M;
Alter tableplace tableplace_name
Add datafile ‘数据文件保存目录/数据文件.dbf’
Size ;
例题:假设表空间neuspace已用尽500MB空间,现要求增加一个数据文件,存放在e:\appdata目录下,文件名为appneudata,大小为500MB,不自动增长。(5分)
Alter tablespace neuspace add
datafile 'e:/ appdata/appneudata.dbf'
size 500M;
select * from DBA_TABLESPACES;
select default_tablespace, username
from dba_users
where username = '用户名';
select tablespace_name, sum(bytes)
from dba_free_space
group by tablespace_name;
alter tablespace tablespace_name rename to new_tablespace_name;
alter tablespace tablespace_name READ ONLY;——只读
alter tablespace tablespace_name READ WRITE ;——读写
alter tablespace tablespace_name ONLINE;——联机
alter tablespace tablespace_name OFFLINE NORAML / TEMPORARY / IMMEDIATE;——立即切换到脱机状态
注释:将表空间设置为脱机状态时应尽量使用NORAML方式,这样在将表空间恢复到联机状态时不需要进行数据库恢复,只有无法使用NORAML进入脱机状态时,采用TEMPORARY。以上两种方式都失败时才使用IMMEDIATE方式。
Create BIGFILE tablespace tablespace_name
Datafile ‘数据文件保存目录/数据文件.dbf’
Size(大小);
DROP tablespace tablespace_name INCLUDING CONTENTS;----把表空间里的数据文件删除
DROP tablespace tablespace_name CASCADE CONSTRAINTS;-----把表空间中的完整的删除
Create user user_name identified by password default tablespace tablespace_name ;
Grant connect,resource to user_name;(为角色分配权限)
注释:
Oracle提供三种标准角色(role):connect/resource/dba。
1.connect role(连接角色)
connect role是使用Oracle简单权限,这种权限只能对其他用户的表进行访问,包括select/insert/update和delete等。
2.resource role(资源角色)
resource role可以创建他们自己的表、序列、过程(procedure)、触发器(trigger)、索引(index) 和簇(cluster)。
3.dba role(数据库管理员角色)
dba role拥有所有的系统权限,包括无限制的空间限额和给其他用户授予各种权限的能力。
例题:以系统管理员身份登录,创建账号tom,设置tom的默认表空间为neuspace。为tom分配connect和resource系统角色,获取基本的系统权限。
然后为tom分配对用户scott的表emp的select权限和对SALARY, MGR属性的update权限。(8分)
create user tom identified by unlock default tablespace neuspace;
grant connect,resource to tom;
grant select on emp to tom;
grant update (SALARY, MGR)on emp to tom ;
alter user username identified by new_password;
revoke connect,resource from username;
二、表操作
create table table_name (字段1 类型,字段2 类型);
注释:
①默认值和约束若为“无”,则可不写
②唯一约束:constraint table_字段名_uk unique,
③检查约束:constraint table_字段名_ck check(检查条件),
④主键:primary key,
⑤外键:constraint table_字段名_fk references 外连接table名(字段),
⑥给字段添加注释:Comment on column table.字段名 is '注释';
例题:按照如下要求建表
属性 | 类型(长度) | 默认值 | 约束 | 含义 |
---|---|---|---|---|
STUNO | 数值 (8) | 无 | 主键 | 学号 |
SNAME | 变长字符 (12) | 无 | 非空 | 姓名 |
SEX | 字符 (2) | 男 | 无 | 性别 |
BIRTHDAY | 日期 | 无 | 无 | 生日 |
变长字符 (20) | 无 | 唯一 | 电子邮件 | |
SCORE | 数值 (5, 2) | 无 | 检查 | 成绩 |
CLASSNO | 数值 (2) | 无 | 外键,关联到表CLASS的CLASSNO主键 | 班级编号 |
create table student
(
STUNO number(8) primary key,
SNAME varchar2(12) not null,
SEX varchar(2) '男',
BIRTHDAY data ,
EMAIL varchar2(20) constraint stu_email unique,
SCORE number(5,2) check(SCORE beweent 0 and 100),
CLASSNO number(2) references class(CLASSNO);
comment on table table.name is ;
comment on column 表名.字段名 is ;
rename 当前表名 to 新表名;
alter table当前表名 rename to新表名;
Alter table 表名 rename column 当前列名 to 新列名;
Alter table 表名 modify (字段名 字段类型 默认值 是否为空); ————默认名和是否为空可以不写
alter table表名add 字段名 类型;
alter table 表名 drop column 字段名;
Drop table 表名; ————删除表结构
Drop table 表名; ————删除所有的表数据,不删除表结构
Delete from 表名; ————删除表数据,后面可以添加where条件
Delete from table.name where 条件;
update table.name set 字段名 = 值 ; ————后面可以添加where条件
Select * into table_backup from table;
Select * into table_backup in‘数据库名’ from table;
Create table table_name as select * from 已经存在的表名 where 1=2;
Insert into table1 select * from table2 where ;
Select 字段名 into table_backup from table ;
Select 字段名 into table_backup from table where;
Select 字段名 into table_backup from table inner join table1 on;
Create index table_字段名_idx(索引名) on table(字段);