Database Design, SQL and JDBC

16
Lecture 28 1 Database Design, SQL and JDBC Overview Objectives of this Lecture Logical Database Models ER Model, Hierarchical and Network data Model and Relational Model JDBC SQL Preview: More on JDBC

description

Database Design, SQL and JDBC. Overview Objectives of this Lecture Logical Database Models ER Model, Hierarchical and Network data Model and Relational Model JDBC SQL Preview: More on JDBC. Objectives of this Lecture. Understand the use of data modeling - PowerPoint PPT Presentation

Transcript of Database Design, SQL and JDBC

Page 1: Database Design, SQL and JDBC

Lecture 28 1

Database Design, SQL and JDBCOverview

Objectives of this Lecture

Logical Database Models

ER Model, Hierarchical and Network data Model and Relational Model

JDBC

SQL

Preview: More on JDBC

Page 2: Database Design, SQL and JDBC

Lecture 28 2

Objectives of this Lecture

Understand the use of data modeling

Understand the different types of logical data models

Learn about Relational Data Model in detail

Understand the concept of JDBC

Learn about SQL

Page 3: Database Design, SQL and JDBC

Lecture 28 3

Why to model data? A model highlights the important aspects of a subject and obscures

unimportant aspects of it.

A data model emphasizes features of interest to the user and makes its interaction with a DBMS transparent.

Physical data models show how the data structures are organized so that their resources are optimized.

Logical data models interprete the data in the context of the application.

The modeling phase of database design is crucial to assure an accurate representation of the user's perspective of the business.

Page 4: Database Design, SQL and JDBC

Lecture 28 4

Logical Data Models

A logical data model is a design tool used in the design of a database system to emphasize features of interest to the user and makes interaction with a DBMS transparent.

A good logical data model is independent of the DBMS and can be moved from one management system to another.

Popular approaches to logical data models include: 1. Entity-Relationship model 2. Hierarchical and Network models. 3. Relational model.

Next we briefly discuss the Relational model.

Page 5: Database Design, SQL and JDBC

Lecture 28 5

Relational Model The most popular logical data model in use today is the relational

model which is noted for its consistency, simplicity and data independence.

It is composed of relations, attributes, domains, keys, tuples and their representations.

A relation is a table of rows and columns. Each column of the table is an attribute.

The domain of each attribute is the collection of values that can be assigned to a particular attribute.

A principal key is one or more attribute values that uniquely identify an entity instance.

A tuple is an ordered sequence of elements. Example the sequence (p,q,r,s,t,u) is a 6-tuple. Each row is an entity instance represented by a tuple.

Page 6: Database Design, SQL and JDBC

Lecture 28 6

Components of Relational ModelRelational modeling has many interchangeable terms for its components as shown below: Relation, table, file

A two-dimensional table consisting of columns and rows; created from the entities of the object model.

Attribute, column, fieldThe columns of the table, usually defined from the attributes of the object model.

Tuple, row, recordThe rows of the table; derived from the entity occurrences of the object model.

Relations:A Relation is a two-dimensional table containing a set of related data. The true requirements of a relation are that: Each cell must be atomic (contain only one value). Each attribute contains the same type of physical and semantic information in all

its cells. Each tuple is unique; there can be no duplicate rows of data. The order of columns and rows is not significant.

Page 7: Database Design, SQL and JDBC

Lecture 28 7

Relation Typically, relations have a representation consisting the relation

name, followed by a list of attributes in form of a tuple with the principal key highlighted.e.g., ADVISOR (AdvId, Adv-Name, Adv-Phone)

The following shows a typical relational data model called an instance table.

RELATION = ADVISOR primary key: AdvId

AdvId Adv Name Adv Phone

66101 Sahalu 333-2111

66102 Al-Ghamdi 405-8888

66103 Sadiq 501-824166104 Badhusha 222-2357

Page 8: Database Design, SQL and JDBC

Lecture 28 8

Relations (cont.)

The following table defines another relation for the entity Student with attributes StuID, StuName, StuPhone and AdvId.RELATION = STUDENT

In relational databases, new relations can be generated from others. For example, the relation “is advised by” can be defined between the relations STUDENT and ADVISOR.

StuId StuName StuPhone AdvId

987654 Al-Amer 452-7634 66104

978956 Al-Quasim 555-3215 66102

993421 Al-Ali 865-3476 66101

981267 Al-Fathah 894-2384 66102

Page 9: Database Design, SQL and JDBC

Lecture 28 9

Definitions of Relational Terms Primary key

Primary keys are essential in relational modeling; one should be specified for each relation. A primary key uniquely identifies a record (or row) in a table; in other words, a particular primary key value returns a record that is identical to no other. A primary key is composed of one column (simple primary key) or a combination of columns (composite primary keys) that provide this unique identification. The best possibilities for primary keys are attributes that seldom change and are familiar to users. The primary key should contain the fewest columns needed to uniquely identify a record.

Simple primary key: Consider the relation ADVISOR where each value of Advisor ID returns a unique

record. Simple primary key AdvId uniquely identifies records.

AdvId AdvName AdvPhone

66101 Sahalu 333-211166102 Al-Ghamdi 405-888866103 Sadiq 501-824166104 Badhusha 222-2357

Page 10: Database Design, SQL and JDBC

Lecture 28 10

