Presentation is loading. Please wait.

Presentation is loading. Please wait.

2009/07/21CSBB LAB1. 2009/07/21CSBB LAB2 Database 目前常見且常用的資料庫為關聯式資料庫 (Relational DataBase, RDB) 。 RDB 利用資料表 (table) 來呈現資料,並將資料表視為一集 合。一個 database 中通常存在多個.

Similar presentations


Presentation on theme: "2009/07/21CSBB LAB1. 2009/07/21CSBB LAB2 Database 目前常見且常用的資料庫為關聯式資料庫 (Relational DataBase, RDB) 。 RDB 利用資料表 (table) 來呈現資料,並將資料表視為一集 合。一個 database 中通常存在多個."— Presentation transcript:

1 2009/07/21CSBB LAB1

2 2009/07/21CSBB LAB2 Database 目前常見且常用的資料庫為關聯式資料庫 (Relational DataBase, RDB) 。 RDB 利用資料表 (table) 來呈現資料,並將資料表視為一集 合。一個 database 中通常存在多個 tables 。 RDBMS – Relational DataBase Management System. Record / tuple Field

3 2009/07/21CSBB LAB3 MySQL MySQL 為一種 RDBMS ,為昇陽電腦 (Sun) 旗下的一 項產品,大部分情況下為免費軟體。 目前最新的版本為 5.4 。 許多生物資料庫也使用 MySQL 來處理資料,例如 Gene ontology 、 ensembl 。 昇陽電腦不久前被甲骨文 (Oracle) 以 74 億美元併購。

