数据库要求
1、建立一个表code_dam,包含以下字段
id int,自增,主键
code nvarchar(20), not null (大坝编号)
name nvarchar(20), not null (大坝名称)
remark nvarchar(200) (备注)
2、编程实现对表code_dam的增删改查功能,包括:
添加大坝编码(添加一条记录到数据库,要求:大坝编号不能重复,大坝名称不能重复)
修改大坝编码(要求:大坝编号不能重复,大坝名称不能重复)
删除一个大坝编码
大坝编码列表(code_dam表中所有的记录,使用列表将其他功能串在一起)
查看大坝编码(显示指定id的大坝编号、名称、备注)
//建立数据库
-- Create table
create table CODE_DAM
(
ID INTEGER not null,
CODE VARCHAR2(20) not null,
NAME VARCHAR2(20) not null,
REMARK VARCHAR2(200)
)
tablespace DAM
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Add comments to the table
comment on table CODE_DAM
is '大坝管理';
-- Add comments to the columns
comment on column CODE_DAM.CODE
is '大坝编号';
comment on column CODE_DAM.NAME
is '大坝名称';
-- Create/Recreate primary, unique and foreign key constraints
alter table CODE_DAM
add constraint ID primary key (ID)
using index
tablespace DAM
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
//自增解决办法
//序列
-- Create sequence
create sequence SEQ_CODE_DAM
minvalue 1
maxvalue 999999999999999999999999999
start with 31
increment by 1
cache 10;
//触发器
CREATE OR REPLACE TRIGGER TRG_code_dam
BEFORE
INSERT ON code_dam
FOR EACH ROW
begin
SELECT SEQ_code_dam.NEXTVAL
INTO:NEW.id
FROM DUAL;
End TRG_code_dam;
//相关存储过程
//查询所有数据
//包
create or replace package showall is
type mycursor is ref cursor;
procedure GetRecords(ret_cursor out mycursor);
end showall;
//包体
create or replace package body showall is
procedure GetRecords(ret_cursor out mycursor) as
begin
open ret_cursor for select * from code_dam;
end GetRecords;
end showall;
//删除数据
create or replace procedure delRecord(id_in in int)
is
begin
delete from code_dam where id=id_in;
commit;
end;
//插入数据
create or replace procedure insertRecord(code_in in varchar2,name_in in varchar2,remark_in in varchar2) is
begin
insert into code_dam (code,name,remark) values (code_in,name_in,remark_in);
commit;
end;
//更新数据
create or replace procedure updataRecord(id_in in int,code_in in varchar2,name_in in varchar2,remark_in in varchar2)
is
begin
update code_dam set code=code_in,name=name_in,remark=remark_in where id=id_in;
commit;
end;
//检查重复
create or replace procedure checkdam
(code_in in varchar2,name_in in varchar,name_out out varchar2)
is
begin
select name into name_out from code_dam where code = code_in or name = name_in;
end checkdam;
//查询单个数据
//包
create or replace package showdam is
type mycursor is ref cursor;
procedure GetRecords(id_in in int,ret_cursor out mycursor);
end showdam;
//包体
create or replace package body showdam is
procedure GetRecords(id_in in int,ret_cursor out mycursor) as
begin
open ret_cursor for select * from code_dam where id = id_in;
end GetRecords;
end showdam;