启动服务server和listen
SQLplus登陆命令:
conn / as sysdba
root
切换scott账户 CONN scott/tiger@orcl
切换hr账户 CONN hr/hrpwd@orcl
管理员账户 CONNECT system/systempwd@orcl
1.查看所有用户:
select * from dba_user;
select * from all_users;
select * from user_users;
2.查看用户系统权限:
select * from dba_sys_privs;
select * from all_sys_privs;
select * from user_sys_privs;
3.查看用户对象权限:
select * from dba_tab_privs;
select * from all_tab_privs;
select * from user_tab_privs;
4.查看所有角色:
select * from dba_roles;
5.查看用户所拥有的角色:
select * from dba_role_privs;
select * from user_role_privs;
//设置时间格式,可以正确插入date数据*************
alter session set nls_date_format = 'yyyy-mm-dd';
设置查询结果的显示格式:每页24行,每行100个字符,SAL列的显示格式为$99,990.00,Cname属性占20个字符位
SET PAGESIZE 24
SET LINESIZE 100
COLUMN SAL FORMTA $99,990.00
COLUMN Cname FORMAT a20 ****
授权(系统特权) GRANT CREATE TABLESPACE TO scott
回收权限 REVOKE CREATE TABLESPACE FROM scott
查询权限 SELECT username,privilege,admin_option FROM user_sys_privs;
授权(用户特权)GRANT SELECT,INSERT,DELETE ON emp TO hr;
dba下创建用户 (注:创建完用户后要授权,如GRANT CREATE SESSION TO stu01;)
DROP USER stu01 CASCADE;
CREATE USER stu01 IDENTIFIED BY stu01pwd
GRANT CONNECT,RESOURCE,DBA TO stu01;(注:授予登陆权限,必须的)
DEFAULT TABLESPACE USERS
TEMPORARY TABLESPACE TEMP;
修改用户 ALTER USER user
删除用户 DROP USER user [CASCADE];
dba下创建角色(注:创建完角色后要授权,如GRANT CREATE SESSION,CREATE VIEW
TO r_stu;)
CREATE ROLE r_stu;
把角色授予用户 GRANT r_stu TO stu01;
删除角色 DROP ROLE role;
创建概要文件,先删除再创建
DROP PROFILE stu_profile CASCADE;
CREATE PROFILE stu_profile LIMIT
……
创建同义词(只有dba可以创建,当然可以授权)
CREATE [PUBLIC] SYNONYM synonym_name FOR name;
删除同义词
DROP SYNONYM synonym_name;
创建数据库--
删除表空间 DROP TABLESPACE data_ts1 INCLUDING CONTENTS;
创建表空间 CREATEE [TEMPORARY] TABLESPACE data_ts1
DATAFILE '%ORACLE_HOME%\database\data_ts1.dbf' SIZE 50M REUSE
UNIFORM SIZE 128K;
指定表空间为 DEFAULT TABLESPACE data_ts1 [TEMPORARY TABLESPACE temp_ts1];后面是还指定了临时表空间
创建和应用撤销表空间
CONN system/systempwd@orcl
DROP TABLESPACE undo_ts1 INCLUDING CONTENTS [AND DATAFILES
CASCADE CONSTRAINTS];
CREATE UNDO TABLESPACE undo_ts1
DATAFILE '%ORACLE_HOME%\database\undo_ts1.dbf' SIZE 50M REUSE;
切换表空间
ALTER SYSTEM SET undo_tablespace = undo_ts1;
创建大文件表空间 CREATE BIGFILE TABLESPACE bigfile_ts1同上了
指定用户的默认数据表空间 ALTER USER scott DEFAULT TABLESPACE bigfile_ts1;
调整表空间大小(参考p77) ALTER TABLESPACE data_ts1
ADD DATAFILE '%ORACLE_HOME%\database\data_ts2.dbf' SIZE 1M;
回退段创建与删除(唉……不写了)
利用查询创建表(备份表)
CREATE TABLE emp_bak
AS SELECT * FROM emp;
创建表时定义完整性约束,有列中定义和列外定义。
创建簇表(下面是存储参数) CREATE CLUSTER emp_dept (deptno NUMBER(3))
在表中引用簇 CREATE TABLE cluster_dept(
DEPTNO NUMBER(3),
DNAME VARCHAR2(14),
LOC VARCHAR2(13))
CLUSTER emp_dept(deptno);
创建簇索引(只有创建簇索引之后才可用簇表)
CREATE INDEX emp_dept_index ON CLUSTER emp_dept
删除簇或簇表 DROP CLUSTER table [ INCLUDING TABLES ];
范围分区表(p96)PARTITION BY RANGE (sal)
将emp表数据转入新建的范围分区表part_emp
散列分区表(p98)PARTITION BY HASH (empno)
PARTITIONS 4
STORE IN (ts1, ts2, ts3, ts4);
列表分区表(p100)PARTITION BY LIST (loc)
组合分区表
外部表
对象表(p105)
嵌套表
表的移动,需要dba授权需要移动表的用户
ALTER USER stu01 QUOTA UNLIMITED ON TS1;
表迁移语句
ALTER TABLE stu MOVE TABLESPACE ts1
修改存储参数 ALTER TABLE student
表结构维护:
修改列定义 ALTER TABLE course MODIFY (cname VARCHAR2(20));
增加列 ALTER TABLE student ADD (birthday DATE DEFAULT sysdate);
删除列 ALTER TABLE table DROP (cname) ;
删除禁用列 ALTER TABLE student DROP COLUMN birthday;
重命名列 ALTER TABLE student RENAME COLUMN age TO sage;
修改表名 ALTER TABLE student RENAME TO new_student;
增加完整性约束
表级完整性约束 (一个表中有多个主键时)
删除约束
截断表(删除表数据,而结构还在)
TRUNCATE TABLE score;
查询视图 DESCRIBE
INSERT INTO part_emp SELECT * FROM emp;
数据插入语句 INSERT INTO emp(EMPNO,ENAME,MGR,SAL,DEPTNO)
VALUES(99,'TESTNAME',7934,1200,10);
//having是对分组之后的筛选,与where不同
select deptno,avg(sal) from emp
group by deptno
having sum(sal)>9000
order by deptno asc;//desc降序
//min函数的select语句只能包含他一个,除非这样用
select ename,sal,deptno from emp
where sal=(select min(sal) from emp
where job='MANAGER');
SHOW ERRORS;//显示PL语句错误位置
SET SERVEROUTPUT ON//打开创建成功开关
create or replace view v_test as
select t.terminal_name as 消费终端,t.terminal_area as 所在校区,c.sum_money as 累计消费
from terminal t,(select terminal_id,sum(consume_money)sum_money
from consume group by terminal_id)c
where t.terminal_id=c.terminal_id order by 累计消费 desc;