`
jdw
  • 浏览: 159285 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

oracle10g 基本操作语句

    博客分类:
  • data
阅读更多

--创建数据库表成功—————————–
--ChaRu数据操作详细SQL记录–
--oracle中显示日期格式为:DD-MON-YY dd是代表日 mon是月份前三个字母大写.yy 年份最后两位实际上存储年是4位 显示的为2位

  select * from customers;
 
  insert into customers(customer_id,first_name,last_name,phone)
  values(1,'chen','ge','15800000000');
 
  insert into customers(customer_id,first_name,last_name,phone)
  values(2,'mar','jie','13800000000');
 
  update customers set first_name='union' where customer_id=1;
 
  delete from customers where customer_id=2;

--如果误删除了数据库记录 可以回滚数据

  rollback;
 
  insert into customers(customer_id, first_name, last_name,phone)
  values(4,'再次ChaRu数据','fuck Again','13600000000');
 
  --ChaRu数据进行单引号和双引号
  insert into customers(customer_id, first_name,last_name)
  values( 5,'测试单引号','Bei”Jing-单引成功了');
 
  insert into customers(customer_id, first_name,last_name)
  values( 6,'测试双引号','The "Great Wall LED"');
 
  select * from customers;

--从一个表向另一个表复制行 (快速的植入数据注意修改了主键为10) 能用select union 测试不成功 可以使用 9i新增的merge语句来快速转移
  insert into customers(customer_id,first_name,last_name);
 
  select 10,first_name,last_name from customers where customer_id=4;

--使用merger来合并行数据 Oracle 9i版本引入了Merge关键之来合并数据

--可以用来将一个表的行合并到另一个表中(如果在转换中需要处理等等 在合并数据)
  create table product_change
  (
    product_id    integer constraint change_pk primary key,
    product_typeid  integer constraint product_type_fkid references product_type(product_id),
    name       varchar2(130) not null,
    description   varchar(130),
    price      number(5,2)
  );


--需求是这样:对于Product 和product_change两个表中product_id相同的行,将Product中各列里值修改成Product_change对应的值. 如果product_id存在
--并匹配 进行更新修改 如果不存在则在Product表ChaRu记录数据 即可 使用merge来操作
  merge into products pro
  using product_change pc on(
    pro.product_id=pc.product_id
  );
--merge into 子句指明了合并操作的目标表(要合并到的表) 命名成一个别名 pro 下面都用这个来替代
--using -on子句指定了一个表连接 上面指定的Product表中Product——id和Product——change表中Product_id建立连接

--当匹配时修改
--when matched then 当一行数据满足了Using…on条件时执行操作 同理而言下面操作
  when matched then
  update set
  pro.product_typeid=pc.product_typeid,
  pro.product_name=pc.name,
  pro.product_content=pc.description,
  pro.product_price=pc.price;

--当不匹配时 ChaRu数据
  when not matched then
  insert(pro.productid,pro.product_typeid,pro.product_name,pro.product_content,pro.product_price)
  values(pc.product_id,pc.product_typeid,pc.name,pc.description,pc.price);
--操作完成


--使用update语句修改行

--定义一个变量
  variable average_product_prices number;
  update products set price=prices*0.75 returning avg(price) into:average_product_prices;

--使用默认值 测试成功
  create table userdefaultdemo
  (
    demo_id integer constraint demo_pk primary key,
    datestatus varchar(200) default 'no placed it is take' not null,
    last_modifieddate date default sysdate not null
  );

  insert into userdefaultdemo(demo_id) values(1);
  drop table userdefaultdemo;

--在更新或ChaRu数据使用Default关键字来设置修改列的值
  update userdefaultdemo set datestatus=default where demo_id=1;
  select * from userdefaultdemo;


--Oracle 10g中创建指定用户并连接数据到数据库

--默认在chendb数据库中创建一个用户
  create user testuser identified by testpass;
--testuser为创建用户用户名
--testpass为创建用户登录密码

--对用户授权限
--登录数据库 connect权限 、创建类似一些诸如表结构的数据 resource权限. 权限由特权用户(例如DBA)使用Grant语句授予的

  grant connect,resource to testuser;
--to 指定为授权的用户

--新用户连接数据库chendb
  connect testuser/testpass; --有语法错误

--新用户创建表(完整的简化版本的创建表语法)

  create [global temporary] table table_name
  (
    colum_name type [constraint constraint_def default default_exp]
  );

  [on commit {delete | preserve} rows]
--控制临时表的有效期 delete说明这些行在事务的末尾就要被删除. preserve说明要在会话末尾删除这些行 默认值为Delete

  tablespace tab_space; --设定数据库占用空间大小

-- global temporary 指定说明当前表的行都是临时的. 称之为临时表. 临时表对当前所有会话都是可见的.但是这些行只是特定于某个会话.

--在Oracle官方上真正完整语法远远比这个要复杂 简化只是常用的设置 以上述语法创建表

  create table order_status
  (
    id integer constraint order_status_pk primary key ,
    status varchar(120) ,
    last_modified date default sysdate
  );

--下面创建一个临时表
  create global temporary table test_orderstatus
  (
    id integer constraint order_statustest_pk primary key,
    status varchar(120),
    last_modifieddate date default sysdate
  ) on commit preserve rows;

--只针对临时表设置当会话结束就删除临时表行数据(Oracle会话如何定义?)

--向临时表ChaRu数据
  insert into test_orderstatus(id,status) values(1,'chenkaiunion 测试临时表数据');
  select * from test_orderstatus;

--当我们断开当前测试用户Testuser 关于数据库chendb连接时 会话就消失 那么关于这个临时表自动被删除

  disconnect; --断开

  connect testuser/testpass; --再次连接查看临时表是否存在
  select * from test_orderstatus; --获得关于表自身一些信息

--查询上面刚刚创建两个表
  select table_name,tablespace_name,temporary
  from user_tables where table_name in ('order_status','test_orderstatus');

--上述查询时一个系统字典表user_tables[其中列 table_name 表名 、tablespace_name-存储该表的表空间(数据库用来存储诸如表子类对象的地方)名. ]
--[temporary 说明该表是否是临时表 如果是则Y 不是则为N]

  select * from user_tables;


--获取表中列的信息
--从user_tab_colums中获取

  select table_name, column_name,data_type,data_length,data_precision,data_scale
  from user_tab_columns where table_name='CHENTEST'; --[表名为全大写]

--对User_tab_colums系统字典表中 Data_precision-【如果为数字列指定了精度 该列就是查询出精度】 data-scale-【数字列小数部分的位数】

--修改表信息
--alert table 主要用于 添加/删除/修改列

--创建表的同时创建主键约束
--无命名
  create table student (
    studentid int primary key not null,
    studentname varchar(8),
    age int
  );
--有命名
  create table students (
    studentid int ,
    studentname varchar(8),
    age int,
    constraint yy primary key(studentid)
  );

--删除表中已有的主键约束
--有命名
  alter table students drop constraint yy;
--无命名可用
  select * from user_cons_columns; --查找表中主键名称得student表中的主键名为SYS_C002715
  alter table student drop constraint SYS_C002715;
--向表中添加主键约束
  alter table student add constraint pk_student primary key(studentid);
 
--禁用约束
  alter table table_name disable constraint constraint_name; --或
  alter table policies disable constraint chk_gender;
--重新启用约束
  ALTER TABLE policies ENABLE CONSTRAINT chk_gender;

--添加列
  alter table test_orderstatus add operator_name varchar(120); --报错【试图访问已经交由事务处理的临时表】 临时表不能被修改
  alter table order_status add operator_name varchar(120);
  select * from order_status; --成功


--修改列【列长度/ 但前提是该列的长度是可以修改的例如 char/Varchar】
--【修改数字列的精度】
  alter table order_status modify status number(20,2);
--【修改列数据类型】
  alter table order_status modify status varchar(50);
--【修改列的默认值】


  alter table order_status modify status varchar(200); --[长度由120增加成200] –成功
  alter table order_status modify status varchar(20);  --【长度由200缩小成20 注意当前表没有任何数据】 –成功

--ChaRu数据
  insert into order_status(id, status) values(2,'chenkai');
  select * from order_status;

--再次缩小列status长度
  alter table order_status modify status varchar(10); --成功了 怪哉!

--【只有在表中没有任何行所有列都为空值时才可以减少列的长度】 但成功了

  alter table order_status add newnumber number(10); --添加新列

--【修改新添加数字列的精度】
  alter table order_status modify newnumber number(5); --成功

--同上【只有在表中没有任何行货所有列都为空值是才可以减少数字列的精度】

--【修改列数据类型】
  alter table order_status modify newnumber char(15); --【由number类型修改char】

--【如果表中还没有任何行或列都为空值 就可以将列修改为任何一中数据类型【包括更短的数据类型】否则只能修改一种兼容的类型类似Varchar 修改成Varchar2】
--【但前提是不能缩短列的长度 才能转换 类似不能将date修改成number类型】

  alter table order_status add newdefault varchar(50) default 'null 默认数据'; --【如果第一次修改 新列中没有值全部添加了默认值】
  insert into order_status(id,status) values(3,'测试');


--【修改列默认值】
  alter table order_status modify newdefault default null@live.cn'; --【修改后只对新添加的列 起了新的默认值作用】
  select * from order_status;

--【删除列】

  alter table order_status drop column operator_name ; --成功

--【添加约束】

--【表示出Oracle中所有约束控制如下:】
--check 【指定一列或一组列必须满足的约束】
--primary key /foreign key /unique/readonly /not null
--check option 指定对视图执行的DML操作必须满足子查询的条件. 后有详解

  A:qizhong从Oracle 9i版本开始独立引入了一个Merge语句.用来快速简单将一个表的合并到另外一个表中.实现的是跨表间数据库合并操作.值得注意 Merge into子句指明了合并操作的目标表. Using……..on子句其实实现的是一个表连接. 上面例子能看出. 而When Matched then 当匹配Using…..on子句条件时操作 同理When not Matched then 实在不匹配是操作.

  B:Oracle 10G 数据库基本同SQL 其中有个Check Option指定对视图执行的DML操作必须满足子查询条件. 详细请查看官方的Oracle SQl手册不在赘述.

  C:在表修改中默认值 数据类型 数字类的精度等 控制上有详细说明. 同SQL雷同出较多. Oracle 10G 注意已经注明. 参考上编码.

分享到:
评论

相关推荐

    Oracle 基本建表语句集

    oracle数据库sql语句操作,基本oracle数据库sql语句集合

    Oracle 10g 学习笔记

    │ oracle 常用傻瓜1000问 - oracle10g - 小呵呵.mht.lnk │ ORACLE 面试问题-技术篇.txt │ oracle10g权限与角色管理初学笔记.txt │ oracle10g的系统视图(sys、system).txt │ oracle10g系统管理之UNDO表空间 - ...

    精通Oracle 10g SQL和PL/SQL

     本书是专门为oracle开发人员而提供的编程指南 通过学习本书 读者不仅可以掌握编写sql语句和pl/sql块的基础知识 而且还可以掌握sql高级特征 正则表达式 flashback查询 merge语句 sql:1999连接 和pl/sql高级特征 ...

    Oracle10g精简绿色版

    在数据库的SQL PLUS工具中用户可以输入不同的SQL语句,该工具可以把数据返回给用户,或是对数据库进行适当的操作。 与以前的版本相比,Oracle 10g具有以下特点。 ● 网格计算:可以将若干低成本的设置集成到一起,...

    Oracle11g从入门到精通2

    《Oracle11g从入门到精通》面向数据库管理人员和数据库开发人员,从实际角度出发,系统地介绍了数据库和Oracle的相关概念和原理、Oracle的数据库管理(如安装与启动,用户权限、备份与恢复等),以及Oracle的应用...

    精通Oracle 10g SQL和PL SQL.pdf

    通过学习《精通Oracle 10g SQL和PL/SQL》,读者不仅可以掌握SQL和PL/SQL的基础知识,而且还可以掌握SQL高级特征(正则表达式、Flashback查询、MERGE语句、SQL:1999连接)和PL/SQL高级特征(记录类型、集合类型、对象...

    精通Oracle10g

    作者以其丰富的SQL数据库工作经验,向读者介绍了从数据库概念到数据库理论,从SQL标准到各种SQL数据库产品,《精通Oracle.10g.Pl.SQL编程》从数据库的编程方法到具体的语句句法,从分布式计算到Internet应用等的有关...

    ORACLE11G宝典.rar 是光盘里面的内容,书太厚咧没法影印啊

     《Oracle11g宝典》以Oracle提供的示例数据库为背景,从基本原理、理论提高、实际操作、经验策略、应用开发等方面,结合命令行方式、开发工具的使用、管理工具的使用、Oracle与Windows之间的关系等知识点,按照学习...

    Oracle 从入门到精通(视频实战版)

    全书以oracle 11g为例,分为4篇,循序渐进地讲述了oracle 11g的基本语法和基本操作,从数据库的安装开始逐步介绍与数据库交互的语句以及管理数据库中的文件、备份与恢复数据库等操作。在数据库应用篇中,还结合.net...

    Oracle Database 11g初学者指南--详细书签版

     本书能使读者快捷地掌握oracle database 11g的基础知识。通过自我评估教程,介绍了核心数据库技术、管理员职责、高可用性以及大型数据库特性。本书带领读者循序渐进地学习数据库设置、管理、编程、备份和恢复。还...

    Oracle11g从入门到精通

    《Oracle11g从入门到精通》面向数据库管理人员和数据库开发人员,从实际角度出发,系统地介绍了数据库和Oracle的相关概念和原理、Oracle的数据库管理(如安装与启动,用户权限、备份与恢复等),以及Oracle的应用...

    Oracle 10g 开发与管理

    本文是由笔者2012年学习oracle数据库时编写的学习札记,其中的题目 多数为老师留下的思考题目。 我相信本文会对初学者使用oracle有一个初步的使用印象。右图为我所参 考的书籍。 目录 第一讲 Oacle关系数据库 ...

    Oracle.11g.从入门到精通 (2/2)

    第6章 Oracle的基本操作 6.1 Oracle的启动与关闭 6.1.1 启动Oracle数据库 6.1.2 关闭Oracle数据库 6.2 表的创建与改进 6.2.1 表的基本概念 6.2.2 表结构设计 6.2.3 表的创建 6.2.4 修改表结构 6.3 索引 6.3.1 索引的...

    oracle基本语句和一些常见用法

    一些基本的SQL语句,包括游标,存储过程等,PLSQL的基本操作手册

    oracle10g课堂练习I(2)

    Oracle Database 10 g :“g”代表网格 1-6 Oracle 数据库体系结构 1-8 数据库结构 1-9 Oracle 内存结构 1-10 进程结构 1-12 Oracle 实例管理 1-13 服务器进程和数据库缓冲区高速缓存 1-14 物理数据库结构 1-...

    Oracle.11g.从入门到精通 (1/2)

    第6章 Oracle的基本操作 6.1 Oracle的启动与关闭 6.1.1 启动Oracle数据库 6.1.2 关闭Oracle数据库 6.2 表的创建与改进 6.2.1 表的基本概念 6.2.2 表结构设计 6.2.3 表的创建 6.2.4 修改表结构 6.3 索引 6.3.1 索引的...

    JSP+Servlet+EJB3.0+Oracle10g火车售票系统

    本系统主要实现火车查询售票功能,主要的功能如下: 客户端管理功能: (1)动车基本信息管理模块;...4.本设计采用JSP+Servlet+EJB3.0+Oracle10g工具进行开发,最后通过JDBC进行与数据库的相关的链接。

    oracle从入门到经通PPT

    全书以Oracle 11g为例,分为4篇,循序渐进地讲述了Oracle 11g的基本语法和基本操作,从数据库的安装开始逐步介绍与数据库交互的语句以及管理数据库中的文件、备份与恢复数据库等操作。在数据库应用篇中,还结合.NET...

Global site tag (gtag.js) - Google Analytics