Top Banner
RiceCAP workshop, June 4 – 9, 2006 Noble Foundation Ardmore, Oklahoma Basic data relations Basic data relations for markers, traits, for markers, traits, and genotypes and genotypes Clare Nelson Dept. of Plant Pathology, Kansas State University
53
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Database lecture

RiceCAP workshop, June 4 – 9, 2006Noble Foundation

Ardmore, Oklahoma

Basic data relations for Basic data relations for markers, traits, and genotypes markers, traits, and genotypes

Clare NelsonDept. of Plant Pathology, Kansas State University

Page 2: Database lecture

PlanPlan

Databases: what and why?Databases: what and why?What's a What's a relationalrelational database? database?

Designing a database for Student dataDesigning a database for Student data Identifying the main data elementsIdentifying the main data elements Identifying their relationsIdentifying their relations

Creating, populating, and querying a Creating, populating, and querying a MySQL databaseMySQL database

Lab:Lab: Do it yourself Do it yourself (with guidance!)(with guidance!)

Page 3: Database lecture

What you'll know What you'll know after lecture and labafter lecture and lab

What are data What are data relationsrelations??What is a relational What is a relational databasedatabase (RDB)? (RDB)?What's a What's a schemaschema??

and how do we design one?and how do we design one?How do we get data into and out of a RDB?How do we get data into and out of a RDB?

What's SQL and how do we use it?What's SQL and how do we use it?And a free bonus tip And a free bonus tip

Page 4: Database lecture

How to use your SQL knowledge in How to use your SQL knowledge in the real worldthe real world

Page 5: Database lecture

What is a database?What is a database?

(Abstract)(Abstract) A structure for storage and A structure for storage and retrieval of dataretrieval of data

(Concrete)(Concrete) An instance of such a An instance of such a structure, containing datastructure, containing data

Is an Excel spreadsheet a database?Is an Excel spreadsheet a database? Is a Is a WWW pageWWW page a database? a database?What about a telephone directory?What about a telephone directory?

Page 6: Database lecture

Is a telephone directory a database?Is a telephone directory a database?

Yes, in a limited way.

It is designed for just one query:It is designed for just one query:FindFind the address and telephone number of person X.the address and telephone number of person X.

What if we wantWhat if we want the names of all residents in the 1700 block of Main St.the names of all residents in the 1700 block of Main St. the address with telephone number 222-2222the address with telephone number 222-2222

Page 7: Database lecture

For For ourour purposes, what is a DB? purposes, what is a DB?

A software structure for data that explicitly A software structure for data that explicitly describes all the describes all the relationsrelations between the between the data types.data types.We'll call this a We'll call this a relationalrelational database or RDB. database or RDB.

What's a What's a data typedata type? What's a ? What's a relationrelation??We'll see by example.We'll see by example.

Page 8: Database lecture

What software manages a RDB?What software manages a RDB?

Microsoft Access, Oracle, Sybase, MySQL, Microsoft Access, Oracle, Sybase, MySQL, PostgreSQL, DB2, Informix...PostgreSQL, DB2, Informix...are database management systems (DBMSs).are database management systems (DBMSs).They are not databasesThey are not databasesA DBMS is software used to create, populate, A DBMS is software used to create, populate,

destroy, query, and administer DBs.destroy, query, and administer DBs.

Page 9: Database lecture

Creating a simple databaseCreating a simple database

What data do I have (in Excel, say)?What data do I have (in Excel, say)?

+----+----------+-------+-------------+---------------+---------------+--------------+| ID | Name | Major | Nationality | Advisor_lname | Advisor_fname | Advisor_dept |+----+----------+-------+-------------+---------------+---------------+--------------+| 0 | Guo | GEN | China | Nelson | Clare | Plant Path || 0 | Guo | GEN | China | Jannink | Jean-Luc | Agronomy || 1 | Mosquera | PLPTH | Colombia | Valent | Barbara | Plant Path || 2 | Smith | STAT | USA | Nelson | Lloyd | Statistics || 3 | Johnson | STAT | USA | Evert | Sam | Philosophy || 3 | Johnson | STAT | USA | Nelson | Lloyd | Statistics |+----+----------+-------+-------------+---------------+---------------+--------------+

Page 10: Database lecture

