Create a simple database with MySQL

Similar documents
MySQL. A practical introduction to database design

MySQL Installation Guide (Windows)

MySQL Installation Guide (Windows)

School of Information and Computer Technology Sirindhorn International Institute of Technology Thammasat University

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

CSCB20 Week 4. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

The URL of the whole system is:

TUTORIAL FOR IMPORTING OTTAWA FIRE HYDRANT PARKING VIOLATION DATA INTO MYSQL

SQL CSCI 201 Principles of Software Development

A practical introduction to database design

Introduction to ERwin

Where Are We? Next Few Lectures. Integrity Constraints Motivation. Constraints in E/R Diagrams. Keys in E/R Diagrams

Oracle Database 10g Express

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

Databases (MariaDB/MySQL) CS401, Fall 2015

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

TUTORIAL FOR IMPORTING OTTAWA FIRE HYDRANT PARKING VIOLATION DATA INTO MYSQL

From theory to practice. Designing Tables for a postgresql Database System. psql. Reminder. A few useful commands

Applied Databases. Sebastian Maneth. Lecture 5 ER Model, normal forms. University of Edinburgh - January 25 th, 2016

Chapter 18: Data Analysis and Mining

Databases CSCI 201 Principles of Software Development

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Applied Databases. Sebastian Maneth. Lecture 5 ER Model, Normal Forms. University of Edinburgh - January 30 th, 2017

Basic SQL. Basic SQL. Basic SQL

Database Management

MySQL Installation Guide (OS X)

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

Lab # 3 Hands-On. DML Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

MySQL Data Modeling Tutorial

Networks and Web for Health Informatics (HINF 6220)

More MySQL ELEVEN Walkthrough examples Walkthrough 1: Bulk loading SESSION

ERD Tutorial: How to Design and Generate SQL Server DB? Written Date : June 19, 2015

Midterm Exam #2 (Version A) CS 122A Winter 2017

Database Management Systems

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Entity-Relationship Models: Good Design and Constraints

Database Systems. phpmyadmin Tutorial

Creating databases using SQL Server Management Studio Express

Draft. Students Table. FName LName StudentID College Year. Justin Ennen Science Senior. Dan Bass Management Junior

ACS-3902 Fall Ron McFadyen 3D21 Slides are based on chapter 5 (7 th edition) (chapter 3 in 6 th edition)

System Admin Manual <SNAPVALET> <Team No- 03>

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Introducing SQL Query Verifier Plugin

CSCB20 Week 3. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

This lecture. Databases - SQL II. Counting students. Summary Functions

Introduction. Example Databases

Databases - SQL II. (GF Royle, N Spadaccini ) Structured Query Language II 1 / 22

MySQL Views & Comparing SQL to NoSQL

Product: DQ Order Manager Release Notes

Chapter 2: Entity-Relationship Model

CS425 Fall 2016 Boris Glavic Chapter 2: Intro to Relational Model

Chapter 7: Entity-Relationship Model

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

Chapter 7: Entity-Relationship Model

CS 1520 / CoE 1520: Programming Languages for Web Applications (Spring 2013) Department of Computer Science, University of Pittsburgh

Unit Assessment Guide

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

This lecture. Basic Syntax

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

Introduction to Database Systems CSE 414

Chapter 9: Working With Data

Conceptual Design. The Entity-Relationship (ER) Model

Oracle Database 10g: Introduction to SQL

THE UNIVERSITY OF AUCKLAND

CSIT115/CSIT815 Data Management and Security Assignment 2

Homework Assignment 3. November 11th, 2014 Due on November 25th, 12:30pm (noon) CS425 - Database Organization Results

SQL: Data Definition Language

Relational model continued. Understanding how to use the relational model. Summary of board example: with Copies as weak entity

Chapter 7: Entity-Relationship Model

A Flat file database. Problems with a flat file database (data redundancy)

Faculty of Environment & Technology

Midterm Review. Winter Lecture 13

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL. Overview

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Relational Model. Week 2

Today Learning outcomes LO2

CS143: Relational Model

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

Referential integrity

CS/INFO 4154: Analytics-driven Game Design

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions.

STIDistrict Query (Basic)

DATABASE DESIGN. Fields in database table have a data type. Some of the data types used in database table are explained below.

Model Question Paper. Credits: 4 Marks: 140

Database Languages. A DBMS provides two types of languages: Language for accessing & manipulating the data. Language for defining a database schema

Instruction Create Table Sql Primary Key Clustered Asc

Chapter 2: Intro to Relational Model

The Relational Model 2. Week 3

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

OKC MySQL Users Group

user specifies what is wanted, not how to find it

Translating an ER Diagram to a Relational Schema

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

1 INTRODUCTION TO EASIK 2 TABLE OF CONTENTS

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

Unit 3 : Relational Database Design

CSCB20 Week 2. Introduction to Database and Web Application Programming. Anna Bretscher Winter 2017

CSE 344 Final Examination

T-SQL Training: T-SQL for SQL Server for Developers

Transcription:

Create a simple database with MySQL 1.Connect the MySQL server through MySQL Workbench You can achieve many database operations by typing the SQL langue into the Query panel, such as creating a database, creating tables, selecting, updating, dropping tables or databases. This document is going to create a simple database instance for student management system in university. 2.Create a database type the following SQL and click the execute (the third icon), Stu_Man is the name of created database. You will find the created database on the schemas panel (left, if not, please refresh). create database Stu_Man; 3.Create tables MySQL is a typical relational database, which contains two basic concepts, entity and Relation (ER). Both entity and relation are described as tables, which are organized into rows and columns. All the data referenced by an attribute are in the same domain and conform to the same constraints.

