------------------------
T3
---------------------------------------------------------------------------
T4
---------------------------------------------------------------------------
T5
---------------------------------------------------------------------------
T6
---------------------------------------------------------------------------
还原scott资料用户
SQL>startup ?/rdbms/admin/utlsampl
SQL>drop user sctt scade;
SQL>select username,sid,serial# from v$session where username='SCOTT';
SQL>alter system kill session '44,21';
4.DDL的管理及操作
数据库对象:表 视图 序列 索引 同义词
命名规则:以字母开头、1-30字符、A-Z/a-z/0-9/_/$、字符开头
database link 128
库名 8
实例名 12
使用内部保留字,小写加上双引号即可
create table "user"....
alter table t1 modify (sal default null);
5.约束
自动命名(sys)、手动命名
列级约束 非空只能在列级定义 逗号隔开
表级约束 空格隔开
非空约束 not null constraint
SQL> create table b(id number not null);
Table created.
SQL> insert into b values(null);
insert into b values(null) *
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."B"."ID")
SQL> alter table b modify (id null);
Table altered.
SQL> alter table b modify (id constraint b_id_null not null);
Table altered.
唯一性约束,自动简历唯一索引
SQL> create table c(id number unique);
Table created.
SQL> insert into c values(1);
1 row created.
SQL> /
insert into c values(1)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C0014385) violated
SQL> select constraint_name from user_constraints where table_name='C';
CONSTRAINT_NAME
------------------------------
SYS_C0014385
SQL>alter table c drop constraint sys_c0014385
Table altered.
SQL> alter table c add constraint a_id_u unique(id);
Table altered.
主键约束
一个表只能有一个主键
主键是唯一的并且非空
可以联合主键,联合主键要求每列都非空
主键唯一定位一行,所有主键也叫逻辑ROWID
主键不是必须的,可以没有
主键是通过索引实现的
索引的名称和主键的名称相同
外键约束
表级列级都可以定义
表级定义关联到子表中的列
执行删除操作时会出现错误,特别注意
检查约束 check constraint
check.....
违反约束 violating constraint
启用/停用 enable disable
建立表使用子查询
create table e as select * from emp; 新表不包含数据
create table e as select * from emp where 0=1;
alter table e read only; 使表只读
alter table e read write;
10g没有只读这个说法,所以只能建成一个试图
SQL> drop table c;
Table dropped.
SQL> show recyclebin;
ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME
---------------- ------------------------------ ------------ -------------------
C BIN$/o6ox8o+iMjgQ2YAqMDn3Q==$0 TABLE 2014-07-19:23:38:02
SQL> flashback table c to before drop;
Flashback complete.
SQL> alter table c add (name varchar2(20));
Table altered.
SQL> select * from c;
ID NAME
---------- --------------------
1
SQL> alter table c modify(name varchar2(28));
Table altered.
SQL> alter table c rename column name to dname;
Table altered.
SQL> alter table c drop column dname;
Table altered.
SQL> alter table c set unused column sex; //标记为不使用,在业务低峰期删除
Table altered.
SQL> alter table c drop unused columns;
Table altered.
SQL> alter table c drop column dname;
Table altered.
6.视图
本身就是一个查询语句
限制数据访问
复杂查询简单化
提供数据独立性
没有自己的数据,来源于查询结果
简单试图 一个表 dml不限制
复杂试图 一个或者多个表 dml限制
create [or replace] [force|noforce] view
.... as subquery
with check option ...
with read only ...
子查询不能包含order by
SQL> select * from session_privs;
PRIVILEGE
----------------------------------------
CREATE SESSION
UNLIMITED TABLESPACE
CREATE TABLE
CREATE CLUSTER
CREATE SEQUENCE
CREATE PROCEDURE
CREATE TRIGGER
CREATE TYPE
CREATE OPERATOR
CREATE INDEXTYPE
SQL> grant create |