《資料庫系統概論》 Chapter8 資料庫程式設計

2021-10-02 23:31:05 字數 2116 閱讀 7843

sql程式設計技術可以有效克服sql語言實現複雜應用方面的不足,提高應用系統和資料庫管理系統間的互操作性

8.1嵌入式sql p245

8.2過程化sql p253

8.3儲存過程和函式 p255

8.4odbc程式設計 259

《答案》例題p110

exec sql select sno,sname,s***,sage,sdept into: hsno, : hname, :h***, : hage, :hdept

from student

where sno=:givesno

利用儲存過程來實現下面的應用:從乙個賬戶轉指定數額的款項到另乙個賬戶中

create procedure transfer(inaccount int, outaccount int, amount float)

as declare

totaldeposit float;

begin

select total into totaldeposit

from account where

accountnum=outaccount;

if totaldeposit is null then

rollback;

return;

end if;

if totaldeposit.totalrollback;

return;

end if;

update account set total=toal-amount

where accountnum=outaccount;

update account set total=total+amount where accountnum=inaccount;

commit;

end;

查詢某一門課程的資訊。要查詢的課程由使用者在程式執行過程中指定,放在主變數中

exec sql begin declare section:

char deptname[20];

char hsno[9];

char hsname[20];

char hs***[2];

int hsage;

int newage;

exec sql end declare section;

long sqlcode;

exec sql include sqlca;//定義sql通訊區

int main(void)

int count=0;

char yn;

printf("please choose the department name:");

scanf("%s", &deptname);

exec sql connect to 學生課程資料庫@localhost:54321 user

"system"/"manager";

exec sql declare sx cursor for /*游標*/

select sno, sname, s***, sage

from student

where sdept=: deptname;

exec sql open sx; /*開啟游標*/

for(; ;)

exec sql fetch sx into: hsno, :hsname, :hs***, :hsage;/*游標向前推進一行,取結果送主變數*/

if(sqlca, sqlcode !=0) break;

if(count++ =0)

printf("");

printf("");

printf("");

dowhile();

if(){

printf("");

scanf("");

exec sql update student

set sage=:newage

where current of sx;/*對student表中的年齡進行更新*/

exec sql colse sx;/*關閉游標*/

exec sql commit work;/*提交更新*/

exec sql disconnect 學生課程資料庫

習題p270

資料庫系統概論 緒論 資料庫系統概論(一)

1.1 資料庫系統概論 1.1.1 資料庫系統概論 1.資料 描述事物的符號記錄稱為是資料 資料的解釋叫做語義,他與資料時密不可分的 記錄是計算機中表示和儲存資料的一種格式或者乙個方法 2.資料庫 把資料以一定的格式存放在計算機儲存裝置上的倉庫就是資料庫 共享性 有組織 永久儲存是資料庫的三大特徵 ...

資料庫系統概論

資料庫系統概論 1 現實世界的資料化過程 現實世界是由實際存在的事物組成的。每種事物都有無窮的特性,事物之間有著錯綜複雜的聯絡。資訊世界是現實世界在人腦中的反映。現實世界中的事物和事物特性在資訊世界中分別反映為實體和實體的屬性。資訊世界是由實體組成的,每種實體都有自己有限的屬性。本質上說,實體是由有...

《資料庫系統概論》

一 概述 1.關係模型的組成 資料結構 二維表 關係 資料庫中全部資料以及資料間的聯絡都以關係來表示。資料操作 增 刪 改 查 資料的約束條件 實體完整性,參照完整性,使用者自定義的完整性。2.關係的資料結構 域 是一組既有相同資料類的值的集合,又稱值域。域中所包含值的個數稱為域的基數。笛卡爾積 所...