Oracle创建表语句详解

 更新时间:2024年07月03日 10:51:56   作者:何以解忧,唯有..  
这篇文章主要介绍了Oracle创建表语句,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

一、前言

oracle 创建表时,表名称会自动转换成大写,oracle 对表名称的大小写不敏感。

oracle 表命名规则:

  • 1、必须以字母开头
  • 2、长度不能超过30个字符
  • 3、避免使用 Oracle 的关键字
  • 4、只能使用A-Z、a-z、0-9、_#S

二、语法

2.1 创建表 create table

-- 创建表: student_info 属主: 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. '修改' 一列或者多列

  • (1) 数据类型
alter table scott.student_info modify address varchar2(100);
alter table scott.student_info modify (id_type varchar(20), id_no varchar2(20));
  • (2) 列名
alter table scott.student_info rename column address to new_address;
  • (3) 表名
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;

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Orcale权限、角色查看创建方法

    Orcale权限、角色查看创建方法

    查看当前用户拥有的系统权限、创建用户、授予拥有会话的权限、授予无空间限制的权限等等,感兴趣的朋友可以参考下哈,希望对你有所帮助
    2013-05-05
  • Oracle SqlPlus设置Login.sql的技巧

    Oracle SqlPlus设置Login.sql的技巧

    sqlplus在启动时会自动运行两个脚本:glogin.sql、login.sql这两个文件,接下来通过本文给大家介绍Oracle SqlPlus设置Login.sql的技巧,对oracle sqlplus设置相关知识感兴趣的朋友一起学习吧
    2016-01-01
  • Oracle如何修改当前的序列值实例详解

    Oracle如何修改当前的序列值实例详解

    很多时候我们都会用到oracle序列,那么我们怎么修改序列的当前值呢?下面这篇文章主要给大家介绍了关于Oracle如何修改当前的序列值的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-05-05
  • 详解Oracle dg 三种模式切换

    详解Oracle dg 三种模式切换

    这篇文章主要介绍了详解Oracle dg 三种模式切换 的相关资料,需要的朋友可以参考下
    2015-12-12
  • 浅谈Oracle数据库的建模与设计

    浅谈Oracle数据库的建模与设计

    浅谈Oracle数据库的建模与设计...
    2007-03-03
  • 使用PLSQL远程连接Oracle数据库的方法(内网穿透)

    使用PLSQL远程连接Oracle数据库的方法(内网穿透)

    Oracle数据库来源于知名大厂甲骨文公司,是一款通用数据库系统,能提供完整的数据管理功能,而Oracle数据库时关系数据库的典型代表,其数据关系设计完备,这篇文章主要介绍了使用PLSQL远程连接Oracle数据库的方法(内网穿透),需要的朋友可以参考下
    2023-03-03
  • Oracle SQL中实现indexOf和lastIndexOf功能的思路及代码

    Oracle SQL中实现indexOf和lastIndexOf功能的思路及代码

    INSTR的第三个参数为1时,实现的是indexOf功能;为-1时实现的是lastIndexOf功能,具体实现如下,感兴趣的朋友可以参考下哈下,希望对大家有所帮助
    2013-05-05
  • Oracle数据库系统使用经验六则

    Oracle数据库系统使用经验六则

    Oracle数据库系统使用经验六则...
    2007-03-03
  • Oracle磁盘排序问题从定位到解决的完整实操指南

    Oracle磁盘排序问题从定位到解决的完整实操指南

    在Oracle数据库运维中,磁盘排序是高频出现的性能问题,不仅会占用大量临时表空间,还会拖慢SQL执行效率,甚至引发数据库整体响应迟缓,本文结合一线运维经验,梳理出发现问题,定位源头,分析原因,优化解决,长期预防的全流程排查方法,需要的朋友可以参考下
    2026-03-03
  • Oracle捕获问题SQL解决CPU过渡消耗

    Oracle捕获问题SQL解决CPU过渡消耗

    本文通过实际业务系统中调整的一个案例,试图给出一个常见CPU消耗问题的一个诊断方法.
    2007-03-03

最新评论