Your SlideShare is downloading. ×
0
RiceCAP workshop, June 4 – 9, 2006 Noble Foundation Ardmore, Oklahoma Basic data relations for markers, traits, and genoty...
Plan <ul><li>Databases: what and why? </li></ul><ul><ul><li>What's a  relational  database? </li></ul></ul><ul><li>Designi...
What you'll know  after lecture and lab <ul><li>What are data  relations ? </li></ul><ul><li>What is a relational  databas...
How to use your SQL knowledge in the real world
What is a database? <ul><li>(Abstract)  A structure for storage and retrieval of data </li></ul><ul><li>(Concrete)  An ins...
Is a telephone directory a database? <ul><li>Yes, in a limited way. </li></ul><ul><li>It is designed for just one query: <...
For  our  purposes, what is a DB? <ul><li>A software structure for data that explicitly describes all the  relations  betw...
What software manages a RDB? <ul><li>Microsoft Access, Oracle, Sybase, MySQL, PostgreSQL, DB2, Informix... </li></ul><ul><...
Creating a simple database <ul><li>What data do I have (in Excel, say)? </li></ul>+----+----------+-------+-------------+-...
Let's build a MySQL database <ul><li>The MySQL  server  is running on my PC </li></ul><ul><ul><li>At the command line, typ...
Table <ul><li>The unit of data organization in a RDB </li></ul><ul><li>Describes a data type or  entity </li></ul><ul><li>...
Creating a table <ul><li>CREATE TABLE STUDENT </li></ul><ul><li>( ID INT(4) NOT NULL, </li></ul><ul><li>Name VARCHAR(10), ...
Viewing the table structure <ul><li>describe STUDENT; </li></ul>+--------------------+-------------+------+-----+---------...
Populating the STUDENT table <ul><li>INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 'China', 'Nelson', 'Clare', 'Plant Path'...
Querying the database <ul><li>SQL (Structured Query Language) </li></ul><ul><ul><li>is how we </li></ul></ul><ul><ul><ul><...
Use SELECT for querying <ul><li>select * from STUDENT; </li></ul>+----+----------+-------+-------------+---------------+--...
A query with conditions <ul><li>SELECT </li></ul><ul><li>FROM </li></ul><ul><li>WHERE </li></ul><ul><li>OR </li></ul>Show ...
Is this a &quot;good&quot; database? <ul><li>Efficiently maintained and queried? </li></ul><ul><ul><li>data easily added, ...
To reduce redundancy: pack multiple data into fields? <ul><li>Why is this a bad idea? </li></ul><ul><ul><li>It's hard to g...
Why not just add attributes? <ul><li>Must know number of advisors in advance of designing the database </li></ul><ul><li>C...
Uncoupling unrelated data <ul><li>It's reasonable to separate advisor from student! </li></ul><ul><ul><li>if we want to ch...
Relational database idea <ul><li>Developed by E. F. Codd in 1970 </li></ul><ul><li>Records in tables are linked by  relati...
Thinking about relations <ul><li>Can a student have > 1 advisor? </li></ul><ul><ul><li>an advisor have > 1 student? </li><...
What is a data  relation ? <ul><li>A rule of association. There are only three: </li></ul><ul><ul><li>One to one ( 11 ) </...
Designing a schema <ul><li>Decide on the main data entities and their attributes </li></ul><ul><li>Determine their relatio...
What are the main entities in the Student data? <ul><li>A &quot;main entity&quot;: one with attributes of its own) </li></...
First table-creation decisions <ul><li>Suppose we start by creating STUDENT and ADVISOR tables. </li></ul>+----+----------...
Create a new STUDENT table <ul><li>drop table STUDENT; </li></ul><ul><li>CREATE TABLE STUDENT </li></ul><ul><li>( ID INT(4...
Repopulate STUDENT table <ul><li>INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 'China'); </li></ul><ul><li>INSERT INTO STUD...
Table ADVISOR <ul><li>CREATE TABLE ADVISOR </li></ul><ul><li>( ID INT(4) NOT NULL, </li></ul><ul><li>LName VARCHAR(10), </...
Populating the ADVISOR table <ul><li>INSERT INTO ADVISOR VALUES (0, 'Nelson', 'James', 'Plant Pathology'); </li></ul><ul><...
Making trouble with SQL <ul><li>Also try to add duplicate data: </li></ul><ul><li>INSERT INTO ADVISOR VALUES (0, 'Nelson',...
How do we link students with advisors? <ul><li>We would like to store the advisors of each student and the students of eac...
First, an easier problem:  1M  and  M1  relations <ul><li>Suppose we decide to place Nationality in its own table. </li></...
Rebuild the STUDENT table... <ul><li>drop table STUDENT; </li></ul><ul><li>... and replace Nationality with a numerical ke...
Repopulate the STUDENT table <ul><li>INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN',  0 ); </li></ul><ul><li>INSERT INTO STUD...
Now how do we find the nationalities of students?  <ul><li>SELECT </li></ul><ul><li>FROM </li></ul><ul><li>WHERE </li></ul...
The  join  operation and  keys <ul><li>Because storing  1M  data types in one table breaks our One Data Instance => One Re...
Primary and foreign keys <ul><li>Primary key : unique identifier (generally a number) for each record in a table. </li></u...
Primary & foreign keys: example STUDENT table +----+----------+-------+--------+ | ID | Name  | Major | Nat_ID | +----+---...
Many-to-many relations <ul><li>We handled the  1M  relation </li></ul><ul><li>1 nationality <=> many students. </li></ul><...
Table-design rules <ul><li>11  relations: data types may be placed in the same table </li></ul><ul><li>1M  or  M1 : create...
Handling the student – advisor relation <ul><li>CREATE TABLE STUDENT </li></ul><ul><li>( ID INT(4) NOT NULL, </li></ul><ul...
Populating the keyed tables <ul><li>INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 0); </li></ul><ul><li>INSERT INTO STUDENT...
Populating the join table INSERT INTO STUDENT_ADVISOR VALUES (0, 0, 1); INSERT INTO STUDENT_ADVISOR VALUES (0, 4, 0); INSE...
Tables set up for  MM  relation describe STUDENT; +--------+-------------+------+-----+ | Field  | Type  | Null | Key | +-...
Who are all Guo's advisers? <ul><ul><li>SELECT </li></ul></ul><ul><ul><li>FROM </li></ul></ul><ul><ul><li>WHERE </li></ul>...
A joining query <ul><li>SELECT </li></ul><ul><li>FROM </li></ul><ul><li>WHERE </li></ul><ul><li>AND </li></ul><ul><li>AND ...
Summary <ul><li>RDBs incorporate  11 ,  1M / M1 , and  MM  relations among data types </li></ul><ul><li>Data are stored ec...
Categories of SQL statement <ul><li>Data definition (DDL) </li></ul><ul><ul><li>CREATE </li></ul></ul><ul><ul><li>ALTER </...
<ul><li>A  verb , such as  SELECT   (Do what?) </li></ul><ul><li>A  predicate  object that specifies field names in tables...
<ul><li>INTEGER  A whole number </li></ul><ul><li>VARCHAR (10) Up to 10 characters </li></ul><ul><li>CHAR (10) Fixed numbe...
Customer order system: entity – relationship diagram
Upcoming SlideShare
Loading in...5
×

Database lecture

389

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
389
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • SELECT Name, Country from STUDENT, NATIONALITY where STUDENT.Nat_ID = NATIONALITY.ID;
  • SELECT Name, LName, FName FROM STUDENT, ADVISOR, STUDENT_ADVISOR WHERE STUDENT.Name = &apos;Guo&apos; AND STUDENT.ID = STUDENT_ADVISOR.Student_ID AND ADVISOR.ID = STUDENT_ADVISOR.Advisor_ID;
  • Transcript of "Database lecture"

    1. 1. RiceCAP workshop, June 4 – 9, 2006 Noble Foundation Ardmore, Oklahoma Basic data relations for markers, traits, and genotypes Clare Nelson Dept. of Plant Pathology, Kansas State University
    2. 2. Plan <ul><li>Databases: what and why? </li></ul><ul><ul><li>What's a relational database? </li></ul></ul><ul><li>Designing a database for Student data </li></ul><ul><ul><li>Identifying the main data elements </li></ul></ul><ul><ul><li>Identifying their relations </li></ul></ul><ul><li>Creating, populating, and querying a MySQL database </li></ul><ul><li>Lab: Do it yourself (with guidance!) </li></ul>
    3. 3. What you'll know after lecture and lab <ul><li>What are data relations ? </li></ul><ul><li>What is a relational database (RDB)? </li></ul><ul><li>What's a schema ? </li></ul><ul><ul><li>and how do we design one? </li></ul></ul><ul><li>How do we get data into and out of a RDB? </li></ul><ul><ul><li>What's SQL and how do we use it? </li></ul></ul><ul><li>And a free bonus tip     </li></ul>
    4. 4. How to use your SQL knowledge in the real world
    5. 5. What is a database? <ul><li>(Abstract) A structure for storage and retrieval of data </li></ul><ul><li>(Concrete) An instance of such a structure, containing data </li></ul><ul><li>Is an Excel spreadsheet a database? </li></ul><ul><li>Is a WWW page a database? </li></ul><ul><li>What about a telephone directory? </li></ul>
    6. 6. Is a telephone directory a database? <ul><li>Yes, in a limited way. </li></ul><ul><li>It is designed for just one query: </li></ul><ul><li>Find the address and telephone number of person X. </li></ul><ul><li>What if we want </li></ul><ul><li>the names of all residents in the 1700 block of Main St. </li></ul><ul><li>the address with telephone number 222-2222 </li></ul>
    7. 7. For our purposes, what is a DB? <ul><li>A software structure for data that explicitly describes all the relations between the data types. </li></ul><ul><ul><li>We'll call this a relational database or RDB. </li></ul></ul><ul><li>What's a data type ? What's a relation ? </li></ul><ul><ul><li>We'll see by example. </li></ul></ul>
    8. 8. What software manages a RDB? <ul><li>Microsoft Access, Oracle, Sybase, MySQL, PostgreSQL, DB2, Informix... </li></ul><ul><ul><li>are database management systems (DBMSs). </li></ul></ul><ul><ul><li>They are not databases </li></ul></ul><ul><ul><li>A DBMS is software used to create, populate, destroy, query, and administer DBs. </li></ul></ul>
    9. 9. Creating a simple database <ul><li>What data do I have (in Excel, say)? </li></ul>+----+----------+-------+-------------+---------------+---------------+--------------+ | 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 | +----+----------+-------+-------------+---------------+---------------+--------------+
    10. 10. Let's build a MySQL database <ul><li>The MySQL server is running on my PC </li></ul><ul><ul><li>At the command line, type </li></ul></ul><ul><ul><li>mysql -u <username> -p </li></ul></ul><ul><ul><li>enter password </li></ul></ul><ul><ul><li>I'm now running the MySql DBMS . </li></ul></ul><ul><li>show databases; </li></ul><ul><li>create database <XXX>; </li></ul><ul><li>use <XXX>; </li></ul><ul><li>show tables; </li></ul>
    11. 11. Table <ul><li>The unit of data organization in a RDB </li></ul><ul><li>Describes a data type or entity </li></ul><ul><li>Each column describes an attribute </li></ul><ul><li>Sample table (a very bad one!) </li></ul>+----+----------+-------+-------------+---------------+---------------+--------------+ | 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 | +----+----------+-------+-------------+---------------+---------------+--------------+
    12. 12. Creating a table <ul><li>CREATE TABLE STUDENT </li></ul><ul><li>( ID INT(4) NOT NULL, </li></ul><ul><li>Name VARCHAR(10), </li></ul><ul><li>Major VARCHAR(5), </li></ul><ul><li>Nationality VARCHAR(10), </li></ul><ul><li>Advisor_lname VARCHAR(10), </li></ul><ul><li>Advisor_fname VARCHAR(10), </li></ul><ul><li>Advisor_dept VARCHAR(10) </li></ul><ul><li>); </li></ul>
    13. 13. Viewing the table structure <ul><li>describe STUDENT; </li></ul>+--------------------+-------------+------+-----+---------+-------+ | 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 | | | | +--------------------+-------------+------+-----+---------+-------+
    14. 14. Populating the STUDENT table <ul><li>INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 'China', 'Nelson', 'Clare', 'Plant Path'); </li></ul><ul><li>INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 'China', 'Jannink', 'Jean-Luc', 'Agronomy'); </li></ul><ul><li>INSERT INTO STUDENT VALUES (1, 'Mosquera', 'PLPTH', 'Colombia', 'Valent', 'Barbara', 'Plant Path'); </li></ul><ul><li>INSERT INTO STUDENT VALUES (2, 'Smith', 'STAT', 'USA', 'Nelson', 'Lloyd', 'Statistics'); </li></ul><ul><li>INSERT INTO STUDENT VALUES (3, 'Johnson', 'STAT', 'USA', 'Evert', 'Sam', 'Philosophy'); </li></ul><ul><li>INSERT INTO STUDENT VALUES (3, 'Johnson', 'STAT', 'USA', 'Nelson', 'Lloyd', 'Statistics'); </li></ul>
    15. 15. Querying the database <ul><li>SQL (Structured Query Language) </li></ul><ul><ul><li>is how we </li></ul></ul><ul><ul><ul><li>insert </li></ul></ul></ul><ul><ul><ul><li>query </li></ul></ul></ul><ul><ul><ul><li>delete </li></ul></ul></ul><ul><ul><ul><li>alter </li></ul></ul></ul><ul><ul><ul><li>sort </li></ul></ul></ul><ul><ul><ul><li>calculate </li></ul></ul></ul><ul><ul><ul><li>and do all other data operations </li></ul></ul></ul>
    16. 16. Use SELECT for querying <ul><li>select * from STUDENT; </li></ul>+----+----------+-------+-------------+---------------+---------------+--------------+ | 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 | +----+----------+-------+-------------+---------------+---------------+--------------+
    17. 17. A query with conditions <ul><li>SELECT </li></ul><ul><li>FROM </li></ul><ul><li>WHERE </li></ul><ul><li>OR </li></ul>Show names and majors of students whose 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, Major STUDENT Nationality = 'China' Major = 'STAT' ;
    18. 18. Is this a &quot;good&quot; database? <ul><li>Efficiently maintained and queried? </li></ul><ul><ul><li>data easily added, dropped, changed? </li></ul></ul><ul><ul><li>If you correct or update repeated data, how can you be sure you've found every instance of the bad data? </li></ul></ul><ul><ul><li>With multiple records for a student, how can you be sure you've found the information you want? </li></ul></ul>+----+----------+-------+-------------+---------------+---------------+--------------+ | 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 | +----+----------+-------+-------------+---------------+---------------+--------------+
    19. 19. To reduce redundancy: pack multiple data into fields? <ul><li>Why is this a bad idea? </li></ul><ul><ul><li>It's hard to get at the attributes of the packed data </li></ul></ul><ul><ul><li>Adding/removing data requires updating old data </li></ul></ul>+----+-------+-------+-------------+-----------------+-----------------+---------------------+ | 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. +----+----------+-------+-------------+---------------+---------------+--------------+
    20. 20. Why not just add attributes? <ul><li>Must know number of advisors in advance of designing the database </li></ul><ul><li>Can't easily find all advisors of student X </li></ul><ul><li>Advisor data: what's it doing in the same table with student, anyway? </li></ul>Statistics Nelson Philosophy Evert USA STAT Johnson 1 Statistics Wang Plant Path Nelson China GEN Guo 0 Ad2 Dept Advisor2 Ad1 Dept Advisor 1 Nationality Major Name ID
    21. 21. Uncoupling unrelated data <ul><li>It's reasonable to separate advisor from student! </li></ul><ul><ul><li>if we want to change or add an attribute to advisor , why should we have to update student records? </li></ul></ul><ul><li>How can we reduce unwanted dependencies? </li></ul>+----+----------+-------+-------------+---------------+---------------+--------------+ | 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 | +----+----------+-------+-------------+---------------+---------------+--------------+
    22. 22. Relational database idea <ul><li>Developed by E. F. Codd in 1970 </li></ul><ul><li>Records in tables are linked by relations between entities </li></ul><ul><li>The structure of an RDB is called the data model or schema </li></ul>
    23. 23. Thinking about relations <ul><li>Can a student have > 1 advisor? </li></ul><ul><ul><li>an advisor have > 1 student? </li></ul></ul><ul><li>Redundancy comes from data types that don't have 1-to-1 relations ! </li></ul><ul><li>Can we use relations to develop rules for designing a schema? </li></ul><ul><li>Let's list the possible relations .... </li></ul>
    24. 24. What is a data relation ? <ul><li>A rule of association. There are only three: </li></ul><ul><ul><li>One to one ( 11 ) </li></ul></ul><ul><ul><li>One to many ( = many to one) ( 1M , M1 ) </li></ul></ul><ul><ul><li>Many to many ( MM ) </li></ul></ul><ul><li>Of what type are the following relations? </li></ul><ul><ul><li>Mother, child </li></ul></ul><ul><ul><li>Student, faculty advisor </li></ul></ul><ul><ul><li>House, street address </li></ul></ul><ul><ul><li>Food prices, weather forecast </li></ul></ul><ul><ul><li>genetic marker, genome map </li></ul></ul>1M MM 11 ?? MM
    25. 25. Designing a schema <ul><li>Decide on the main data entities and their attributes </li></ul><ul><li>Determine their relations </li></ul><ul><li>Create a table for each entity </li></ul><ul><li>Add columns for attributes </li></ul><ul><li>Add columns that link tables according to the data relations </li></ul><ul><li>Add tables to reduce redundancy </li></ul>
    26. 26. What are the main entities in the Student data? <ul><li>A &quot;main entity&quot;: one with attributes of its own) </li></ul><ul><li>Student </li></ul><ul><ul><li>with ID, name, major, nationality, advisors </li></ul></ul><ul><li>Advisor </li></ul><ul><ul><li>with first name, last name, department </li></ul></ul>+----+----------+-------+-------------+---------------+---------------+--------------+ | 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 | +----+----------+-------+-------------+---------------+---------------+--------------+
    27. 27. First table-creation decisions <ul><li>Suppose we start by creating STUDENT and ADVISOR tables. </li></ul>+----+----------+-------+-------------+---------------+---------------+--------------+ | 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 | +----+----------+-------+-------------+---------------+---------------+--------------+
    28. 28. Create a new STUDENT table <ul><li>drop table STUDENT; </li></ul><ul><li>CREATE TABLE STUDENT </li></ul><ul><li>( ID INT(4) NOT NULL, </li></ul><ul><li>Name VARCHAR(10), </li></ul><ul><li>Major VARCHAR(5), </li></ul><ul><li>Nationality VARCHAR(10), </li></ul><ul><li>); </li></ul><ul><li>Advisor_lname VARCHAR(10), </li></ul><ul><li>Advisor_fname VARCHAR(10), </li></ul><ul><li>Advisor_dept VARCHAR(10) </li></ul><ul><li>(leave these out for now .... why?) </li></ul>
    29. 29. Repopulate STUDENT table <ul><li>INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 'China'); </li></ul><ul><li>INSERT INTO STUDENT VALUES (1, 'Mosquera', 'PLPTH', 'Colombia'); </li></ul><ul><li>INSERT INTO STUDENT VALUES (2, 'Smith', 'STAT', 'USA'); </li></ul><ul><li>INSERT INTO STUDENT VALUES (3, 'Johnson', 'STAT', 'USA'); </li></ul>select * from STUDENT; +----+----------+-------+---------+ | ID | Name | Major | Country | +----+----------+-------+---------+ | 0 | Guo | GEN | China | | 1 | Mosquera | PLPTH | Colombia| | 2 | Smith | STAT | USA | | 3 | Johnson | STAT | USA | +----+----------+-------+---------+
    30. 30. Table ADVISOR <ul><li>CREATE TABLE ADVISOR </li></ul><ul><li>( ID INT(4) NOT NULL, </li></ul><ul><li>LName VARCHAR(10), </li></ul><ul><li>FName VARCHAR(5), </li></ul><ul><li>Department VARCHAR(20) </li></ul><ul><li>); </li></ul><ul><li>Why do we always put an ID field in these tables? </li></ul>
    31. 31. Populating the ADVISOR table <ul><li>INSERT INTO ADVISOR VALUES (0, 'Nelson', 'James', 'Plant Pathology'); </li></ul><ul><li>INSERT INTO ADVISOR VALUES (1, 'Valent', 'Barbara', 'Plant Pathology'); </li></ul><ul><li>INSERT INTO ADVISOR VALUES (2, 'Nelson', 'Lloyd', 'Statistics'); </li></ul><ul><li>INSERT INTO ADVISOR VALUES (3, 'Evert', 'Sam', 'Philosophy'); </li></ul><ul><li>INSERT INTO ADVISOR VALUES (4, 'Jannink', 'Jean-Luc', 'Agronomy'); </li></ul><ul><li>select * from ADVISOR; </li></ul>+----+---------+-------+-----------------+ | 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 | +----+---------+-------+-----------------+
    32. 32. Making trouble with SQL <ul><li>Also try to add duplicate data: </li></ul><ul><li>INSERT INTO ADVISOR VALUES (0, 'Nelson', 'James', 'Plant Pathology'); </li></ul><ul><li>INSERT INTO ADVISOR VALUES (1, 'Valent', 'Barbara', 'Plant Pathology'); </li></ul><ul><li>What happened? </li></ul><ul><li>MySQL didn't prevent us from ruining the nonredundant design... </li></ul><ul><li>We'll see how to do this later. </li></ul>
    33. 33. How do we link students with advisors? <ul><li>We would like to store the advisors of each student and the students of each advisor... </li></ul><ul><li>But with a MM relation, we can't store these data in either table.... </li></ul><ul><ul><li>(Why not?) </li></ul></ul><ul><li>What do we do? </li></ul>
    34. 34. First, an easier problem: 1M and M1 relations <ul><li>Suppose we decide to place Nationality in its own table. </li></ul><ul><li>(Why might we want to do this? </li></ul><ul><li>CREATE TABLE NATIONALITY </li></ul><ul><li>( ID INT(4) NOT NULL, </li></ul><ul><li>Country VARCHAR(25) </li></ul><ul><li>); </li></ul><ul><li>INSERT INTO NATIONALITY VALUES (0, 'China'); </li></ul><ul><li>INSERT INTO NATIONALITY VALUES (1, 'Colombia'); </li></ul><ul><li>INSERT INTO NATIONALITY VALUES (2, 'USA'); </li></ul>+----++----+----+ | ID | Country | +----+----------+ | 0 | China | | 1 | Colombia | | 2 | USA | +----+----------+
    35. 35. Rebuild the STUDENT table... <ul><li>drop table STUDENT; </li></ul><ul><li>... and replace Nationality with a numerical key! </li></ul><ul><li>CREATE TABLE STUDENT </li></ul><ul><li>( ID INT(4) NOT NULL, </li></ul><ul><li>Name VARCHAR(10), </li></ul><ul><li>Major VARCHAR(5), </li></ul><ul><li>Nat_ID INT(4) </li></ul><ul><li>); </li></ul>
    36. 36. Repopulate the STUDENT table <ul><li>INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 0 ); </li></ul><ul><li>INSERT INTO STUDENT VALUES (1, 'Mosquera', 'PLPTH', 1 ); </li></ul><ul><li>INSERT INTO STUDENT VALUES (2, 'Smith', 'STAT', 2 ); </li></ul><ul><li>INSERT INTO STUDENT VALUES (3, 'Johnson', 'STAT', 2 ); </li></ul><ul><li>select * from STUDENT; </li></ul>+----+----------+-------+--------+ | ID | Name | Major | Nat_ID | +----+----------+-------+--------+ | 0 | Guo | GEN | 0 | | 1 | Mosquera | PLPTH | 1 | | 2 | Smith | STAT | 2 | | 3 | Johnson | STAT | 2 | +----+----------+-------+--------+
    37. 37. Now how do we find the nationalities of students? <ul><li>SELECT </li></ul><ul><li>FROM </li></ul><ul><li>WHERE </li></ul>+----------+----------+ | 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, Country STUDENT, NATIONALITY ( is this enough? ) STUDENT.Nat_ID = NATIONALITY.ID;
    38. 38. The join operation and keys <ul><li>Because storing 1M data types in one table breaks our One Data Instance => One Record rule, we </li></ul><ul><ul><li>place the two data types into two tables </li></ul></ul><ul><ul><li>specify their relations with primary and foreign keys </li></ul></ul><ul><ul><li>use a join query to extract attributes from both tables at once </li></ul></ul>
    39. 39. Primary and foreign keys <ul><li>Primary key : unique identifier (generally a number) for each record in a table. </li></ul><ul><li>Foreign key : an identifier in one table that matches the primary key of another table </li></ul><ul><ul><li>It serves to join the two tables. </li></ul></ul><ul><ul><li>It's the address of a record, rather than a copy of the data in the record. </li></ul></ul><ul><ul><ul><li>This decouples the attributes of two different data types. </li></ul></ul></ul>
    40. 40. Primary & 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
    41. 41. Many-to-many relations <ul><li>We handled the 1M relation </li></ul><ul><li>1 nationality <=> many students. </li></ul><ul><li>How do we handle the MM relation </li></ul><ul><li>many students <=> many advisors ? </li></ul>
    42. 42. Table-design rules <ul><li>11 relations: data types may be placed in the same table </li></ul><ul><li>1M or M1 : create two tables, with primary and foreign keys </li></ul><ul><li>MM : create three tables, with two primary keys and a composite foreign key . </li></ul>
    43. 43. Handling the student – advisor relation <ul><li>CREATE TABLE STUDENT </li></ul><ul><li>( ID INT(4) NOT NULL, </li></ul><ul><li>Name VARCHAR(10), </li></ul><ul><li>Major VARCHAR(5), </li></ul><ul><li>Nat_ID INT(4), </li></ul><ul><li>Primary key (ID), </li></ul><ul><li>Foreign key (Nat_ID) references NATIONALITY(ID) </li></ul><ul><li>); </li></ul><ul><li>CREATE TABLE ADVISOR </li></ul><ul><li>( ID INT(4) NOT NULL, </li></ul><ul><li>LName VARCHAR(10), </li></ul><ul><li>FName VARCHAR(5), </li></ul><ul><li>Department VARCHAR(20), </li></ul><ul><li>Primary key (ID) </li></ul><ul><li>); </li></ul>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?
    44. 44. Populating the keyed tables <ul><li>INSERT INTO STUDENT VALUES (0, 'Guo', 'GEN', 0); </li></ul><ul><li>INSERT INTO STUDENT VALUES (1, 'Mosquera', 'PLPTH', 1); </li></ul><ul><li>INSERT INTO STUDENT VALUES (2, 'Smith', 'STAT', 2); </li></ul><ul><li>INSERT INTO STUDENT VALUES (3, 'Johnson', 'STAT', 2); </li></ul><ul><li>INSERT INTO ADVISOR VALUES (0, 'Nelson', 'James', 'Plant Pathology'); </li></ul><ul><li>INSERT INTO ADVISOR VALUES (1, 'Valent', 'Barbara', 'Plant Pathology'); </li></ul><ul><li>INSERT INTO ADVISOR VALUES (2, 'Nelson', 'Lloyd', 'Statistics'); </li></ul><ul><li>INSERT INTO ADVISOR VALUES (3, 'Evert', 'Sam', 'Philosophy'); </li></ul><ul><li>INSERT INTO ADVISOR VALUES (4, 'Jannink', 'Jean-Luc', 'Agronomy'); </li></ul><ul><li>Try repeating the last command. What happens? </li></ul>
    45. 45. Populating 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 | +------------+------------+----------+
    46. 46. Tables set up for MM relation describe 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?
    47. 47. Who are all Guo's advisers? <ul><ul><li>SELECT </li></ul></ul><ul><ul><li>FROM </li></ul></ul><ul><ul><li>WHERE </li></ul></ul><ul><ul><li>AND </li></ul></ul><ul><ul><li>AND </li></ul></ul><ul><li>Name, LName, FName </li></ul><ul><ul><li>STUDENT, ADVISOR, STUDENT_ADVISOR </li></ul></ul><ul><ul><li>STUDENT.Name = 'Guo' </li></ul></ul><ul><ul><li>STUDENT.ID = STUDENT_ADVISOR.Student_ID </li></ul></ul><ul><ul><li>ADVISOR.ID = STUDENT_ADVISOR.Advisor_ID; </li></ul></ul>
    48. 48. A joining query <ul><li>SELECT </li></ul><ul><li>FROM </li></ul><ul><li>WHERE </li></ul><ul><li>AND </li></ul><ul><li>AND </li></ul><ul><li>AND </li></ul>Name, LName STUDENT, ADVISOR, STUDENT_ADVISOR LName = &quot;Nelson&quot; FName = &quot;Lloyd &quot; ADVISOR_ID = ADVISOR.ID STUDENT_ID = STUDENT.ID; How to select all the students who have James Nelson as an advisor?
    49. 49. Summary <ul><li>RDBs incorporate 11 , 1M / M1 , and MM relations among data types </li></ul><ul><li>Data are stored economically </li></ul><ul><ul><li>with each table describing one data &quot;entity&quot; and its attributes </li></ul></ul><ul><ul><li>and key fields encoding the relations between entities </li></ul></ul><ul><li>Data are extracted with SQL SELECT statements </li></ul><ul><ul><li>and data from different tables are extracted with joins </li></ul></ul>
    50. 50. Categories of SQL statement <ul><li>Data definition (DDL) </li></ul><ul><ul><li>CREATE </li></ul></ul><ul><ul><li>ALTER </li></ul></ul><ul><ul><li>DROP </li></ul></ul><ul><ul><li>RENAME </li></ul></ul><ul><ul><li>TRUNCATE </li></ul></ul><ul><li>Data manipulation (DML) </li></ul><ul><ul><li>INSERT </li></ul></ul><ul><ul><li>UPDATE </li></ul></ul><ul><ul><li>DELETE </li></ul></ul><ul><li>Data control (DCL) </li></ul><ul><ul><li>GRANT </li></ul></ul><ul><ul><li>REVOKE </li></ul></ul><ul><li>Data retrieval (DQL) </li></ul><ul><ul><li>SELECT </li></ul></ul><ul><li>Transaction control (TC) </li></ul><ul><ul><li>COMMIT </li></ul></ul><ul><ul><li>ROLLBACK </li></ul></ul>
    51. 51. <ul><li>A verb , such as SELECT (Do what?) </li></ul><ul><li>A predicate object that specifies field names in tables ( * means all fields) (To what?) </li></ul><ul><li>A prepositional clause describing the table on which the verb acts ( FROM tablename ) (Where?) </li></ul><ul><li>A conditional or adverbial clause ( WHERE Advisor = &quot;Nelson&quot; ) (How?) </li></ul><ul><li>Various useful helping verbs such as ORDER BY </li></ul>Elements of SQL statements
    52. 52. <ul><li>INTEGER A whole number </li></ul><ul><li>VARCHAR (10) Up to 10 characters </li></ul><ul><li>CHAR (10) Fixed number of characters </li></ul><ul><li>DATE A date </li></ul><ul><li>DATETIME Date and time </li></ul><ul><li>FLOAT Floating-point number </li></ul><ul><li>TEXT allows up to 65535 characters </li></ul><ul><li>DECIMAL (10, 2) Up to 10 digits before the point, 2 after </li></ul><ul><li>Note: Oracle uses different number types. ALL DBMSs are not interchangeable! </li></ul>How MySQL stores numbers
    53. 53. Customer order system: entity – relationship diagram
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×