4 2009/07/21CSBB LAB4 MySQL 資料型態 種類範圍說明 Char(N) [ binary]N=1~255 個字元 binary :分辨大小寫固定長度 VarChar(N) [ binary]N=1~255 個字元 binary :分辨大小寫可變長度 TinyBlob最大長度255個字元(2^8-1) Blob (Binary large objects)儲存二進位資料, 且有分大小寫 TinyText最大長度255個字元(2^8-1) Blob最大長度65535個字元(2^16-1) Text最大長度65535個字元(2^16-1) MediumBlob最大長度 16777215 個字元(2^24-1) MediumText最大長度 16777215 個字元(2^24-1 LongBlob最大長度4294967295個字元 (2^32-1) LongText最大長度4294967295個字元 (2^32-1) Enum集合最大數目為65535 列舉(Enumeration), Enum單選、Set複選 文字型態

5 2009/07/21CSBB LAB5 MySQL 資料型態 種類範圍說明 TinyInt[M] [UNSIGNED] -128~127 UNSIGNED : 0~255 SmallInt[M] [UNSIGNED] -32768~32767 UNSIGNED :0~ 65535 MediumInt[M] [UNSIGNED] -8388608~8388607 UNSIGNED :0~16777215 Int[M] [UNSIGNED] -2^31~2^31-1 UNSIGNED : 0~2^32 BigInt[M] [UNSIGNED] -2^63~2^63-1 UNSIGNED : 0~2^64 Float [(M,D)]-3.4E+38~3.4E+38( 約 ) 註: M 為長度, D 為小數,Float 4 bytes,Double 8 bytes Double [(M,D)]-1.79E+308~1.79E+308( 約 ) 數值

6 2009/07/21CSBB LAB6 MySQL 資料型態 種類範圍說明 Date日期(yyyy-mm-dd) Time時間(hh:mm:ss) DateTime日期與時間組合(yyyy-mm-dd hh:mm:ss) TimeStampyyyymmddhhmmss Year年份yyyy 日期時間

7 2009/07/21CSBB LAB7 建立一 MySQL 資料表 / 庫 1. 利用 phpMyAdmin 的視覺化介面。 簡單且容易上手。 修改不易,需要重新填寫。 2. 利用 SQL 語法。 需要先花時間學習 SQL 語法。 程式中如果需要新增 Table ,可利用 SQL 。 新增類似的資料庫時很方便。

8 2009/07/21CSBB LAB8 SQL SQL (Structured Query Language) 是一種專門用來處 理關聯式資料的標準程式語言,於 1970 年代誕生。 雖然 SQL 的標準化作業持續由 ANSI 與 ISO 這兩個組 織維護,但是各資料庫的軟體商也持續在擴充自 己獨特的功能。 不同的 RDBMS ,其 SQL 語法不具相容性。 => SQL 不是 Standard Query Language

9 2009/07/21CSBB LAB9 基本 SQL 語法 資料定義語言 (DDL) – CREATE :建立資料庫或資料表。 – ALTER :變更資料庫或資料表的結構。 – DROP :刪除資料庫或資料表。 資料操作語言 (DML) – INSERT :新增資料。 – SELECT :搜尋資料。 – UPDATE :更新資料。 – DELETE :刪除資料。

10 2009/07/21CSBB LAB10 CREATE 新增資料庫。 – CREATE DATABASE db_name; 新增資料表。 – CREATE TABLE [if not exists] table_name (col_name column_definition, … ) [table options];

11 2009/07/21CSBB LAB11 Create Table CREATE TABLE `award_count` ( `STU_NO` varchar(8) NOT NULL, `AWARD_COUNT` tinyint(4) default NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; CREATE TABLE `depart` ( `DEPT` char(4) NOT NULL, `DEPT_NAME` varchar(10) NOT NULL, `DEPT_NAME_CH` varchar(40) NOT NULL, `DEPT_NAME_EN` varchar(80) default NULL, `DIV_KEY` char(4) NOT NULL, `IS_USED` char(1) default NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

12 2009/07/21CSBB LAB12 ALTER ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name alter_specification [, alter_specification]... alter_specification: table_options | ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ] | ADD [COLUMN] (col_name column_definition,...) | ADD {INDEX|KEY} [index_name] | CHANGE [COLUMN] old_col_name create_definition | DROP [COLUMN] col_name | DROP PRIMARY KEY | DROP INDEX index_name 修改資料表

13 2009/07/21CSBB LAB13 Alter Table ALTER TABLE `award_count` ADD `Remark` VARCHAR( 100 ) NULL AFTER `AWARD_COUNT` ; ALTER TABLE `award_count` DROP `remark` ; ALTER TABLE `award_count` CHANGE `Remark` `remark` VARCHAR( 80 ) NOT NULL ; 新增欄位 修改欄位 刪除欄位

14 2009/07/21CSBB LAB14 DROP DROP {DATABASE | SCHEMA} [IF EXISTS] db_name DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name]... 刪除資料庫 刪除資料表

15 2009/07/21CSBB LAB15 資料操作語言 (DML) INSERT :新增資料。 SELECT :搜尋資料。 UPDATE :更新資料。 DELETE :刪除資料。

16 2009/07/21CSBB LAB16 INSERT INSERT [LOW_PRIORITY | DELAYED] [IGNORE] [INTO] tbl_name [(col_name,...)] VALUES (expression,...),(...),... 或 INSERT [LOW_PRIORITY | DELAYED] [IGNORE] [INTO] tbl_name [(col_name,...)] SELECT... 或 INSERT [LOW_PRIORITY | DELAYED] [IGNORE] [INTO] tbl_name SET col_name=expression, col_name=expression,... 新增資料

17 2009/07/21CSBB LAB17 Insert Data Insert into `award_count` ( STU_NO, AWARD_COUNT, Remark ) values ('970001', 2, '97 、 98' ), ('980001', 1, '98' ) ; Insert into `award_count` SET STU_NO = '970001', AWARD_COUNT = 2, Remark = '97 、 98'; 新增資料

18 2009/07/21CSBB LAB18 Insert Data INSERT INTO `depart` VALUES ('AM', ' 應數所 ', ' 應用數學研究所 ', 'Applied Mathematics', 'SCI', '0'), ('ASTR', ' 天文所 ', ' 天文研究所 ', 'Astronomy', 'SCI', '1'), ('CD', ' 計管所 ', ' 計算機決策管理研究所 ', 'Computer and Decision Science', 'SCI', '0'), ('CHE', ' 化工系 ', ' 化學工程學系 ', 'Chemical Engineering', 'ENGI', '1'), ('CHEM', ' 化學系 ', ' 化學系 ', 'Chemistry', 'SCI', '1'), ('COM', ' 通訊所 ', ' 通訊工程研究所 ', 'Communications Engineering', 'EECS', '1'), ('CS', ' 資工系 ', ' 資訊工程學系 ', 'Computer Science', 'EECS', '1'), ('ECUP', ' 電資院學士班 ', ' 電機資訊學院學士班 ', 'Electrical Engineering & Computer Science Undergra', 'EECS', '1'), ('EE', ' 電機系 ', ' 電機工程學系 ', 'Electrical Engineering', 'EECS', '1'), ('EM', ' 工管所 ', ' 工程管理研究所 ', 'Engineering Management', 'ENGI', '0'), ('ENE', ' 電子所 ', ' 電子工程研究所 ', 'Electronic Engineering', 'EECS', '1'), ('ENGI', ' 工學院院招生 ', ' 工學院院招生 ', ' ', 'ENGI', '1'), ('IE', ' 工工系 ', ' 工業工程學系 ', 'Industrial Engineering', 'ENGI', '0'), ('IEEM', ' 工工系 ', ' 工業工程與工程管理學系 ', 'Industrial Engineering & Engineering Management', 'ENGI', '1'), ('IEM', ' 工工在職班 ', ' 工業工程與工程管理學系碩士在職專班 ', 'Industrial Engineering & Engineering Management', 'ENGI', '1'), ('IPT', ' 光電所 ', ' 光電工程研究所 ', 'Photonics Technologies', 'EECS', '1'), ('ISA', ' 資應所 ', ' 資訊系統與應用研究所 ', 'Information Systems and Applications', 'EECS', '1'), ('MATH', ' 數學系 ', ' 數學系 ', 'Mathematics', 'SCI', '1'), ('MB', ' 分生所 ', ' 分子與細胞生物研究所 ', 'Life Science', 'SCI', '0'), ('MEMS', ' 微機電所 ', ' 微機電系統工程研究所 ', 'Microelectromechanical System', 'ENGI', '0'), ('MS', ' 材料系 ', ' 材料科學工程學系 ', 'Materials Science and Engineering', 'ENGI', '1'), ('NEMS', ' 奈微所 ', ' 奈米工程與微系統研究所 ', 'Institute of NanoEngineering and MicroSystems', 'ENGI', '1'), ('OET', ' 光電專班 ', ' 產業研發碩士光電科技專班 ', 'Industrial Technology R & D Master Program on Opt', 'ENGI', '1'), ('PHYS', ' 物理系 ', ' 物理學系 ', 'Physics', 'SCI', '1'), ('PME', ' 動機系 ', ' 動力機械工程學系 ', 'Power Mechanical Engineering', 'ENGI', '1'), ('PS', ' 高分所 ', ' 高分子研究所 ', 'Polymer Science', 'SCI', '0'), ('RDDM', ' 半導體專班 ', ' 產業研發碩士半導體元件及製程專班 ', 'Industrial Technology R&D Master Program on', 'EECS', '1'), ('RDIC', ' 積電專班 ', ' 產業研發碩士積體電路設計專班 ', 'Industrial Technology R&D Master Program on IC Des', 'EECS', '1'), ('SCI', ' 理學院學士學程 ', ' 理學院學士學位學程 ', 'College of Science Double Major Program', 'SCI', '1'), ('STAT', ' 統計所 ', ' 統計學研究所 ', 'Statistics', 'SCI', '1'); 新增資料

19 2009/07/21CSBB LAB19 SELECT 搜尋資料 SELECT select_expr [, select_expr...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC],... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC],...] [LIMIT {[offset,] row_count | row_count OFFSET offset}]

