- Oracle 数据库
- 常用sql
--查询表是否锁定
select object_name,
machine,
s.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 '128,495';
--查看数据库序列
select * from dba_sequences;
--查看表空间
select * from dba_tablespaces;
--创建表空间
create tablespace TEST2 datafile 'D:\ TEST2.dbf' size 2000m
autoextend on next 2000m maxsize 20480m extent management local;
--创建用户
create user TEST2 identified by TEST2 default tablespace TEST2 quota 2000m on users;
--删除用户
drop user TEST2 cascade;
--用户授权
grant all privileges to TEST2;
--查看序列
select * from user_sequences
--删除序列
drop SEQUENCE BK_SEQ_table
----创建序列
CREATE SEQUENCE SEQ_table --序列名
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
CACHE 10;
----创建触发器
create or replace trigger BK_SEQ_table_trigger
before insert on BK_SEQ_table
for each row
when(new.ID is null)
begin
select SEQ_table.nextval into:NEW.ID from dual;
end;
--新增字段
alter table table add tablename1 varchar(40);
--修改字段类型
alter table table modify table_name DATE;
--修改字段类型长度
alter table table modify(table_name varchar(255));
--增加字段注释
COMMENT ON COLUMN table.ID IS '编号';
--删除字段
alter table bank_manage drop (manage_status);
--对scott 解锁
alter user scott account unlock;
--授予dba
grant dba to scott;
---创建数据库
CREATE DATABASE database-name
--删除数据库
drop database dbname
--添加主键
alter table table add primary key (ID)
--复制表结构和数据
create table table1 select * from table2;
--复制指定字段
create table table1 as select id, name from table2 where 1>1;
--修改表名
alter table table1 rename to table2;
--表数据复制
insert into table1 (select * from table2);
--复制表结构
create table table1 select * from table2 where 1>1;
--排序查询前20条效率高
select * from (select rownum rn, d.* from DJDRIVER d where rownum<=20 )p where p.rn>=10;
--删除表数据
truncate table table
--连接数据库
ecif/[email protected]/oraecif4
--数据库监听
MDMDB_R16UAT =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.240.6.69)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = oraecif4)
)
)