oralce 经典习题系列

数据库数据 -插入,更新,删除

The problem

创建数据表pet,并对表进行插入、更新与删除操作,pet表结构如表所示。

字段名 数据类型 主键 外键 非空 唯一 自增
name varchar(20) ture false not null true false
owner varchar(20) false false false false false
species varchar(20) false false not null false false
sex varchar(1) false false not null false false
birth date false false not null false false
death date false false false false false
插入数据:
name owner Species sex birth death
fluffy Horold cat f 2003-10-12 2010-08-12
Claws Gwen cat m 2004-08-10 null
Buffy null dog f 2009-08-11 null
Fang Benny dog m 2000-05-15 null
Bowser Diane dog m 2003-04-16 2009-11-12
Chirpy null bird f 2008-05-19 null

(1) 首先创建数据表pet,使用不同的方法将表7-4中的记录插入到pet表中。 (2)使用UPDATE语句将名称为Fang的狗的主人改为Kevin。 (3)将没有主人的宠物的owner字段值都改为Duck。 (4)删除已经死亡的宠物记录。 (5)删除所有表中的记录


The answer

1 实现代码----orcale 插入删除更新 create table pet( name varchar(20) primary key, owner varchar(20), species varchar(20) not null, sex varchar(1) not null, birth date not null, death date, CONSTRAINT ck_sex CHECK (sex in('m','f')) ); --插入测试数据 insert into pet values ('Fluffy','Harold','cat','f','2003-10-12','2010-08-12'); insert into pet values ('Claws','Gwen','cat','m','2004-08-10',''); insert into pet values ('Buffy','null','dog','f','2009-08-11',''); insert into pet values ('Fang','Benny','dog','m','2000-05-15',''); insert into pet values ('Bowser','Diane','dog','m','2003-04-16','2009-11-12'); insert into pet values ('Chirpy','null','bird','f','2008-05-19',''); select * from pet; 2 实现代码update pet set owner ='Kevin' where name ='Fang'; commit; 3 实现代码update pet set owner ='Duck' where Owner is null; commit; 4 实现代码 delete from pet where death is not null; commit; 5 实现代码 delete from pet; commit;