This instance contains three tables, that are tb_student (student information), tb_course (course information), and tb_student_course (the scores of student for course). We can describe them using the following ER-Diagram (https://en.wikipedia.org/wiki/entity relationship_model) Rectangles: entities, Diamonds: relationships Circles: attributes Underline means this attribute is the primary key which is the uniquely identifying attribute. Create these tables by executing the following SQLs: create table Stu_Man.tb_student ( id int(11) not null auto_increment primary key, Name varchar(32) not null, Age int default 0,check(Age>0 and Age<=100), Gender boolean default 0,check(Gender=0 or Gender=1), Major varchar(32) not null ); create table Stu_Man.tb_course ( id int(5) not null auto_increment primary key, Name varchar(32) not null,

Credit int(2) ); create table Stu_Man.tb_student_course ( id int(5) not null auto_increment primary key, StudentID int(11), CourseID int(5), Score float(5) ); tips: you can execute specific SQLs with selecting them, otherwise workbench will execute all the current SQLs in the query panel. you will see your created tables (refresh): 4.Import or insert (add) data into tables Insert by executing SQL:

Insert into Stu_Man.tb_student (id, Name, Age, Gender, Major) values (001, Marry, 21, 0, Law ); or using the visual grid: select the table you want to edit, and click the grid icon (third) or right click the mouse and then select ; you will see the current data of this table, such as the record we just inserted by executing SQL: you can achieve data edit (edit, insert, delete) or export and import data (many data formats are legal, such as.sql,.csv and so on). 5. One most important operation: Select if I want to find out the students whose majors are ComputerScience : SELECT * FROM Stu_Man.tb_student where Major="ComputerScience"; * means selecting all the attributes;

If I just want to know their names: SELECT Name FROM Stu_Man.tb_student where Major="ComputerScience"; Selecting from multiple tables with union: such as selecting students who are studying Management course SELECT tb_student.name FROM Stu_Man.tb_student, Stu_Man.tb_student_course, Stu_Man.tb_course where tb_student.id = tb_student_course.studentid and tb_student_course.courseid = tb_course.id and tb_course.name = "Management"; 6. Advanced functions (statistics), group by and roll up (http://dev.mysql.com/doc/refman/5.7/en/group-by-modifiers.html) If I want to know the number of students for each course:

select Stu_Man.tb_student_course.CourseID, count(distinct(stu_man.tb_student_course.studentid)) from Stu_Man.tb_student_course group by tb_student_course.courseid; If I want to know the number of students for each course and also the number of all the students: select Stu_Man.tb_student_course.CourseID, count(distinct(stu_man.tb_student_course.studentid)) from Stu_Man.tb_student_course group by tb_student_course.courseid with rollup;

the difference is the last row in the second selection. The GROUP BY clause permits a WITH ROLLUP modifier that causes extra rows to be added to the summary output. These rows represent higher-level (or super-aggregate) summary operations. ROLLUP thus enables you to answer questions at multiple levels of analysis with a single query. It can be used, for example, to provide support for OLAP (Online Analytical Processing) operations. Another example: If I want to know the total scores, average scores for each student and the students from each major SELECT tb_student.major, tb_student.id as studentid, sum(tb_student_course.score) as sum_score,avg(tb_student_course.score) as avg_score FROM Stu_Man.tb_student,Stu_Man.tb_student_course where tb_student.id = tb_student_course.studentid group by tb_student.major, tb_student.id with rollup; More complicated SQLs (please refer http://www.w3schools.com/sql/, http://dev.mysql.com/doc/refman/5.7/en/examples.html ) The referred database instance and another more complicated database for e-commerce Download the example database in SQL format. You can import the complete database instance directly without building step by step. Export database or a certain table:

Click the Data Export, and then select the target database or table, click the Export to Self- Contained File option, and finally click the Start Export button. Import database or tables: 1.Download the referred database instance for student management https://mega.nz/#!upzcuqbi!wb9cwiv5yz_r0nkns-slbjwcdyaqc6um6cvzpz7ywqw 2.Create a new schema for the database, can be named as Stu_Man

3. Set the created database as the default schema 4.Open the downloaded SQL file 5.Execute this SQL script and you will find the database on the left (refresh schemas). A more complicated database instance for e-commerce:

1.Download the complicated instance https://mega.nz/#!xtjigl6t!toc-xg4e3bvxyxwooiumfkjxybgmkltjoqnmyrfnpjg and import this database in the similar way (above). 2.ER-diagram for this data 3.Data description this database contains four tables, i.e., user, item, merchant_inf and behavior Table name user item Merchant_inf behavior #column 2 5 2 5 #row 49 100 247 980 Some attribute descriptions: User table: the user information User_id, Name, (the user name, which is private and invisible) Item table: the item information Id, Name, Price,

Category, Category_id, Merchant_inf table: the merchant information Merchant_id, Budget, budget constraints imposed on the merchant Behavior table: users behaviors, such as click or buy some items User_id, Merchant_id, Time_stamp, the time of behavior Item_id, Online_Action_id, 0 denotes click while 1 for buy