Website Series 5 - MySQL

37
MySQL 5/14 - Website Series 5 [email protected]

Transcript of Website Series 5 - MySQL

MySQL5/14 - Website Series 5

[email protected]

Concept A kind of relational database. We store the same data only in one place. Give schema to data and id to each record. Use id to connect different records.

Database StructureDatabase

TableColumn

Column

TableColumn

Column

Table

Database …

Table Structure

id Username Password Email …

1 Eugene 123 [email protected]

2 Test 456 [email protected]

Table Structure

id Username Password Email …

1 Eugene 123 [email protected]

2 Test 456 [email protected]

Columns / Fields / Attributes

Table Structure

id Username Password Email …

1 Eugene 123 [email protected]

2 Test 456 [email protected]

Columns / Fields / Attributes

Records / Rows

Table Structure

id Username Password Email …

1 Eugene 123 [email protected]

2 Test 456 [email protected]

Columns / Fields / Attributes

Records / Rows

Primary Key

Types Of Column int float, double char(字串 ), varchar(可變長度的字串 ) text(一段較長的字串 ) enum (單選選項 ), set (多選選項 ) timestamp (時間 )

Basic Operation Create Drop / Delete Insert Select Update

Use Show Desc / describe

Let’s get started!

CREATE Syntax Create Database

CREATE database mydatabase; Create Table

CREATE table mytable (id int(20) primary key auto_increment,username varchart(20),name varchar(10),type enum(‘public’,’private’) default ‘public’…tsp timestamp default current_timestamp

);

CREATE Syntax Create Database

CREATE database mydatabase; Create Table

CREATE table mytable (id int(20) primary key auto_increment,username varchart(20),name varchar(10),type enum(‘public’,’private’) default ‘public’…tsp timestamp default current_timestamp

);

CREATE Syntax Create Database

CREATE database mydatabase; Create Table

CREATE table mytable (id int(20) primary key auto_increment,username varchart(20),name varchar(10),type enum(‘public’,’private’) default ‘public’…tsp timestamp default current_timestamp

);

CREATE Syntax Create Database

CREATE database mydatabase; Create Table

CREATE table mytable (id int(20) primary key auto_increment,username varchart(20),name varchar(10),type enum(‘public’,’private’) default ‘public’…tsp timestamp default current_timestamp

);

INSERT Syntax Insert data into table

INSERT into mytable (username, name) value (‘testtest’,’eugene’);

SELECT Syntax (Basic) Select all

SELECT * from mytable; Only several columns

SELECT id, name from mytable; Give order

SELECT * from mytable order by tsp desc; (asc) With limited number

SELECT * from mytable where type=‘public’ limit 20; Give some criteria

SELECT * from mytable where id = 1; Count with group

SELECT count(distinct id) from mytable group by type;

UPDATE Syntax Update is more a combination of select and alter data. UPDATE mytable set name=‘EUGENE’ where id=1;

UPDATE Syntax Update is more a combination of select and alter data. UPDATE mytable set name=‘EUGENE’ where id=1;

DELETE, DROP Syntax Delete a record

DELETE from test where id=2; Delete a table

DROP table mytable; Delete a database

DROP database mydatabse;

CreateReadUpdateDelete

Example@db_example.sql

(revised from 小龜 )

Some More SELECT Syntax Count singer type

SELECT count(distinct singerType) from singer; Count the number of singer in each type

SELECT singerType, count(distinct singerId) as count from singer group by singerType;

Select first 10 songsSELECT * from song limit 10;

Select second 10 songsSELECT * from song limit 10,10;

Cross Table Selection Simply select from 2 tables, MySQL will find all the match

recordsSELECT * from singer, song where song.singerId = singer.singerId;

Join Table SelectionLeft join

Right join

Join Table Selection (Left Join) SELECT song.*, songrank.thisRank, songrank.prevRank

from songrank left join song on songrank.songId = song.songId;

Join Table Selection (Left Join) SELECT song.*, songrank.thisRank, songrank.prevRank

from songrank left join song on songrank.songId = song.songId;

The part being joined into songrank

Join Table Selection (Right Join) SELECT song.*, songrank.thisRank, songrank.prevRank

from songrank right join song on songrank.songId = song.songId;

Join Table Selection (Right Join) SELECT song.*, songrank.thisRank, songrank.prevRank

from songrank right join song on songrank.songId = song.songId;

The part being joined into songrank

Basic Guideline For Design Tables Store different data that have different purpose in different

tables and use cross table selection to combine them together. Don’t store the same data twice.

Way to Organize Data

Way to Organize Data One-to-One

Ex: ISBN for every book, product id of each songs on iTunes online shop,… Save as a column

Way to Organize Data One-to-One

Ex: ISBN for every book, product id of each songs on iTunes online shop,… Save as a column

One-to-Many Ex: Singer of every song(if each song can only have one singer) Save singer and song information in separate tables and save singer id in

song table

Way to Organize Data One-to-One

Ex: ISBN for every book, product id of each songs on iTunes online shop,… Save as a column

One-to-Many Ex: Singer of every song(if each song can only have one singer) Save singer and song information in separate tables and save singer id in

song table Many-to-Many

Ex: Students and courses(each students may be participated in multiple courses)

Save student and course information in two tables and create a table that save the relationship

Many-to-Many ExampleStudent ID Name1 小眼2 中眼3 大眼

Course ID

Name Teacher ID

1 HTML 52 CSS 83 PHP 5

Relation ID Student ID Course ID1 1 12 2 13 1 34 3 35 2 2

Many-to-Many ExampleStudent ID Name1 小眼2 中眼3 大眼

Course ID

Name Teacher ID

1 HTML 52 CSS 83 PHP 5

Relation ID Student ID Course ID1 1 12 2 13 1 34 3 35 2 2

Grade9040995060

Conclusion Database schema design often is the first things we do when

designing a website. Imagine how you would use data in order to design a good

schema Follow the design rules to design tables. Insert some fake data to test your selections.

Reference 小龜教學網站

http://140.114.89.25/~geniusturtle/tutor_web/php_sqlCmd.php 資料庫正規化基本概念

https://support.microsoft.com/en-us/kb/283878/zh-tw