Let's build a MySQL databaseLet's build a MySQL database

The MySQL The MySQL serverserver is running on my PC is running on my PCAt the command line, typeAt the command line, typemysql -u <username> -penter passwordenter password I'm now running the I'm now running the MySql MySql DBMSDBMS..

show databases;create database <XXX>;use <XXX>;show tables;

Page 11: Database lecture

TableTable

The unit of data organization in a RDBThe unit of data organization in a RDBDescribes a data type or Describes a data type or entityentityEach column describes an Each column describes an attributeattributeSample table (a very bad one!)Sample table (a very bad one!)

+----+----------+-------+-------------+---------------+---------------+--------------+| ID | Name | Major | Nationality | Advisor_lname | Advisor_fname | Advisor_dept |+----+----------+-------+-------------+---------------+---------------+--------------+| 0 | Guo | GEN | China | Nelson | Clare | Plant Path || 0 | Guo | GEN | China | Jannink | Jean-Luc | Agronomy || 1 | Mosquera | PLPTH | Colombia | Valent | Barbara | Plant Path || 2 | Smith | STAT | USA | Nelson | Lloyd | Statistics || 3 | Johnson | STAT | USA | Evert | Sam | Philosophy || 3 | Johnson | STAT | USA | Nelson | Lloyd | Statistics |+----+----------+-------+-------------+---------------+---------------+--------------+

Page 12: Database lecture

Creating a tableCreating a table

CREATE TABLE STUDENT( ID INT(4) NOT NULL,Name VARCHAR(10),Major VARCHAR(5),Nationality VARCHAR(10),Advisor_lname VARCHAR(10),Advisor_fname VARCHAR(10),Advisor_dept VARCHAR(10)

);

Page 13: Database lecture

Viewing the table structureViewing the table structure

describe STUDENT;

+--------------------+-------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+--------------------+-------------+------+-----+---------+-------+| ID | int(4) | NO | | | || Name | varchar(10) | YES | | | || Major | varchar(5) | YES | | | || Nationality | varchar(10) | YES | | | || Advisor_lname | varchar(10) | YES | | | || Advisor_fname | varchar(10) | YES | | | || Advisor_dept | varchar(10) | YES | | | |+--------------------+-------------+------+-----+---------+-------+

Page 14: Database lecture

Populating the STUDENT tablePopulating the STUDENT table

INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 'China', 'Nelson', 'Clare', 'Plant Path');

INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 'China', 'Jannink', 'Jean-Luc', 'Agronomy');

INSERT INTO STUDENT VALUES (1, 'Mosquera', 'PLPTH', 'Colombia', 'Valent', 'Barbara', 'Plant Path');

INSERT INTO STUDENT VALUES (2, 'Smith', 'STAT', 'USA', 'Nelson', 'Lloyd', 'Statistics');

INSERT INTO STUDENT VALUES (3, 'Johnson', 'STAT', 'USA', 'Evert', 'Sam', 'Philosophy');

INSERT INTO STUDENT VALUES (3, 'Johnson', 'STAT', 'USA', 'Nelson', 'Lloyd', 'Statistics');

Page 15: Database lecture

Querying the databaseQuerying the database

SQL (Structured Query Language)SQL (Structured Query Language) is how weis how we

insertinsertqueryquerydeletedeletealteraltersortsortcalculatecalculateand do all other data operationsand do all other data operations

Page 16: Database lecture

Use SELECT for queryingUse SELECT for querying

select * from STUDENT;

+----+----------+-------+-------------+---------------+---------------+--------------+| ID | Name | Major | Nationality | Advisor_lname | Advisor_fname | Advisor_dept |+----+----------+-------+-------------+---------------+---------------+--------------+| 0 | Guo | GEN | China | Nelson | Clare | Plant Path || 0 | Guo | GEN | China | Jannink | Jean-Luc | Agronomy || 1 | Mosquera | PLPTH | Colombia | Valent | Barbara | Plant Path || 2 | Smith | STAT | USA | Nelson | Lloyd | Statistics || 3 | Johnson | STAT | USA | Evert | Sam | Philosophy || 3 | Johnson | STAT | USA | Nelson | Lloyd | Statistics |+----+----------+-------+-------------+---------------+---------------+--------------+

