oracle 入门级基本操作

--添加字段语法
alter table test add(date varchar2(32));
--修改字段类型语法
alter table test modify date default sysdate;
--删除某个字段语法
alter table test drop column date;
--重命名字段语法
alter table test rename column date to startTime;
--重命名表名语法
alter table test rename to new_test
--创建序列
create sequence USER_SEQUENCE
minvalue 1
maxvalue 99999999999999999999
start with 139721
increment by 1
cache 10
cycle;
--查看剩余表空间
	--如果用户权限不管请使用dba权限的用户查看或者登录sys用户给当前用户授权
		--sys/admin123 用户登入数据库grant dba to user_name;
select a.tablespace_name,
       a.bytes / 1024 / 1024 "total mb",
       (a.bytes - b.bytes) / 1024 / 1024 "used mb",
       b.bytes / 1024 / 1024 "free mb",
       round(((a.bytes - b.bytes) / a.bytes) * 100, 2) "percent_used"
  from (select tablespace_name, sum(bytes) bytes
          from dba_data_files
         group by tablespace_name) a,
       (select tablespace_name, sum(bytes) bytes, max(bytes) largest
          from dba_free_space
         group by tablespace_name) b
 where a.tablespace_name = b.tablespace_name
 order by ((a.bytes - b.bytes) / a.bytes) desc;
 --
删除表数据	
***谨慎使用***
==========
 (drop > truncate > delete) 
--快速删除并且不记录日志删除表数据 truncate table test;
--delete语句为DML(data maintain Language),这个操作会被放到 rollback segment中,事务提交后才生效 delec table
应用范围:
    TRUNCATE 只能对TABLE;
    DELETE可以是table和view
	TRUNCATE和DELETE只删除数据DROP则删除整个表(结构和数据)。
使用场景分析
------
 1. 如果想删除部分数据用delete,注意带上where子句,回滚段要足够大;
 2. 如果想删除表,当然用drop;
 3. 如果想保留表而将所有数据删除,如果和事务无关,用truncate即可;
 4. 如果和事务有关,或者想触发trigger,还是用delete;
 5.如果是整理表内部的碎片,可以用truncate跟上reuse stroage,再重新导入/插入数据
查询表数据:
=========
	 --统计全表数据量
select count(1) from test t;
	 --是否包含字符串
select * from test t where instr(t.content,'电脑')>1;
	 --替换字符串
select replace(content,'电脑','笔记本') from test t;
	 --重复主键
select count(1) from test where id in(select id from test group by id having count(id)>1);
	--行转列查询语法
SELECT REGEXP_SUBSTR ('1,2,3', '[^,]+', 1,rownum)from dual connect by rownum<=LENGTH ('1,2,3') - LENGTH (regexp_replace('1,2,3', ',', ''))+1;
--删除重复主键保留最大主键
delete from test where (id) in (select id from test group by id having count(id) > 1) and ROWID NOT IN
 (select min(ROWID) from test group by id having count(*) > 1); 
	--快速备份表数据
	 create table new_test as select * from test;
	--查询锁表语句:
select object_name,machine,sid,s.serial# from v$locked_object l, dba_objects o, v$session s where l.OBJECT_ID=o.object_id and l.session_id=s.sid;
--alter system kill session '928,45981';
	--杀掉进程'sid,serial'
--alter system kill session '1422,2877';
--设置数据库非归档模式
alter system set log_archive_start=true scope=spfile;

管理表空间:
=========
--查询表空间中数据文件具体位置和文件名,表空间名
Select * FROM DBA_DATA_FILES;
--创建自增长表空间(以1g自增长扩展表空间)Linux
create tablespace test
logging 
datafile'+DATA/orcl/datafile/test1.dat'
size 100m autoextend on next 1024m maxsize unlimited extent management local;
--创建自定义大小的表空间(这里设置给30g)Linux
create tablespace test2 
logging 
datafile'+DATA/orcl/datafile/test2.dat'
size 30g autoextend off extent management local;
--扩展指定的表空间大小 (test1表空间一次扩大10g)Linux
alter tablespace test add datafile '+DATA/orcl/datafile/test1.dat'size 10g;
--创建本地ORCL表空间 Windows
create tablespace test 
logging 
datafile'D:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\test1.DBF'
size 10m autoextend on next 1024m maxsize unlimited extent management local;
--修改用户在表空间上限额的命令
alter user test用户 quota unlimited on test表空间

用户管理
=========
--创建用户
create user test identified by test123
--账号解锁
alter user test用户 account unlock 
--修改账号密码
alter user admin identified by admin123;
--将密码设置为永不过期
alter profile dsfault limit password_life_time unlimited;
--登录dba权限用户给当前用户授权
grant dba to test用户
--回收权限
revoke dba from test;
--一般用户只给两个权限 查询权限/连接权限
grant connect to test;
grant resource to test;
--删除用户基本语法:
drop user 用户名;
--删除用户和表空间(该表/表空间的主键是另一个表的外键,如果不用cascade关键字就会报错)
drop  tablespace test including contents and DATAFILES CASCADE CONSTRAINTS;
drop user test cascade;

基本创建索引的原则:
=========
 1. 索引会加快查询速度,但是会影响更新的速度,因为更新后要维护索引。
 2. 索引不是越多越好,要是频繁查询的where条件列上创建索引。
 3. 小表或唯一值极少的列上不要建索引,要在大表以及不同内容多的列上创建索引。

--创建索引
create index P_TEST_startTime on P_TEST (startTime)
  tablespace test
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
--查看索引
show index from P_TEST
--删除索引
drop INDEX P_TEST_startTime ON P_TEST

猜你喜欢

转载自blog.csdn.net/x15270772831/article/details/82534835