oracle基本语句

--创建表

CREATE TABLE 表名 (

t_name varchar(20),

t_password varchar(20)

) --tablespace 表空间名字;


--查询表TEST2的字段类型
select * from user_tab_columns where table_name='TEST2'

 

--向test数据表插入一条数据
insert into test values ('name1','password1');

insert into test select name,password from test2;--把test2的name个password插入test

insert into test( name, password) values('name1','password1'); --表字段和值对应

 

--从test数据表删除一条数据
delete from test where name='name1';

 

--从test数据表更改数据

update test

set name=‘name2’,password='password2'

where name=‘name1’;

 

--从test数据表查询数据

select name,password from test where name='name1' group by name having password = 'password2' order by id asc;

group by 的字段必须在select中被查询,having会在group by 之后执行,按照order by之后的字段排序, asc从小到大(默认),desc从大到小。

 

--去重:distinct 

select distinct name from test;--对 name去重


--范围:between
where creat_time between sysdate-7 and sysdate;--creat_time 在7天以内,sysdate为系统当前时间


--相似查询
where name like 's'||'%' name的值以s开头

--连接字符串
a||b


--字符串改为小写
Lower('A') 相当于a

 

--取前十
where rownum<11 order by ...

 

--两表联合查询
name key1
a 1
b 2

name key2
a 3
b 4

name key1 key2
a 1 3
b 2 4


select 字段名列表 from 表名 [inner] join 表名 on 连接条件 [和where条件一样的表达式]

select * FROM tes1 t1 INNER JOIN test2 t2 ON t1.name = t2.name; --两表联合查询

name key1
a 1
b 2

name key2
a 3
b 4

name key
a 1
b 2
a 3
b 4
集合查询(无重复):select * from table_name union
select * from table_name;
集合查询(有重复):select * from table_name union all
select * from table_name;
差 集 查 询:select * from table_name minus
select * from table_name;
--例子
select sum(num1) num from (
select t.providername as name,count(id) as num1 from gyk.tv_wlml_exchangeresource t group by t.providername
UNION ALL
select t.odcontactname as name,count(roid) as num1 from gyk.tv_wlml_wlml t group by t.odcontactname
)

 


--datalink(用于跨数据库查询)
CREATE DATABASE LINK db_wlzh CONNECT TO wlzh IDENTIFIED BY wlzh USING '(DESCRIPTION =(ADDRESS_LIST =(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.19)(PORT = 1521)))(CONNECT_DATA =(SERVICE_NAME = wlzh)))'

 


创建用户-create user 用户名 identified by 密码;(需要SYS/SYSTEM权限才能建立用户)
赋予权限-grant resource to 用户名;(建表权限)
赋予查询权限-grant select on emp to 用户名;
赋予修改权限-grant update on emp to 用户名;
赋予所有访问权限-grant all on emp to 用户名;
--------------------------------------------------------
收回查询权限-revoke select on emp from 用户名;
传递权限-grant select on emp to 用户名2 with grant option;
账户锁定-
creata profile 名称 limit failed_login_attcmpts 输入次数限制 password_lock_time 锁定天数;
------------------------------DBA权限登录
alter user 想要锁定的用户名 profile 名称;
------------------------------DBA权限登录
解锁用户锁定-alter user 用户名 account unlock;
定期修改密码-create profile 名字 limit password_life_time 天数 password_grace_time 宽限天数;

切换用户-conn system/密码;
更改密码-password 用户名;
删除用户-drop user 用户名 cascade(删除用户及用户建立的所有表);


查询同样结构两表中empno的不同数据-select * from emp_tmp where empno not in(select empno from emp);

 


select to_char(add_months(sysdate,-1),'yyyymmdd') from dual;--取上个月的日期;add_months假如日期是月的最后一天,ri将会自动调整为更改后的最后一天。
select to_char((sysdate-30),'yyyymmdd') from dual; ---取当前日期前30天日期;

 

--函数

FUNCTION HZ
(inputStr in VARCHAR2)
RETURN VARCHAR2 iS
outputStr varchar2(10);
BEGIN
SELECT c_spell INTO outputStr FROM BASE$CHINESE WHERE C_WORD = inputStr;
RETURN outputStr;
END HZ;

 

 --rank() 排序

select name,score,rank() over(partition by name order by score) tt from t; --先按name排序,再按分数排序, rank()是名次,有并列的时候会占用下一名次 335
NAME SCORE TT
---------- ----- ----------
数学 67 1
数学 77 2
数学 78 3
数学 88 4
数学 99 5
语文 60 1
语文 70 2
语文 80 3 <----
语文 80 3 <----
语文 90 5
select name,score,dense_rank() over(partition by name order by score) tt from t;--dense_rank()是名次,有并列的时候不会占用下一名次 334
NAME SCORE TT
---------- ----- ----------
数学 67 1
数学 77 2
数学 78 3
数学 88 4
数学 99 5
语文 60 1
语文 70 2
语文 80 3 <----
语文 80 3 <----
语文 90 4
select name,score,row_number() over(partition by name order by score) tt from t; --row_number不会出现同名次(其实是行号)
NAME SCORE TT
---------- ----- ----------
数学 67 1
数学 77 2
数学 78 3
数学 88 4
数学 99 5
语文 60 1
语文 70 2
语文 80 3 <----
语文 80 4 <----
语文 90 5

posted @ 2019-06-25 11:12  不咬人的兔子  阅读(247)  评论(0编辑  收藏  举报