Page 17: Database lecture

A query with conditionsA query with conditions

SELECTSELECT

FROMFROM

WHEREWHERE

OROR

Show names and majors of students whose Show names and majors of students whose nationality is Chinese or whose major is STAT.nationality is Chinese or whose major is STAT.

+----+----------+-------+-------------+---------------+---------------+--------------+| ID | Name | Major | Nationality | Advisor_lname | Advisor_fname | Advisor_dept |+----+----------+-------+-------------+---------------+---------------+--------------+| 0 | Guo | GEN | China | Nelson | Clare | Plant Path || 0 | Guo | GEN | China | Jannink | Jean-Luc | Agronomy || 1 | Mosquera | PLPTH | Colombia | Valent | Barbara | Plant Path || 2 | Smith | STAT | USA | Nelson | Lloyd | Statistics || 3 | Johnson | STAT | USA | Evert | Sam | Philosophy || 3 | Johnson | STAT | USA | Nelson | Lloyd | Statistics |+----+----------+-------+-------------+---------------+---------------+--------------+

Name, MajorName, Major

STUDENTSTUDENT

Nationality Nationality == 'China' 'China'

Major Major == 'STAT' 'STAT';;

Page 18: Database lecture

Is this a "good" database?Is this a "good" database?

Efficiently maintained and queried?Efficiently maintained and queried? data easily added, dropped, changed?data easily added, dropped, changed? If you correct or update repeated data, how can you If you correct or update repeated data, how can you

be sure you've found every instance of the bad data?be sure you've found every instance of the bad data? With multiple records for a student, how can you be With multiple records for a student, how can you be

sure you've found the information you want?sure you've found the information you want?

+----+----------+-------+-------------+---------------+---------------+--------------+| ID | Name | Major | Nationality | Advisor_lname | Advisor_fname | Advisor_dept |+----+----------+-------+-------------+---------------+---------------+--------------+| 0 | Guo | GEN | China | Nelson | Clare | Plant Path || 0 | Guo | GEN | China | Jannink | Jean-Luc | Agronomy || 1 | Mosquera | PLPTH | Colombia | Valent | Barbara | Plant Path || 2 | Smith | STAT | USA | Nelson | Lloyd | Statistics || 3 | Johnson | STAT | USA | Evert | Sam | Philosophy || 3 | Johnson | STAT | USA | Nelson | Lloyd | Statistics |+----+----------+-------+-------------+---------------+---------------+--------------+

Page 19: Database lecture

To reduce redundancy: pack To reduce redundancy: pack multiple data into fields?multiple data into fields?

Why is this a bad idea? It's hard to get at the attributes of the packed data Adding/removing data requires updating old data

+----+-------+-------+-------------+-----------------+-----------------+---------------------+| ID | Name | Major | Nationality | Advisor_lname | Advisor_fname | Advisor_dept |+----+-------+-------+-------------+-----------------+-----------------+---------------------+| 0 | Guo | GEN | China | Nelson, Jannink | Clare, Jean-Luc | Plant Path, Agronomy | 3 | Smith | STAT | USA | Nelson | Lloyd | Statistics ||etc. etc.+----+----------+-------+-------------+---------------+---------------+--------------+

Page 20: Database lecture

Why not just add attributes?Why not just add attributes?

Must know number of advisors in advance Must know number of advisors in advance of designing the databaseof designing the database

Can't easily find all advisors of student XCan't easily find all advisors of student XAdvisor data: what's it doing in the same Advisor data: what's it doing in the same

table with student, anyway?table with student, anyway?

IDID NameName MajorMajor NationalityNationality Advisor 1Advisor 1 Ad1 DeptAd1 Dept Advisor2Advisor2 Ad2 DeptAd2 Dept

00 GuoGuo GENGEN ChinaChina NelsonNelson Plant PathPlant Path WangWang StatisticsStatistics

11 JohnsonJohnson STATSTAT USAUSA EvertEvert PhilosophyPhilosophy NelsonNelson StatisticsStatistics

Page 21: Database lecture

Uncoupling unrelated dataUncoupling unrelated data

It's reasonable to separate advisor from It's reasonable to separate advisor from student!student! if we want to change or add an attribute to if we want to change or add an attribute to

advisoradvisor, why should we have to update , why should we have to update studentstudent records? records?

