Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 28 Introduction to Databases II - Database Design, SQL and JDBC Overview  Objectives of this Lecture  Logical Database Models  ER Model, Hierarchical.

Similar presentations


Presentation on theme: "1 Lecture 28 Introduction to Databases II - Database Design, SQL and JDBC Overview  Objectives of this Lecture  Logical Database Models  ER Model, Hierarchical."— Presentation transcript:

1 1 Lecture 28 Introduction to Databases II - 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: Data Communications and Networks

2 2 Lecture 28 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

3 3 Lecture 28 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.

4 4 Lecture 28 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.

5 5 Lecture 28 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.

6 6 Lecture 28 Components of Relational Model Relational 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, field The columns of the table, usually defined from the attributes of the object model.  Tuple, row, record The 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.

7 7 Lecture 28 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 = ADVISORprimary key: AdvId AdvId Adv Name Adv Phone 66101 Sahalu 333-2111 66102 Al-Ghamdi 405-8888 66103 Sadiq 501-8241 66104 Badhusha 222-2357

8 8 Lecture 28 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. StuIdStuNameStuPhoneAdvId 987654Al-Amer452-763466104 978956Al-Quasim555-321566102 993421Al-Ali865-347666101 981267Al-Fathah894-238466102

9 9 Lecture 28 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-2111 66102 Al-Ghamdi 405-8888 66103 Sadiq 501-8241 66104 Badhusha 222-2357

10 10 Lecture 28 Definitions of Relational Terms Composite 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 key A 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. StudentIDCourseGrade 1001COE31295 1001ENG10190 1005COE31285 1006COE43087 1006ICS20295 1010COE10292

11 11 Lecture 28 Definitions of Relational Terms Foreign 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-8241 66104 Badhusha 222-2357 StuIdStuNameStuPhoneAdvId 987654Al-Amer452-763466104 978956Al-Quasim555-321566102 993421Al-Ali865-347666101 981267Al-Helal894-238466102

12 12 Lecture 28 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  JDBC TM 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 Driver MS Access DBMS Engine DB Sending query Result of the query

13 13 Lecture 28 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 FROM [WHERE ] [GROUP BY ] [ORDER BY ]  Explanation:  For  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.

14 14 Lecture 28 Communication with DB from Java Application  For  A table name or list of table name is used  For  The operators are relational and logical and the operands are the column names  For and ]  List of columns are specified  Example:  SELECT StuId, StuName FROM STUDENT WHERE AdvId = “66102”  The table Result of the query 978956 Al-Quasim 981267 Al-Helal StuIdStuNameStuPhoneAdvId 987654Al-Amer452-763466104 978956Al-Quasim555-321566102 993421Al-Ali865-347666101 981267Al-Helal894-238466102

15 15 Lecture 28 Communication with DB from Java Application  UPDATE  Format  UPDATE SET = [WHERE ]; --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 StuIdStuNameStuPhoneAdvId 987654Al-Amer452-763466104 978956Al-Quasim555-321566102 993421Al-Ali865-347666101 981267Al-Helal894-238466102 StuIdStuNameStuPhoneAdvId 987654Al-Amer452-763466104 978956Al-Quasim555-321566102 993421Al-Ali865-347666109 981267Al-Helal894-238466102

16 16 Lecture 28 Communication with DB from Java Application  INSERT  Format  INSERT INTO [( )] VALUES ( );  Example  INSERT INTO STUDENT VALUES (“994433”,”Al-Ghamdi”,”866-2687”,”66105”)  The table before insert The table after insert StuIdStuNameStuPhoneAdvId 987654Al-Amer452-763466104 978956Al-Quasim555-321566102 993421Al-Ali865-347666101 981267Al-Helal894-238466102 StuIdStuNameStuPhoneAdvId 987654Al-Amer452-763466104 978956Al-Quasim555-321566102 993421Al-Ali865-347666109 981267Al-Helal894-238466102 994433Al-Ghamdi866-268766105


Download ppt "1 Lecture 28 Introduction to Databases II - Database Design, SQL and JDBC Overview  Objectives of this Lecture  Logical Database Models  ER Model, Hierarchical."

Similar presentations


Ads by Google