20 2009/07/21CSBB LAB20 Select Data select AWARD_COUNT FROM `award_count` where STU_NO='970001'; 查詢資料 select * FROM `depart` where IS_USED = '1' Order by DIV_KEY;

21 2009/07/21CSBB LAB21 Select Data 查詢資料 Select DIV_KEY, count(DEPT) FROM `depart` where IS_USED = '1' group by DIV_KEY Order by DIV_KEY DESC;

22 2009/07/21CSBB LAB22 UPDATE UPDATE [LOW_PRIORITY] tbl_name SET col_name1=expr1,col_name2=expr2,... [WHERE where_definition] [LIMIT #] 更新資料

23 2009/07/21CSBB LAB23 Update Data Update `depart` set IS_USED = '0' where DIV_KEY='ENGI'; 更新資料 Update `depart` set IS_USED = '1' where DIV_KEY <> 'ENGI'; 更新資料

24 2009/07/21CSBB LAB24 DELETE DELETE [LOW_PRIORITY] FROM tbl_name [WHERE where_definition] [LIMIT rows] 刪除資料 Delete from `depart` where IS_USED = '0' ; 刪除資料


Download ppt "2009/07/21CSBB LAB1. 2009/07/21CSBB LAB2 Database 目前常見且常用的資料庫為關聯式資料庫 (Relational DataBase, RDB) 。 RDB 利用資料表 (table) 來呈現資料,並將資料表視為一集 合。一個 database 中通常存在多個."

Similar presentations


Ads by Google