oracle10g 基本操作语句

本文详细介绍Oracle数据库中的数据操作,包括创建表、数据增删改查、使用临时表、权限分配及表结构调整等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

--创建数据库表成功—————————–
--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 注意已经注明. 参考上编码.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值