How can we reduce unwanted How can we reduce unwanted dependencies?dependencies?

+----+----------+-------+-------------+---------------+---------------+--------------+| ID | Name | Major | Nationality | Advisor_lname | Advisor_fname | Advisor_dept |+----+----------+-------+-------------+---------------+---------------+--------------+| 0 | Guo | GEN | China | Nelson | Clare | Plant Path || 1 | Guo | GEN | China | Jannink | Jean-Luc | Agronomy || 2 | Mosquera | PLPTH | Colombia | Valent | Barbara | Plant Path || 3 | Smith | STAT | USA | Nelson | Lloyd | Statistics || 4 | Johnson | STAT | USA | Evert | Sam | Philosophy || 5 | Johnson | STAT | USA | Nelson | Lloyd | Statistics |+----+----------+-------+-------------+---------------+---------------+--------------+

Page 22: Database lecture

Relational database ideaRelational database idea

Developed by E. F. Codd in 1970Developed by E. F. Codd in 1970Records in tables are linked by Records in tables are linked by relationsrelations

between entitiesbetween entitiesThe structure of an RDB is called the data The structure of an RDB is called the data

modelmodel or or schemaschema

Page 23: Database lecture

Thinking about relationsThinking about relations

Can a student have > 1 advisor?Can a student have > 1 advisor?an advisor have > 1 student?an advisor have > 1 student?

Redundancy comes from data types that Redundancy comes from data types that don't have 1-to-1 don't have 1-to-1 relationsrelations!!

Can we use Can we use relationsrelations to develop rules for to develop rules for designing a schema?designing a schema?

Let's list the possible Let's list the possible relationsrelations........

Page 24: Database lecture

What is a data What is a data relationrelation?? A rule of association. There are only three:A rule of association. There are only three:

1.1. One to one (One to one (1111))

2.2. One to many ( = many to one) (One to many ( = many to one) (1M1M, , M1M1))

3.3. Many to many (Many to many (MMMM))

Of what type are the following relations?Of what type are the following relations? Mother, childMother, child Student, faculty advisorStudent, faculty advisor House, street addressHouse, street address Food prices, weather forecastFood prices, weather forecast genetic marker, genome mapgenetic marker, genome map

1M

MM

11

??

MM

Page 25: Database lecture

Designing a schemaDesigning a schema

Decide on the main data entities and their Decide on the main data entities and their attributesattributes

Determine their relationsDetermine their relationsCreate a table for each entityCreate a table for each entityAdd columns for attributesAdd columns for attributesAdd columns that link tables according to Add columns that link tables according to

the data relationsthe data relationsAdd tables to reduce redundancyAdd tables to reduce redundancy

Page 26: Database lecture

What are the main entities in the What are the main entities in the Student data?Student data?

A "main entity": one with attributes of its A "main entity": one with attributes of its own)own)

StudentStudentwith ID, name, major, nationality, with ID, name, major, nationality, advisorsadvisors

AdvisorAdvisorwith first name, last name, departmentwith first name, last name, department

+----+----------+-------+-------------+---------------+---------------+--------------+| ID | Name | Major | Nationality | Advisor_lname | Advisor_fname | Advisor_dept |+----+----------+-------+-------------+---------------+---------------+--------------+| 0 | Guo | GEN | China | Nelson | Clare | Plant Path || 0 | Guo | GEN | China | Jannink | Jean-Luc | Agronomy || 1 | Mosquera | PLPTH | Colombia | Valent | Barbara | Plant Path || 2 | Smith | STAT | USA | Nelson | Lloyd | Statistics || 3 | Johnson | STAT | USA | Evert | Sam | Philosophy || 3 | Johnson | STAT | USA | Nelson | Lloyd | Statistics |+----+----------+-------+-------------+---------------+---------------+--------------+

Page 27: Database lecture

First table-creation decisionsFirst table-creation decisions

Suppose we start by creating STUDENT Suppose we start by creating STUDENT and ADVISOR tables.and ADVISOR tables.

