Oracle常用sql
Oracle不像Sqlserver,并没有提供l默认约束,但提供了默认值,效果一样。
--------------------------- 在建表时设置默认约束---------------------------
create table t1 (tname varchar2(20) default 'yang');
--------------------------- 在修改表时设置默认约束---------------------------
alter table t1 modify (tname varchar2(20) default 'yang');
--------------------------- 创建序列---------------------------
create sequence ZHPT_P_USER_SEQ
minvalue 1
maxvalue 999999999
start with 86283141
increment by -3026
nocache
cycle
order;
--------------------------- 创建存储过程---------------------------
CREATE OR REPLACE PROCEDURE create_table
IS
v_Cursor NUMBER;--定义游标
v_CreateString VARCHAR2(100);--这个变量存放创建表的SQL语句。
BEGIN
v_Cursor := DBMS_SQL.OPEN_CURSOR;--打开游标
v_CreateString := 'CREATE TABLE tp (id int,name varchar2(20))';--创建表的SQL语句。
DBMS_SQL.PARSE(v_Cursor, v_CreateString, DBMS_SQL.V7);执行建表的SQL语句
DBMS_SQL.CLOSE_CURSOR(v_Cursor);--关闭游标
END create_table;
--------------------------- 执行存储过程---------------------------
execute create_table;
---------------------------创建函数执行从A表到B表一条一条导入数据并验证---------------------------
create or replace function cux_user_imp(code out VARCHAR2) RETURN VARCHAR2 is
/*
作者:ocean
主要功能:数据导入测试
创建日期:2015年11月26日
*/
cux_a varchar2(10); --姓名变量
cux_i number;
cux_user p_user_temp%rowtype;
cursor cux_user_cr is
select * from p_user_temp p where p.xb = '1';
begin
delete from p_user;
commit;
cux_i := 1;
for cux_user in cux_user_cr loop
cux_a := cux_user.name;
if cux_a = 'lxx' then
dbms_output.put_line(cux_a);
elsif cux_a <> 'lxx' then
insert into p_user
(p_user.id,
p_user.name,
p_user.age,
p_user.tel,
p_user.csri,
p_user.xb)
values
(cux_user.id,
cux_user.name,
cux_user.age,
cux_user.tel,
cux_user.csri,
cux_user.xb
);
cux_i := cux_i + 1;
dbms_output.put_line(to_char(cux_i) || ' ' || cux_user.name);
commit;
end if;
end loop;
return 'ok';
end;
--------------------------- 简单主键定义如下并设置为自增(只取出主键定义)---------------------------
typeid int not null primary key identity(1,1),
--------------------------- 表和序列的关系,是通过业务逻辑的SQL语句来维护---------------------------
即:insert into table_name (column_name) values (seq_name.nextval);
--------------------------- 向表中加时间字段默认值为系统时间---------------------------
insert into table(j) values(to_date('2000-11-26 00:04:47','yyyy-mm-dd hh24:mi:ss'));
create table test
(id int,
starttime date default sysdate not null );
插入测试数据:
insert into test (id) values (1);
commit;
--------------------------- 创建表的时候调用序列 需要写一个触发器---------------------------
create or replace trigger 你的表名_tri
before insert on 你用到的表
for each row
DECLARE
BEGIN
SELECT 你的序列名.nextval into :new.id from dual;
end test_oo_tri;
--------------------------- A表数据导入B表,如果长度不够截取---------------------------
insert into test_PP(ID,NAME) select o.id,SUBSTR(o.name,0,3) from test_oo o;
insert into pUser p (p.名,p.性别,p.年龄,p.电话) select substr(u.姓名,1,20),u.性别,u.年龄,u.电话 from User u
说明:截取函数oracle中是substr,sqlserver中是substring
不管大于不大于20长度全截取。
--------------------------- A表数据导入B表---------------------------
insert into ZHPT_P_USER select * from PUSER
按字段插入
Insert into B (字段1,字段2,字段3) select 字段1,字段2,字段3 from A;
---------------------------给一个表循环插入1000条数据---------------------------
declare
idx number(22) := 8;
val varchar2(22) := 'test';
begin
loop
idx := idx + 1;
insert into abook2 values (idx, val || TO_CHAR( idx ) );
exit when idx > 1000;
end loop;
end;
--------------------------- 查询表中某个字段的值---------------------------
select * from puser where XM='王茜红统计'
--------------------------- 查询字段备注---------------------------
select * from user_tab_comments where comments like '%处室%'
--------------------------- 插入---------------------------
Insert into table_name 字段 1,字段 2、、字段 n values (字段值 1,字段值 2、、字段值 n );
INSERT INTO lxx VALUES(10,'ACCOUNTING','NEW YORK');
--------------------------- 循环插入---------------------------
begin
for i in 1..10 loop
insert into table_name values (...);
end loop;
end;
--------------------------- 序列---------------------------
AA3349_BFA001_SEQUENCE
sql:
1)-- Create sequence
create sequence AA3349_BFA001_SEQUENCE
minvalue 1
maxvalue 999999999999999999999999999
start with 81
increment by 1
cache 20;
--------------------------- 修改表 字段 结构---------------------------
alter table AA3350
add constraint BFA001 primary key (BFA001)
using index
tablespace FSSR
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table AA3350
is '文件审核表';
-- Add comments to the columns
comment on column AA3350.bfa303
is '文号';
comment on column AA3350.faf006
is '审核日期';
comment on column AA3350.faf007
is '审核人';
comment on column AA3350.faf008
is '审核说明';
comment on column AA3350.bfa314
is '修订状态';
comment on column AA3350.bfa001
is '序号';
-------------------------- 导入、导出--------------------------
导入:imp 数据库名/数据库密码@服务 file=D:\名称.dmp full=y
导出:exp 数据库名/数据库密码@服务 file=D:\名称.dmp
---------------------------建立表空间--------------------------
CREATE TABLESPACE "FSSR"
LOGGING
DATAFILE 'D:\oracle\product\10.2.0\oradata\orcl\FSSR.ora' SIZE 500M
AUTOEXTEND ON NEXT 100M
MAXSIZE UNLIMITED EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO
------------------------ 更新字段-------------------------
update 表名 set 字段名='更新数据'
Oracle常用sql的更多相关文章
- oracle常用SQL语句(汇总版)
Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, ...
- oracle 常用sql语句
oracle 常用sql语句 1.查看表空间的名称及大小 select t.tablespace_name, round(sum(bytes/(1024*1024)),0) ts_sizefrom d ...
- Oracle常用SQL查询(2)
三.查看数据库的SQL 1 .查看表空间的名称及大小 select t.tablespace_name, round ( sum (bytes / ( 1024 * 1024 )), 0 ) ts ...
- Oracle常用SQL查询
一.ORACLE的启动和关闭 1.在单机环境下要想启动或关闭oracle系统必须首先切换到oracle用户,如下: su - oracle a.启动Oracle系统 oracle>svrmgrl ...
- ORACLE 常用SQL查询
一.ORACLE的启动和关闭 1 .在单机环境下 要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下 su - oracle a.启动ORACLE系统 oracle > sv ...
- Oracle常用SQL语句大全
常用Oracle数据库SQL语句汇总. 1.常用操作 --清空回收站purge recyclebin;--查询回收站select * from recyclebin--查询Oracle版本信息sele ...
- Oracle常用sql命令
1.查看数据库归档是开启还是关闭SQL> archive log list 更改数据库归档模式: SQL> shutdown immediateSQL> startup mountS ...
- oracle 常用sql字符函数介绍
常用字符函数介绍 1.ascii 返回与指定的字符对应的十进制数: SQL>select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') ...
- Oracle 常用Sql 语句
Oracle数据库常常被用作项目开发的数据库之一:有时隔段时间没使用就会忘记一些常用的sql语法,所以我们有必要记录下常用的sql 语句,当我们需要时可以快速找到并运用. 1 创建表空间.创建用户及授 ...
- Oracle常用sql语句(一)
# Sql的分类 # DDL (Data Definition Language):数据定义语言,用来定义数据库对象:库.表.列等: CREATE. ALTER.DROP DML(Data Manip ...
随机推荐
- GetComponent
GetComponent 的几种写法: 1.AutoRotation cmp1=(AutoRotation) GetComponent(typeof(AutoRotation)); 2.AutoRot ...
- where,having与 group by连用的区别
select 列a,聚合函数 from 表名 where 过滤条件 group by 列a having 过滤条件 group by 字句也和where条件语句结合在一起使用.当结合在一起时,wher ...
- smarty变量调节器
smarty中变量调解器的作用:在模板中需要对PHP分配过来的变量在输出之前,对变量进行处理 注册变量调解器方式:$smarty->registerPlugin("modifier&q ...
- mysql客户端授权后连接失败问题
在本地(192.168.1.152)部署好mysql环境,授权远程客户机192.168.1.%连接本机的mysql,在iptables防火墙也已开通3306端口.如下:mysql> select ...
- 【转】【Asp.Net】Asp.net发送邮件的两种方法小结
这几天看了一下Asp.net发送邮件方面的东西,记得之前的IIS6上有SMTP服务器,可以直接利用这个进行邮件发送,现在的开发环境是Windows 7,找了半天没有找到,到网络上查了才知道原来wind ...
- Win2008R2配置WebDeploy
一.配置服务器 1.安装管理服务 2.点击管理服务进行配置 3.安装WebDeploy 3.1通过离线安装包方式安装: https://www.iis.net/downloads/microsoft/ ...
- .net程序集强命名(签名)
要想得到强签名的dll有两种情况: 1.给项目添加强命名 在你的项目右键->属性->签名,勾选“为程序集签名”,新建 或 浏览已经新建过的.pfx文件,然后重新build项目,生成的dll ...
- c语言 动态数组
C语言中,在声明数组时,必须明确告诉编译器数组的大小,之后编译器就会在内存中为该数组开辟固定大小的内存.有些时候,用户并不确定需要多大的内存,使用多大的数组,为了保险起见,有的用户采用定义一个大数组的 ...
- Flume的安装与配置
Flume的安装与配置 一. 资源下载 资源地址:http://flume.apache.org/download.html 程序地址:http://apache.fayea.com/fl ...
- sql语句or与union all的执行效率比较
看到一篇文章是讲sql语句or与union all的执行效率比较的,以前没怎么注意这个问题,感觉文章写的不错,转来一看. 文章原链接:http://www.cunyoulu.com/zhuanti/q ...