Definitions of Relational TermsComposite primary key: Consider the relation STUDENT where each student can take more than one

course. Student ID, Class, or Grade alone does not return a unique record; however, a composite primary key of Student ID-Class does. Composite primary key Student ID-Course defines unique records.

Foreign keyA foreign key is an attribute in a relation that is also a primary key in another relation. This foreign key-primary key match allows references between relations in order to locate information.

StudentID Course Grade

1001 COE312 95

1001 ENG101 90

1005 COE312 85

1006 COE430 87

1006 ICS202 95

1010 COE102 92

Page 11: Database Design, SQL and JDBC

Lecture 28 11

Definitions of Relational TermsForeign key Example :

Relation ADVISOR where AdvID is the primary key. Relation STUDENT where StuID is the primary key and AdvID is a foreign key.

RELATION = ADVISOR RELATION = STUDENT

AdvId Adv Name Adv Phone

66101 Sahalu 333-2111

66102 Al-Ghamdi 405-8888

66103 Sadiq 501-824166104 Badhusha 222-2357

StuId StuName StuPhone AdvIdAdvId

987654 Al-Amer 452-7634 66104

978956 Al-Quasim 555-3215 66102

993421 Al-Ali 865-3476 66101

981267 Al-Helal 894-2384 66102

Page 12: Database Design, SQL and JDBC

Lecture 28 12

Communication with DB from Java Application

To communicate with the DB from Java application, we need the following.1. An interface which connects Java application and the DBMS (JDBC - Drivers)

2. A language to communicate (SQL: Structured Query Language)

1. JDBC JDBCTM API provides universal data access from the Java programming language. The

JDBC API is a Java API for accessing virtually any kind of tabular data from relational databases.

To use the JDBC API with a particular database management system, you need a JDBC technology-based driver to mediate between JDBC technology and the database. The latest Java SDK includes a JDBC-ODBC Bridge driver that makes most Open Database Connectivity (ODBC) drivers available to programmers using the JDBC API.

Java Application

JDBC-ODBC Bridge DriverMS Access

DBMSEngine

DB

Sending query

Result of the query

Page 13: Database Design, SQL and JDBC

Lecture 28 13

Communication with DB from Java Application

2. SQL Structured Query Language is used to write queries to the database in order to get

information from the database and to update the information to the DB Here we will see some simple queries [their format and some simple examples]

SELECT UPDATE INSERT

SELECT Format:

SELECT <LIST OF COLUMNS> FROM <LIST OF TABLES> [WHERE <CONDITION(S)>] [GROUP BY <GROUPING COLUMN(S)>] [ORDER BY <ORDERING COLUMN(S)>]

Explanation: For <LIST OF COLUMNS>

a * is used to select all the columns of the specified table(s). Individual columns can be selected by specifying the column names separated by

comas. If columns from different table are needed, then tablename.columnname is specified.

Page 14: Database Design, SQL and JDBC

Lecture 28 14

Communication with DB from Java Application

For <LIST OF TABLES> A table name or list of table name is used

For <CONDITION(S)> The operators are relational and logical and the operands are the column names

For <GROUPING COLUMN(S)> and <ORDERING COLUMN(S)>] List of columns are specified

Example: SELECT StuId, StuName FROM STUDENT WHERE AdvId = “66102”

The table Result of the query978956 Al-

Quasim

981267 Al-Helal

StuId StuName StuPhone AdvIdAdvId

987654 Al-Amer 452-7634 66104

978956 Al-Quasim 555-3215 66102

993421 Al-Ali 865-3476 66101

981267 Al-Helal 894-2384 66102

Page 15: Database Design, SQL and JDBC

Lecture 28 15

Communication with DB from Java Application

UPDATE Format

UPDATE <TABLE NAME> SET <COLUMN NAME> = <VALUE> [WHERE <CONDITION>]; --if the Where clause is left out, all rows will be updated according to the Set statement.

Example UPDATE STUDENT SET AdvId = “66109” WHERE StuId = “993421”

The table before update The table after update

StuId StuName StuPhone AdvId

987654 Al-Amer 452-7634 66104

978956 Al-Quasim 555-3215 66102

993421 Al-Ali 865-3476 66101

981267 Al-Helal 894-2384 66102

StuId StuName StuPhone AdvId

987654 Al-Amer 452-7634 66104

978956 Al-Quasim 555-3215 66102

993421 Al-Ali 865-3476 66109

981267 Al-Helal 894-2384 66102

Page 16: Database Design, SQL and JDBC

Lecture 28 16

Communication with DB from Java Application

INSERT Format

INSERT INTO <TABLE NAME> [(<COLUMN LIST>)] VALUES (<VALUE LIST>);

Example INSERT INTO STUDENT VALUES (“994433”,”Al-Ghamdi”,”866-2687”,”66105”) The table before insert The table after insert

StuId StuName StuPhone AdvId

987654 Al-Amer 452-7634 66104

978956 Al-Quasim 555-3215 66102

993421 Al-Ali 865-3476 66101

981267 Al-Helal 894-2384 66102

StuId StuName StuPhone AdvId

987654 Al-Amer 452-7634 66104

978956 Al-Quasim 555-3215 66102

993421 Al-Ali 865-3476 66109

981267 Al-Helal 894-2384 66102

994433 Al-Ghamdi 866-2687 66105