+----+----------+-------+-------------+---------------+---------------+--------------+| ID | Name | Major | Nationality | Advisor_lname | Advisor_fname | Advisor_dept |+----+----------+-------+-------------+---------------+---------------+--------------+| 0 | Guo | GEN | China | Nelson | Clare | Plant Path || 0 | Guo | GEN | China | Jannink | Jean-Luc | Agronomy || 1 | Mosquera | PLPTH | Colombia | Valent | Barbara | Plant Path || 2 | Smith | STAT | USA | Nelson | Lloyd | Statistics || 3 | Johnson | STAT | USA | Evert | Sam | Philosophy || 3 | Johnson | STAT | USA | Nelson | Lloyd | Statistics |+----+----------+-------+-------------+---------------+---------------+--------------+

Page 28: Database lecture

Create a new STUDENT tableCreate a new STUDENT table

drop table STUDENT;

CREATE TABLE STUDENT

( ID INT(4) NOT NULL,

Name VARCHAR(10),

Major VARCHAR(5),

Nationality VARCHAR(10),

);

Advisor_lname VARCHAR(10),

Advisor_fname VARCHAR(10),

Advisor_dept VARCHAR(10)

(leave these out for now .... why?)

Page 29: Database lecture

Repopulate STUDENT tableRepopulate STUDENT table

INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 'China');

INSERT INTO STUDENT VALUES (1, 'Mosquera', 'PLPTH', 'Colombia');

INSERT INTO STUDENT VALUES (2, 'Smith', 'STAT', 'USA');

INSERT INTO STUDENT VALUES (3, 'Johnson', 'STAT', 'USA');

select * from STUDENT;

+----+----------+-------+---------+| ID | Name | Major | Country |+----+----------+-------+---------+| 0 | Guo | GEN | China || 1 | Mosquera | PLPTH | Colombia|| 2 | Smith | STAT | USA || 3 | Johnson | STAT | USA |+----+----------+-------+---------+

Page 30: Database lecture

Table ADVISORTable ADVISOR

CREATE TABLE ADVISOR( ID INT(4) NOT NULL,LName VARCHAR(10),FName VARCHAR(5),Department VARCHAR(20)

);

Why do we always put an ID field in these tables?Why do we always put an ID field in these tables?

Page 31: Database lecture

Populating the ADVISOR tablePopulating the ADVISOR tableINSERT INTO ADVISOR VALUES (0, 'Nelson', 'James', 'Plant Pathology');INSERT INTO ADVISOR VALUES (1, 'Valent', 'Barbara', 'Plant Pathology');INSERT INTO ADVISOR VALUES (2, 'Nelson', 'Lloyd', 'Statistics');INSERT INTO ADVISOR VALUES (3, 'Evert', 'Sam', 'Philosophy');INSERT INTO ADVISOR VALUES (4, 'Jannink', 'Jean-Luc', 'Agronomy');

select * from ADVISOR;

+----+---------+-------+-----------------+| ID | LName | FName | Department |+----+---------+-------+-----------------+| 0 | Nelson | James | Plant Pathology || 1 | Valent | Barba | Plant Pathology || 2 | Nelson | Lloyd | Statistics || 3 | Evert | Sam | Philosophy || 4 | Jannink | Jean- | Agronomy |+----+---------+-------+-----------------+

Page 32: Database lecture

Making trouble with SQLMaking trouble with SQL

Also try to add duplicate data:INSERT INTO ADVISOR VALUES (0, 'Nelson', 'James',

'Plant Pathology');INSERT INTO ADVISOR VALUES (1, 'Valent', 'Barbara',

'Plant Pathology');

What happened?

MySQL didn't prevent us from ruining the nonredundant design...

We'll see how to do this later.We'll see how to do this later.

Page 33: Database lecture

How do we link students with How do we link students with advisors?advisors?

We would like to store the advisors of We would like to store the advisors of each student and the students of each each student and the students of each advisor...advisor...

But with a But with a MMMM relation, we can't store relation, we can't store these data in either table....these data in either table.... (Why not?)(Why not?)

What do we do?What do we do?

Page 34: Database lecture

First, an easier problem: First, an easier problem: 1M1M and and M1M1 relations relations

