Oracle 数据库 SQL 语句(一)

本文详细介绍了Oracle数据库中创建表、修改表名、添加和删除字段、删除表、插入和查询数据、更新和删除数据、Truncate语句以及各种数据复制的方法。包括主键设置、表和字段注释、条件查询、子查询、分组和聚合函数的使用等核心操作。

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

一、创建表(所有的字符都为英文)
  • create table 表名 (字段 类型,…)
  • 创建表的时候设置主键:create table 表名 (字段1 类型 primary key, 字段2 类型 not null…)
  • 已创建表设置主键:alter table 表名 add constraint 主键名称 primary key(设置主键的字段);
  • 表添加注释:comment on table 表名 is ‘注释内容’。
  • 字段添加注释:comment on column 表名.字段名 is ‘字段注释’;
二、修改表名
  • alter table 原表名 rename to 新表名
三、添加字段
  • alter table 表名 add 字段名 类型
四、修改字段名
  • alter table 表名 rename column 原字段名 to 新字段名
五、删除字段
  • alter table 表名 drop column 字段名
  • 删除多个字段:alter table 表名 drop (字段名1, 字段名2)
六、删除表
  • drop table 表名
七、插入数据
  • 全表插入,格式要求 values 里面的值的数量与顺序要与表中的字段一致
    insert into 表名 values (值1, 值2, …)
  • 指定字段插入
    insert into 表名 (字段名1,…) values (值1,…)
八、查询数据
  • 查询所有数据:select * from 表名。

  • 查询指定字段:select 字段名,… from 表名。

  • 条件查询:select * from 表名 where 条件。

  • 查看某个时间点的表数据:select * from 表名 as of timestamp to_date(‘20190619 1500’,‘yyyymmdd hh24:mi:ss’)。

  • 去重查询:select distinct job from 表名。

  • 模糊查询:select * from 表名 where 字段名 like “%模糊的内容%”。(% 表示多个未知的字符,_ 表示一个未知的字符)。

  • 两个数之间:select * from 表名 where 字段名 between 数值1 and 数值2。

  • 某个字段多个值查询:select * from 表名 where 字段名 in (值1,值2,…)。

  • 某个字段为 null 或者不为 null :select * from 表名 where 字段名 is null / is not null。

  • 关联查询:select one.* , two.* from 表1 one, 表2 two where one.字段名 = two.字段名。

  • 别名查询
    select 字段名 as “自定义名称” from 表名。
    select 字段名 “自定义名称” from 表名。
    select 字段名 自定义名称 from 表名。

  • 升(降)序查询(order by 需要放在最后,asc 升序,desc 降序,不写默认升序)
    select * from 表名 order by 字段名 asc(desc)。
    多个值排序:select * from 表名 order by 字段名1,字段名2…。

  • 分组查询,通常和聚合函数结合使用(group by)
    select * from 表名 where 条件 group by 分组字段 order by 字段。
    条件分组,聚合函数的条件过滤只能用having(having)
    select * from 表名 where 条件 group by 分组字段 having 条件 order by 字段。

  • 子查询:嵌套到 SQL 语句里面的查询 SQL 语句称为子查询,子查询可以嵌套无数层
    select * from 表名 where 字段名 = (select 字段名 from 表名 where 条件)。
    子查询可以写在 where 后面作为条件,可以写在 select 后面作为字段,还可以写在 from 后面作为一张新表,作为新表时必须起别名。

九、更新数据
  • update 表名 set 字段名 = 值 (where 条件)。不加 where 条件则对整张表数据操作
十、删除数据
  • delete from 表名 where 条件。不加 where 条件则删除整张表数据留下空表。
十一、Truncate 语句
  • truncate table 表名; 删除表再创建一样的空表。
  • delete from 表名; 删除数据留下空表。
  • drop table 表名; 删除表。
十二、表数据复制
  • insert into table1 (select * from table2)。
十三、复制表结构
  • create table table1 select * from table2 where 1 > 1。
十四、复制表结构和数据
  • create table table1 select * from table2。
十五、复制指定字段
  • create table table1 as select id, name from table2 where 1 > 1。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值