update resident set tall=(mod(id,20)+160);
update resident set gender='女' where mod(id,2)=1;
将两个列名相同的表的记录 合成一张表
select trim(t1.*) from BMNC_PMARTVW.T98_OD_ROUTE_DD t1;
参数:num-被补齐的字符串
6-补齐到几位。
‘0’-用哪个字符补齐。如果未设置,则添加空格
select lpad(num,6,'0') from shopMBase;
结果:
LPAD(NUM,6,'0')
---------------
000379
000381
000384
000385
000387
更新:
update shopmbase set num=lpad(num,6,'0');
指令:a||b
select num||'01' from shopmbase
用法1:
select max(field) from table
举例:
select max(ddate) from tb_oil;
用法2:
select f1,max(f2) from table group by f1;
结果:
常见错误,缺少group by f1:
Select TO_CHAR(BirthDate, 'YYYY-MM-DD') From resident;
to_date('"+resi.BirthDate.toString()+"','yyyy-mm-dd');
select engiID,count(engiID) from shopMbase group by engiID;
ENGIID COUNT(ENGIID)
---------- -------------
1 2852
Null值用0替换
select a.ID,a.Name,nvl(b.sum,0) as totalSum from shopengi a
left join (select engiID,count(engiID) as sum from shopMbase group by engiID) b
on a.ID=b.engiID;
select a.ID,a.name,b.allCount from attrshop a right join (select district,count(*) as allCount from shopmbase group by district) b
on a.ID=b.district;
单独查看最新时间的记录,使用max(fieldA), fieldB group b field B,不能select出其他字段
select wingname,max(ddate) from tb_oil group by wingname;
select * from shopmbase where address like '%北京%';
说明:active类型是varchar2; 目标:取北京地区的活跃商户
select * from shopmbase where (address like '%北京%' or Name like '%北京%') and active is not null;
行数:3042行
背景:oracle不支持 top语句
select * from
(select ID from shopmBase order by ID asc)
where rowNum<=3;
rownum作用:为select 结果添加列rownum (行号)。
为何要用别名:因为rownum不能作为列名。
为何用子查询:因为rownum是执行完select之后再添加伪列。
select b.* from
(select a.*,rownum rn from (select * from shopmbase where hyid=10 and isactivated=2 and district=4 order by id) a) b
where rn>=5 and rn<=10;
结论:3层select
第一层 非rn条件和order
第二层 select a.*, rownum rn
第三层 select b.*, rn where rn条件
Ajax,时间控制在0.2s以内,则不必blockUI
t.no as no,
select top 1 t1.* from BMNC_PMARTVW.T98_OD_ROUTE_DD t1;
说明: 使用简称, 之后 from 全称 简称, 是简化查询的 重要习惯。
select t1.f1,t1.f2 from tableA t1
left join tableB t2
on t1.id=t2.id
select a.*,b.attrvid from resident a
left join resiattr b
on a.id=b.resiid;
select attrvid.attrvalue,c.ResiID,c.NAME from attrvid
right join
(select a.resiID,a.attrVID,b.NAME from resiattr a
left join resident b
on a.resiid=b.id) c
on attrvid.attrvid=c.attrvid;
select t1.prod_id as id,
t1.prod_nme as name,
trim(t1.prod_cate_cd) as categoryCode,
trim(t3.prod_cate_desc) as categoryName,
trim(t2.payment_type_code) as paymentTypeCode,
trim(t4.payment_type_name) as paymentTypeName
from bmnc_pdatavw.t50_product t1
left join bmnc_pdatavw.t50_prod_payment_his t2
on t1.prod_id = t2.prod_id
and t2.start_dt <= current_date
and t2.end_dt > current_date
left join bmnc_pcodevw.t99_prod_cate_cd t3
on t1.prod_cate_cd = t3.prod_cate_cd
left join bmnc_pcodevw.t99_prod_payment_cd t4
on t2.payment_type_code = t4.payment_type_code
where t1.effect_dt <= current_date
and t1.invalid_dt > current_date
select e.*,f.name as dName from (select c.*,d.value as isVal from (select a.*,b.value as hyVal from shopmbase a
left join shopothattr b
on a.hyid=b.id) c
left join shopothattr d
on c.isactivated=d.id) e
left join shopdistrict f
on e.district=f.id;
工具:Oracle SQL Developer
选择表,右键-导入数据
d
工具:Oracle SQL Developer
具体:菜单->工具->数据库导出
成果:.sql文件
备注:导出时,不勾选“方案”,这样sql语句中不会包含 用户.
工具:Oracle SQL Developer
具体:菜单->文件->打开, 选择.sql文件, 执行
第一步: 清空原来的表,序列,触发器
drop table shopmbase;
drop table shopOthattr;
drop table shopDistrict;
drop table shopEngi;
drop sequence seq_attrShop;
drop sequence seq_shopEngi;
drop sequence shm_sequence;
第二步:使用导入脚本.sql
在xml中写查询语句
可以插入 判断(if), 循环(foreach)
.xml
#{变量名}
<update id="delete" parameterType="long">
<![CDATA[
update tf_fare_proposal
set del_flg = 1
where id = #{id}
]]>
</update>
select * from v$version
序号 |
规则 |
举例 |
1 |
Insert,select这些关键字不区分大小写 |
|
2 |
表名不区分大小写 |
|
3 |
列名区分大小写,列名只能用双引号 |
|
4 |
字符串只能用单引号 |
|
1>varchar2的默认值是null. 某列类型是varchar2, 插入记录,若该列未设置值,则列值为null.
insert into resident(NAME) values('测试用户1');
select * from resident where NAME='测试用户1';
2>用 IDNUM is null来 表示 IDNUM是空
select * from resident where NAME='测试用户1' and IDNUM is null;
select * from resident where NAME='测试用户1' and IDNUM=null;
select * from resident where NAME='测试用户1' and IDNUM='';
3>’’不是null.
4>如果列值是null,
用Java的 Result.getString(“IDNUM”)将 提示 列名无效
是null column is null
不是null column is not null
create tablespace gxktablespace
datafile 'F:\TableSpaces\gxkTableSpace\gxktablespace.dbf'
size 32m
autoextend on
next 32m maxsize 2048m
extent management local;
效果:在zfmi文件夹下就能看到表空间文件gxktablespace2.dbf了
临时表空间:
create temporary tablespace tempTestTS
tempfile 'F:\TableSpaces\gxkTableSpace\tempTestTS.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
drop tablespace zfmi_temp including contents and datafiles;
效果:在zfmi文件夹下表空间文件消失。
create user test identified by test
default tablespace testTS;
GRANT connect,resource,dba TO test;
drop user gxk;
Grant dba 登录身份 Normal
Grant sysdba 登陆身份 sysdba
Unlimited tablespace 允许 gxk使用任意表空间
//自己写的语句
create table tb_Test(
"ID" number not null primary key,
"Name" varchar(50) not null,
"Age" number,
"Job" varchar(50)
);
//sql developer生成
CREATE TABLE "SYSTEM"."TB_TEST"
( "ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(50 BYTE) NOT NULL ENABLE,
"AGE" NUMBER,
"JOB" VARCHAR2(50 BYTE),
CONSTRAINT "TB_TEST_PK" PRIMARY KEY ("ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "SYSTEM" ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "SYSTEM" ;
drop table tb_Test
create table TB_PERATTRVALID(
PERID NUMBER NOT NULL,
ATTRVALID NUMBER NOT NULL,
CONSTRAINT TB_PERATTRVALID_PK PRIMARY KEY(PERID,ATTRVALID),
CONSTRAINT TB_PERATTRVALID_FK1 FOREIGN KEY(PERID) REFERENCES TB_TEST(ID),
CONSTRAINT TB_PERATTRVALID_FK2 FOREIGN KEY(ATTRVALID) REFERENCES TB_ATTRVAL(ATTRVALID)
)
create table TB_ATTRVAL(
ATTRVALID NUMBER PRIMARY KEY,
ATTRVALNAME VARCHAR(100) NOT NULL,
ATTRKINDID NUMBER,
CONSTRAINT TB_ATTRVAL_FK1 FOREIGN KEY(ATTRKINDID) references TB_ATTRKIND(ID)
)
1.创建sequence
CREATE SEQUENCE emp_sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
NOCACHE -- 不建缓冲区
以上代码完成了一个序列(sequence)的建立过程,名称为emp_sequence,范围是从1开始到无限大(无限大的程度是由你机器决定的),nocycle 是决定不循环,如果你设置了最大值那么你可以用cycle 会使seq到最大之后循环.对于nocache顺便说一下如果你给出了cache值那么系统将自动读取你的cache值大小个seq
,这样在反复操作时会加快运行速度,但如果遭遇意外情况如当机了或oracle死了,则下次取出的seq值将和上次的不连贯.(如果连不连贯无所谓建议用cache,因为时间就是金钱呀!跑题了!)
书接上文,你只有了表和序列还不够,还需要一个触发器来执行它!代码如下:
2.创建trigger
CREATE TRIGGER "tgr_tbTest" BEFORE
INSERT ON TB_TEST FOR EACH ROW WHEN (new.id is null)
begin
select emp_sequence.nextval into :new.id from dual;
end;
3.插入数据测试
INSERT INTO example(Name,phone,address) Values('Cao','56498543','Heibei');
insert into tb_test("Name","Age","Job") values('sasuke',28,'SE');表名,关键字不区分大小写。
列名区分大小写。
delete from tb_test;
备注:不需要 *;
无论何时,列名一定要用 引号包围起来,并且区分大小写。
ALTER TABLE TB_TEST ADD COMPANY VARCHAR2(100);
如果列存在 约束,先删除约束,再删除列。
不存在,则直接删除
ALTER TABLE 表名 DROP CONSTRAINT 默认约束名
GO
ALTER TABLE 表名 DROP COLUMN 字...
CREATE SEQUENCE emp_sequence
INCREMENT BY 1 -- 每次加几个
START WITH 1 -- 从1开始计数
NOMAXVALUE -- 不设置最大值
NOCYCLE -- 一直累加,不循环
NOCACHE -- 不建缓冲区
drop sequence emp_sequence;
CREATE TRIGGER "触发器名称" BEFORE
INSERT ON example FOR EACH ROW WHEN (new.id is null)
begin
select emp_sequence.nextval into :new.id from dual;
end;
Person
列名 |
描述 |
ID |
人ID |
Name |
人姓名 |
PerAttrVaID (双外键;双外键成主键)
列名 |
描述 |
attrValID |
属性值ID |
personID |
人ID |
AttrVal
列名 |
描述 |
attrValID |
属性值ID (主键) |
attrVal |
属性值 (非空) |
attrKindID |
种类ID (外键) |
AttrKind
列名 |
描述 |
attrKindID |
属性种类ID (主键) |
Name |
种类名 |
//查看所有属性名和属性值
select A.ATTRVALID,B.NAME,A.ATTRVALNAME FROM TB_ATTRVAL A
LEFT JOIN tb_attrkind B
ON A.ATTRKINDID=B.KINDID;
教育程度可能取值
描述:
Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found
原因:Oracle并发 Processes数到达上限,拒绝当前请求。