Suppose we decide to place Nationality in its own table.Suppose we decide to place Nationality in its own table.(Why might we want to do this?(Why might we want to do this?

CREATE TABLE NATIONALITY( ID INT(4) NOT NULL,

Country VARCHAR(25));

INSERT INTO NATIONALITY VALUES (0, 'China');INSERT INTO NATIONALITY VALUES (1, 'Colombia');INSERT INTO NATIONALITY VALUES (2, 'USA');

+----++----+----+| ID | Country |+----+----------+| 0 | China || 1 | Colombia || 2 | USA |+----+----------+

Page 35: Database lecture

Rebuild the STUDENT table...Rebuild the STUDENT table...

drop table STUDENT;

... and replace Nationality with a numerical key!... and replace Nationality with a numerical key!

CREATE TABLE STUDENT

( ID INT(4) NOT NULL,

Name VARCHAR(10),

Major VARCHAR(5),

Nat_ID INT(4)

);

Page 36: Database lecture

Repopulate the STUDENT tableRepopulate the STUDENT tableINSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 0);INSERT INTO STUDENT VALUES (1, 'Mosquera', 'PLPTH', 1);INSERT INTO STUDENT VALUES (2, 'Smith', 'STAT', 2);INSERT INTO STUDENT VALUES (3, 'Johnson', 'STAT', 2);

select * from STUDENT;

+----+----------+-------+--------+| ID | Name | Major | Nat_ID |+----+----------+-------+--------+| 0 | Guo | GEN | 0 || 1 | Mosquera | PLPTH | 1 || 2 | Smith | STAT | 2 || 3 | Johnson | STAT | 2 |+----+----------+-------+--------+

Page 37: Database lecture

Now how do we find the Now how do we find the nationalities of students? nationalities of students?

SELECT

FROM

WHERE

+----------+----------+| Name | Country |+----------+----------+| Guo | China || Mosquera | Colombia || Smith | USA || Johnson | USA |+----------+----------+

This is called a join operation.

STUDENT table+----+----------+-------+--------+| ID | Name | Major | Nat_ID |+----+----------+-------+--------+| 0 | Guo | GEN | 0 || 1 | Mosquera | PLPTH | 1 || 2 | Smith | STAT | 2 || 3 | Johnson | STAT | 2 |+----+----------+-------+--------+

NATIONALITY table+----++----+----+| ID | Country |+----+----------+| 0 | China || 1 | Colombia || 2 | USA |+----+----------+

Name, CountrySTUDENT, NATIONALITY (is this enough?)STUDENT.Nat_ID = NATIONALITY.ID;

Page 38: Database lecture

The The joinjoin operation and operation and keyskeys

Because storing Because storing 1M1M data types in one data types in one table breaks our One Data Instance => table breaks our One Data Instance => One Record rule, weOne Record rule, weplace the two data types into two tablesplace the two data types into two tablesspecify their relations with specify their relations with primaryprimary and and foreign foreign

keyskeysuse a use a joinjoin query to extract attributes from both query to extract attributes from both

tables at oncetables at once

Page 39: Database lecture

Primary and foreign keysPrimary and foreign keys

Primary keyPrimary key: unique identifier (generally a : unique identifier (generally a number) for each record in a table.number) for each record in a table.

Foreign keyForeign key: an identifier in one table that : an identifier in one table that matches the primary key of another tablematches the primary key of another table It serves to It serves to joinjoin the two tables. the two tables. It's the It's the addressaddress of a record, rather than a copy of a record, rather than a copy

of the data in the record.of the data in the record.This decouples the attributes of two different data This decouples the attributes of two different data

types.types.

Page 40: Database lecture

Primary & foreign keys: examplePrimary & foreign keys: example

STUDENT table+----+----------+-------+--------+| ID | Name | Major | Nat_ID |+----+----------+-------+--------+| 0 | Guo | GEN | 0 || 1 | Mosquera | PLPTH | 1 || 2 | Smith | STAT | 2 || 3 | Johnson | STAT | 2 |+----+----------+-------+--------+

NATIONALITY table+----++----+----+| ID | Country |+----+----------+| 0 | China || 1 | Colombia || 2 | USA |+----+----------+

A primary key A foreign key

Page 41: Database lecture

Many-to-many relationsMany-to-many relations

We handled the We handled the 1M1M relation relation

1 nationality <=> many students.

How do we handle the How do we handle the MMMM relation relation

many students <=> many advisors??

Page 42: Database lecture

Table-design rulesTable-design rules

1111 relations: data types may be placed in relations: data types may be placed in the same tablethe same table

1M1M or or M1M1: create two tables, with : create two tables, with primaryprimary and and foreign keysforeign keys

MMMM: create three tables, with two : create three tables, with two primary primary keyskeys and a composite and a composite foreign keyforeign key..

Page 43: Database lecture

Handling the student – advisor Handling the student – advisor relationrelation

CREATE TABLE STUDENT( ID INT(4) NOT NULL,

Name VARCHAR(10),Major VARCHAR(5),Nat_ID INT(4),Primary key (ID),Foreign key (Nat_ID) references NATIONALITY(ID)

);

CREATE TABLE ADVISOR( ID INT(4) NOT NULL,

LName VARCHAR(10),FName VARCHAR(5),Department VARCHAR(20),Primary key (ID)

);

CREATE TABLE STUDENT_ADVISOR

( Student_ID INT(4) NOT NULL,

Advisor_ID INT(4) NOT NULL,

Is_major CHAR(1),

primary key (Student_ID, Advisor_ID),

foreign key (Student_ID) references STUDENT(ID),

foreign key (Advisor_ID) references ADVISOR(ID)

);

Notice that we are telling MySQL about the keys.

What will MySQL do with this?

Page 44: Database lecture

Populating the keyed tablesPopulating the keyed tablesINSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 0);INSERT INTO STUDENT VALUES (1, 'Mosquera', 'PLPTH', 1);INSERT INTO STUDENT VALUES (2, 'Smith', 'STAT', 2);INSERT INTO STUDENT VALUES (3, 'Johnson', 'STAT', 2);

