最近學習使用ORACLE數據庫,建表如下,高手看看有什么不對(當)之處。
Posted on 2006-05-04 21:51 喜悅天天收獲 閱讀(1302) 評論(0) 編輯 收藏 所屬分類: 數據庫相關數據庫要求
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;