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;

--创建约束
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);

--添加约束
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
);

--根据现有用户的表创建一张一样的表
create table emp as select * from zhuhongdan.employee;

--字符函数:
--lower(列名|表达式)函数:转换成小写
select lower('SQL: Structural Query Language') from dual; --dual是系统的一个虚表(伪表)

--upper(列名|表达式)函数:转换成大写
select upper('SQL: Structural Query Language') from dual;

--initcap(列名|表达式)把没个支付的头一个字母转换成大写,其余转换成小写
select initcap('SQL is an ENGLISH LIKE language') from dual;

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

--substr(列名|表达式,m,[n])返回指定的子串,该子串从第m个字符开始,其长度为n
select substr('SQL lets you concentrate on what has to be done',14) from dual;

--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<=5
select * 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;

猜你喜欢

转载自www.cnblogs.com/luckgood/p/11737212.html
今日推荐