時間:2010-02-24 10:41來源:http://edu.codepub.com/2010/01 作者:網(wǎng)絡(luò)
一、常用語法
view plaincopy to clipboardprint?
--1. 刪除表時級聯(lián)刪除約束
drop table 表名 cascade constraint
--2. 當(dāng)父表中的內(nèi)容被刪除后,子表中的內(nèi)容也被刪除
on delete casecade
--3. 顯示表的結(jié)構(gòu)
desc 表名
--4. 創(chuàng)建新的用戶
create user [username] identified by [password]
--5. 給用戶分配權(quán)限
grant 權(quán)限1、權(quán)限2...to 用戶
ex:grant create session to [username] --此時只能連接到數(shù)據(jù)庫
grant connect,resource to [username] --此時權(quán)限能滿足要求
grant select,delete on scott.emp to [username]
--6. 回收權(quán)限
revoke select ,delete on scott.emo from [username]
--7. 修改用戶密碼
alter user [username] identified by [password]
--8. 下次登錄時提示修改密碼
alter user [username] password expired
--9. 鎖定用戶
alter user [username] account lock
--10. 解鎖被鎖定的用戶
alter user [username] account unlock
--1. 刪除表時級聯(lián)刪除約束
drop table 表名 cascade constraint
--2. 當(dāng)父表中的內(nèi)容被刪除后,子表中的內(nèi)容也被刪除
on delete casecade
--3. 顯示表的結(jié)構(gòu)
desc 表名
--4. 創(chuàng)建新的用戶
create user [username] identified by [password]
--5. 給用戶分配權(quán)限
grant 權(quán)限1、權(quán)限2...to 用戶
ex:grant create session to [username] --此時只能連接到數(shù)據(jù)庫
grant connect,resource to [username] --此時權(quán)限能滿足要求
grant select,delete on scott.emp to [username]
--6. 回收權(quán)限
revoke select ,delete on scott.emo from [username]
--7. 修改用戶密碼
alter user [username] identified by [password]
--8. 下次登錄時提示修改密碼
alter user [username] password expired
--9. 鎖定用戶
alter user [username] account lock
--10. 解鎖被鎖定的用戶
alter user [username] account unlock 二、常用命令
view plaincopy to clipboardprint?
--1. 設(shè)置顯示寬度
set linesize 100;
--2. 設(shè)置每頁顯示條數(shù)
set pagesize 30;
3. 用記事本打開
em a.sql
--4. 執(zhí)行文件a中的代碼,可指定文件的路徑 @d:a.txt
@ a
--5. 根據(jù)用戶名和密碼連接數(shù)據(jù)庫 如果連接超級管理員(sys) 則應(yīng)加上as sysdba;
conn 用戶名/密碼
--6. 顯示當(dāng)前連接的用戶
show user;
--7. 得到當(dāng)前用戶下的所有表
select * from tab;
--8. 查看表結(jié)構(gòu)
desc temp;
--9. 繼續(xù)執(zhí)行上一個查詢語句
/
--10. 清屏
clear scr;
--1. 設(shè)置顯示寬度
set linesize 100;
--2. 設(shè)置每頁顯示條數(shù)
set pagesize 30;
3. 用記事本打開
em a.sql
--4. 執(zhí)行文件a中的代碼,可指定文件的路徑 @d:a.txt
@ a
--5. 根據(jù)用戶名和密碼連接數(shù)據(jù)庫 如果連接超級管理員(sys) 則應(yīng)加上as sysdba;
conn 用戶名/密碼
--6. 顯示當(dāng)前連接的用戶
show user;
--7. 得到當(dāng)前用戶下的所有表
select * from tab;
--8. 查看表結(jié)構(gòu)
desc temp;
--9. 繼續(xù)執(zhí)行上一個查詢語句
/
--10. 清屏
clear scr; 三、常用函數(shù)
·字符函數(shù)
view plaincopy to clipboardprint?
--1. 將小寫字母轉(zhuǎn)換成大寫,dual 為一虛表
select upper('coolszy') from dual;
--2. 將大寫字母轉(zhuǎn)換成小寫
select lower('KUKA') from dual;
--3. 將每個單詞的首字母大寫,其他位置的字母小寫
select initcap('kuKA aBc') from dual;
--4. 連接字符串,但沒有||好用
select concat('Hello',' world') from dual;
--5. 截取字符串,第二個參數(shù)是從第幾個字母開始截取(從1開始,如果是一個負(fù)數(shù),則從結(jié)尾數(shù)起),第三個參數(shù)是需要截取的字母的個數(shù)
select substr('hello',2,3) from dual;
--6. 求字符串長度
select length('hello') from dual;
--7. 替換字符串
select replace('HELLO','L','x') from dual;
--1. 將小寫字母轉(zhuǎn)換成大寫,dual 為一虛表
select upper('coolszy') from dual;
--2. 將大寫字母轉(zhuǎn)換成小寫
select lower('KUKA') from dual;
--3. 將每個單詞的首字母大寫,其他位置的字母小寫
select initcap('kuKA aBc') from dual;
--4. 連接字符串,但沒有||好用
select concat('Hello',' world') from dual;
--5. 截取字符串,第二個參數(shù)是從第幾個字母開始截取(從1開始,如果是一個負(fù)數(shù),則從結(jié)尾數(shù)起),第三個參數(shù)是需要截取的字母的個數(shù)
select substr('hello',2,3) from dual;
--6. 求字符串長度
select length('hello') from dual;
--7. 替換字符串
select replace('HELLO','L','x') from dual; ·數(shù)值函數(shù)
view plaincopy to clipboardprint?
--1. 四舍五入
select round(789.536) from dual;
select round(789.536,2) from dual;
select round(789.536,-1) from dual;
--2. 舍去小數(shù),但不進(jìn)位
select trunc(789.536) from dual;
select trunc(789.536,2) from dual;
select trunc(789.536,-2) from dual;
--3. 求余
select mod(10,3) from dual;
--1. 四舍五入
select round(789.536) from dual;
select round(789.536,2) from dual;
select round(789.536,-1) from dual;
--2. 舍去小數(shù),但不進(jìn)位
select trunc(789.536) from dual;
select trunc(789.536,2) from dual;
select trunc(789.536,-2) from dual;
--3. 求余
select mod(10,3) from dual; ·日期函數(shù)
view plaincopy to clipboardprint?
--1. 返回當(dāng)前日期
select sysdate from dual;
--2. 返回兩個日期之間的月數(shù)
select months_between(sysdate,'16-6月 -09') from dual;
--3. 返回加上指定月數(shù)后的日期
select add_months(sysdate,4) from dual;
--4. 返回當(dāng)前日期之后的下一個星期一的日期
select next_day(sysdate,'星期一') from dual;
--5. 求本月的最后一天
select last_day(sysdate) from dual;
--1. 返回當(dāng)前日期
select sysdate from dual;
--2. 返回兩個日期之間的月數(shù)
select months_between(sysdate,'16-6月 -09') from dual;
--3. 返回加上指定月數(shù)后的日期
select add_months(sysdate,4) from dual;
--4. 返回當(dāng)前日期之后的下一個星期一的日期
select next_day(sysdate,'星期一') from dual;
--5. 求本月的最后一天
select last_day(sysdate) from dual; ·轉(zhuǎn)換函數(shù)
view plaincopy to clipboardprint?
--1. to_char
select to_char(sysdate,'yyyy') year,to_char(sysdate,'mm'),to_char(sysdate,'dd') from dual;
select to_char(sysdate,'yyyy-mm-dd') from dual;
select to_char(sysdate,'fmyyyy-mm-dd') from dual; --取消月和日前面的0
select to_char('20394','99,999') from dual; --分割錢 9表示格式
select to_char('2034','L99,999') from dual; --加上錢幣符號
--2. to_number
select to_number('123')*to_number('2') from dual;
--3. to_date
select to_date('1988-07-04','yyyy-mm-dd') from dual;
--1. to_char
select to_char(sysdate,'yyyy') year,to_char(sysdate,'mm'),to_char(sysdate,'dd') from dual;
select to_char(sysdate,'yyyy-mm-dd') from dual;
select to_char(sysdate,'fmyyyy-mm-dd') from dual; --取消月和日前面的0
select to_char('20394','99,999') from dual; --分割錢 9表示格式
select to_char('2034','L99,999') from dual; --加上錢幣符號
--2. to_number
select to_number('123')*to_number('2') from dual;
--3. to_date
select to_date('1988-07-04','yyyy-mm-dd') from dual; ·通用函數(shù)
view plaincopy to clipboardprint?
--1. 如果為null,則用0代替
select nvl(null,0) from dual;
--2. 類似于 switch...case...
select decode(2,1,'內(nèi)容是1',2,'內(nèi)容是2',3,'內(nèi)容是3') from dual;