SQL常用表操作

工作中作为数据开发人员经常会遇到针对表或表中数据进行操作、修改的情景。本文记录不同数据库的一些常见操作场景,方便使用时快速检索查询。

Hive

创建表

最常见的情形之一就是表的创建,为了规范化,需要对表的命名制定规则,一般体现使用的业务域和使用场景以及更新策略等信息。

-- 指定压缩格式parquet
create table if not exists tableName(
  id int comment '数值型字段注释',
  name string comment '字符串字段注释',
  entry_time timestamp comment '日期注释'
)comment '表注释-一般写表名中文名'
stored as parquet;

-- 指定压缩格式snappy
create table if not exists tableName(
  id int comment '数值型字段注释',
  name string comment '字符串字段注释',
  entry_time timestamp comment '日期注释'
)comment '表注释-一般写表名中文名'
STORED AS ORC
TBLPROPERTIES (
  'orc.compress'='SNAPPY'
);

-- 建立分区
create table if not exists tableName(
  id int comment '数值型字段注释',
  name string comment '字符串字段注释',
  entry_time timestamp comment '日期注释'
)comment '表注释-一般写表名中文名'
PARTITIONED BY (
  entry_time string COMMENT '时间分区')
  
-- 通过select创建表(一般在表字段太多时快速创建,但是没有注释等信息)
CREATE TABLE tableName
   ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
   STORED AS ORC
as
select * from tableName2;

-- 创建表复制源表结构,不复制数据(可以复制源表建表语句包括注释)
create table tableName like tableName2;
插入数据
  • 直接插入数据
-- 一次插入一条
insert into tableName(ID,name,entry_time) values (1,'zhangsan','2023-09-01')
-- 一次插入多条
insert into tableName(ID,name,entry_time) values (2,'lisi','2023-09-01'),																(3,'wangwu','2023-09-01')
  • 插入select结果
-- 增量插入
insert into tableName
select * from tableName2;
-- 全量插入(覆盖前一天数据)
insert overwrite table tableName
select * from tableName2;
添加注释
-- 添加表注释
ALTER TABLE 表名 SET TBLPROPERTIES ('comment' = '标注释')

-- 添加字段注释
alter table 表名 change column 列名 列名 string comment '字段注释';
添加字段
alter table tableName add columns
(
字段1 string comment '注释1',
字段2 string comment '注释2',
);
更新表数据
update tableName set entry_time='2023-09-10' where name in ('zhangsan','lisi')

Gbase

创建表
create table if not exists tableName(
  id int comment '数值型字段注释',
  name varchar(50) comment '字符串字段注释',
  entry_time datetime comment '日期注释'
)comment='表注释'
添加注释
-- 添加表注释
alter table tableName comment '表注释'

-- 	添加字段注释
alter table tableName modify 字段名 varchar(60) comment '字段注释'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值