三、DDL(数据库定义语言)

三、DDL(数据库定义语言)

1、创建数据库表

可以将sql文本直接导入数据库中

相关语句:@path

#假设 ot_schema.sql 文件位于 F:\website\oraok\ot 目录中,则执行下面的语句 :
SQL>@F:\website\oraok\ot\11g\ot_schema.sql

创建数据库的语句:

create table username.tablename{
	name 类型;
}
#例子
-- 创建表: 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;

2、修改数据库表(alert table)

语句:

alert table username.tablename add(添加字段)/modify(修改字段类型)/rename column(修改字段名称) / rename to(修改表名) / drop column(删除一列) /drop(删除多列)
#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);

3、删除表(drop table)

语句格式:

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

4、清空表 truncate table

语句格式:

truncate username.tablename;
#-- 删除表内数据
truncate table scott.student_info;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值