oracle 筆記1

--建表
create table users(
id number not null primary key,
name varchar2(50) not null,
pwd varchar2(50) not null
);
--建立序列
create sequence users_seq;git

--建立約束
create table userinfo(
id number not null primary key check(regexp_like(id, '^\d{4}$\')),--約束id只能是4位數字
name varchar2(50) not null,
pwd varchar2(50) not null
);
--添加列
alter table userinfo add(sex varchar2(1) not null);
--修改字段長度
alter table userinfo modify(sex varchar2(2));
--修改列名
alter table userinfo rename column id to uerid;
--添加性別約束只能爲男或女
alter table userinfo add constraint ck_sex check (sex = '男' or sex = '女');函數

 

--約束入職日期必須大於出生日期
create table employee(
id number not null primary key check(regexp_like(id, '^\d{4}$\')),
birthday date not null,
hiredate date not null
);
alter table employee add constraint ck_date check(birthday<hiredate);oop

--添加約束
create table employees(
id number not null primary key check(regexp_like(id, '^\d{4}$\')),
name varchar2(20) not null,
sex varchar2(2) not null constraint ck_sexes check(sex in('男','女')),
birthday date not null,
hiredate date not null
);regexp

--根據現有用戶的表建立一張同樣的表
create table emp as select * from zhuhongdan.employee;three

 

 

--字符函數:
--lower(列名|表達式)函數:轉換成小寫
select lower('SQL: Structural Query Language') from dual; --dual是系統的一個虛表(僞表)ip

--upper(列名|表達式)函數:轉換成大寫
select upper('SQL: Structural Query Language') from dual;字符串

--initcap(列名|表達式)把沒個支付的頭一個字母轉換成大寫,其他轉換成小寫
select initcap('SQL is an ENGLISH LIKE language') from dual;it

--concat(列名|表達式,列名|表達式把第一個和第二個字符串鏈接成一個字符串
select concat('SQL alows you to manipulate the data in DB',' without any programming knowledge') from dual;io

--substr(列名|表達式,m,[n])返回指定的子串,該子串從第m個字符開始,其長度爲n
select substr('SQL lets you concentrate on what has to be done',14) from dual;table

--length(列名|表達式)返回列或表達式中字符串的長度。
select length('SQL does no let you concentrate on how it will be achieved') from dual;

--TRIM([leading|trailing|both]要去掉的字符from源字符串)頭(leading)部、尾(trailing)部或頭部和尾部中(both)
select trim('?' from '?SQL*PLUS is the SQL implementation used in an Oracle RDBMS or ORDBMS.') from dual;

--replace(正文表達式,要搜尋的字符串,替換字符串)該函數用於在「正文表達式」中查找「要搜尋的字符串」,若是找到了就用「替換字符串」替代。
select replace('SQL*PLUS supports loops or if statements','supports','does not support') from dual;


--數字型函數:
--round(列名|表達式,n)該函數將列名或表達式所表示的數值四捨五入到小數點的n位
--trunc(列名|表達式,n)該函數將列名或表達式所表示的數值取到小數點的後n位
select round(168.888,1),trunc(168.888,1) from dual;
--mod(m,n)該函數將m除以n並取餘數
select mod(1900,400) from dual;


--日期函數:
/*Year:
yy two digits 兩位年 顯示值:07
yyy three digits 三位年 顯示值:007
yyyy four digits 四位年 顯示值:2007

Month:
mm number 兩位月 顯示值:11
mon abbreviated 字符集表示 顯示值:11月,如果英文版,顯示nov
month spelled out 字符集表示 顯示值:11月,如果英文版,顯示november

Day:
dd number 當月第幾天 顯示值:02
ddd number 當年第幾天 顯示值:02
dy abbreviated 當週第幾天簡寫 顯示值:星期五,如果英文版,顯示fri
day spelled out 當週第幾天全寫 顯示值:星期五,如果英文版,顯示friday
ddspth spelled out, ordinal twelfth

Hour:
hh two digits 12小時進制 顯示值:01
hh24 two digits 24小時進制 顯示值:13

Minute:
mi two digits 60進制 顯示值:45

Second:
ss two digits 60進制 顯示值:25

其它
Q digit 季度 顯示值:4
WW digit 當年第幾周 顯示值:44
W digit 當月第幾周 顯示值:1*/

--獲取當前時間
select sysdate from dual;
--日期和字符轉換函數用法(to_date,to_char)
--獲取年月日轉換成字符串
select to_char(sysdate,'yyyy-mm-dd') from dual;
--將字符串轉換成日期
select to_date('2004-05-07 13:23:44','yyyy-mm-dd hh24:mi:ss') from dual;
--求某天是星期幾
select to_char(to_date('2016-10-11','yyyy-mm-dd'),'day') from dual;
--兩個日期間的天數
select floor(sysdate - to_date('2016-10-10','yyyy-mm-dd')) from dual;

--轉換函數:
--字符串到數值
select '3.14159' + 20 from dual;
--數值到字符串
select '100' || 124 from dual;

--分頁SQL--rownum是從1開始,查詢前五條爲rownum>0 and rownum<=5select * from ( select t.*,rownum from users t) where rownum>0 and rownum<=5; select * from (select t.*,rownum from users t order by id desc) where rownum>0 and rownum<=5;

相關文章
相關標籤/搜索