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;
1. 复制表结构及其数据: create table table_name_new as select * from table_name_old 2. 只复制表结构: create table table_name_new as select * from table_name_old where 1=2; 或者: create table table_name_new like table_name_old 3. 只复制表数据: 如果两个表结构一样: insert into table_name_new select * from table_name_old 如果两个表结构不一样: insert into table_name_new(column1,column2...) select column1,column2... from table_name_old
插入验证数据:
-- 插入数据 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;
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
2017-04-02 sql2008拒绝了对对象 (数据库 ,架构'dbo')的SELECT权限