Oracle 表详解(table)

1 概述

表 table
作用:存储数据
设计规范
尽量遵从 3NF(范式)
名称长度不超过 30 字符(会有报错提醒)
名称只能以 '字母' 开头,可由 '数字、_、$、#' 组成

2 语法

2.1 创建表 create table

-- 属主: scott (默认当前用户)
create table scott.student_info (
  sno         number(10) constraint pk_si_sno primary key,
  sname       varchar2(10),
  sex         varchar2(2),
  create_date date
);

-- 添加注释
comment on table scott.student_info is '学生信息表';
comment on column scott.student_info.sno is '学号';
comment on column scott.student_info.sname is '姓名';
comment on column scott.student_info.sex is '性别';
comment on column scott.student_info.create_date is '创建日期';

-- 语句授权,如:给 hr 用户下列权限
grant select, insert, update, delete on scott.student_info to hr;

插入验证数据:

-- 插入
insert into scott.student_info (sno, sname, sex, create_date)
values (1, '张三', '男', sysdate);
insert into scott.student_info (sno, sname, sex, create_date)
values (2, '李四', '女', sysdate);
insert into scott.student_info (sno, sname, sex, create_date)
values (3, '王五', '男', sysdate);

-- 修改
update scott.student_info si
   set si.sex = '女'
 where si.sno = 3;

-- 删除 
delete scott.student_info si where si.sno = 1; 

-- 提交
commit; 

-- 查询
select * from scott.student_info;

2.2 修改表 alter table

-- 1. 新增 一列 或 多列
alter table scott.student_info add address varchar2(50);
alter table scott.student_info add (id_type varchar2(2), id_no varchar2(10));

-- 2. 修改 一列 或 多列
---- 数据类型
alter table scott.student_info modify address varchar2(100);
alter table scott.student_info modify (id_type varchar(20), id_no varchar2(20));

---- 列名
alter table scott.student_info rename column address to new_address;

---- 表名
alter table scott.student_info rename to new_student_info ;
alter table scott.new_student_info rename to student_info;
   
-- 3. 删除 一列 或 多列
---- 请注意:删除多列时,不需要关键字 column
alter table scott.student_info drop column sex;
alter table scott.student_info drop (id_type, id_no);

2.3 删除表 drop table

-- 删除表结构
drop table scott.student_info;

2.4 清空表 truncate table

-- 清空表数据
truncate table scott.student_info;

2.5 查询表、列、备注信息

权限从大到小: 'dba_xx' > all_xx > user_xx ('dba_xx' DBA 用户才有权限)

1. 查询表信息       select * from dba_tables; -- all_tables、user_tables
2. 查询表的备注信息  select * from dba_tab_comments;
3. 查询列信息       select * from dba_tab_cols t order by t.column_id;
4. 查询列的备注信息  select * from dba_col_comments;

3 扩展

3.1 Oracle 范式详解(3NF)

3.2 Oracle 约束详解(constraints)

  • 2
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼丸丶粗面

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值