1、scott用戶
-- 以system管理員登錄鎖定scott用戶
alter user scott account lock;
-- 以system管理員登錄解鎖scott用戶
alter user scott account unlock;
-- 以system管理員用戶設(shè)置scott用戶密碼
alter user scott identfied by tiger;
前提條件:
使用system管理員賬號登錄了,這時解鎖了scott普通用戶
在scott用戶下,有emp員工信息表
-- 前提:在scott普通賬號下,有一個emp表
-- emp 員工信息表
-- empno 員工編號 number數(shù)字類型
--ename 員工姓名 varchar2字符串類型
--job 工作崗位 varchar2字符串類型
--mgr 上級領(lǐng)導(dǎo) number數(shù)字類型
--hiredate 入職日期 date時間類型
--sal 工資,number類型
--comm 獎金,number類型
--deptno 部門編號 number
后面的內(nèi)容都基于scott的emp表進行操作
2、DQL操作 -- 查詢
? ? ? ?DQL操作:對已有的表進行數(shù)據(jù)的查詢和篩選
? ? ? ?select -- 選擇、查詢
? ? ? ?select * from 表名; scott用戶可以省略
? ? ? ?*:通配符,表示所有列和列名同時存在時,應(yīng)寫為:表名.*文章來源:http://www.zghlxwxcb.cn/news/detail-667611.html
2.1、select 查詢語句
-- 1、查詢?nèi)繑?shù)據(jù)
-- 此時在管理員賬號,查詢scott賬號下的emp表
select * from scott.emp;
-- 如果當前是scott,可以省略scott
-- select * from emp;
-- 2、查詢指定列的數(shù)據(jù)
-- select 列1,列2 from 表;
select empno,ename from scott.emp;
-- 3、查詢滿足條件的值
-- 查詢工資>3000的人
select empno,ename from scott.emp where sal >= 3000;
select emp.*,sal from scott.emp where sal >= 3000;
2.2、對數(shù)字列進行計算
-- 4、對數(shù)字列進行運算
/*
+
-
*
/
% -- 取余
*/
select emp.*,sal+200,sal-50,sal*1.3,sal/1.2 from scott.emp;
-- emp.*是查詢到了emp表的所有列,然后后面就可以拼接其他列,一共查詢到12列
-- 對列進行運算,會產(chǎn)生一個新的列
-- 每一列都是獨立的!都是獨立的重新從原有基礎(chǔ)上進行運算,而不是從前一列再運算
2.3、向上ceil/下floor取整
/*
取整:
1、ceil() 向上取整
2、floor() 向下取整
*/
-- 當小數(shù)不為零,ceil向上(往大的數(shù)走)取整
select emp.*,ceil(sal+200.5) from scott.emp;
-- 當小數(shù)不為0,floor往下(往小的數(shù)走)取整
select emp.*,floor(sal+200.2) from scott.emp;
-- 如果小數(shù)部分為0,ceil和floor直接取整數(shù)部分
-- celi和floor并不是四舍五入,如果是-4.1,同時使用celi和floor
-- ceil(-4.1)取到的值是-4(向上取整,往大了?。?-- floor(-4.1)取到的值則是-5(向下取整,往小的?。?/code>
2.4、時間date
/*
時間:date
*/
-- sysdate 系統(tǒng)時間,返回值是 日期
-- current_date 當前時間,返回值是 日期
select sysdate,current_date from dual;
2.5、截取日期
-- 2、截取sal表中的日期:hiredate
/*
a的值為時間列,n的值可以有以下:
1、mm - 返回當月第一天
2、yy - 返回當年第一天
3、dd - 返回當前年月日
4、yyyy - 返回當年第一天
5、d - (星期天)返回當前星期的第一天
6、hh - 當前時間
7、mi - 沒有秒的精準度
*/
select trunc(hiredate,'dd') from scott.emp;
select trunc(hiredate,'yy') from scott.emp;
2.6、四舍五入
/*
四舍五入、截取
1、round(a,n) 四舍五入,n表示小數(shù)位,n省略則四舍五入到整數(shù)部分
2、trunc() 用于截取時間和數(shù)字的函數(shù)
3、trunc(a,n) 用于截取時間和數(shù)字的函數(shù),n表示小數(shù)的位數(shù),n省略表示截取到整數(shù)部分
*/
select * from scott.emp;
-- 1、運算 sal工資,乘1.2然后四舍五入
select round(sal * 1.2,2) from scott.emp;
2.7 列別名
/*
取別名:
1、給列取別名(列可以是已有的列,也可以是運算生成的列) 在低版本語法中需要加上as
select age 別名,name 別名
注意:
1、as可以省略
2、列名不能重復(fù)
3、別名建議不重復(fù),別名可以用雙引號進行包裹
4、別名不要使用關(guān)鍵字
*/
select * from scott.emp;
select ename "名字" from scott.emp; -- 給emp的ename取別名
2.8 表別名
/*
對表取別名
表取別名后,出現(xiàn)表名時候必須要使用別名,否則報錯
別名不能重復(fù),表別名可以使用雙引號
*/
select * from scott.emp a; -- 這是給表取了個別名a
select * from scott.emp "a+1"; -- 這種別名可以用雙引號包括
-- 給表的列取別名
select emp.*,sal/1.2 sal1,trunc(sal/1.2) sal2 from scott.emp;
-- 此時,如果給表取一個別名,這個時候在列的部分使用 emp就不對了
select e.*,sal/1.2 sal1,trunc(sal/1.2) sal2 from scott.emp e;
-- 這個時候emp就應(yīng)該換成別名來代替了,如果不用別名,提示標識符錯誤
-- 例如這樣
select e.ename from scott.emp e;
2.9 where查詢
/*
帶where條件的查詢:
-- 可帶有 > < = <= >= (!+ <>)不等于
-- 比較運算符兩邊的數(shù)據(jù)類型需要一致
*/
-- 精準查詢
select * from scott.emp where deptno = 10; -- deptno 部門號
-- 數(shù)字類型是可以加上 單引號的
select * from scott.emp where deptno = '10';
-- 范圍篩選:> < >= <=
select * from scott.emp where sal < '1000';
2.10 模糊查詢
-- 模糊查詢
-- -1、以 A開頭的: A%
-- -2、以A結(jié)尾的:%A
-- -3、包含A:%A%
-- -4、
-- 語法:查詢 where 條件列 like '模糊條件'
-- 找出以A開頭的
select * from scott.emp where ename like 'A%';
-- 找出以A結(jié)尾的
select * from scott.emp where ename like '%A';
-- 找出包含A的
select * from scott.emp where ename like '%A%';
2.11 用函數(shù)來模糊查詢
-- 在數(shù)據(jù)量大的情況下,link的弊端會體現(xiàn)出來
/*
這個時候,就可以使用到函數(shù)substr()
substr(str,begin,[num])在str中從begin位置開始連續(xù)截取num數(shù)量的字符
可以正向和反向截取
正向從1開始,反向從-1開始
substr(str,begin)在str中從begin位置開始截取str字符串的末尾,如果沒有就返回空值
字符串的最后一位正向數(shù)是字符串的長度 反向數(shù)是-1
1、以A開頭:
substr(字符串,開始的位置,截取的長度) = 'A
substr(字符串,1,1) = 'A'
2、以A結(jié)尾
substr('字符串',-1,1) = 'A'
substr(字符串,-1) = 'A' 從末尾結(jié)尾時,后面的1可以省略
*/
-- 語句:查詢以A開頭
select * from scott.emp where substr(ename,1,1) = 'A';
-- 語句:查詢以A結(jié)尾
select * from scott.emp where substr(ename,-1,1) = 'A';
select * from scott.emp where substr(ename,-1) = 'A';
/*
也可以用字符串的長度函數(shù)來操作
substr(字符串,-length(字符串),1) = 'A'
substr(字符串,length(字符串),1) = 'A'
substr(字符串,length(字符串)) = 'A'
*/
-- 1、以A開頭
select * from scott.emp where substr(ename,-length(ename),1) = 'A';
-- 2、以A結(jié)尾
select * from scott.emp where substr(ename,length(ename),1) = 'A';
select * from scott.emp where substr(ename,-length(ename)) = 'A';
-- 如果在這里給列取了別名,那么在 查詢條件中使用列別名,就會出錯,因為
-- 別名是在查詢結(jié)束以后才會產(chǎn)生,在查詢期間是無效的
--3、查詢員工姓名第三位是A的
select * from scott.emp where ename like '__A%';
select ename,substr(ename,3,1) from scott.emp where substr(ename,3,1) = 'A';
-- 4、查詢第三位是A第五位是E的記錄
select * from scott.emp where ename like '__A_E%';
select ename,substr(ename,3,1),substr(ename,5,1) from scott.emp
where
substr(ename,3,1) = 'A'
and
substr(ename,5,1) = 'E';
2.12 邏輯運算符
/*
邏輯運算符:
and:兩個條件同時滿足,并且
or:兩個條件滿足其一,和
not:不是
-- 執(zhí)行順序:先not、再and,后or
*/
邏輯運算符練習文章來源地址http://www.zghlxwxcb.cn/news/detail-667611.html
-- 1、查詢10號部門和20號部門的員工信息
select * from scott.emp where deptno = 10 or deptno = 20;
-- 2、查詢10號部門中工資超過2000的員工信息(兩個條件同時滿足條件,并且)
select * from scott.emp where deptno = 10 and sal > 2000;
-- 3、查詢10號部門中工資超過2000的員工信息和20號部門且工資小于1000的(兩個條件同時滿足條件,并且)
select * from scott.emp where deptno = 10 and sal > 2000;
select * from scott.emp where deptno = 10 and sal > 2000
or deptno = 20 and sal < 1000;
-- 4、查詢10號和20號兩個部門中,工資都高于2000的員工信息(并且)
select * from scott.emp where (deptno = 10 or deptno = 20) and sal > 2000;
-- 5、查詢不是20號部門的員工
select * from scott.emp where deptno <> 20;
select * from scott.emp where deptno != 20;
-- 6、找出工資大于800的員工 CLERK 和工資大于900的銷售員 SALESMAN
select * from scott.emp where (job = 'CLERK' and sal > 800) or (job = 'SALESMAN' and sal > 800);
到了這里,關(guān)于Oracle-day1:scott用戶、查詢、取整、截取、模糊查詢、別名——23/8/23的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!