-- 作业一, 使用自己的用户登录, 完成如下操作, 并且创建 5 条测试数据
-- 创建学生表(stu), 字段如下:
-- 学号(stuID)
-- 姓名(stuName)
-- 性别(stuSex)
-- 入学时间(stuDate)
- create table stu (
- stuID number(12),
- stuName varchar(5),
- stuSex varchar(2) check(stuSex in('男','女')),
- stuDate date
- );
-- 二, 请创建一个表, 表名为 phone, 表结构如下
电话号码(PHONENUM VARCHAR2(8))
电话费 (PAY number(8,2))
号码等级(NUMLEVEL VARCHAR2(4))
费用日期(PAYDATE varchar2(12)
- create table phone(
- phonenum varchar2(8),
- pay number(8,2),
- numlevel varchar2(4),
- paydate varchar2(12)
- );
1, 插入以下两条数据.
123456, 600, pt04, 20051220
888888, 900, pt05, 20051019
- insert into phone (phonenum,pay,numlevel,paydate) values(123456,600,'pt04',20051220);
- insert into phone (phonenum,pay,numlevel,paydate) values(888888,900,'pt05',20051019);
2, 创建一个备份表结构名为 phone_bak, 将 phone 中的数据插入 phone_bak 中.
create table phone_bak as select * from phone;
3, 将电话号码为 123456 的电话费改为 1000, 执行回滚操作
- update phone set paydate=1000 where phonenum=123456;
- rollback;
4, 在 phone 表中, 如果 PHONENUM 字段中的数据为'123456', 则返回'Y'否则返回'N'
select decode(phonenum,'123456','y','n') from phone;
5, 将 phone 表中的电话号码为 123456 的记录的费用日期 (PAYDATE) 字段的日期转变成 YYYY/MM/DD 格式
- select to_date(paydate,'yyyy/mm/dd') from phone where phonenum=123456;
- update phone set paydate=to_date(paydate,'yyyy/mm/dd') where phonenum=123456;
6, 将电话号码为 888888 的号码等级改为 pt04 并提交
- update phone set numlevel='pt04' where phonenum=888888;
- commit;
-- 三, 创建满足下面条件的三个数据库表
-- 表名: Card
作用: 存储上网卡信息
-------------------------------------------------------------------
列名 数据类型 长度 是否为空 字段说明
ID varchar 10 否 主键, 不允许有相同值
PassWord varchar 50 否 密码
Balance int 4 是 卡上余额
UserName varchar 50 是 持卡人姓名
- -------------------------------------------------------------------
- create table Card (
- ID varchar(10) primary key,
- PassWord varchar(50) not null,
- Balance number(4),
- UserName varchar(50)
- );
-- 添加注释内容
- comment on column Card.password is '密码';
- comment on column Card.Balance is '卡上余额';
-- 表名: Computer
作用: 存储计算机及状态信息
-------------------------------------------------------------------
列名 数据类型 长度 是否为空 字段说明
ID varchar 10 否 主键, 不允许有相同值
CardID varchar 1 否 是否正在使用
ComputerID varchar 100 是 备注和说明信息
- -------------------------------------------------------------------
- create table Computer (
- ID varchar(10) primary key,
- CardID varchar(1) not null,
- ComputerID varchar(100) not null
- );
表名: Record
作用: 存储每次上机的信息
-------------------------------------------------------------------
列名 数据类型 长度 是否为空 字段说明
ID numeric 8 否 主键, 不允许有相同值
CardID varchar 10 否 外键, 引用 Card 表的 ID 字段
ComputerID varchar 10 否 外键, 引用 Computer 表的 ID 字段
BeginTime data 4 是 开始上机时间
EndTime data 4 是 下机时间
Fee data 9 是 本次上机费用
- -------------------------------------------------------------------
- create table Record (
- ID number(8) not null,
- CardID varchar(10) not null,
- ComputerID varchar(10) not null,
- BeginTime date,
- EndTime date,
- Fee date
- );
- foreign KEY(CardID) REFERences Card(ID)
-- 查看约束
select constraint_name,table_name from user_constraints where table_name =upper('computer');
-- 注释内容
- comment on column Record.Fee is '本次上机费用';
- comment on column Record.BeginTime is '开始上机时间';
- comment on column Record.Fee is '本次上机费用';
- comment on column Record.EndTime is '下机时间';
创建表之后, 编写和实施约束, 要求如下:
1, 针对 Record 表的 CardID,ComputerID 字段分别与 Card 表, Computer 表建立主外键关系(引用完整性约束)
alter table Record add constraint fk_CardID foreign key(CardID) references Computer(ID);
2,Card 表中, 卡上的余额不能超过 1000
alter table Card add constraint ck_Balance check(balance<=1000);
3,Computer 表中, OnUse 只能是 0 或者 1
- alter table computer add (onUse number(2));
- alter table computer add constraint ck_onUse check (onUse in(0,1));
4,Record 表中, EndTime 不能早于 BeginTime (错的)
alter table computer add constraint ck_Time check(to_date(EndTime,'yyyy-mm-dd hh24:mi:ss')>to_date(BeginTime,'yyyy-mm-dd hh24:mi:ss'));
-- 作业 02 修改字段
1, 为 stu 表添加年龄字段(stuAge), 类型为(number(8)), 并且统一赋值为 30.
alter table stu add (stuAge number(8) default(30));
2, 修改 stuName 字段为(varchar2(20))
alter table stu modify(stuName varchar(20));
3, 删除 studate 字段
alter table stu drop studate;
-- 作业 04-1 添加约束
创建班级表(clsses), 字段包含:
[班级编号(cid), 班级名称(cname)]
- create table classes(
- cid number(8),
- cname varchar2(20)
- );
需求如下:
1, 为 "班级编号添" 加主键约束
alter table classes add constraint pk_cid primary key(cid);
2, 为 "班级名称" 添加非空约束
alter table classes modify (cname not null);
3, 插入 3 条测试数据
- insert into classes values(1001,'蓝桥 1 班');
- insert into classes values(1002,'蓝桥 2 班');
- insert into classes values(1003,'蓝桥 3 班');
创建学生表(stus), 字段包含:
[学号(stuID), 姓名(stuName), 性别(stuSex), 年龄(stuAge), 入学时间(stuDate), 班级编号(cid)]
create table stus(
stuID number(8), -- 学号
stuName varchar2(20), -- 姓名
stuSex char(2), -- 性别
stuAge number(8), -- 年龄
stuDate Date, -- 入学时间
cid number(8) -- 班级编号
);
需求如下:
1, 为 stuID 添加主键约束
alter table stus add constraint py_stuID primary key(stuID);
2, 为 stuName 添加非空约束
alter table stus modify (stuName not null);
3, 为 stuSex 添加默认值
alter table stus modify (stuSex char(4));-- 修改长度
alter table stus modify (stuSex default '男');
4, 为 stuAge 添加检查约束 0-100
alter table stus add constraints ck_stuAge check(stuAge>=0 and stuAge<=100);
5, 为 stuDate 添加唯一性约束
alter table stus add constraint un_stuDate unique(stuDate);
6, 为 cid 添加外键约束
alter table stus add constraint fk_cid foreign key (cid) references classes (cid) on delete cascade; -- 添加外键
select constraint_name,table_name from user_constraints where table_name =upper('stus');-- 查看约束
alter table stus drop CONSTRAINT fk_cid; -- 删除外键约束
7, 添加 5 条测试数据
- insert into stus values(1801,'Ben','男',21,to_date('2015/5/19','yyyy-mm-dd'),1001);
- insert into stus values(1800,'BenQ','女',19,to_date('2017/2/29','yyyy-mm-dd'),1003);
- insert into stus values(1802,'Chen','男',22,to_date('2018/6/22','yyyy-mm-dd'),1002);
- insert into stus values(1803,'Legend','男',25,to_date('2014/8/19','yyyy-mm-dd'),1003);
- insert into stus values(1804,'Wave','女',21,to_date('2016/5/19','yyyy-mm-dd'),1002);
- insert into stus values(1805,'QQ','女',20,to_date('2016/7/21','yyyy-mm-dd'),1003);
--04-2 约束练习
1, 创建表 emp1, 字段如下
- eno char(3),
- ename char(6),
- sex char(2),
- age number(2),
- dno char(3)
- create table emp1(
- eno char(3),
- ename char(6),
- sex char(2),
- age number,
- dno char(3)
- );
2, 并插入如下数据
1 ,TOM , 男 , 21 ,'001'
2 ,JERRY , 男 ,21 ,'002'
3 ,KATE , 女 ,21 ,'003'
4 ,MARY , 女 ,21 ,'004'
5 ,JACK , 男 ,21 ,'005'
- insert into emp1 values(1,'TOM','男',21,001);
- insert into emp1 values(2,'JERRY','男' ,21,'002');
- insert into emp1 values(3,'KATE','女',21,003);
- insert into emp1 values(4,'MARY','女' ,21,'004');
- insert into emp1 values(5,'JACK','男',21,'005');
3, 在 eno 字段上创建主键约束
alter table emp1 add constraint pk_ eno primary key(eno);
4, 在 ename 字段上创建非空约束
alter table emp1 modify (ename not null);
5, 创建检查约束判断 age 在 18 到 60 岁之间的男性或者 age 在 18 到 55 岁之间的女性
alter table emp1 add constraint ck_age check ((sex='男') and (age between 18 and 60) or (sex='女' and age between 18 and 55));
insert into emp1 values (666,'Legend','男',20,100);-- 测试数据
insert into emp1 values (66,'Lily','女',120,100);
6, 在 dno 字段上创建唯一性约束
alter table emp1 add constraint un_dno unique(dno);
7, 创建和 emp1 表字段相同的 emp_bak 表, 将 emp_bak 表的 eno 字段与 emp 表的 eno 字段创建外键约束
create table emp_bak ad select * from emp1;
alter table emp1 add constraint fk_emo foreign key emp_bak(eno) references emp1(eno);
-- 作业 04-3 添加约束
创建 clsses 表:
- create table classes
- (
- cid number(8),
- cname varchar2(20)
- );
为 cid 添加主键约束
alter table classes add constraint pk_cid primary key(cid);
创建 stus 表:
create table stus(
stuID number(8), -- 学号
stuName varchar2(20), -- 姓名
stuSex char(2), -- 性别
stuAge number(8), -- 年龄
stuDate Date, -- 入学时间
cid number(8)
);
为 stuID 添加主键约束
alter table stus add constraint pk_stuID primary key(stuID);
为 stuName 添加非空约束
alter table stus modify (stuName not null);
为 stuSex 添加默认值
alter table stus modify (stuSex dafault '女');
为 stuAge 添加检查约束 0-100
alter table stus add constraint ck_stuAge check(stuAge between 0 and 100);
为 stuDate 添加唯一性约束
alter table stus add constraint un_stuDate unique (stuDate);
为 cid 添加外键约束
alter table stus add constraint fk_cid foreign key (cid) reference classes (cid);
来源: http://www.bubuko.com/infodetail-2607553.html