• 검색 결과가 없습니다.

406.426 Design & Analysis of Database System

N/A
N/A
Protected

Academic year: 2022

Share "406.426 Design & Analysis of Database System"

Copied!
26
0
0

로드 중.... (전체 텍스트 보기)

전체 글

(1)

406 426 Design & Analysis of Database System 406 426 Design & Analysis of Database System 406.426 Design & Analysis of Database System

Chapter 9

406.426 Design & Analysis of Database System

Chapter 9 p p

2007 Fall 2007 Fall

지도교수: 박 종 헌 교수님 (jonghun@snu.ac.kr) 담당조교 정범석 (b d l03@ k ) 담당조교: 정범석 (bumdol03@snu.ac.kr)

Digital Interaction Lab.

(2)

Database Programming

™ Objective: to access a database from an application program (as j pp p g ( opposed to interactive interfaces)

™ Why? An interactive interface is convenient but not sufficient; a

majority of database operations are made thru application programs (nowadays thru web applications)

(nowadays thru web applications)

406.426 Design & Analysis of Database System 2

(3)

Database Programming Approaches

™ Embedded commands: database commands are embedded in a general-purpose programming language

™ Library of database functions: available to the host language for database calls; known as an API

™ A brand new, full-fledged language (minimizes impedance mismatch)

(4)

Impedance Mismatch

™ Incompatibilities between a host programming language and the p p g g g g database model, e.g.,

„ type mismatch and incompatibilities; requires a new binding for each language

„ set vs. record-at-a-time processing

„ need special iterators to loop over query results and manipulate individual

„ need special iterators to loop over query results and manipulate individual values

406.426 Design & Analysis of Database System 4

(5)

Steps in Database Programming

1. Client program opens a connection to the database serverp g p

2. Client program submits queries to and/or updates the database 3. When database access is no longer needed, client program

terminates the connection

(6)

Embedded SQL

™ Most SQL statements can be embedded in a general-purpose hostQ g p p programming language such as COBOL, C, Java

™ An embedded SQL statement is distinguished from the host language statements by EXEC SQL and a matching END-EXEC (or semicolon)

h d i bl ( d i b th l ) ll fi d ith

„ shared variables (used in both languages) usually prefixed with a colon (:) in SQL

406.426 Design & Analysis of Database System 6

(7)

Embedded SQL in C Programming Examples Programming Examples

loop = 1;p ;

while (loop) {

prompt (“Enter SSN: “, ssn);

EXEC SQL

select FNAME, LNAME, ADDRESS, SALARY into :fname, :lname, :address, :salary from EMPLOYEE where SSN == :ssn;

if (SQLCODE == 0) printf(fname, …);

else printf(“SSN does not exist: “, ssn);

t(“M SSN? (1 0 ) “ l )

prompt(“More SSN? (1=yes, 0=no): “, loop);

END-EXEC

(8)

Dynamic SQL

™ Objective: executing new (not previously compiled) SQL statements at j g ( p y p ) Q run-time

„ a program accepts SQL statements from the keyboard at run-time

„ a point-and-click operation translates to certain SQL query

™ Dynamic update is relatively simple; dynamic query can be complex

„ because the type and number of retrieved attributes are unknown at compile time

at compile time

406.426 Design & Analysis of Database System 8

(9)

Dynamic SQL: An Example

EXEC SQL BEGIN DECLARE SECTION;Q ; varchar sqlupdatestring[256];

EXEC SQL END DECLARE SECTION;

prompt (“Enter update command:“, sqlupdatestring);

EXEC SQL PREPARE sqlcommand FROM :sqlupdatestring;

EXEC SQL EXECUTE sqlcommand;

(10)

Embedded SQL in Java

™ SQLJ: a standard for embedding SQL in JavaQ g Q

™ An SQLJ translator converts SQL statements into Java (to be executed thru the JDBC interface)

™ Certain classes, e.g., java.sql have to be imported

406.426 Design & Analysis of Database System 10

(11)

Java Database Connectivity

™ JDBC: SQL connection function calls for Java programmingQ p g g

™ A Java program with JDBC functions can access any relational DBMS that has a JDBC driver

™ JDBC allows a program to connect to several databases (known as data sources)

(12)

Steps in JDBC Database Access

1. Import JDBC library p y (java.sql.*)(j q )

2. Load JDBC driver: Class.forname(“oracle.jdbc.driver.OracleDriver”)

3. Define appropriate variables

4. Create a connect object (via getConnection)

5. Create a statement object from the Statement class:

1. PreparedStatment

2. CallableStatement

6 Identify statement parameters (to be designated by question marks) 6. Identify statement parameters (to be designated by question marks) 7. Bound parameters to program variables

8. Execute SQL statement (referenced by an object) via JDBC’s ( y j )

executeQuery

9. Process query results (returned in an object of type ResultSet) i 2 di ti l t bl

406.426 Design & Analysis of Database System 12

„ ResultSet is a 2-dimentional table

(13)

SQLJ

(14)

Database Programming with Functional Calls

™ Embedded SQL provides static database programmingQ p p g g

™ API: dynamic database programming with a library of functions

„ advantage: no preprocessor needed (thus more flexible)

„ drawback: SQL syntax checks to be done at run-time

406.426 Design & Analysis of Database System 14

(15)

a C program segment with SQL/CLI

(16)

a JAVA program segment with JDBC

406.426 Design & Analysis of Database System 16

(17)

a JAVA program segment with JDBC (cont.)

(18)

Database Stored Procedures

™ Persistent procedures/functions (modules) are stored locally and p ( ) y executed by the database server (as opposed to execution by clients)

™ Advantages:

„ if the procedure is needed by many applications, it can be invoked

b f th (th d d li ti )

by any of them (thus reduce duplications)

„ execution by the server reduces communication costs

„ enhance the modeling power of views

„ enhance the modeling power of views

406.426 Design & Analysis of Database System 18

(19)

Stored Procedure Constructs

™ A stored procedure

™ A stored procedure

CREATE PROCEDURE procedure-name (params) local-declarations

local declarations procedure-body;

™ A stored function

CREATE FUNCTION fun-name (params) RETRUNS return-type local-declarations

function-body;

™ Calling a procedure or function

(20)

SQL Persistent Stored Modules

™ SQL/PSM: part of the SQL standard for writing persistent stored Q p Q g p modules

™ SQL + stored procedures/functions + additional programming constructs

b hi d l i t t t

„ e.g., branching and looping statements

„ enhance the power of SQL

406.426 Design & Analysis of Database System 20

(21)

MySQL GUI Tools

™ download(http://dev.mysql.com/downloads/gui-tools/5.0.html)( p y q g )

(22)

MySQL GUI Tools cont.

406.426 Design & Analysis of Database System 2 2

(23)

MySQL GUI Tools cont.

•Query Browser를 통해 DB 접속

•Query Browser를 통해 DB 접속

(24)

MySQL GUI Tools cont.

쿼리 입력 창 쿼리 결과

쿼리 결과

데이터베이스 목록

테이블 목록

406.426 Design & Analysis of Database System 24

(25)

MySQL GUI Tools cont.

sql문 가져오기

(26)

참조

관련 문서

Using web page, user sends to web server information about database, web server creates query sentence for database connection. Using created query sentence,

According to the recent diversification of ship and the rapid growth of her size, a database which can cover various ships is requested for

Yin, Efficient Mining of Partial Periodic Patterns in Time Series Database,

Make sure to include all appropriate entities, attributes, relationships,

Entries will be added to the /etc/oratab file as needed by Database Configuration Assistant when a database is created Finished running generic part of root.sh

„ 모듈화 : 전체 프로그램의 복잡한 문제를 함수 내부의 문제로 국한시켜 프로그램을 체계적으로 쉽게 프로그래밍을 해나갈 수

The purpose of the study is to develop a sensor data collection and monitoring system with database using IoT techrology and to apply the ststem to three

This study suggests the necessity of internship program utilizing foreign Korean-entrepreneurs networks, the necessity of human network database for