INSERT INTO ADVISOR VALUES (0, 'Nelson', 'James', 'Plant Pathology');

INSERT INTO ADVISOR VALUES (1, 'Valent', 'Barbara', 'Plant Pathology');

INSERT INTO ADVISOR VALUES (2, 'Nelson', 'Lloyd', 'Statistics');INSERT INTO ADVISOR VALUES (3, 'Evert', 'Sam', 'Philosophy');INSERT INTO ADVISOR VALUES (4, 'Jannink', 'Jean-Luc',

'Agronomy');

Try repeating the last command. What happens?Try repeating the last command. What happens?

Page 45: Database lecture

Populating the join tablePopulating the join table

INSERT INTO STUDENT_ADVISOR VALUES (0, 0, 1);

INSERT INTO STUDENT_ADVISOR VALUES (0, 4, 0);

INSERT INTO STUDENT_ADVISOR VALUES (1, 1, 1);

INSERT INTO STUDENT_ADVISOR VALUES (2, 2, 1);

INSERT INTO STUDENT_ADVISOR VALUES (3, 3, 0);

INSERT INTO STUDENT_ADVISOR VALUES (3, 2, 1);

select * from STUDENT_ADVISOR;+------------+------------+----------+| Student_ID | Advisor_ID | Is_major |+------------+------------+----------+| 0 | 0 | 1 || 0 | 4 | 0 || 1 | 1 | 1 || 2 | 2 | 1 || 3 | 3 | 0 || 3 | 2 | 1 |+------------+------------+----------+

Page 46: Database lecture

Tables set up for Tables set up for MMMM relation relationdescribe STUDENT;

+--------+-------------+------+-----+

| Field | Type | Null | Key |

+--------+-------------+------+-----+

| ID | int(4) | NO | PRI |

| Name | varchar(10) | YES | |

| Major | varchar(5) | YES | |

| Nat_ID | int(4) | YES | MUL |

+--------+-------------+------+-----+

describe ADVISOR;

+------------+-------------+------+-----+

| Field | Type | Null | Key |

+------------+-------------+------+-----+

| ID | int(4) | NO | PRI |

| LName | varchar(10) | YES | |

| FName | varchar(5) | YES | |

| Department | varchar(20) | YES | |

+------------+-------------+------+-----+

describe STUDENT_ADVISOR;

+------------+---------+------+-----+

| Field | Type | Null | Key |

+------------+---------+------+-----+

| Student_ID | int(4) | NO | PRI |

| Advisor_ID | int(4) | NO | PRI |

| Is_major | char(1) | YES | |

+------------+---------+------+-----+

How would you extract the names of all advisors of student named Guo?

