基于Oracle的数据定义语言DDL

本文详细介绍了如何使用Oracle的数据定义语言DDL进行数据库操作,包括创建Student, Course, SC三张基本表,修改表结构,创建索引,删除表及索引,以及插入和查询数据。通过具体示例展示了ALTER TABLE、CREATE INDEX和DROP语句的用法。" 7106946,1055894,Android视频文件上传实践,"['Android开发', '文件上传', 'HTTP协议', 'Socket编程']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 分别建立Student,Course,SC基本表。

create table Student
(
   sno char(5) primary key,
   sname varchar(10) unique,
   ssex char(2) not null,
   sage number(3) default 0,
   sdept varchar(10)
);
create table Course
(
   cno char(1) primary key,
   cname varchar2(15) not null,
   cpno char(1),
   ccredit number(1)
);
create table SC
(
   sno char(5) not null,
   cno char(1) not null,
   grade number(3),
   primary key(sno, cno),
   foreign key(sno) references Student(sno),
   foreign key(cno) references Course(cno)
);
综上可看出,定义时公式为:
create table 表名称
(
   数据名称 类型(限制常量) [限制类型],
   [限制类型(名称) references 表名称(数据名称) ]
);

二. 部分操作

  1.  显示基本结构:DESC 基本表名;      
DESC Student;
DESC Course;
DESC SC; 

   2.   显示用户模式内的基本表和视图:select * from cat;

select * from Student;
select * from Course;
select * from SC;

   3.   用alter table语句修改基本表结构:

     (1)对Student表增加“籍贯”属性列,varchar(20);     

alter table Student add native_place varchar(20);

     (2)对Student表增加“入学时间”属性列,日期型;

alter table Student add entrance_time date;

     (3)对Course和SC表更新涉及“课程号”,“先行课程”属性列长度为2;

alter table Course modify cno char(2);
alter table Course modify cpno char(2);
alter table SC modify cno char(2);

     (4)删除学生姓名必须取唯一值得约束;

alter table Student drop unique(sname);
综上,alter table语句基本格式为:
alter table 表名 add 新属性名 限制条件;
alter table 表名 modify 属性名 限制条件;
alter table 表名 drop 限制条件(属性名);

   4.   用create index语句建立索引

      (1)对Student表按姓名升序建唯一索引;

create unique index Stuname on Student(sname asc);

      (2)对Course表按课程名称降序建唯一索引;

create unique index Couname on Course(cname desc);

      (3)对SC表按成绩降序和学号升序建立索引;

create unique index Scno on SC(grade desc, sno asc);

综上,create index语句基本格式为:

create 限制类型 index 索引名称 on 表名(属性名称 desc, 属性名称 asc);

其中asc可以省略不写,顺序可以颠倒

   5.   用drop语句删除基本表及索引;

drop Student;
drop index Stuname;

   6.   用insert语句向以上三个基本表插入元组;

insert into Student values('95001','李永','男',20,'cs','北京','27-2月-02');

   7.   用select语句显示各表的内容;

select * from Student;
select * from Course;
select * from SC;
select sysdate from dual;   -----显示系统时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值