Download presentation
Presentation is loading. Please wait.
Published byMargaretMargaret Holland Modified over 8 years ago
1
Working with MySQL A290/A590, Fall 2014 10/07/2014
2
The MySQL Server A MySQL server can be given SQL commands, executes them, and returns the results to the connected application – Like Apache, we need to assign a port number on the system to MySQL on which it will listen for requests Starting the server Stopping the server
3
Command Line Interface Starting the MySQL client Commands for looking around in the database –SHOW DATABASES –USE database –SHOW TABLES –DESCRIBE table
4
Databases Advantages – concurrency – fast access to the data that is transparent to the db user – uses tables, a general data structure that can implement any data model – unified language for accessing and manipulating the data in the database (SQL) Disadvantages – For certain tasks, can be an overkill as far as complexity and storage requirements are concerned
5
Database Tables A typical database table definition has: – a name – a list of columns and their data types – a list of constraints primary key to ensure uniqueness (if necessary) foreign keys to facilitate relationships w/ other tables indices to facilitate fast look ups
6
Database Structure For Quizes iddescriptioncreation_date 1Favorite Things Quiz 10/07/2014 10:22 idtextquiz_id 1What is your favorite color? 1 2What is your favorite book? 1 … idtextpoint_valuequestion_id 1Red11 2Green101 … quiz table question table answer table
7
Data Relationships One-To-Many: the relationship between the quiz and question tables, or question and answer tables – One quiz can have many questions, but a question can only have one associated quiz – One question can have many possible answers, but an answer can have only one associated question
8
Keeping Track of Users and Their Quizes idemailpassword 1dnikolov@i ndiana.edu … iddescriptioncreation_date 1Favorite Things Quiz 10/07/2014 10:22 AM quiz table user table user_idquiz_idscoredate_taken 111510/07/2014 11:00 AM … user_scores table
9
Table Relationships Many-To-Many: the relationship defined in the user_scores table – One quiz can be taken by many users – One user can take many quizes
10
Creating The Database Schema CREATE TABLE quiz ( id INT NOT NULL AUTO_INCREMENT, description VARCHAR(255), create_time DATETIME NOT NULL, PRIMARY KEY(id) ) CREATE TABLE question ( id INT NOT NULL AUTO_INCREMENT, text VARCHAR(255), quiz_id INT NOT NULL, PRIMARY KEY (id), FOREIGN KEY (quiz_id) REFERENCES quiz(id) ON DELETE CASCADE ) CREATE TABLE answer ( id INT NOT NULL AUTO_INCREMENT, text VARCHAR(255) NOT NULL, point_value INT NOT NULL, question_id INT NOT NULL, PRIMARY KEY(id), FOREIGN KEY question_id REFERENCES question(id) ON DELETE CASCADE )
11
Creating the Database Schema CREATE TABLE user ( id INT NOT NULL AUTO_INCREMENT, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL PRIMARY KEY (id) ) CREATE TABLE user_scores ( user_id INT NOT NULL, quiz_id INT NOT NULL, score INT NOT NULL, date_taken DATETIME NOT NULL, PRIMARY KEY (user_id, quiz_id), FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE, FOREIGN KEY (quiz_id) REFERENCES quiz(id) ON DELETE CASCADE )
12
Filling In the Details Data types: – Numbers: INT, LONGINT, NUMERIC, FLOAT, DOUBLE – Strings: VARCHAR( >), TEXT, BLOB – Other: DATETIME NOT NULL vs NULL: whether to allow empty values or not PRIMARY KEY and FOREIGN KEY CASCADE: Keeping the data clean and robust
13
Viewing and Manipulating Data in MySQL SELECT – SELECT * FROM quiz; – SELECT a.text, a.point_value FROM answer a – SELECT a.text FROM answer a WHERE a.point_value > 2 INSERT – INSERT INTO quiz VALUES (NULL, 'Test Quiz', NOW()) DELETE – DELETE FROM answer WHERE point_value >= 3 UPDATE – UPDATE quiz SET description = 'Test' WHERE id=1
14
Viewing Data From Multiple Tables How do we view all questions and their answers for a particular quiz iddescriptioncreation_dat e 1Favorite Things Quiz 10/07/2014 10:22 idtextquiz_id 1What is your favorite color? 1 2What is your favorite book? 1 … idtextpoint_valuequestion_id 1Red11 2Green101 …
15
Using INNER JOIN SELECT quiz.description, q.text, a.text, a.point_value FROM quiz q INNER JOIN question qn ON q.id =qn.quiz_id INNER JOIN answer a ON qn.id=a.question_id
16
Interface Between Python and MySQL See examples.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.