Page 47: Database lecture

Who are all Guo's advisers?Who are all Guo's advisers?

SELECTSELECT

FROMFROM

WHEREWHERE

ANDAND

ANDAND

Name, LName, FNameName, LName, FName

STUDENT, ADVISOR, STUDENT, ADVISOR, STUDENT_ADVISORSTUDENT_ADVISOR

STUDENT.Name = 'Guo'STUDENT.Name = 'Guo'

STUDENT.ID STUDENT.ID == STUDENT_ADVISOR.Student_IDSTUDENT_ADVISOR.Student_ID

ADVISOR.ID ADVISOR.ID == STUDENT_ADVISOR.Advisor_ID;STUDENT_ADVISOR.Advisor_ID;

Page 48: Database lecture

A joining queryA joining query

SELECTSELECT

FROMFROM

WHEREWHERE

ANDAND

ANDAND

ANDAND

Name, LNameName, LNameSTUDENT, ADVISOR, STUDENT_ADVISORSTUDENT, ADVISOR, STUDENT_ADVISORLName LName == "Nelson" "Nelson"FName FName == "Lloyd "Lloyd ""ADVISOR_ID = ADVISOR.IDADVISOR_ID = ADVISOR.IDSTUDENT_ID = STUDENT.ID;STUDENT_ID = STUDENT.ID;

How to select all the students who have How to select all the students who have James Nelson as an advisor?James Nelson as an advisor?

Page 49: Database lecture

SummarySummary

RDBs incorporate RDBs incorporate 1111, , 1M1M//M1M1, and , and MMMM relations relations among data typesamong data types

Data are stored economicallyData are stored economically with each table describing one data "entity" and its with each table describing one data "entity" and its

attributesattributes and and key key fields encoding the relations between entitiesfields encoding the relations between entities

Data are extracted with SQL Data are extracted with SQL SELECTSELECT statementsstatements and data from different tables are extracted with and data from different tables are extracted with joinsjoins

Page 50: Database lecture

Categories of SQL statementCategories of SQL statement Data definition (DDL)Data definition (DDL)

CREATECREATE ALTERALTER DROPDROP RENAMERENAME TRUNCATETRUNCATE

Data manipulation Data manipulation (DML)(DML) INSERTINSERT UPDATEUPDATE DELETEDELETE

Data control (DCL)Data control (DCL) GRANTGRANT REVOKEREVOKE

Data retrieval (DQL)Data retrieval (DQL) SELECTSELECT

Transaction control Transaction control (TC)(TC) COMMITCOMMIT ROLLBACKROLLBACK

Page 51: Database lecture

A A verbverb, such as , such as SELECTSELECT (Do what?)(Do what?) A A predicatepredicate object that specifies field names in object that specifies field names in

tables (tables (** means all fields) means all fields) (To what?)(To what?) A A prepositional prepositional clause describing the table on clause describing the table on

which the verb acts (which the verb acts (FROM tablenameFROM tablename) ) (Where?)(Where?)

A A conditionalconditional or adverbial clause ( or adverbial clause (WHERE WHERE Advisor = "Nelson"Advisor = "Nelson") ) (How?)(How?)

Various useful helping verbs such as Various useful helping verbs such as ORDER ORDER BYBY

Elements of SQL statements Elements of SQL statements

Page 52: Database lecture

INTEGERINTEGER A whole number A whole number VARCHARVARCHAR(10) Up to 10 characters(10) Up to 10 characters CHARCHAR(10) Fixed number of characters(10) Fixed number of characters DATEDATE A date A date DATETIMEDATETIME Date and time Date and time FLOATFLOAT Floating-point number Floating-point number

TEXTTEXT allows up to 65535 characters allows up to 65535 characters DECIMALDECIMAL(10, 2) Up to 10 digits before the point, 2 (10, 2) Up to 10 digits before the point, 2

afterafter Note: Oracle uses different number types. Note: Oracle uses different number types. ALL DBMSs are not interchangeable!ALL DBMSs are not interchangeable!

How MySQL stores numbersHow MySQL stores numbers

Page 53: Database lecture

Customer order system:Customer order system:entity – relationship diagramentity – relationship diagram

Customer order system:Customer order system:entity – relationship diagramentity – relationship diagram