SQL (¥i¥H°á¦r¥À S. Q. L. ©Î sequel)

¥Î¨Ó«Ø¥ß¡B¾Þ§@¤Î¦s¨úÃöÁp¦¡¸ê®Æ®w (Relational Database) ªº»y¨¥´N¬O SQL¡C¦Ó¹ï©ó SQL ªº¼Ð·Ç¤Æ§@·~¡A¥D­n¬O¥Ñ ANSI »P ISO ³o¨â­Ó²Õ´©Ò±À°Ê¡C

SQL »y¨¥¥i¤j­P¤À¬°´X­ÓÃþ§O

Data definition ¸ê®Æ©w¸q (DDL)

¥Î¨Ó©w¸q¸ê®Æ®w¡B¸ê®Æªí¡BÀ˵øªí¡B¯Á¤Þ¡B¹w¦sµ{§Ç¡BIJµoµ{§Ç¡B¨ç¼Æµ¥¸ê®Æ®wª«¥ó¡C±`¨£ªº«ü¥O¦³¡G

Data manipulation ¸ê®Æ¾ÞÁa (DML)

¥Î¨Ó³B²z¸ê®Æªí¸Ìªº¸ê®Æ¡A±`¨£ªº«ü¥O¦³¡G

Queries ¸ê®Æ¬d¸ß (DQL)

¥Î¨Ó¬d¸ß¸ê®Æªí¸Ìªº¸ê®Æ¡G

Data control ¸ê®Æ±±¨î (DCL)

¥Î¨Ó±±¨î¸ê®Æªí¡BÀ˵øªíªº¦s¨úÅv­­¡A±`¨£ªº«ü¥O¦³¡G

Transaction controls ¥æ©ö

¥æ©ö¬O³æ¤@¤u§@³æ¤¸¡C ¦pªG¥æ©ö¦¨¥\¡A«K·|½T©w¥æ©ö´Á¶¡©Ò­×§ïªº©Ò¦³¸ê®Æ¡A¥B·|¦¨¬°¸ê®Æ®w¤¤¥Ã¤[ªº¤º®e¡C ¦pªG¥æ©öµo²{¿ù»~¡A¥²¶·¨ú®ø©Î¦^´_¡A«K·|²M°£©Ò¦³ªº¸ê®Æ­×§ï¡C

¡@

¡@

SQL »yªk Syntax

Åý§Ú­Ì¥ý¨Ó²á²á²Õ¦¨ SQL »yªkªº¤¸¯À¦³¤°»ò¡C

¸ê®Æªí (Database Tables)

¸ê®Æ®w¤¤³Ì­«­nªºª«¥ó´N¬O¸ê®Æªí (table)¡A¸ê®Æ®w¥Ñ¤@­Ó©Î¤@­Ó¤Wªº¸ê®Æªí©Òºc¦¨¡A¨C­Ó¸ê®Æªí¦WºÙ¦b¸ê®Æ®w¤¤³£¬O°ß¤@ªº¡A¸ê®Æªí¤¤¨C¤@ª½¦æ (column) ºÙ¤§¬°Äæ¦ì¡A¨C­ÓÄæ¦ì³£¦³¨ä¸ê®Æ«¬ºA (data type)¡A¥Ñ¤£¦PÄæ¦ì©Ò²Õ¦¨ªº¾î¦C (row)¡AºÙ¤§¬°°O¿ý (record)¡A§Ú­ÌÁ|¤@­Ó¥s°µ "customers" ªº¸ê®Æªí§@¬°¨Ò¤l¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678

¦¹¸ê®Æªí¦@¥]§t 2 µ§°O¿ý¡A5 ­ÓÄæ¦ì (S_Id, Name, City, Address, Phone)

¸ê®Æªí¦WºÙ¦³°Ï¤À¤j¤p¼g (case-sensitive)¡A¦ý¬Y¨Ç¸ê®Æ®w¦b Windows §@·~¨t²Î¤¤¬O¤£¤À¤j¤p¼gªº¡A¦Ó¬°¤F¤è«KºûÅ@³Ì¦n²Î¤@§Aªº©R¦W¤è¦¡¡C

SQL ±Ô­z¥y (Statements)

§Ú­Ì§Q¥Î SQL ±Ô­z¥y¨Ó¸ò¸ê®Æ®w·¾³q¡B¤U¹F«ü¥O¡ASQL »y¨¥¬O¥Ñ©R¥O (commands) ¡B¤l¥y (clauses) ¡B¹Bºâ¤l (operators) ¤Î ¨ç¼Æ (functions) ©Ò²Õ¦¨¡A³q±`¤@­Ó SQL ±Ô­z¥y¥Ñ¤@¬q©R¥O¥y¶}©l´y­z±z­n¹ï¸ê®Æ®w­n¨Dªº°Ê§@¡A±µµÛ¥i¯à·|±µµÛ±ø¥ó»y¥y¡A³Ì«á¥H¤À¸¹ ";" µ²§ô¡G

¦³¨Ç¸ê®Æ®w¨Ã¤£±j¨î¤@©w­n¥[¤Wµ²§À¤À¸¹¡C

SELECT * FROM customer;

¤W­±³o±ø SQL ±Ô­z¥yªí¥Ü±q customer ¸ê®Æªí¨ú¥X©Ò¦³¸ê®Æ¡C

SQL »y¥y¤£°Ï¤À¤j¤p¼g (case-insensitive)¡A¦p select * from customer; ©M¤W¨Ò¬O¬Û¦Pªºµ²ªG¡C

¤°»ò¬O©R¥O (Commands)

«Ø¥ß·sªº¸ê®Æ®w¡B¸ê®Æªí¡BÄæ¦ì¤Î¡B¯Á¤Þµ¥¡A©Î«Ø¥ß¬d¸ßªí¡B±Æ§Ç¡B¹LÂo¸ê®Æ¡B¬d¸ß¡B­×§ï¡B·s¼W¤Î§R°£¸ê®Æµ¥°Ê§@¡C(§Y CREATE¡BDROP¡BALTER¡BSELECT¡BINSERT¡BUPDATE¡BDELETE µ¥©R¥O)

¤°»ò¬O¤l¥y (Clauses)

¤l¥y¬O¥Î¨Ó³]©w¤Î¾Þ§@§Aªº SQL query¡A¨Ò¦p¡G

¤l¥y »¡©ú
FROM «ü©w§@¥Îªº¸ê®Æªí
WHERE ³]©w¬d¸ß±ø¥ó
GROUP BY ³]©w¤À²Õ±ø¥ó
ORDER BY ³]©w¿é¥Xªº±Æ§Ç

¤°»ò¬O¹Bºâ¤l (Operators)

¹Bºâ¤l¥Î¨ÓÀ°§U SQL »y¥y³B²z¼Æ­È¡B¦r¦ê©Î¶i¦æÅÞ¿è¹Bºâ¤Î¤ñ¸û±ø¥ó¡C

¤°»ò¬O¨ç¼Æ (Functions)

SQL »y¨¥¤º«Ø³\¦h¨ç¼Æ¥i¥Hª½±µ¦b SQL »y¥y¸Ì­±¨Ï¥Î¡A¹³¬O¨ú±o¬YÄæ¦ì¥[Á`«á¼Æ­È¡B¨ú±o¬YÄæ¦ì¤º³Ì¤j©Î³Ì¤p­Èµ¥µ¥¡C

SQL ±Ô­z¥yªº®Ñ¼g²ßºD

±N©Ò¦³ªº°õ¦æ»y¥y¤¤ªº SQL ÃöÁä¦r¤j¼g¬O¤@­Ó¨}¦nªº SQL ®Ñ¼g²ßºD¡A³o·|À°§U§A§ó»´©öªº¥hÀ˵ø§Aªº SQL »yªk¡C

¦¹¥~¡A§A¥i¥H±N¸ûªøªº SQL »y¥y©î¦¨¦h¦æ®Ñ¼g¡A³o·|Åý§A¤ñ¸û®e©ö¾\Ū¡I

SELECT column_one, column_two
FROM table_name
WHERE table_id = 123;

¡@

¡@

SQL ªº¸ê®Æ«¬§O (Data Types)

SQL °ò¥»¤W¦³¤U¦C´XºØ¸ê®Æ«¬§O¡A§Ú­Ì¥²»Ý¤F¸ÑÀx¦s¦UÃþ«¬¸ê®Æ­È­n¤À§O±Ä¥Î¦óºØ¸ê®Æ«¬§OÄæ¦ì¥H«K¶i¦æ¸ê®Æ®w³]­p¡C

¼Æ­È¸ê®Æ (Numeric Data)

SQL ªº¼Æ­È«¬ºA¦³ integer, float, money µ¥¡A¨Ï¥Î¼Æ­È¸ê®Æ¦³¤@­Ó¦n³B´N¬O§A¯à·f°t¤º«Øªº¼Æ­È¨ç¼Æ¨Ó°µ¸ê®Æ³B²z¡A¨Ò¦p SUM() ¨ç¼Æ§Y¥iª½±µÀò±o¸Ó¼Æ­ÈÄæ¦ìªºÁ`¦X¡C

¦r¦ê(¤¸)¸ê®Æ (Character & Strings Data)

Àx¦s¦r¤¸©Î²Å¸¹¤§¸ê®Æ«¬§O¡C

¤é´Á/®É¶¡¸ê®Æ (Date Data)

¥Î¨Ó°O¿ý¤é´Á/®É¶¡ªº¸ê®Æ«¬§O¡A¦³ date, time, timestamp µ¥¡C

¥¬ªL­È (Boolean Data)

true/false, Yes/No, 1/0¡C

ªÅ­È (NULL Data)

ªÅ­È¡A¨S¦³¸ê®Æ¦s¦b©óÄæ¦ì¡C³q±`¦b«Ø¥ß¸ê®Æªí®É¡A§A¥i¥H³]©wÄæ¦ì¬O§_¤¹³\ªÅ­È¡C

CREATE TABLE customer (
  C_id INT PRIMARY KEY,
  Name VARCHAR(10) NOT NULL,
  Address VARCHAR(255) NULL,
  Phone VARCHAR(10) NULL
);

NULL ­È©M¤@­ÓªÅ¦r¦ê '' ¬O¦³¤£¤@¼Ë·N²[ªº¡C¨Ò¦p¡A¤U¦C SQL ±Ô­z¥y¡G

INSERT INTO customer (phone) VALUES (NULL);
INSERT INTO customer (phone) VALUES ('');

¨ä¤¤²Ä¤@­Ó±Ô­z¥yªº·N¨ýµÛ¤£ª¾¹D¹q¸Ü¸¹½X¡F¦Ó²Ä¤G­Ó«h¬O·N¨ýµÛ¨S¦³¹q¸Ü¡C

¦b¤j¦hªº¸ê®Æ®w¤¤¡A§A¤£¯à¯Á¤Þ¤¹³\¦³ NULL ­ÈªºÄæ¦ì¡C§A¥²¶·Án©ú­n¯Á¤ÞªºÄæ¦ì¬° NOT NULL¡A¦¹¥~¡A§A¤]¤£¯à´¡¤J NULL ¨ì¨ã¦³¯Á¤ÞªºÄæ¦ì¤¤¡C

¥t¥~­È±oª`·Nªº¬O¡ANULL ³o¦r­±±`¶q (Literal) ¦b¬Y¨Ç¯S®í±¡§Î¤U¨Ï¥Î¥NªíµÛ¤£¦P§t·N¡G

  1. ­Y§A±N NULL ´¡¤J¸ê®Æªí¤¤ªº TIMESTAMP Äæ¦ì¡A«h¥Nªí¥Ø«eªº¤é´Á©M®É¶¡¡C
  2. ­Y§A±N NULL ´¡¤J¤@­Ó AUTO_INCREMENT Äæ¦ì¡A«h¥Nªí¥Ø«e¶¶§Ç¤¤ªº¤U¤@­Ó¸¹½X¡C

¡@

SQL ¹Bºâ¤l Operators

SQL ¦³ 3 ºØÃþ«¬ªº¹Bºâ¤l¡A¤À§O¬Oºâ¼Æ¹Bºâ¤l¡BÅÞ¿è¹Bºâ¤l¤Î¤ñ¸û¹Bºâ¤l¡C

ºâ¼Æ¹Bºâ¤l (Arithmetic Operators)

¹Bºâ¤l ¥Î³~ ¥Îªk µ²ªG
+ ¥[ªk 1 + 2 3
- ´îªk 5 - 4 1
* ­¼ªk 6 * 6 36
/ °Ó¼Æ 8 / 5 1.6
% ¾l¼Æ 8 % 5 3

ÅÞ¿è¹Bºâ¤l (Logical Operators)

¹Bºâ¤l »y·N
AND ¥B
OR ©Î
NOT «D

¤ñ¸û¹Bºâ¤l (Comparison Operators)

¹Bºâ¤l »y·N ¥Îªk µ²ªG
< ¤p©ó 3 < 5 True
> ¤j©ó 8 > 1 True
<= ¤p©óµ¥©ó 20 <= 10 False
>= ¤j©óµ¥©ó 4 >= 6 False
= µ¥©ó 2 = 2 True
<> ¤£µ¥©ó 7 <> 6 True
BETWEEN ¦b¬Y­Ó½d³ò¤º    
LIKE ²Å¦X¼Ò¦¡±ø¥ó    
IN ­È¦s¦b©óIN¶°¦X¤¤

¡@

¡@

¡@

¡@

¸ê®Æ©w¸q DDL

CREATE DATABASE ±Ô­z¥y (SQL CREATE DATABASE Statement)

CREATE DATABASE ¬O§Ú­Ì¥Î¨Ó«Ø¥ß¤@­Ó·s¸ê®Æ®wªº»yªk¡C

CREATE DATABASE »yªk (Syntax)

CREATE DATABASE database_name;

¨Ò¦p¡A§Ú­Ì·Q«Ø¥ß¤@­Ó¥s°µ foo ªº¸ê®Æ®w¡G

CREATE DATABASE foo;

³o¼Ë´N«Ø¥ß§¹¦¨¡A²³æ§a¡I

±µµÛ§Ú­Ì´N¥i¥H·s¼W¸ê®Æªí¨ì¦¹¸ê®Æ®w¤¤Åo - CREATE TABLE

¡@

¡@

CREATE TABLE ±Ô­z¥y (SQL CREATE TABLE Statement)

CREATE TABLE ¬O§Ú­Ì¦b¸ê®Æ®w¤¤¥Î¨Ó«Ø¥ß¤@­Ó·s¸ê®Æªíªº»yªk¡C

CREATE TABLE »yªk (Syntax)

CREATE TABLE table_name (
  column_name1 data_type,
  column_name2 data_type,
  column_name3 data_type,
  ¡P¡P¡P
);

data_type ¥Î¨Ó«ü©w¸ÓÄæ¦ì¸ê®ÆÀx¦sªº¸ê®Æ«¬§O¡A§A¥i¥H¥t¥~¥h²Ó¬Ý¦U®a¸ê®Æ®wªº¤å¥ó¬Ý¦³´£¨Ñ­þ¨Ç«¬§O³á¡I(¹³¬O MySQL)

°²³]§Ú­Ì²{¦b·Q«Ø¥ß¤@­Ó "customers" ¸ê®Æªí¡A¨ä¤¤¥]§t³o´X­ÓÄæ¦ì - C_Id, Name, Address, Phone¡G

CREATE TABLE customers (
  C_Id INT,
  Name varchar(50),
  Address varchar(255),
  Phone varchar(20)
);

³o¼Ë´N«Ø¦n¤F¡I·s«Ø¸ê®Æªí¬Ý°_¨Ó·|¹³¬O³o­Ó¼Ë¤l¡G

C_Id Name Address Phone

±µµÛ¡A§Ú­Ì´N¥i¥H¥Î INSERT INTO ¨Ó¶ë¤J¸ê®ÆÅo¡I

¡@

¡@

ALTER TABLE ±Ô­z¥y (SQL ALTER TABLE Statement)

ALTER TABLE ¬O¥Î¨Ó¹ï¤w¦s¦bªº¸ê®Æªíµ²ºc§@§ó§ï¡C»yªk«¬¦¡¦p¤U¡G

ALTER TABLE table_name ...;

°²³]²{¦b§Ú­Ì¤w¸g«Ø¥ß¦n¤@­Ó "customers" ¸ê®Æªí¡G

C_Id Name Address Phone

±µµÛ¡A§Ú­Ì­n¦p¦ó...

¼W¥[Äæ¦ì (ADD COLUMN)

»yªk

ALTER TABLE table_name ADD column_name datatype;

¨Ò¦p¡A¦pªG§Ú­Ì·Q¼W¥[¤@­Ó Discount Äæ¦ì¡G

ALTER TABLE customers ADD Discount VARCHAR(10);

§ó§ïÄæ¦ì¸ê®Æ«¬§O (ALTER COUMN TYPE)

»yªk

ALTER TABLE table_name ALTER COLUMN column_name datatype;

¨Ò¦p¡A§ó§ï Discount Äæ¦ìªº¸ê®Æ«¬§O¡G

ALTER TABLE customers ALTER COLUMN Discount DECIMAL(18, 2);

§R°£Äæ¦ì (DROP COLUMN)

»yªk

ALTER TABLE table_name DROP COLUMN column_name;

¨Ò¦p¡A§R°£ Discount Äæ¦ì¡G

ALTER TABLE customers DROP COLUMN Discount;

¡@

¡@

DROP TABLE / TRUNCATE TABLE / DROP DATABASE Statement

§Ú­Ì¥i¥H¥Î DROP TABLE / TRUNCATE TABLE / DROP DATABASE ¨Ó§R°£¸ê®Æªí©Î¸ê®Æ®w¡C

§R°£¸ê®Æªí (DROP TABLE)

§¹¥þ§R°£¾ã­Ó¸ê®Æªí¡C

DROP TABLE table_name;

¶È§R°£¸ê®Æªí¤º®e¡A¦ý«O¯dµ²ºc (TRUNCATE TABLE)

TRUNCATE TABLE table_name;

¸ê®ÆªíÁÙ¦b¡A¥u¬O¸ê®Æ²MªÅ¤F¡C

§R°£¸ê®Æ®w (DROP DATABASE)

DROP DATABASE database_name;

¡@

¡@

SQL Constraint ­­¨î

Constraint ¥Î¨Ó¦³±ø¥ó¦a­­¨î­þ¨Ç¸ê®Æ¤~¥i¥H³Q¦s¤J¸ê®Æªí¤¤¡A¤]´N¬O¹ïÄæ¦ì§@¬ù§ô¡C³o¨Ç­­¨î¥i¥H¦b«Ø¥ß¸ê®Æªí®É CREATE TABLE «ü©w±ø¥ó¡A©Î¬O¤§«á¦A­×§ï ALTER TABLE «ü©w¡C

SQL ¦³­þ´XºØÃþ«¬ªº­­¨î±ø¥ó¡H

¡@

¡@

NOT NULL ­­¨î (SQL NOT NULL Constraint)

NOT NULL ¥Î¨Ó­­¨î¸ÓÄæ¦ì¤£¯à±µ¨üªÅ­È¡A¦Ó¦b¹w³]ªº±¡ªp¤U¡A¤@­ÓÄæ¦ì¬O¤¹³\±µ¨üªÅ­Èªº¡C

·í¬YÄæ¦ì­­¨î¬° NOT NULL ®É¡A«h¦b·s¼W¸ê®Æ®É¸ÓÄæ¦ì¤@©w­n¦³­È¡C

¨Ò¦p¡A§Ú­Ì«Ø¥ß¤@±i customer ¸ê®Æªí¡A¨Ã­­¨î¨ä C_Id ¤Î Name Äæ¦ì­È¤£¯à¬OªÅ­È¡G

CREATE TABLE customer (
  C_Id INT NOT NULL,
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20)
);

¡@

¡@

UNIQUE °ß¤@­­¨î (SQL UNIQUE Constraint)

UNIQUE ¥Î¨Ó«OÃÒÄæ¦ì¦b¸ê®Æªí¤¤ªº°ß¤@©Ê¡A¬ù§ô¸ê®Æªí¤¤ªºÄæ¦ì¤£¯à¦³­«½Æªº¸ê®Æ¡C

¤@­Ó¸ê®Æªí¥i¦³¦h­Ó UNIQUE Äæ¦ì¡A¦¹¥~ UNIQUE Äæ¦ì¤¤¥i¥H±µ¨ü NULL ­È¡C

°²³]§Ú­Ì­n¹ï¡ucustomer¡v¸ê®Æªí­­¨î¡uC_Id¡vÄæ¦ì¤£¯à¦³­«½Æ­È¦s¦b¡G

¦b«Ø¥ß¸ê®Æªí®É CREATE TABLE...

CREATE TABLE customer (
  C_Id INT NOT NULL UNIQUE,
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20)
);

©Î³o¼Ë¼g¡G

CREATE TABLE customer (
  C_Id INT NOT NULL,
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20),
  UNIQUE (C_Id)
);

´À°ß¤@Áä©R¦W»P¦hÄæ¦ìªº°ß¤@­­¨î

§Ú­Ì­­¨î C_Id ¤Î Name ³o¨â­ÓÄæ¦ì¦@¦P°ß¤@¡ACONSTRAINT «á­±±µµÛªº§Y¬O¦¹°ß¤@Á䪺¦WºÙ¡C

CREATE TABLE customer (
  C_Id INT NOT NULL,
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20),
  CONSTRAINT u_Customer_Id UNIQUE (C_Id, Name)
);

§ó§ï¸ê®Æªí­­¨î ALTER TABLE...

ALTER TABLE customer ADD UNIQUE (C_Id);

´À°ß¤@Áä©R¦W»P¦hÄæ¦ìªº°ß¤@­­¨î¡G

ALTER TABLE customer
ADD CONSTRAINT u_Customer_Id UNIQUE (C_Id, Name);

²¾°£¸ê®Æªí­­¨î ALTER TABLE...

MySQL

ALTER TABLE customer DROP INDEX u_Customer_Id;

SQL Server / Oracle / MS Access

ALTER TABLE customer DROP CONSTRAINT u_Customer_Id;

¡@

¡@

PRIMARY KEY ¥DÁä­­¨î (SQL PRIMARY KEY Constraint)

PRIMARY KEY ¥Î¨Ó«OÃÒÄæ¦ì¦b¸ê®Æªí¤¤ªº°ß¤@©Ê¡A¥DÁäÄæ¦ì¤¤ªº¨C¤@µ§¸ê®Æ¦b¸ê®Æªí¤¤³£¥²»Ý¬O¿W¤@µL¤Gªº¡C

PRIMARY KEY ¦³ÂIÃþ¦ü UNIQUE ¥[¤W NOT NULL¡C

¤@­Ó¸ê®Æªí¤¤¥u¯à¦³¤@­Ó PRIMARY KEY¡A¦ý¬O¥i¥H¦³¦h­Ó UNIQUE¡C

°²³]§Ú­Ì­n±N customer ¸ê®Æªí¤¤ªº C_Id Äæ¦ì³]¬°¥DÁä¡G

¦b«Ø¥ß¸ê®Æªí®É CREATE TABLE...

CREATE TABLE customer (
  C_Id INT NOT NULL PRIMARY KEY,
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20)
);

©Î³o¼Ë¼g¡G

CREATE TABLE customer (
  C_Id INT NOT NULL,
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20),
  PRIMARY KEY (C_Id)
);

´À¥DÁä©R¦W»P¦hÄæ¦ìªº²Õ¦XÁä (Composite Primary Keys)¡G

CREATE TABLE customer (
  C_Id INT NOT NULL,
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20),
  CONSTRAINT pk_Customer_Id PRIMARY KEY (C_Id, Name)
);

§Ú­Ì­­¨î C_Id ¤Î Name ³o¨â­ÓÄæ¦ì¬°¥DÁä¡ACONSTRAINT «á­±±µµÛªº§Y¬O¦¹¥DÁ䪺¦WºÙ¡C

·í¥DÁä¥]§t¦h­ÓÄæ¦ì®É¡A§Ú­ÌºÙ¤§¬°²Õ¦XÁä (Composite Key)¡C

§ó§ï¸ê®Æªí­­¨î ALTER TABLE...

ALTER TABLE customer ADD PRIMARY KEY (C_Id);

´À¥DÁä©R¦W»P¦hÄæ¦ìªº²Õ¦XÁä¡G

ALTER TABLE customer
ADD CONSTRAINT u_Customer_Id PRIMARY KEY (C_Id, Name);

²¾°£¸ê®Æªí­­¨î ALTER TABLE...

MySQL

ALTER TABLE customer DROP PRIMARY KEY;

SQL Server / Oracle / MS Access

ALTER TABLE customer DROP CONSTRAINT pk_PersonID;

¡@

¡@

FOREIGN KEY ¥~Áä­­¨î (SQL FOREIGN KEY Constraint)

¥~Áä¬O¤@­Ó (©Î¦h­Ó) «ü¦V¨ä¥¦¸ê®Æªí¤¤¥DÁ䪺Äæ¦ì¡A¥¦­­¨îÄæ¦ì­È¥u¯à¨Ó¦Û¥t¤@­Ó¸ê®Æªíªº¥DÁäÄæ¦ì¡A¥Î¨Ó½T©w¸ê®Æªº°Ñ¦Ò§¹¾ã©Ê (Referential Integrity)¡C

¦pªG·Q¦b MySQL ¸ê®Æ®w¤¤¨Ï¥Î¥~Áä­­¨î¡A¥²»ÝÅý¸ê®Æªí¨Ï¥Î InnoDB Àx¦s¤ÞÀº¡C

Åý§Ú­Ì²³æ¤F¸Ñ¤@¤U¤°»ò¬O¥~Áä¡G

³o¬O¤@­Ó«È¤á¸ê®Æªí customers

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¦Ó³o¬O«È¤á­q³æªº¸ê®Æªí orders

O_Id Order_No C_Id
1 2572 3
2 7375 3
3 7520 1
4 1054 2

¦b³o¸Ì§Ú­Ì·|·Q¦³¤@­Ó­­¨î¡A´N¬O¦b«È¤á­q³æ¸ê®Æªí¤¤ªº«È¤á¡A³£¤@©w­n¦b customers ¸ê®Æªí¤¤¦s¦b¡C©Ò¥H§Ú­Ì»Ý­n¦b orders ¸ê®Æªí¤¤³]©w¤@­Ó¥~Áä¡A¦A±N¦¹¥~Áä«ü¦V customers ¸ê®Æªí¤¤ªº¥DÁä¡A¥H½T©w©Ò¦³¦b orders ¸ê®Æªí¤¤ªº«È¤á³£¦s¦b©ó customers ¸ê®Æªí¤¤¡A¤~¤£·|¦³¥ô¦ó«ÕÆF­q³æªº¥X²{¡I

FOREIGN KEY Constraint

°²³]§Ú­Ì­n±N customer ¸ê®Æªí¤¤ªº C_Id Äæ¦ì³]¬°¥~Áä¡G

¦b«Ø¥ß¸ê®Æªí®É CREATE TABLE...

CREATE TABLE orders (
  O_Id INT NOT NULL,
  Order_No INT NOT NULL,
  C_Id INT,
  PRIMARY KEY (O_Id),
  FOREIGN KEY (C_Id) REFERENCES customers(C_Id) 
);

´À¥~Áä©R¦W»P¦hÄæ¦ìªº¥~Áä¡G

CREATE TABLE orders (
  O_Id INT NOT NULL PRIMARY KEY,
  Order_No INT NOT NULL,
  C_Id INT,
  CONSTRAINT fk_Cusomer_Id FOREIGN KEY (C_Id) REFERENCES customers(C_Id) 
);

§Ú­Ì­­¨î C_Id ¬°¥~Áä¡ACONSTRAINT «á­±±µµÛªº§Y¬O¦¹¥~Á䪺¦WºÙ¡A¥t¤@­Ó­«ÂI¬O°O±o customers ¸ê®Æªí¤¤»Ý±N C_Id ³]¬°¥DÁä¡C

§ó§ï¸ê®Æªí­­¨î ALTER TABLE...

ALTER TABLE orders
ADD FOREIGN KEY (C_Id) REFERENCES customers(C_Id);

´À¥~Áä©R¦W»P¦hÄæ¦ìªº¥~Áä¡G

ALTER TABLE orders
ADD CONSTRAINT fk_Cusomer_Id FOREIGN KEY (C_Id) REFERENCES customers(C_Id);

²¾°£¸ê®Æªí­­¨î ALTER TABLE...

MySQL

ALTER TABLE orders DROP FOREIGN KEY fk_Cusomer_Id;

SQL Server / Oracle / MS Access

ALTER TABLE orders DROP CONSTRAINT fk_Cusomer_Id;

¡@

¡@

CHECK Àˬd­­¨î (SQL CHECK Constraint)

CHECK ­­¨î¥Î¨Ó¬ù§ôÄæ¦ì¤¤ªº¥i¥Î­È¡A¥H«OÃÒ¸ÓÄæ¦ì¤¤ªº¸ê®Æ­È³£·|²Å¦X±z³]©wªº±ø¥ó¡C

°²³]§Ú­Ì­n­­¨î customer ¸ê®Æªí¤¤ªº C_Id Äæ¦ì­È³£¥²»Ý­n¤j©ó 0¡G

¦b«Ø¥ß¸ê®Æªí¦P®É CREATE TABLE...

CREATE TABLE customer (
  C_Id INT NOT NULL CHECK (C_Id>0),
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20)
);

©Î³o¼Ë¼g¡G

CREATE TABLE customer (
  C_Id INT NOT NULL,
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20),
  CHECK (C_Id>0)
);

¦b MySQL ¼W¥[ CHECK ­­¨î¤£·|¦³¿ù»~¡A¦ý¬O¨S¦³¥Î¡ACHECK ¨Ã¤£·|³Q°õ¦æ³á¡I

´ÀÀˬd­­¨î©R¦W»P¦hÄæ¦ìªºÀˬd¡G

CREATE TABLE customer (
  C_Id INT NOT NULL,
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255),
  Phone VARCHAR(20),
  CONSTRAINT chk_Customer CHECK (C_Id>0 AND Name!='XXX')
);

§ó§ï¸ê®Æªí­­¨î ALTER TABLE...

ALTER TABLE customer ADD CHECK (C_Id>0);

´À¥DÁä©R¦W»P¦hÄæ¦ìªº²Õ¦XÁä¡G

ALTER TABLE customer
ADD CONSTRAINT chk_Customer CHECK (C_Id>0 AND Name!='XXX');

²¾°£¸ê®Æªí­­¨î ALTER TABLE...

ALTER TABLE customer DROP CONSTRAINT chk_Customer;

¡@

¡@

DEFAULT ¹w³]­È­­¨î (SQL DEFAULT Constraint)

DEFAULT ­­¨î¥Î¨Ó³]©wÄæ¦ìªº¹w³]­È¡C·í§A¦b INSERT ¸ê®Æ®É­Y¸ÓÄæ¦ì¨S«ü©w­È«h·|±Ä¥Î¹w³]­È¡C

°²³]§Ú­Ì­n³]©w customer ¸ê®Æªí¤¤ªº Address Äæ¦ì¹w³]­È¬° "¥¼ª¾"¡G

¦b«Ø¥ß¸ê®Æªí¦P®É CREATE TABLE...

CREATE TABLE customer (
  C_Id INT NOT NULL,
  Name VARCHAR(50) NOT NULL,
  Address VARCHAR(255) DEFAULT '¥¼ª¾',
  Phone VARCHAR(20)
);

§ó§ï¸ê®Æªí­­¨î ALTER TABLE...

ALTER TABLE customer ALTER COLUMN Address SET DEFAULT '¥¼ª¾';

SQL Server

ALTER TABLE customer ADD DEFAULT '¥¼ª¾' FOR Address;

²¾°£¸ê®Æªí­­¨î ALTER TABLE...

ALTER TABLE customer ALTER COLUMN Address DROP DEFAULT;

SQL Server

ALTER TABLE table_name DROP constrain_name;

Oracle

ALTER TABLE table_name MODIFY column_name DEFAULT NULL;

¡@

¡@

AUTO INCREMENT Äæ¦ì (SQL AUTO INCREMENT column)

AUTO INCREMENT Äæ¦ì·|¦Û°Ê»¼¼W¸ê®Æ¦æªº­È¡A¦]¬°¨C¦¸·s¼W¸ê®Æ®ÉÄæ¦ì­È³£·|¦Û°Ê»¼¼W¤]´N¬O»¡ AUTO INCREMENT Äæ¦ì­È·|¬O°ß¤@ªº¡A¸ÓÄæ¦ì¥Î³~´N¹³¬O¤@­ÓÃѧO½X©Î¬y¤ô¸¹¡A¦Ó AUTO INCREMENT ±`»P Primary Key ¤@°_·f°t¨Ï¥Î¡C

³]©w AUTO INCREMENT Äæ¦ì

MySQL

CREATE TABLE customers (
  C_Id INT AUTO_INCREMENT,
  Name varchar(50),
  Address varchar(255),
  Phone varchar(20),
  PRIMARY KEY (C_Id)
);

MySQL »yªk¨Ï¥Î AUTO_INCREMENT ³oÃöÁä¦r¡Cª`·N­n±N AUTO_INCREMENT Äæ¦ì«ü©w¬° PRIMARY KEY¡A§_«h·|¦³¿ù»~¡I

·s¼W¤@µ§¸ê®Æ¡G

INSERT INTO customers (Name, Address, Phone)
VALUES ('©m¦WXXX', '¦a§}XXX', '¹q¸ÜXXX');

C_Id Äæ¦ì¤£»Ý­n«ü©w­È¡AMySQL ¹w³]·|¥Ñ1¶}©l³v¦C¦Û°Ê»¼¼W (2, 3, 4...)¡C¤£¹L§A¤]¥i¥H´À AUTO_INCREMENT Äæ¦ì«ü©w¤@­Ó°_©l­È¡A»yªk¦p¤U¡G

ALTER TABLE table_name AUTO_INCREMENT=°_©l¼Æ¦r;

SQL Server

CREATE TABLE customers (
  C_Id INT IDENTITY PRIMARY KEY,
  Name varchar(50),
  Address varchar(255),
  Phone varchar(20)
);

SQL Server »yªk¨Ï¥Î IDENTITY ³oÃöÁä¦r¡C

·s¼W¤@µ§¸ê®Æ¡G

INSERT INTO customers (Name, Address, Phone)
VALUES ('©m¦WXXX', '¦a§}XXX', '¹q¸ÜXXX');

SQL Server ¹w³]·|¥Ñ1¶}©l³v¦C¦Û°Ê»¼¼W (2, 3, 4...)¡C¤£¹L§A¤]¥i¥H´À IDENTITY Äæ¦ì«ü©w¤@­Ó°_©l­È»P»¼¼W¶q¡A»yªk¦p¤U¡G

±N IDENTITY §ï¦¨ IDENTITY(°_©l­È, »¼¼W¶q)

Oracle

Oracle ¤ñ¸û¤£¤@¼Ë¡A§Ú­Ì»Ý­n¥ý«Ø¥ß¤@­Ó Sequence¡AµM«á»¼¼W­È«h±q³o­Ó Sequence §ì¡C

Sequence «Ø¥ß»yªk¡G

CREATE SEQUENCE sequence_name
START WITH 1     --³]©w°_©l­È
INCREMENT BY 1;  --³]©w»¼¼W¶q

·s¼W¤@µ§¸ê®Æ¡G

INSERT INTO customers (C_Id, Name, Address, Phone)
VALUES (sequence_name.NEXTVAL ,'©m¦WXXX', '¦a§}XXX', '¹q¸ÜXXX');

NEXTVAL ¥Î¨Ó¨ú±o¤U¤@­Ó­È¡C

MS Access

CREATE TABLE customers (
  C_Id INT PRIMARY KEY AUTOINCREMENT,
  Name varchar(50),
  Address varchar(255),
  Phone varchar(20)
);

Access »yªk¨Ï¥Î AUTOINCREMENT ³oÃöÁä¦r¡C

·s¼W¤@µ§¸ê®Æ¡G

INSERT INTO customers (Name, Address, Phone)
VALUES ('©m¦WXXX', '¦a§}XXX', '¹q¸ÜXXX');

Access ¹w³]·|¥Ñ 1 ¶}©l³v¦C¦Û°Ê»¼¼W (2, 3, 4...)¡C¤£¹L§A¤]¥i¥H´À AUTOINCREMENT Äæ¦ì«ü©w¤@­Ó°_©l­È»P»¼¼W¶q¡A»yªk¦p¤U¡G

±N AUTOINCREMENT §ï¦¨ AUTOINCREMENT(°_©l­È, »¼¼W¶q)

¡@

¡@

CREATE INDEX ±Ô­z¥y (SQL CREATE INDEX Statement)

¦p¦P¦pªG¤@¥»®Ñ¦³¯Á¤Þ (index) ±z¥i¥H§ó§Öªº§ä¨ì»Ý­nªº¸ê®Æ¡A¸ê®Æ®w¤]¬O¦P¼Ë¹D²z¡A¦pªG¤@±i¸ê®Æªí¤¤¨S¦³¯Á¤Þ¡A¦b¬d¸ß¸ê®Æ®É´N¥²»Ý¥ý§â¾ã±i¸ê®ÆªíŪ¹L¤@¹M (scan table) ¦AºCºC¥h§ä¸ê®Æ¡A«D±`ªº¨S¦³®Ä²v¡A¦]¦¹´À¸ê®Æªí¥[¤W "¾A·íªº" ¯Á¤Þ·|¤j¤j¥[§Ö¬d¸ß³t«×®@¡C

¬°¤°»ò»¡¾A·íªº¡H¦]¬°¤@­Ó³]¦³¯Á¤Þªº¸ê®Æªí·í§A¦b INSERT¡BUPDATE ©Î DELETE ®É¡A¯Á¤Þ¤]»Ý­nÃB¥~ªá¸ê·½¥h§ó·s¡A©Ò¥H³Ì¦n¥u¦b±`³Q¬d¸ß¨ìªºÄæ¦ì§@¯Á¤Þ¡A¤£µM±o¤£Àv¥¢°Ú¡I

CREATE INDEX »yªk (SQL CREATE INDEX Syntax)

CREATE INDEX index_name ON table_name (column_name);

«Ø¥ß¦hÄæ¦ì¯Á¤Þ (Multiple-Column Index)

CREATE INDEX index_name ON table_name (column_name1, column_name2...);

¤°»ò®É­Ô«Ø¥ß¦hÄæ¦ì¯Á¤Þ¤ñ¸û¦X¾A¡H¦pªG±z±`¹ï¤@±i¸ê®Æªí¬d¸ß WHERE column_name1='xxx' AND column_name2='yyy'¡A³o®É§A´N¥i¥H¹ï column_name1 ¤Î column_name2 ³o¨â­ÓÄæ¦ì«Ø¥ß¤@­Ó¦@¦P¯Á¤Þ¡C

DROP INDEX ±Ô­z¥y (SQL DROP INDEX Statement)

§Ú­Ì­n«ç»ò±N¯Á¤Þ²¾°£±¼¡H´N¬O¨Ï¥Î DROP INDEX¡C

MySQL

ALTER TABLE table_name DROP INDEX index_name;

SQL Server

DROP INDEX table_name.index_name;

Oracle

DROP INDEX index_name;

MS Access

DROP INDEX index_name ON table_name;

¡@

¡@

View À˵øªí¡Bµø¹Ï (SQL View)

View ¬OÂÇ¥Ñ SQL SELECT ¬d¸ß°ÊºA²Õ¦X¥Í¦¨ªº¸ê®Æªí (¥ç§Y View ¬O¥Ñ¬d¸ß±o¨ìªºµ²ªG¶°²Õ¦X¦Ó¦¨ªº¸ê®Æªí)¡CView ¤ºªº¸ê®Æ¬ö¿ý¬O¥Ñ¨ä¥¦¹ê»Ú¦s¦bªº¸ê®Æªí¤¤²£¥Íªº¡A¥¦´N¹³¬O¤@­ÓµêÀÀ¸ê®Æªí¡A¹ê»Ú¤W¸ê®Æ®w (©Î»¡¬OµwºÐ) ¸Ì­±¬O¤£¦s¦b³o¤@­Ó¸ê®Æªíªº (¥u¦s¦b¦¹ View ªº¬ÛÃö©w¸q)¡A¦ý¬O§Ú­Ì¨Ï¥Î¤W«o¦³¦p¹ê»Ú¦s¦bªº¸ê®Æªí - ©Ò¦³ªº SQL ¬d¸ß»yªk³£¥i¥H¾Þ§@¦b¦¹ View ¤W¡C

¸ê®Æªí¬O¤@ºØ¹êÅéµ²ºc (physical structure)¡A¦Ó View ¬O¤@ºØµêÀÀµ²ºc (virtual structure)¡C

View ¦³­þ¨Ç¯S©Ê

  1. ¥[±j¸ê®Æ®wªº¦w¥þ©Ê¡AView ¥i¥H±N¹êÅé¸ê®Æªíµ²ºcÁôÂð_¨Ó¡A¦P®É­­¨î¨Ï¥ÎªÌ¥u¥i¥HÀ˵ø¤Î¨Ï¥Î­þ¨Ç¸ê®ÆªíÄæ¦ì¡C
  2. À˵øªí¬O°ßŪªº¡A¥ç§Y¥~³¡¨Ï¥ÎªÌµLªkª½±µ³z¹L View ¥h­×§ï¤º³¡¸ê®Æ¡C
  3. ±N½ÆÂøªº SQL ¬d¸ß¥]¸Ë¦b View ¤¤¡A¥i¥H²¤Æ¬d¸ßªº½ÆÂø度¡C
  4. ·í¸ê料ªíµ²ºc¦³ÅÜ更®É¡A¥u»Ý­n更§ï View ªº³]©w¡A不»Ý更§ïµ{¦¡¡C

«Ø¥ß View (SQL CREATE VIEW)

CREATE VIEW view_name [(column_list)] AS
SELECT column_name(s)
FROM table_name
WHERE condition;

¨Ò¦p¡G

CREATE VIEW v AS SELECT qty, price, qty*price AS value FROM t;

¤@­Ó View ¥i¥H¥Ñ¬d¸ß¹êÅé¸ê®Æªí¦Ó«Ø¥ß¡A¥ç¥i¥H¬d¸ß¨ä¥¦¤w¦s¦bªº View ¦Ó«Ø¥ß¡C

§ó·s View (SQL CREATE OR REPLACE VIEW)

¦pªG¥[¤W OR REPLACE ¤l¥yªº·N«ä´N¬O­Y¦P¦Wªº View ¤w¸g¦s¦b´NÂл\¨ú¥N¥¦¡C¦pªG View ¤£¦s¦b¡A§Ú­Ì¥i¥H§â CREATE OR REPLACE VIEW ¬Ý°µ¬O¦p¦P CREATE VIEW¡F¦Ó¦pªG View ¤w¦s¦b¡A§Ú­Ì¥i¥H§â CREATE OR REPLACE VIEW ¬Ý°µ¬O ALTER VIEW¡C

CREATE OR REPLACE VIEW view_name [(column_list)] AS
SELECT column_name(s)
FROM table_name
WHERE condition;

§R°£ View (SQL DROP VIEW)

DROP VIEW view_name;

View ¨Ï¥Î¹ê¨Ò (Example)

°²³]³o¬O¤@­Ó²£«~­q³æ¸ê®Æªí p_orders¡G

Product Price Quantity
LCD 4000 100
CPU 5000 200

§Ú­Ì¥i¥H«Ø¥ß¤@­Ó¤è«K¬d¸ß¦U²£«~°â¥XÁ`ÃBªº View¡G

CREATE VIEW view_p_sum (Product, P_SUM) AS
SELECT Product, Price*Quantity FROM p_orders GROUP BY Product;

±µµÛ¡A§A´N¥i¥H¹³¾Þ§@¤@¯ë¸ê®Æªí¡G

SELECT * FROM view_p_sum
Product P_SUM
CPU 1000000
LCD 400000

¡@

¡@

¡@

¡@

¡@

¸ê®Æ¾ÞÁa DML

INSERT INTO ±Ô­z¥y (SQL INSERT INTO Statement)

INSERT INTO ¬O¥Î¨Ó·s¼W¸ê®Æ¦Ü¬Y¸ê®Æªí (table)¡C

INSERT INTO »yªk (SQL INSERT INTO Syntax)

INSERT INTO table_name (column1, column2, column3...)
VALUES (value1, value2, value3...);

©Î¡A¥i¥H²¼g¦¨³o¼Ë¡G

INSERT INTO table_name
VALUES (value1, value2, value3...);

¨Ï¥Î²¼gªº»yªk¨C­ÓÄæ¦ìªº­È³£¥²»Ý­n¨Ì§Ç¿é¤J¡C

INSERT INTO ¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤·s¼W¤@ÅU«Èªº¸ê®Æ¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678

§Ú­Ì¥i¥H¨Ï¥Î¥H¤Uªº INSERT INTO ±Ô­z¥y¡G

INSERT INTO customers (C_Id, Name, City, Address, Phone)
VALUES (3, '§õ¤T', '°ª¶¯¿¤', 'ZZ¸ô300¸¹', '07-12345678');

©Î¡A²¼g¡G

INSERT INTO customers
VALUES (3, '§õ¤T', '°ª¶¯¿¤', 'ZZ¸ô300¸¹', '07-12345678');

¬d¸ß·s¼W«áªºµ²ªG¦p¤U¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¥u¿é¤J´X­Ó¯S©wªºÄæ¦ì­È

§A¤]¥i¥H¥u¿é¤J´X­Ó¯S©wªºÄæ¦ì­È¡G

INSERT INTO customers (C_Id, Name, City)
VALUES (3, '§õ¤T', '°ª¶¯¿¤');

¬d¸ß·s¼W«áªºµ²ªG¦p¤U¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤

¤@¦¸·s¼W¦hµ§¸ê®Æ (INSERT INTO SELECT)

»yªk¡G

INSERT INTO table_name
VALUES (value1_1, value2_2, value3_3,¡P¡P¡P),
(value2_1, value2_2, value2_3,¡P¡P¡P),
(value3_1, value3_2, value3_3,¡P¡P¡P),
¡P¡P¡P¡P¡P¡P;

©Î§Q¥Î¤l¬d¸ß¡A±q¨ä¥¦ªº¸ê®Æªí¤¤¨ú±o¸ê®Æ¨Ó§@¤@¦¸¦hµ§·s¼W¡G

INSERT INTO table_name (column1, column2, column3,...)
SELECT othercolumn1, othercolumn2, othercolumn3,...
FROM othertable_name;

¦b¤l¬d¸ß¤¤§A¤]¥i¥H§Q¥Î WHERE¡BGROUP BY ¤Î HAVING µ¥¤l¥y¨Ó§@¦³±ø¥óªº·s¼W¸ê®Æ¡C

¡@

¡@

UPDATE ±Ô­z¥y (SQL UPDATE Statement)

¦pªG§Ú­Ì­n­×§ï¸ê®Æªí¤¤ªº¸ê®Æ§Ú­Ì´N·|»Ý­n¥Î¨ì UPDATE¡C

UPDATE »yªk (SQL UPDATE Syntax)

UPDATE table_name
SET column1=value1, column2=value2, column3=value3¡P¡P¡P
WHERE some_column=some_value;

WHERE ±ø¥ó¦¡°O±o­n¥[®@¡I³o¼Ë¤~·|¥u§ó·s¯S©w¬Y(´X)µ§¸ê®Æ¡A¤£µM "¥þ³¡ªº" ¸ê®Æ³£·|§ó§ï¡C

UPDATE ±Ô­z¥y¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤§ó§ïÅU«È¤ý¤Gªº³sµ¸¹q¸Ü¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

§Ú­Ì¥i¥H¨Ï¥Î¥H¤Uªº UPDATE ±Ô­z¥y¡G

UPDATE customers SET Phone='03-87654321' WHERE Name='¤ý¤G';

§ó·s«áªºµ²ªG¦p¤U¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-87654321
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¡@

¡@

¡@

DELETE FROM ±Ô­z¥y (SQL DELETE FROM Statement)

DELETE FROM ¬O¥Î¨Ó§R°£¸ê®Æªí¤¤ªº¸ê®Æ¡C

DELETE FROM »yªk (SQL DELETE FROM Syntax)

DELETE FROM table_name
WHERE column_name operator value;

WHERE ±ø¥ó¦¡°O±o­n¥[®@¡I¤£µM "¥þ³¡ªº" ¸ê®Æ³£·|§R°£¤F¡C

DELETE FROM ±Ô­z¥y¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤§R°£ÅU«È¤ý¤Gªº¸ê®Æ¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

§Ú­Ì¥i¥H¨Ï¥Î¥H¤Uªº DELETE FROM ±Ô­z¥y¡G

DELETE FROM customers WHERE Name='¤ý¤G';

§R°£«áªº¸ê®Æªí²{¦bÅܦ¨¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¤@¦¸§R°£¸ê®Æªí¤¤©Ò¦³ªº¸ê®Æ

§Ú­ÌÁÙ¥i¥H³o¼Ë¤@¦¸§R°£¬Y¸ê®Æªí¤¤©Ò¦³ªº¸ê®Æ¡G

DELETE FROM table_name;

©ÎªÌ¡G

DELETE * FROM table_name;

¡@

¡@

SELECT INTO ±Ô­z¥y (SQL SELECT INTO Statement)

SELECT INTO ¥Î¨Ó±q¬Y¸ê®Æªí¬d¸ß©Ò±o¤§¸ê®Æ¶°µ²ªG·s¼W¨ì¥t¤@­Ó·s«Øªº¸ê®Æªí¤¤¡C¦¹¤@«ü¥O±`¥Î¨Ó½Æ»s³Æ¥÷¸ê®Æªí¡A©Î±N¸ê®Æªí¿é¥X¦Ü¥t¤@¸ê®Æ®w¤¤¡C

SELECT INTO »yªk (SQL SELECT INTO Syntax)

SELECT table_column1, table_column2, table_column3...
INTO new_table_name [IN another_database]
FROM table_name;

¨ä¤¤ new_table_name ¬°±ý·s«Øªº¸ê®Æªí¦WºÙ¡A¸Ó¸ê®Æªí·|¦Û°Ê«Ø¥ß¡A¥B¤£¥i»P¤w¸g¦s¦bªº¸ê®Æªí¦WºÙ¬Û¦P¡F¦Ó another_database ¬°¦Ü¥~³¡¸ê®Æ®wªº¸ô®|¡C

SELECT INTO ±Ô­z¥y¥Îªk (Example)

¨Ò¦p¡A§Ú­Ì²{¦b·Q³Æ¥÷ÅU«È¸ê®Æªí customers¡G

SELECT * INTO customers_backup FROM customers;

©ÎªÌ¡A§Ú­Ì·Q§â¥¦³Æ¥÷¨ì backup ¸ê®Æ®w¤¤¡G

SELECT *
INTO customers_backup IN 'backup.mdb'
FROM customers;

§Ú­Ì¤]¥i¥H¥u½Æ»s¸ê®Æªíµ²ºc¡G

SELECT * INTO new_table_name FROM table_name WHERE 0=1;

MySQL ¸ê®Æ®w¤£¤ä´© SELECT INTO¡A¦ý§A¥i¥H¥Î INSERT...SELECT ¨Ó¹F¨ì¦P¼Ëªº¥\¯à¡C

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¸ê®Æ¬d¸ß DQL

SELECT ±Ô­z¥y (SQL SELECT Statement)

SELECT «Ü¥i¯à¬O³Ì±`¥Î¨ìªº SQL »y¥y¡A¥¦¬O¥Î¨Ó±q¸ê®Æ®w¨ú±o¸ê®Æ¡A³o­Ó°Ê§@§Ú­Ì³q±`ºÙ¤§¬°¬d¸ß (query)¡A¸ê®Æ®w¨Ì SELECT ¬d¸ßªº­n¨D·|ªð¦^¤@­Óµ²ªG¸ê®Æªí (result table)¡A§Ú­Ì³q±`ºÙ¤§¬°¸ê®Æ¶° (result-set)¡C

SELECT »yªk (SQL SELECT Syntax)

SELECT ±Ô­z¥y¥D­n¥Ñ¨â³¡¤Àºc¦¨¡A²Ä¤@³¡¤À¬O­n "®³¤°»ò" ¸ê®Æ (­Y¦³¦h¶µ¥Î³r¸¹¹j¶})¡F²Ä¤G³¡¤À«h¬° "±q­þ®³"¡C

SELECT table_column1, table_column2, table_column3...
FROM table_name;

SELECT ¬d¸ß¥Îªk (SQL SELECT Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤¨ú±oÅU«Èªº©m¦W (Name) ¤Î¹q¸Ü (Phone)¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

§Ú­Ì¥i¥H¤U³o­Ó SELECT ¬d¸ß±Ô­z¥y¡G

SELECT Name, Phone FROM customers;

ªð¦^ªºµ²ªG (¸ê®Æ¶°) ·|¹³³o¼Ë¡G

Name Phone
±i¤@ 02-12345678
¤ý¤G 03-12345678
§õ¤T 07-12345678

SELECT *

¦pªG§Ú­Ì·Q¤@¦¸¨ú±o¾ã±i¸ê®Æªí¸Ì©Ò¦³ªº¸ê®Æ§Ú­Ì¥i¥H¦b SELECT »y¥y¸Ì¥Î * ³o­Ó¯S®í²Å¸¹¡C

SELECT * FROM customers;

³o­Ó SELECT ¬d¸ßªð¦^ªº¸ê®Æ¶°·|¬O¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

­Y«D¥²­n¡A§Ú­ÌÀ³¸ÓºÉ¶qÁקK¨Ï¥Î SELECT *¡A¦]¬°¤@¦¸¨ú±o¾ã±i¸ê®Æªí·|¤ñ¸û¯Ó¶O¨t²Î¸ê·½¡A°O¦í¤@­Ó­ì«h¡A¨ú±o»Ý­nªº¸ê®Æ´N¦n¡A¤£¦h®³¤]¤£¤Ö®³¡C¨Ò¦p­Y¥u·Qª¾¹D«È¤áªº¹q¸Ü¤£»Ý­n³s¥Lªº¦í§}¤]¤@°_¨ú±o¡C

¡@

¡@

WHERE ¬d¸ß¤l¥y (SQL WHERE Clause)

§Ú­Ì¥i¥H¶i¤@¨B¦b SELECT ¬d¸ß»y¥y¨Ï¥Î WHERE ÃöÁä¦r·f°t¹Bºâ¤l¨Ó¨ú¥X "²Å¦X±ø¥ó" ªº¬ö¿ý­È¡C

WHERE »yªk (SQL WHERE Syntax)

SELECT table_column1, table_column2...
FROM table_name
WHERE column_name operator value;

WHERE ¤l¥y¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤¬d¸ß "¤ý¤G" ªº¸ê®Æ¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678
4 ³¯¥| ¥x¥_¥« AA¸ô400¸¹ 02-87654321

§Ú­Ì¥i¥H¤U "¨ú¥Xªí¤¤©m¦WÄæ¦ì­È¬O¤ý¤G" ¦p¦¹±ø¥óªº SELECT ±Ô­z¥y¡G

SELECT * FROM customers WHERE Name = '¤ý¤G';

¬d¸ßªºµ²ªG¦p¤U¡G

C_Id Name City Address Phone
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678

¬d¸ß±ø¥ó¤¤ªº "¦r¦ê­È" »Ý­n¥Î³æ¤Þ¸¹ '' ¥]¦í¡A§_«h·|¥X²{¿ù»~¡F¥t¤@¤è­±¡A¹ï©ó¼Æ­È«¬§O«h¤£¥i¥H¥[¤W¤Þ¸¹¡A§_«h¤]·|¥X²{¿ù»~¡C³Q³æ¤Þ¸¹³ò¦íªº¦r¤¸§Yªí¥Ü¨ä¸ê®Æ«¬§O¬°¦r¦ê¡A¦ý¦³¨Ç¸ê®Æ®w¤]±µ¨ü¥ÎÂù¤Þ¸¹¡C

¦h­Ó¬d¸ß±ø¥ó¦¡

§A¥i¥H¨Ï¥Î AND ©Î OR ¹Bºâ¤l¦P®É¤U¹F¦h­Ó±ø¥ó¡G

SELECT table_column1, table_column2...
FROM table_name
WHERE column_name1 operator value1
AND column_name2 operator value2
[AND|OR]...;

Á|­Ó¨Ò¤l¡A§Ú­Ì·Q¦b¤U­±³o­Ó customers ¸ê®Æªí¤¤¬d¸ß¦í¦b¥x¥_¥«¦Ó¥BÁ~¤ô¤j©ó 5 ¸UªºÅU«È©m¦W¤Î³sµ¸¹q¸Ü¡G

C_Id Name City Address Phone Salary
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678 25000
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678 30000
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678 30000
4 ³¯¥| ¥x¥_¥« AA¸ô400¸¹ 02-87654321 50000

¥i¥H³o¼Ë¬d¸ß¡G

SELECT Name, Phone FROM table_name
WHERE City = '¥x¥_¥«' AND Salary >= 50000;

¬d¸ßªºµ²ªG¦p¤U¡G

Name Phone
³¯¥| 02-87654321

§Q¥Î¶ê¬A¸¹ () ¥i¥H¨Ó°õ¦æ§ó½ÆÂøªº¬d¸ß±ø¥ó¡A¶ê¬A¸¹¤º³ò¦íªºªº±ø¥ó¦¡·|Àu¥ý§PÂ_¡C¨Ò¦p¡G

WHERE ±ø¥ó¤@ AND (±ø¥ó¤G OR ±ø¥ó¤T)

¦¹ SQL ³¯­z¦¡·|Àu¥ý¹ï±ø¥ó¤G»P±ø¥ó¤T°µ OR ÅÞ¿è¹Bºâ¡A©Ò±oµ²ªG¦A±µµÛ©M±ø¥ó¤@°µ AND ÅÞ¿è¹Bºâ¡C

¡@

¡@

ORDER BY ÃöÁä¦r (SQL ORDER BY Keyword)

§Ú­Ì¥i¥H±N SELECT ¨ú±oªº¸ê®Æ¶°¨Ì¬YÄæ¦ì¨Ó§@±Æ§Ç¡A¦Ó±Æ§Ç¤À§O¥i¥H¥Ñ¤p¦Ü¤j (ascending; ¹w³])¡A©Î¥Ñ¤j¦Ü¤p (descending)¡C

ORDER BY »yªk (SQL ORDER BY Syntax)

SELECT table_column1, table_column2...
FROM table_name
ORDER BY column_name1 ASC|DESC, column_name2 ASC|DESC...

ORDER BY ¬d¸ß¥Îªk (Example)

±q¤U­±ªº employees ¸ê®Æªí¤¤¨ú¥X©Ò¦³­û¤uªº¸ê®Æ¨Ã¨Ì¾ºÙ¨Ó§@±Æ§Ç (§Y¦r¥À¶¶§Ç)¡G

E_Id Name Title
1 Allen crew
2 Tom manager
3 Chris crew
4 Bill crew

§Ú­Ì¥i¥H¤U³o­Ó SELECT ±Ô­z¥y¡G

SELECT * FROM employees ORDER BY Title;

¬d¸ßªºµ²ªG¦p¤U¡G

E_Id Name Title
1 Allen crew
3 Chris crew
4 Bill crew
2 Tom manager

­Y»y¥y¤¤¨S¥[¤W ASC ©Î DESC ÃöÁä¦r¡A¹w³]Àq»{¬° ASC¡C

¨Ì¾ºÙ¨Ó§@¥¿¦V±Æ§Ç¤§«á¦A¨Ì¦WºÙ¨Ó§@¤Ï¦V±Æ§Ç¡G

SELECT * FROM employees ORDER BY Title ASC, Name DESC;

¬d¸ßªºµ²ªG¦p¤U¡G

E_Id Name Title
3 Chris crew
4 Bill crew
1 Allen crew
2 Tom manager

SELECT »y¥y¤¤¥Î¨Ó§@±Æ§ÇªºÄæ¦ì¦WºÙ¡A°£¤FÄæ¦ì¦WºÙ¥~¡A¤]¥i¥H¨Ï¥Î SELECT »y¥y¬d¸ßªºÄæ¦ì¶¶§Ç¡C¦p¡ASELECT ÃöÁä¦r«áªº²Ä¤@­ÓÄæ¦ì (table_column1) ¬° 1¡A²Ä¤G­ÓÄæ¦ì (table_column2) «h¬° 2¡C¦]¦¹¡A§Ú­Ì¤]¥i¥H±N¤W¨Ò¤¤ªº SQL ±Ô­z¥y§ï¼g¦¨¡G

SELECT * FROM employees ORDER BY 3 ASC, 2 DESC;

¡@

¡@

¡@

SELECT DISTINCT (SQL SELECT DISTINCT Statement)

¤@­Ó¸ê®Æªíªº¬YÄæ¦ì¤¤¥i¯à·|¦³¦h­Ó¬ö¿ý³£¬O¬Û¦P­Èªº±¡ªp¡A¦b SELECT ¬d¸ß»y¥y¤¤§Ú­Ì¥i¨Ï¥Î DISTINCT ÃöÁä¦r¹LÂo­«½Æ¥X²{ªº¬ö¿ý­È¡C

SELECT DISTINCT »yªk (SELECT DISTINCT Syntax)

SELECT DISTINCT table_column1, table_column2...
FROM table_name;

SELECT DISTINCT ¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤¬d¸ßÅU«È¤À§G¦b­þ´X­Ó¿¤¥«¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678
4 ³¯¥| ¥x¥_¥« AA¸ô400¸¹ 02-87654321

§Ú­Ì¥i¥H¤U³o­Ó SELECT ¬d¸ß±Ô­z¥y¡G

SELECT DISTINCT City FROM customers;

ªð¦^ªºµ²ªG¦p¤U¡G

City
¥x¥_¥«
·s¦Ë¿¤
°ª¶¯¿¤

­ì¥»¸ê®Æªíªº City Äæ¦ì¤¤¦³¨â­Ó­«½Æ­È¥x¥_¥«¡A¥i¬O§Ú­Ì¥u·Qª¾¹D¦³­þ´X­Ó¿¤¥«¦³ÅU«È¦Ó¤w¡A¬G§Ú­Ì¨Ï¥Î DISTINCT ÃöÁä¦r¨Ó­­¨î¶È¨ú¥XÄæ¦ì¤¤ "¤£¬Û¦P" ªº­È¡C

­Y±µµÛ SELECT DISTINCT «á­±¦³«ü©w¨â­Ó¥H¤WªºÄæ¦ì¡A«h­n²Å¦X©Ò¦³Äæ¦ì­È¬Ò¦P¼Ë­«½Æªº±¡ªp¤U¸Óµ§¸ê®Æ¤~·|³Q±Ë±ó¡C­Y¥u¦³¨ä¤¤¤@­ÓÄæ¦ì­È¬Û¦P¦ý¨ä¥¦Äæ¦ì­È¨Ã¤£¦P¡A«h¤´·|¨ú¥X¸Óµ§¸ê®Æ¡C

¡@

¡@

¡@

TOP, LIMIT, ROWNUM ¤l¥y (SQL TOP, LIMIT, ROWNUM Clause)

TOP (SQL Server), LIMIT (MySQL), ROWNUM (Oracle) ³o¨Ç»yªk¨ä¹ê³£¬O¦P¼Ëªº¥\¯à¡A³£¬O¥Î¨Ó­­¨î±zªº SQL ¬d¸ß»y¥y³Ì¦h¥u¼vÅT´Xµ§¸ê®Æ¡A¦Ó¤£¦Pªº»yªk«h¥u¦]¤£¦Pªº¸ê®Æ®w¹ê§@®É±Ä¥Î¤£¦Pªº¦WºÙ¡C

§Ú­Ì¨Ó¬Ý¬Ý¥H¤Uªº¨Ò¤l¡G

§Ú­Ì«ç»ò±q¤U­±³o­Ó customers ¸ê®Æªí¤¤¡H

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678
4 ³¯¥| ¥x¥_¥« AA¸ô400¸¹ 02-87654321

¥u¨ú¥X«e¨âµ§¸ê®Æ©O¡H

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678

MySQL - LIMIT »yªk (SQL LIMIT Syntax)

SELECT table_column1, table_column2...
FROM table_name LIMIT number;

§A¥i¥H³o¼Ë¤U LIMIT ¬d¸ß»y¥y¡G

SELECT * FROM customers LIMIT 2;

SQL Server - TOP »yªk (SQL TOP Syntax)

SELECT TOP number|percent table_column1, table_column2...
FROM table_name;

§A¥i¥H³o¼Ë¤U TOP ¬d¸ß»y¥y¡G

SELECT TOP 2 * FROM customers;

©ÎªÌ³o¼Ë¡G

SELECT TOP 50 PERCENT * FROM customers;

Oracle - ROWNUM »yªk (ROWNUM Syntax)

SELECT table_column1, table_column2...
FROM table_name WHERE ROWNUM <= number;

§A¥i¥H³o¼Ë¤U ROWNUM ¬d¸ß»y¥y¡G

SELECT * FROM customers WHERE ROWNUM <= 2;

¡@

¡@

(NOT) IN ¹Bºâ¤l (SQL (NOT) IN Operator)

IN ·f°t WHERE ¤l¥y¥i¥H¥Î¨Ó­­©w¥²»Ý²Å¦X¬Y¨ÇÄæ¦ì­È¬°±ø¥ó¨Ó·j´M¸ê®Æªí¤¤ªº¯S©w¸ê®Æ¡C

IN »yªk (SQL IN Syntax)

SELECT table_column1, table_column2, table_column3...
FROM table_name
WHERE column_name
IN (value1, value2, value3...);

IN ¹Bºâ¤l¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤¨ú±oÅU«È±i¤@©Î§õ¤Tªº¸ê®Æ¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

§Ú­Ì¥i¥H³o¼Ë§@ SQL ¬d¸ß¡G

SELECT * FROM customers
WHERE Name IN ('±i¤@', '§õ¤T');

¬d¸ß«áªð¦^ªºµ²ªG·|¬O¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

NOT IN

¬Û¤Ïªº¡ANOT IN ´N¬O¤£¥]§t¦b±ø¥ó¸Ìªºªº¸ê®Æ§Ú³q³q­n¤F¡A¦p¤W¨Ò¦h¥[¤W NOT¡G

SELECT * FROM customers
WHERE Name NOT IN ('±i¤@', '§õ¤T');

¬d¸ß«áªð¦^ªºµ²ªG·|¬O¡G

C_Id Name City Address Phone
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678

¡@

¡@

¡@

(NOT) BETWEEN ¹Bºâ¤l (SQL (NOT) BETWEEN Operator)

¤£¦P©ó IN ¹Bºâ¤l¬O¨Ì¤£³sÄòªº­È§@¬° WHERE ¬d¸ß¤l¥yªº¿z¿ï±ø¥ó¡ABETWEEN «h¬O¥Î¨Ó­­©w¨Ì¬Y½d³ò¤º³sÄòªº­È§@¬°±ø¥ó¨Ó·j´M¸ê®Æªí¤¤ªº¯S©w¸ê®Æ¡C

§@¬°¬d¸ß½d³ò±ø¥óªºÄæ¦ì«¬ºA¥i¬°¼Æ­È¡B¤é´Á©Î¦r¦ê¡A¨ä¤¤¦r¦ê¬O¨Ì·Ó¦r¥À±Æ¦C¶¶§Ç¨Ó¬É©w½d³ò¡C

BETWEEN »yªk (SQL BETWEEN Syntax)

SELECT table_column1, table_column2, table_column3...
FROM table_name
WHERE column_name
BETWEEN value1 AND value2;

BETWEEN ¹Bºâ¤l¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤¬d¸ß­q³æ¼Æ¶q¤¶©ó 1000~4000 ªºÅU«È¸ê®Æ¡G

C_Id Name City Address Phone Quantity
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678 1000
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678 3000
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678 5000

§Ú­Ì¥i¥H³o¼Ë§@ SQL ¬d¸ß¡G

SELECT * FROM customers
WHERE Quantity
BETWEEN 1000 AND 4000;

¬d¸ß«áªð¦^ªºµ²ªG·|¬O¡G

C_Id Name City Address Phone Quantity
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678 1000
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678 3000

NOT BETWEEN

¬Û¤Ïªº¡ANOT BETWEEN ´N¬O¤£¥]§t¦b³o½d³ò¸Ìªºªº¸ê®Æ§Ú³q³q­n¤F¡A¦p¤W¨Ò¦h¥[¤W NOT¡G

SELECT * FROM customers
WHERE Quantity
NOT BETWEEN 1000 AND 4000;

¬d¸ß«áªð¦^ªºµ²ªG·|¬O¡G

¡@

¡@

¡@

¸U¥Î¦r¤¸ (SQL Wildcards)

¸U¥Î¦r¤¸¬O»P LIKE ¹Bºâ¤l¤@°_·f°t¨Ï¥Îªº¡A§Ú­Ì¥i¥H§Q¥Î¸U¥Î¦r¤¸¨Ó«Ø¥ß¤@­Ó¼Ò¦¡ (pattern)¡A¶i¦Ó¨Ì¦¹¼Ò¦¡¬°±ø¥ó¨Ó¶i¦æ¸ê®Æ¬d¸ß¡C

¸U¥Î¦r¤¸ºØÃþ

¸U¥Î¦r¤¸ ·N¸q
% ¥Î¨Ó¥N´À¡u¹s­Ó¡v¦Ü¡u¦h­Ó¡v¦r¤¸
_ ¥Î¨Ó¥N´À¡u¤@­Ó¡v¦r¤¸
[charlist] ¥Î¨Ó¥N´À¡u¤@­Ó¡v¦b¦CÁ|½d³ò¤ºªº¦r¤¸
[^charlist] ©Î [!charlist] ¥Î¨Ó¥N´À¡u¤@­Ó¡v¤£¦b¦CÁ|½d³ò¤ºªº¦r¤¸

¸U¥Î¦r¤¸¥Îªk (Example)

¼Ò¦¡ ¼Ò¦¡·N¸q
abc% ©Ò¦³¥H abc ¶}ÀYªº¦r¦ê¡C¦p abc¡Babcx ©Î abcxxx µ¥¦r¦ê¬Ò²Å¦X¦¹¼Ò¦¡¡C
%abc% ©Ò¦³¥]§t abc ªº¦r¦ê¡C¦p abc¡Bxxabcy ©Î abcxxxx µ¥¦r¦ê¬Ò²Å¦X¦¹¼Ò¦¡¡C
a_ ¥H a ¬°¶}ÀYªº¨â­Ó¦r¡C¦p ab¡Bac ©Î ad µ¥¦r¦ê¬Ò²Å¦X¦¹¼Ò¦¡¡A¦ý a ´N¤£²Å¦XÅo¡A¦]¬°¦¹¼Ò¦¡­­©w a ¤§«á¤@©w­n±µ¥ô¤@¦r¤¸¡C
a[bcd] ¥H a ¬°¶}ÀY¨ä«á±µµÛ b ©Î c ©Î d ¨ä¤¤¤@­Ó¦r¤¸¡C¦p ab¡Bac ©Î ad µ¥¦r¦ê¬Ò²Å¦X¦¹¼Ò¦¡¡A¦ý ae ´N¤£²Å¦XÅo¡C
a[^bcd] ¥H a ¬°¶}ÀY¨ä«á±µµÛ«D b ©Î c ©Î d ªº¥ô¦ó¤@­Ó¦r¤¸¡C¦p ax¡Bay ©Î az µ¥¦r¦ê¬Ò²Å¦X¦¹¼Ò¦¡¡A¦ý ab ´N¤£²Å¦XÅo¡C

¨Ò¦p¡A§ä¦í¦b¥x¥_¿¤¥«ªº«È¤á¡G

SELECT * FROM customers WHERE Address LIKE '¥x¥_%';

¡@

¡@

(NOT) LIKE ¹Bºâ¤l (SQL (NOT) LIKE Operator)

LIKE ¹Bºâ¤l·f°t WHERE ¤l¥y¥i¥H¨Ì¤@¯S©w¼Ò¦¡ (Pattern) ¬°±ø¥ó¨Ó·j´M¸ê®Æªí¤¤ªº¯S©w¸ê®Æ¡C

LIKE »yªk (SQL LIKE Syntax)

SELECT table_column1, table_column2, table_column3...
FROM table_name
WHERE column_name LIKE pattern;

LIKE ¹Bºâ¤l¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤¨ú±o¦í¦b¥x¥_¿¤¥«ªºÅU«È¸ê®Æ¡G

C_Id Name Address Phone
1 ±i¤@ ¥x¥_¥«XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ZZ¸ô300¸¹ 07-12345678
4 ³¯¥| ¥x¥_¿¤AA¸ô400¸¹ 02-87654321

§Ú­Ì¥i¥H¨Ï¥Î³o¼Ëªº LIKE ¬d¸ß»y¥y¡G

SELECT * FROM customers WHERE Address LIKE '¥x¥_%';

¬d¸ßµ²ªG¦p¤U¡G

C_Id Name Address Phone
1 ±i¤@ ¥x¥_¥«XX¸ô100¸¹ 02-12345678
4 ³¯¥| ¥x¥_¿¤AA¸ô400¸¹ 02-87654321

NOT LIKE

¬Û¤Ïªº¡ANOT LIKE ´N¬O¤£¥]§t¦b±ø¥ó¸Ìªºªº¸ê®Æ§Ú³q³q­n¤F¡A¦p¤W¨Ò¦h¥[¤W NOT¡G

SELECT * FROM customers
WHERE Address NOT LIKE '¥x¥_%';

¬d¸ß«áªð¦^ªºµ²ªG·|¬O¡G

C_Id Name Address Phone
2 ¤ý¤G ·s¦Ë¿¤YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ZZ¸ô300¸¹ 07-12345678

¡@

¡@

¡@

AS (Alias¡B§O¦W) (SQL Alias)

¦b SQL ¤¤§Ú­Ì¥i¥H´À¸ê®Æªí©ÎÄæ¦ì¦WºÙ¨ú¤@­Ó§O¦W (Alias)¡A³o¥i¥H¨Ï¦WºÙ½ÆÂøªº SQL ¬d¸ß»y¥y§ó©öŪ¥B¥i¥H¦³§óª½Æ[ªº¬d¸ßµ²ªG¡C

AS »yªk - ¸ê®Æªí§O¦W (SQL AS Syntax for Tables)

SELECT table_column1, table_column2, table_column3...
FROM table_name AS alias_name;

AS »yªk - Äæ¦ì§O¦W (SQL AS Syntax for Columns)

SELECT table_column AS alias_name
FROM table_name;

SQL AS §O¦W¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº orders ¸ê®Æªí¤¤¬d¸ß¸ê®Æ¡G

Customer_Name Product_Name Quantity
±i¤@ ·Æ¹« 1000
¤ý¤G ¿Ã¹õ 2000
§õ¤T ¦Lªí¾÷ 500
³¯¥| ¿Ã¹õ 1500

§Ú­Ì¥i¥H³o¼Ë¬d¸ß¡G

SELECT Customer_Name AS Customer,
Product_Name AS Product,
Quantity
FROM orders;

¬d¸ßµ²ªG¦p¤U¡G

Customer Product Quantity
±i¤@ ·Æ¹« 1000
¤ý¤G ¿Ã¹õ 2000
§õ¤T ¦Lªí¾÷ 500
³¯¥| ¿Ã¹õ 1500

¦³ª`·N¨ì¶Ü¡Hªð¦^µ²ªGªºÄæ¦ì¦WºÙÅܤF¡C


¦AÁ|­Ó¨Ò¤l¡A¦pªG§Ú­Ì·Q¨ú±o¦U²£«~Á`¾P°â¶q¥i¥H³o¼Ë¬d¸ß¡G

SELECT o.Product, SUM(Quantity) AS Total
FROM orders AS o
GROUP BY o.Product;

¬d¸ßµ²ªG¦p¤U¡G

Product Total
·Æ¹« 1000
¿Ã¹õ 3500
¦Lªí¾÷ 500

¡@

¡@

¡@

JOIN ³s±µ (SQL JOIN)

SQL JOIN (³s±µ) ¬O§Q¥Î¤£¦P¸ê®Æªí¤§¶¡Äæ¦ìªºÃö³s©Ê¨Óµ²¦X¦h¸ê®Æªí¤§À˯Á¡C

SQL JOIN ¬Oµ²¦X¦h­Ó¸ê®Æªí¦Ó²Õ¦¨¤@©â¶Hªº¼È®É©Ê¸ê®Æªí¥H¨Ñ¸ê®Æ¬d¸ß¡A¦b­ì¦U¸ê®Æªí¤¤¤§¬ö¿ý¤Îµ²ºc¬Ò¤£·|¦]¦¹³s±µ¬d¸ß¦Ó§ïÅÜ¡C

¨Ò¦p¡A³o¬O¤@­Ó«È¤á¸ê®Æªí customers¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¦Ó³o¬O²£«~­q³æªº¸ê®Æªí orders¡G

O_Id OrderNo C_Id
1 2572 3
2 7375 3
3 7520 1
4 1054 1
5 1257 5

¨ä¤¤¡AC_Id ¬O«È¤á¸ê®Æªí¤¤ªº¥DÁä (Primary Key) Äæ¦ì¡A§Ú­Ì«ç»ò±N³o¨â±i¤£¦Pªº¸ê®Æªí¨Ì¬ÛÃöÄæ¦ì¨Ó§@­Ó³s±µµ²¦X¥H«K¬d¸ß©O¡H³o´N¬O±µ¤U¨Óªº¥DÃD Join¡I

SQL ªº JOIN ¬d¸ß¦³­þ´XºØÃþ«¬¡H

  1. INNER JOIN ¤º³¡³s±µ
  2. LEFT (OUTER) JOIN ¥ª¥~³¡³s±µ
  3. RIGHT (OUTER) JOIN ¥k¥~³¡³s±µ
  4. FULL (OUTER) JOIN ¥þ³¡¥~³¡³s±µ
  5. CROSS JOIN ¥æ¤e³s±µ
  6. NATURAL JOIN ¦ÛµM³s±µ

¡@

¡@

¡@

INNER JOIN ÃöÁä¦r (SQL INNER JOIN Keyword) - ¤º³¡³s±µ

INNER JOIN (¤º³¡³s±µ) ¬°µ¥­È³s±µ¡A¥²»Ý«ü©wµ¥­È³s±µªº±ø¥ó¡A¦Ó¬d¸ßµ²ªG¥u·|ªð¦^²Å¦X³s±µ±ø¥óªº¸ê®Æ¡C

INNER JOIN »yªk (SQL INNER JOIN Syntax)

SELECT table_column1, table_column2...
FROM table_name1
INNER JOIN table_name2 
ON table_name1.column_name=table_name2.column_name;

©Î³o¼Ë¼g¡G

SELECT table_column1, table_column2...
FROM table_name1
INNER JOIN table_name2 
USING (column_name);

INNER JOIN ¬d¸ß¥Îªk (Example)

³o¬O¤@­Ó«È¤á¸ê®Æªí customers¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¦Ó³o¬O²£«~­q³æªº¸ê®Æªí orders¡G

O_Id Order_No C_Id
1 2572 3
2 7375 3
3 7520 1
4 1054 1
5 1257 5

²{¦b§Ú­Ì·Q¦C¥X©Ò¦³«È¤áªº­q³æ½s¸¹¸ê®Æ¡A§Ú­Ì¥i¥H§@¤@­Ó INNER JOIN ¬d¸ß¡G

SELECT customers.Name, orders.Order_No
FROM customers
INNER JOIN orders
ON customers.C_Id=orders.C_Id;

¨ä¤¤¥ÎÂI¸¹³s±µ¤§ XXX.YYY ªí¥Ü XXX ¸ê®Æªí¤¤ªº YYY Äæ¦ì¡C

¬d¸ßµ²ªG¦p¤U¡G

Name Order_No
§õ¤T 2572
§õ¤T 7375
±i¤@ 7520
±i¤@ 1054

¬d¸ßµ²ªG¥u·|ªð¦^²Å¦X³s±µ±ø¥óªº¸ê®Æ¡I

¡@

¡@

¡@

LEFT JOIN ÃöÁä¦r (SQL LEFT JOIN Keyword) - ¥ª¥~³¡³s±µ

LEFT JOIN ¥i¥H¥Î¨Ó«Ø¥ß¥ª¥~³¡³s±µ¡A¬d¸ßªº SQL ±Ô­z¥y LEFT JOIN ¥ª°¼¸ê®Æªí (table_name1) ªº©Ò¦³°O¿ý³£·|¥[¤J¨ì¬d¸ßµ²ªG¤¤¡A§Y¨Ï¥k°¼¸ê®Æªí (table_name2) ¤¤ªº³s±µÄæ¦ì¨S¦³²Å¦Xªº­È¤]¤@¼Ë¡C

LEFT JOIN »yªk (SQL LEFT JOIN Syntax)

SELECT table_column1, table_column2...
FROM table_name1
LEFT JOIN table_name2 
ON table_name1.column_name=table_name2.column_name;

¦³¨Ç¸ê®Æ®wªº»yªk·|¬O LEFT OUTER JOIN¡C

LEFT JOIN ¬d¸ß¥Îªk (Example)

³o¬O¤@­Ó«È¤á¸ê®Æªí customers¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¦Ó³o¬O²£«~­q³æªº¸ê®Æªí orders¡G

O_Id Order_No C_Id
1 2572 3
2 7375 3
3 7520 1
4 1054 1
5 1257 5

²{¦b§Ú­Ì·Q¬d¸ß©Ò¦³«È¤á»P¨ä­q³æª¬ªpªº¸ê®Æ¡A§Ú­Ì¥i¥H§@¤@­Ó LEFT JOIN ¬d¸ß¡G

SELECT customers.Name, orders.Order_No
FROM customers
LEFT JOIN orders
ON customers.C_Id=orders.C_Id;

¬d¸ßµ²ªG¦p¤U¡G

Name Order_No
±i¤@ 7520
±i¤@ 1054
¤ý¤G
§õ¤T 2572
§õ¤T 7375

LEFT JOIN ·|ªð¦^¥ª°¼¸ê®Æªí¤¤©Ò¦³¸ê®Æ¦C¡A´Nºâ¨S¦³²Å¦X³s±µ±ø¥ó¡A¦Ó¥k°¼¸ê®Æªí¤¤¦pªG¨S¦³¤Ç°tªº¸ê®Æ­È´N·|Åã¥Ü¬° NULL¡C

¡@

¡@

RIGHT JOIN ÃöÁä¦r (SQL RIGHT JOIN Keyword) - ¥k¥~³¡³s±µ

¬Û¹ï©ó LEFT JOIN¡ARIGHT JOIN ¥i¥H¥Î¨Ó«Ø¥ß¥k¥~³¡³s±µ¡A¬d¸ßªº SQL ±Ô­z¥y RIGHT JOIN ¥k°¼¸ê®Æªí (table_name2) ªº©Ò¦³°O¿ý³£·|¥[¤J¨ì¬d¸ßµ²ªG¤¤¡A§Y¨Ï¥ª°¼¸ê®Æªí (table_name2) ¤¤ªº³s±µÄæ¦ì¨S¦³²Å¦Xªº­È¤]¤@¼Ë¡C

RIGHT JOIN »yªk (SQL RIGHT JOIN Syntax)

SELECT table_column1, table_column2¡P¡P¡P
FROM table_name1
RIGHT JOIN table_name2 
ON table_name1.column_name=table_name2.column_name;

¦³¨Ç¸ê®Æ®wªº»yªk·|¬O RIGHT OUTER JOIN¡C

RIGHT JOIN ¬d¸ß¥Îªk (Example)

³o¬O¤@­Ó«È¤á¸ê®Æªí customers¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¦Ó³o¬O²£«~­q³æªº¸ê®Æªí orders¡G

O_Id Order_No C_Id
1 2572 3
2 7375 3
3 7520 1
4 1054 1
5 1257 5

²{¦b§Ú­Ì·Q¬d¸ß©Ò¦³­q³æ»P¬ÛÀ³ªº«È¤á¤§¸ê®Æ¡A§Ú­Ì¥i¥H§@¤@­Ó RIGHT JOIN ¬d¸ß¡G

SELECT customers.Name, orders.Order_No
FROM customers
RIGHT JOIN orders
ON customers.C_Id=orders.C_Id;

¬d¸ßµ²ªG¦p¤U¡G

Name Order_No
§õ¤T 2572
§õ¤T 7375
±i¤@ 7520
±i¤@ 1054
1257

RIGHT JOIN ·|ªð¦^¥k°¼¸ê®Æªí¤¤©Ò¦³¸ê®Æ¦C¡A´Nºâ¬O¨S¦³²Å¦X³s±µ±ø¥ó¡A¦Ó¥ª°¼¸ê®Æªí¤¤¦pªG¨S¦³¤Ç°tªº¸ê®Æ­È´N·|Åã¥Ü¬° NULL¡C

¡@

¡@

FULL JOIN ÃöÁä¦r (SQL FULL JOIN Keyword) ¡Ð ¥þ³¡¥~³¡³s±µ

FULL JOIN §Y¬° LEFT JOIN »P RIGHT JOIN ªºÁp¶°¡A¥¦·|ªð¦^¥ª¥k¸ê®Æªí¤¤©Ò¦³ªº¬ö¿ý¡A¤£½×¬O§_²Å¦X³s±µ±ø¥ó¡C

FULL JOIN »yªk (SQL FULL JOIN Syntax)

SELECT table_column1, table_column2...
FROM table_name1
FULL JOIN table_name2 
ON table_name1.column_name=table_name2.column_name;

FULL JOIN ¬d¸ß¥Îªk (Example)

³o¬O¤@­Ó«È¤á¸ê®Æªí customers¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¦Ó³o¬O²£«~­q³æªº¸ê®Æªí orders¡G

O_Id Order_No C_Id
1 2572 3
2 7375 3
3 7520 1
4 1054 1
5 1257 5

§Ú­Ì¨Ó§@¤@­Ó FULL JOIN ¬d¸ß¡G

SELECT customers.Name, orders.Order_No
FROM customers
FULL JOIN orders
ON customers.C_Id=orders.C_Id;

¬d¸ßµ²ªG¦p¤U¡G

¡@
Name Order_No
§õ¤T 2572
§õ¤T 7375
±i¤@ 7520
±i¤@ 1054
1257
¤ý¤G

¡@

MySQL ¸ê®Æ®w¤¤¨S¦³ FULL JOIN¡A¦ý¬O¥i¥H¥Î UNION ¨Ó¼ÒÀÀ¡C

¡@

¡@

CROSS JOIN ÃöÁä¦r (SQL CROSS JOIN Keyword) - ¥æ¤e³s±µ

¥æ¤e³s±µ¬°¨â­Ó¸ê®Æªí¶¡ªº²Ã¥d¨à­¼¿n (Cartesian product)¡A¨â­Ó¸ê®Æªí¦bµ²¦X®É¡A¤£«ü©w¥ô¦ó±ø¥ó¡A§Y±N¨â­Ó¸ê®Æªí¤¤©Ò¦³ªº¥i¯à±Æ¦C²Õ¦X¥X¨Ó¡A¥H¤U¨Ò¦Ó¨¥ CROSS JOIN ¥X¨Óªºµ²ªG¸ê®Æ¦C¼Æ¬° 3¡Ñ5=15 µ§¡A¦]¦¹¡A·í¦³ WHERE¡BON¡BUSING ±ø¥ó®É¤£«Øij¨Ï¥Î¡C

CROSS JOIN »yªk (SQL CROSS JOIN Syntax)

SELECT table_column1, table_column2...
FROM table_name1
CROSS JOIN table_name2;

©Î³o¼Ë¼g¡G

SELECT table_column1, table_column2...
FROM table_name1, table_name2;

©Î³o¼Ë¼g¡G

SELECT table_column1, table_column2...
FROM table_name1
JOIN table_name2;

FULL JOIN ¬d¸ß¥Îªk (Example)

³o¬O¤@­Ó«È¤á¸ê®Æªí customers¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¦Ó³o¬O²£«~­q³æªº¸ê®Æªí orders¡G

O_Id Order_No C_Id
1 2572 3
2 7375 3
3 7520 1
4 1054 1
5 1257 5

§Ú­Ì¨Ó§@¤@­Ó CROSS JOIN ¬d¸ß¡G

SELECT customers.Name, orders.Order_No
FROM customers
CROSS JOIN orders;

¬d¸ßµ²ªG¦p¤U¡G

Name Order_No
±i¤@ 2572
¤ý¤G 2572
§õ¤T 2572
±i¤@ 7375
¤ý¤G 7375
§õ¤T 7375
±i¤@ 7520
¤ý¤G 7520
§õ¤T 7520
±i¤@ 1054
¤ý¤G 1054
§õ¤T 1054
±i¤@ 1257
¤ý¤G 1257
§õ¤T 1257

¡@

¡@

NATURAL JOIN ÃöÁä¦r (SQL NATURAL JOIN Keyword) - ¦ÛµM³s±µ

¦ÛµM³s±µ¦³ NATURAL JOIN¡BNATURAL LEFT JOIN¡BNATURAL RIGHT JOIN¡A¨â­Óªí®æ¦b¶i¦æ JOIN ®É¡A¥[¤W NATURAL ³o­ÓÃöÁä¦r¤§«á¡A¨â¸ê®Æªí¤§¶¡¦P¦WªºÄæ¦ì·|³Q¦Û°Êµ²¦X¦b¤@°_¡C

NATURAL JOIN »yªk (SQL NATURAL JOIN Syntax)

SELECT table_column1, table_column2...
FROM table_name1
NATURAL JOIN table_name2;

NATURAL JOIN ¬d¸ß¥Îªk (Example)

³o¬O¤@­Ó«È¤á¸ê®Æªí customers¡G

C_Id Name City Address Phone
1 ±i¤@ ¥x¥_¥« XX¸ô100¸¹ 02-12345678
2 ¤ý¤G ·s¦Ë¿¤ YY¸ô200¸¹ 03-12345678
3 §õ¤T °ª¶¯¿¤ ZZ¸ô300¸¹ 07-12345678

¦Ó³o¬O²£«~­q³æªº¸ê®Æªí orders¡G

O_Id Order_No C_Id
1 2572 3
2 7375 3
3 7520 1
4 1054 1
5 1257 5

²{¦b§Ú­Ì·Q¦C¥X©Ò¦³«È¤áªº­q³æ½s¸¹¸ê®Æ¡A§Ú­Ì¥i¥H§@¤@­Ó NATURAL JOIN ¬d¸ß¡G

SELECT customers.Name, orders.Order_No
FROM customers
NATURAL JOIN orders;

¬d¸ßµ²ªG¦p¤U¡G

Name Order_No
§õ¤T 2572
§õ¤T 7375
±i¤@ 7520
±i¤@ 1054

ª`·N¨ì¤F¶Ü¡Hªð¦^µ²ªG¦Pµ¥©ó¤U­±³o­Ó INNER JOIN ¬d¸ß:

SELECT customers.Name, orders.Order_No
FROM customers
INNER JOIN orders
ON customers.C_Id=orders.C_Id;

¡@

¡@

UNION ¹Bºâ¤l (SQL UNION Operator)

UNION ¹Bºâ¤l¥Î¨Ó±N¨â­Ó(¥H¤W) SQL ¬d¸ßªºµ²ªG¦X¨Ö°_¨Ó¡A¦Ó¥Ñ UNION ¬d¸ß¤¤¦U§O SQL »y¥y©Ò²£¥ÍªºÄæ¦ì»Ý­n¬O¬Û¦Pªº¸ê®Æ«¬§O¤Î¶¶§Ç¡C

UNION ¬d¸ß¥u·|ªð¦^¤£¦P­Èªº¸ê®Æ¦C¡A¦³¦p SELECT DISTINCT¡C

UNION ´N¬O¹³¬O OR (Áp¶°)¡A¦pªG¬ö¿ý¦s¦b©ó²Ä¤@­Ó¬d¸ßµ²ªG¶°©Î²Ä¤G­Ó¬d¸ßµ²ªG¶°¤¤¡A´N·|³Q¨ú¥X¡C

UNION »P JOIN ¤£¦Pªº¦a¤è¦b©ó¡AJOIN ¬O§@¾î¦Vµ²¦X (¦X¨Ö¦h­Ó¸ê®Æªíªº¦UÄæ¦ì)¡F¦Ó UNION «h¬O§@««ª½µ²¦X (¦X¨Ö¦h­Ó¸ê®Æªí¤¤ªº¬ö¿ý)¡C

UNION »yªk (SQL UNION Syntax)

SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2;

UNION ¬d¸ßªð¦^ªº¸ê®Æ¶°Äæ¦ì¦WºÙ¡A³q±`·|¨Ì¾Ú²Ä¤@­Ó SELECT ¬d¸ßªºÄæ¦ì¦WºÙ¡C

UNION ¹Bºâ¤l¬d¸ß¥Îªk (Example)

³o¬O¤½¥q¦b¥xÆW¦a°Ï¾P°âªº²£«~¸ê®Æªí products_taiwan¡G

P_Id P_Name
1 LCD
2 CPU
3 RAM

³o¬O¤½¥q¦b¤¤°ê¤j³°¦a°Ï¾P°âªº²£«~¸ê®Æªí products_china¡G

P_Id P_Name
1 Keyboard
2 CPU
3 LCD

²{¦b§Ú­Ì·Q¬d¥X¤½¥q©Ò¦³²£«~Ãþ§Oªº¸ê®Æ¡A§Ú­Ì¥i¥H§@¤@­Ó UNION ¬d¸ß¡G

SELECT P_Name FROM products_taiwan
UNION
SELECT P_Name FROM products_china;

¬d¸ßµ²ªG¦p¤U¡G

P_Name
LCD
CPU
RAM
Keyboard

UNION ALL

¬Û¤Ïªº¡AUNION ALL «h·|¦C¥X©Ò¦³ªº¸ê®Æ¡A¤£½×¬O§_­«½Æ¡C

¨Ò¦p¬d¸ß¡G

SELECT P_Name FROM products_taiwan
UNION ALL
SELECT P_Name FROM products_china;

¬d¸ßµ²ªG¦p¤U¡G

P_Name
LCD
CPU
RAM
Keyboard
CPU
LCD

¡@

¡@

INTERSECT ¹Bºâ¤l (SQL INTERSECT Operator)

¬Û¹ï©ó UNION ¹³¬O OR (Áp¶°)¡AINTERSECT ¹Bºâ¤l«h¹³¬O AND (¥æ¶°)¡A¦pªG¬ö¿ý¦s¦b©ó²Ä¤@­Ó¬d¸ßµ²ªG¶°¤º¦P®É¥ç¦s¦b©ó²Ä¤G­Ó¬d¸ßµ²ªG¶°¤º®É¡A¤~·|³Q¨ú¥X¡C

INTERSECT »yªk (SQL INTERSECT Syntax)

SELECT column_name(s) FROM table_name1
INTERSECT
SELECT column_name(s) FROM table_name2;

¨â­Ó SELECT ¬d¸ß©Ò²£¥ÍªºÄæ¦ì»Ý­n¬O¬Û¦Pªº¸ê®Æ«¬§O¤Î¶¶§Ç¡C

INTERSECT ¬d¸ßªð¦^ªº¸ê®Æ¶°Äæ¦ì¦WºÙ¡A³q±`·|¨Ì¾Ú²Ä¤@­Ó SELECT ¬d¸ßªºÄæ¦ì¦WºÙ¡C

INTERSECT ¹Bºâ¤l¬d¸ß¥Îªk (Example)

³o¬O¤½¥q¦b¥xÆW¦a°Ï¾P°âªº²£«~¸ê®Æªí products_taiwan¡G

P_Id P_Name
1 LCD
2 CPU
3 RAM

³o¬O¤½¥q¦b¤¤°ê¤j³°¦a°Ï¾P°âªº²£«~¸ê®Æªí products_china¡G

P_Id P_Name
1 Keyboard
2 CPU
3 LCD

²{¦b§Ú­Ì·Q¬d¥X¦b¥xÆW¦a°Ï¤Î¤¤°ê¤j³°¦a°Ï¬Ò¦³¾P°âªº²£«~Ãþ§Oªº¸ê®Æ¡A§Ú­Ì¥i¥H§@¤@­Ó INTERSECT ¬d¸ß¡G

SELECT P_Name FROM products_taiwan
INTERSECT
SELECT P_Name FROM products_china;

¬d¸ßµ²ªG¦p¤U¡G

¡@
P_Name
LCD
CPU

¡@

MySQL ¥Ø«eÁÙ¤£¤ä´© INTERSECT¡C

¡@

¡@

¡@

MINUS ¹Bºâ¤l (SQL MINUS Operator)

·í MINUS ¹Bºâ¤lµ²¦X¤F¨â­Ó SELECT ¬d¸ß»y¥y¡A¥¦·|±N (²Ä¤@­Ó¬d¸ßµ²ªG¶°) ´î¥h (¦P®É¦s¦b©ó²Ä¤@­Ó¬d¸ßµ²ªG¶°»P²Ä¤G­Ó¬d¸ßµ²ªG¶°ªº¸ê®Æ¬ö¿ý)¡AµM«áªð¦^¨äµ²ªG¡C

MINUS »yªk (SQL MINUS Syntax)

SELECT column_name(s) FROM table_name1
MINUS
SELECT column_name(s) FROM table_name2;

MINUS ¬d¸ßªð¦^ªº¸ê®Æ¶°Äæ¦ì¦WºÙ¡A³q±`·|¨Ì¾Ú²Ä¤@­Ó SELECT ¬d¸ßªºÄæ¦ì¦WºÙ¡C

¨â­Ó SELECT ¬d¸ß©Ò²£¥ÍªºÄæ¦ì»Ý­n¬O¬Û¦Pªº¸ê®Æ«¬§O¤Î¶¶§Ç¡C

MINUS ¹Bºâ¤l¬d¸ß¥Îªk (Example)

³o¬O¤½¥q¦b¥xÆW¦a°Ï¾P°âªº²£«~¸ê®Æªí products_taiwan¡G

P_Id P_Name
1 LCD
2 CPU
3 RAM

³o¬O¤½¥q¦b¤¤°ê¤j³°¦a°Ï¾P°âªº²£«~¸ê®Æªí products_china¡G

P_Id P_Name
1 Keyboard
2 CPU
3 LCD

²{¦b§Ú­Ì·Q¬d¥X¦³¦b¥xÆW¦a°Ï¾P°â¦ý¬O¨S¦³¦b¤¤°ê¤j³°¦a°Ï¾P°âªº²£«~¡A§Ú­Ì¥i¥H§@¤@­Ó MINUS ¬d¸ß¡G

SELECT P_Name FROM products_taiwan
MINUS
SELECT P_Name FROM products_china;

¬d¸ßµ²ªG¦p¤U¡G

¡@
P_Name
RAM

¡@

MySQL ¥Ø«eÁÙ¤£¤ä´© MINUS¡C

¡@

¡@

¤l¬d¸ß (SQL Subquery)

§Ú­Ì¥i¥H±N¤@­Ó SQL ¬d¸ß»y¥y¶ë¤J¥t¤@­Ó SQL ¬d¸ß»y¥y¤¤¡A³o´N¬O¤l¬d¸ß (subquery)¡C¤l¬d¸ß¬O¤@­Ó¬d¸ß¤ºªº¬d¸ß¡A§Ú­Ì¥i¥H¥Î¨Ó³s±µ¸ê®Æªí¡A©Î¦b¤£¯à¨Ï¥Î³æ¤@»y¥y¨Ó§¹¦¨ªº¬d¸ß®É§Ú­Ì´N»Ý­n¥Î¨ì¤l¬d¸ß¡C

¤l¬d¸ß»yªk (SQL Subquery Syntax)

SELECT table_column1, table_column2, table_column3...
FROM table_name
WHERE Äæ¦ì¦WºÙ ¤ñ¸û¹Bºâ¤l (SELECT ¤l¬d¸ß);

¤º³¡ªº SELECT ¬d¸ßºÙ¬°¤l¬d¸ß¡A¦Ó¥~³¡ªº SELECT ¬d¸ß§YºÙ¬°¥D¬d¸ß (main query)¡C

¤l¬d¸ß¥Îªk (Example)

§Ú­Ì²{¦b§Q¥Î¤l¬d¸ß¨Ó¹F¨ì¸ò MINUS ¬Û¦Pªº¬d¸ßµ²ªG¡C

³o¬O¤½¥q¦b¥xÆW¦a°Ï¾P°âªº²£«~¸ê®Æªí products_taiwan¡G

P_Id P_Name
1 LCD
2 CPU
3 RAM

³o¬O¤½¥q¦b¤¤°ê¤j³°¦a°Ï¾P°âªº²£«~¸ê®Æªí products_china¡G

P_Id P_Name
1 Keyboard
2 CPU
3 LCD

²{¦b§Ú­Ì·Q¬d¥X¦³¦b¥xÆW¦a°Ï¾P°â¦ý¬O¨S¦³¦b¤¤°ê¤j³°¦a°Ï¾P°âªº²£«~¡A§Ú­Ì¥i¥H§@¤@­Ó¤l¬d¸ß¡G

SELECT P_Name FROM products_taiwan
WHERE P_Name NOT IN 
(SELECT P_Name FROM products_china);

¬d¸ßµ²ªG¦p¤U¡G

P_Name
RAM

¡@

¡@

(NOT) EXISTS ¹Bºâ¤l (SQL (NOT) EXISTS Operator)

EXISTS ¹Bºâ¤l¥i¥H³s±µ¤l¬d¸ß¡A¥Î¨Ó§PÂ_¤l¬d¸ß¬O§_¦³ªð¦^ªºµ²ªG¡A¦pªG¦³µ²ªGªð¦^«h¬°¯u¡B§_«h¬°°²¡C­Y EXISTS ¬°¯u¡A´N·|Ä~Äò°õ¦æ¥~¬d¸ß¤¤ªº SQL¡F­Y EXISTS ¬°°²¡A«h¾ã­Ó SQL ¬d¸ß´N¤£·|ªð¦^¥ô¦óµ²ªG¡C

NOT EXISTS «h¬O¬Û¹ï©ó EXISTS¡A§PÂ_¬°°²¤~·|Ä~Äò°õ¦æ¥~¬d¸ß¡C

EXISTS ¹Bºâ¤l¥Îªk (Example)

§Ú­Ì¥H IN ¹Bºâ¤l¨Ó»P EXISTS §@¤@¤ñ¸û¡A¤U¦C¨â­Ó SQL ¬d¸ß¬Ò·|ªð¦^¦P¼Ëªºµ²ªG¡G

SELECT * FROM table_a
WHERE EXISTS
(SELECT * FROM table_b WHERE table_b.id=table_a.id);

¬Û·í©ó¡G

SELECT * FROM table_a
WHERE id
in (SELECT id FROM table_b);

MySQL ¥Ø«eÁÙ¤£¤ä´© EXISTS¡C

¡@

¡@

CASE ÃöÁä¦r (SQL CASE Keyword)

CASE Ãþ¦ü©óµ{¦¡»y¨¥¸Ìªº if/then/else »y¥y¡A¥Î¨Ó§@ÅÞ¿è§PÂ_¡C

CASE »yªk (SQL CASE Syntax)

CASE
  WHEN condition THEN result
  [WHEN¡P¡P¡P]
  [ELSE result]
END;

©Î¬O¡G

CASE expression
  WHEN value THEN result
  [WHEN¡P¡P¡P]
  [ELSE result]
END;

­Y¬Ù²¤¤F ELSE ¤l¥y¥B¬¢µL¬Û²Åªº±ø¥ó«h·|ªð¦^ NULL¡C

CASE ÃöÁä¦r¥Îªk (Example)

°²³]§Ú­Ì§@¤@­Ó°Ý¨÷½Õ¬d¡G±z³ßÅw³o­Óºô¯¸¶Ü¡H 1.³ßÅw¡@2.¤£³ßÅw¡@3.ÁÙOK

°Ý¨÷µ²ªG¸ê®Æªí questionnaire ¦p¤U¡G

Name Answer
±i¤@ 1
¤ý¤G 2
§õ¤T 3

§Ú­Ì¥i¥H§@¥H¤U³o­Ó SQL ¬d¸ß¡G

select Name, case Answer
  when 1 then '³ßÅw'
  when 2 then '¤£³ßÅw'
  when 3 then 'ÁÙOK'
END
FROM questionnaire;

©Î¬O¡G

select Name, case
  when Answer=1 then '³ßÅw'
  when Answer=2 then '¤£³ßÅw'
  when Answer=3 then 'ÁÙOK'
END
AS Answer
FROM questionnaire;

¬d¸ßµ²ªG¦p¤U¡G

Name Answer
±i¤@ ³ßÅw
¤ý¤G ¤£³ßÅw
§õ¤T ÁÙOK

¡@

¡@

¡@

¡@

¡@

SQL ¨ç¼Æ

¨ç¼Æ (SQL Functions)

SQL ¥D­n¦³¤T¤jÃþ¤º«Ø¨ç¼Æ¡A¤À§O¬O»E¦X¨ç¼Æ (Aggregate Functions)¡B¦r¦ê¨ç¼Æ (String Functions) »P¼Æ¾Ç¹Bºâ¨ç¼Æ (Mathematical Functions)¡C

»E¦X¨ç¼Æ (SQL Aggregate Functions)

¦r¦ê¨ç¼Æ (SQL String Functions)

¼Æ­È¨ç¼Æ (SQL Mathematical Functions)

¡@

¡@

AVG() ¨ç¼Æ (SQL AVG() Function)

AVG() ¨ç¼Æ¥Î¨Ó­pºâ¤@¼Æ­ÈÄæ¦ìªº¥­§¡­È¡C

AVG() »yªk (SQL AVG() Syntax)

SELECT AVG(column_name) FROM table_name;

AVG() ¨ç¼Æ¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº students ¸ê®Æªí¤¤¬d¸ß¾Ç¥Íªº¥­§¡¨­°ª¡G

S_Id Name Height
1 ±i¤@ 170
2 ¤ý¤G 176
3 §õ¤T 173

§Ú­Ì¥i¥H¤U³o­Ó SELECT ¬d¸ß±Ô­z¥y¡G

SELECT AVG(Height) FROM students;

ªð¦^ªºµ²ªG¦p¤U¡G

AVG(Height)
173

¡@

¡@

COUNT() ¨ç¼Æ (SQL COUNT() Function)

COUNT() ¨ç¼Æ¥Î¨Ó­pºâ²Å¦X¬d¸ß±ø¥óªºÄæ¦ì¬ö¿ýÁ`¦@¦³´Xµ§¡C

COUNT() »yªk (SQL COUNT() Syntax)

SELECT COUNT(column_name) FROM table_name;

­YÄæ¦ì­È¬° NULL¡A«h¸Óµ§°O¿ý¤£·|³Q COUNT ­pºâ¶i¥h¡C

COUNT() ¨ç¼Æ¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº orders ¸ê®Æªí¤¤¬d¸ß "±i¤@" Á`¦@¦³´Xµ§­q³æ¡G

O_Id Price Customer
1 1000 ±i¤@
2 2000 ¤ý¤G
3 500 §õ¤T
4 1300 ±i¤@
5 1800 ¤ý¤G

§Ú­Ì¥i¥H¤U³o­Ó SELECT ¬d¸ß±Ô­z¥y¡G

SELECT COUNT(Customer) FROM orders WHERE Customer='±i¤@';

ªð¦^ªºµ²ªG¦p¤U¡G

COUNT(Customer)
2

COUNT(*)

COUNT(*) ¬O¥Î¨Ó­pºâ¸ê®Æªí¤¤°O¿ýªºÁ`µ§¼Æ¡C

SELECT COUNT(*) FROM table_name;

¥H¤W¨Ò¡A¦pªG§Ú­Ì·Q¬d¸ß orders ¸ê®Æªí¤¤¦³¦h¤Öµ§¸ê®Æ¡A¥i¥H³o¼Ë¬d¸ß¡G

SELECT COUNT(*) FROM orders;

¬d¸ßªºµ²ªG¦p¤U¡G

COUNT(*)
5

COUNT(DISTINCT column_name)

COUNT °t¦X DISTINCT ¥i¥H¥Î¨Ó§ä¥X¸ê®Æªí¤¤¦³¦h¤Öµ§¤£¬Û¦Pªº¸ê®Æ ¡C

SELECT COUNT(DISTINCT column_name) FROM table_name;

¥H¤W¨Ò¡A¦pªG§Ú­Ì·Q¬d¸ß orders ¸ê®Æªí¤¤¦³¦h¤Ö¦ì¤£¦PªºÅU«È¡ASQL ¬d¸ß¦p¤U¡G

SELECT COUNT(DISTINCT Customer) FROM orders;

ªð¦^ªºµ²ªG¦p¤U¡G

COUNT(DISTINCT Customer)
3

¡@

¡@

MAX() ¨ç¼Æ (SQL MAX() Function)

MAX() ¨ç¼Æ¥Î¨Ó¨ú±o¯S©wÄæ¦ì¤¤ªº³Ì¤j¬ö¿ý­È¡C

MAX() »yªk (SQL MAX() Syntax)

SELECT MAX(column_name) FROM table_name;

MAX() ¨ç¼Æ¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº orders ¸ê®Æªí¤¤¬d¸ß³æµ§­q³æªº³Ì°ªª÷ÃB¡G

O_Id Price Customer
1 1000 ±i¤@
2 2000 ¤ý¤G
3 500 §õ¤T
4 1300 ±i¤@
5 1800 ¤ý¤G

§Ú­Ì¥i¥H¤U³o­Ó SELECT ¬d¸ß±Ô­z¥y¡G

SELECT MAX(Price) FROM orders;

ªð¦^ªºµ²ªG¦p¤U¡G

MAX(Price)
2000

¡@

¡@

MIN() ¨ç¼Æ (SQL MIN() Function)

MIN() ¨ç¼Æ¥Î¨Ó¨ú±o¯S©wÄæ¦ì¤¤ªº³Ì¤p¬ö¿ý­È¡C

MIN() »yªk (SQL MIN() Syntax)

SELECT MIN(column_name) FROM table_name;

MIN() ¨ç¼Æ¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº orders ¸ê®Æªí¤¤¬d¸ß³æµ§­q³æªº³Ì§Cª÷ÃB¡G

O_Id Price Customer
1 1000 ±i¤@
2 2000 ¤ý¤G
3 500 §õ¤T
4 1300 ±i¤@
5 1800 ¤ý¤G

§Ú­Ì¥i¥H¤U³o­Ó SELECT ¬d¸ß±Ô­z¥y¡G

SELECT MIN(Price) FROM orders;

ªð¦^ªºµ²ªG¦p¤U¡G

MIN(Price)
500

¡@

¡@

SUM() ¨ç¼Æ (SQL SUM() Function)

SUM() ¨ç¼Æ¥Î¨Ó­pºâ¤@¼Æ­ÈÄæ¦ìªºÁ`¦X¡C

SUM() »yªk (SQL SUM() Syntax)

SELECT SUM(column_name) FROM table_name;

SUM() ¨ç¼Æ¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº orders ¸ê®Æªí¤¤¬d¸ß©Ò¦³­q³æªºª÷ÃBÁ`¦X¡G

O_Id Price Customer
1 1000 ±i¤@
2 2000 ¤ý¤G
3 500 §õ¤T
4 1300 ±i¤@
5 1800 ¤ý¤G

§Ú­Ì¥i¥H¤U³o­Ó SELECT ¬d¸ß±Ô­z¥y¡G

SELECT SUM(Price) FROM orders;

ªð¦^ªºµ²ªG¦p¤U¡G

SUM(Price)
6600

¡@

¡@

GROUP BY ±Ô­z¥y (SQL GROUP BY Statement)

GROUP BY ±Ô­z¥y·f°t»E¦X¨ç¼Æ (aggregation function) ¨Ï¥Î¡A¬O¥Î¨Ó±N¬d¸ßµ²ªG¤¤¯S©wÄæ¦ì­È¬Û¦Pªº¸ê®Æ¤À¬°­Y¤z­Ó¸s²Õ¡A¦Ó¨C¤@­Ó¸s²Õ³£·|¶Ç¦^¤@­Ó¸ê®Æ¦C¡C­Y¨S¦³¨Ï¥Î GROUP BY¡A»E¦X¨ç¼Æ°w¹ï¤@­Ó SELECT ¬d¸ß¡A¥u·|ªð¦^¤@­Ó·JÁ`­È¡C

»E¦X¨ç¼Æ«üªº¤]´N¬O AVG()¡BCOUNT()¡BMAX()¡BMIN()¡BSUM() µ¥³o¨Ç¤º«Ø¨ç¼Æ¡C

GROUP BY »yªk (SQL GROUP BY Syntax)

SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name1, column_name2...;

GROUP BY ¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº orders ¸ê®Æªí¤¤¬d¸ß­Ó§OÅU«Èªº­q³æª÷ÃBÁ`¦X¡G

O_Id Price Customer
1 1000 ±i¤@
2 2000 ¤ý¤G
3 500 §õ¤T
4 1300 ±i¤@
5 1800 ¤ý¤G

§Ú­Ì¥i¥H¤U³o­Ó SELECT ¬d¸ß±Ô­z¥y¡G

SELECT Customer, SUM(Price) FROM orders
GROUP BY Customer;

ªð¦^ªºµ²ªG¦p¤U¡G

Customer SUM(Price)
±i¤@ 2300
¤ý¤G 3800
§õ¤T 500

¤F¸Ñ GROUP BY ªº¥Î³~¤F¶Ü¡H¥¦±N Customer Äæ¦ì­È¬Û¦Pªº¸ê®Æ³£¤À§@¦P¤@²Õ¨Ó­pºâ (¥[Á`) Åo¡I

­Y GROUP BY «á­±«ü©w¨â­Ó¥H¤WªºÄæ¦ì®É¡A«h­n²Å¦X©Ò¦³Äæ¦ì­È¬Ò¬Û¦P¸ê®Æ¤~·|³Q¤À¬°¤@²Õ¡C

¡@

¡@

HAVING ¤l¥y (SQL HAVING Clause)

HAVING ¤l¥y¬O¥Î¨Ó¨ú¥N WHERE ·f°t»E¦X¨ç¼Æ (aggregate function) ¶i¦æ±ø¥ó¬d¸ß¡A¦]¬° WHERE ¤£¯à»P»E¦X¨ç¼Æ¤@°_¨Ï¥Î¡C

»E¦X¨ç¼Æ«üªº¤]´N¬O AVG()¡BCOUNT()¡BMAX()¡BMIN()¡BSUM() µ¥³o¨Ç¤º«Ø¨ç¼Æ¡C

HAVING »yªk (SQL HAVING Syntax)

SELECT column_name(s), aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name1, column_name2...
HAVING aggregate_function(column_name) operator value;

HAVING ¤l¥y¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº orders ¸ê®Æªí¤¤¬d¸ß­q³æª÷ÃBÁ`¦X¤p©ó 1000 ªºÅU«È¡G

O_Id Price Customer
1 1000 ±i¤@
2 2000 ¤ý¤G
3 500 §õ¤T
4 1300 ±i¤@
5 1800 ¤ý¤G

§Ú­Ì¥i¥H¤U³o­Ó SELECT ¬d¸ß±Ô­z¥y¡G

SELECT Customer, SUM(Price) FROM orders
GROUP BY Customer
HAVING SUM(Price)<1000;

ªð¦^ªºµ²ªG¦p¤U¡G

Customer SUM(Price)
§õ¤T 500

¡@

¡@

ASCII() ¨ç¼Æ (SQL ASCII() Function)

ASCII() ¨ç¼Æ¬O¥Î¨Ó¨ú±o¦r¤¸ªº ASCII ½X¡C¬Û¹ï©ó ASCII() ªº¨ç¼Æ¬° CHAR()¡C

ASCII() »yªk (Syntax)

ASCII(¦r¤¸);

ASCII() ¨ç¼Æ¬d¸ß¥Îªk (Example)

§Ú­Ì³o¼Ë¬d¸ß¡G

SELECT ASCII('a'),   
ASCII('A');

¥i¥H±o¨ìµ²ªG¡G

ASCII('a') ASCII('A')
97 65

¡@

¡@

¡@

CHAR() ¨ç¼Æ (SQL CHAR() Function)

CHAR() ¨ç¼Æ¥i¥H¨Ì ASCII ½X¨ú±o¨ä¬Û¹ïÀ³¤§¦r¤¸¡C¬Û¹ï©ó CHAR() ªº¨ç¼Æ¬° ASCII()¡C

CHAR() »yªk (Syntax)

CHAR(ASCII½X);

CHAR() ¨ç¼Æ¬d¸ß¥Îªk (Example)

§Ú­Ì³o¼Ë¬d¸ß¡G

SELECT CHAR(65),   
CHAR(97);

¥i¥H±o¨ìµ²ªG¡G

CHAR('65') CHAR('97')
A a

¡@

¡@

CONCAT() ¨ç¼Æ (SQL CONCAT() Function)

CONCAT() ¨ç¼Æ¥Î¨Ó¦X¨Ö¦h­ÓÄæ¦ìªº­È¡C

MySQL »yªk - CONCAT(str1, str2,...)

°²³]§Ú­Ì¦³¤@­Ó customers ¸ê®Æªí¡G

C_Id Name
1 Smith
2 Brad

§Ú­Ì³o¼Ë¬d¸ß¡G

SELECT CONCAT(C_Id, '-', Name) FROM customers;

¥i¥H±o¨ìµ²ªG¡G

CONCAT(C_Id, Name)
1-Smith
2-Brad

SQL Server »yªk +

SQL Server ¬O¥Î¥[¸¹ + ¨Ó§@¦X¨Ö¡C

¦P¤W¨Ò¡A§Ú­Ì¥i¥H³o¼Ë¡G

SELECT C_Id + '-' + Name FROM customers;

±o¨ìµ²ªG¡G

CONCAT(C_Id, Name)
1-Smith
2-Brad

Oracle »yªk - CONCAT(), ||

Oracle ¸ò MySQL ¤@¼Ë³£¦³ CONCAT() ³o­Ó¨ç¼Æ¡A¦ý¥¦³Ì¦h¥u¯à¿é¤J¨â­Ó°Ñ¼Æ¡C¦b Oracle ¤¤¦pªG±z·Q¦X¨Ö¦h¸ê®Æ­È¡A¥i¥H¨Ï¥Î ||¡C ¦P¤W¨Ò¡A§Ú­Ì¥i¥H¦p¦¹¡G

SELECT C_Id || '-' || Name FROM customers;

ªð¦^ªºµ²ªG¦p¤U¡G

CONCAT(C_Id, Name)
1-Smith
2-Brad

¡@

¡@

LENGTH()¡BLEN() ¨ç¼Æ (SQL LENGTH()¡BLEN() Function)

LENGTH()¡BLEN() ¨ç¼Æ¬O¥Î¨Ó¨ú±o¦r¦êªø«×¡C

LENGTH()¡BLEN() »yªk (Syntax)

SELECT LENGTH(column_name) FROM table_name;

MySQL ¨ç¼Æ¦WºÙ¬O¥Î LENGTH()¡FOracle¤@¼Ë¥Î LENGTH()¡FSQL Server «h¬O¥Î LEN()¡C

LENGTH()¡BLEN() ¨ç¼Æ¬d¸ß¥Îªk (Example)

¨Ò¦p¡A¤U­±¬O¤@­Ó customers ¸ê®Æªí¡G

C_Id Name
1 Smith
2 Brad

§Ú­Ì¦p¦¹¤U SQL¡G

SELECT LENGTH(Name) FROM customers;

¥i¥H±o¨ì¡G

LENGTH(Name)
5
4

¡@

¡@

REPLACE() ¨ç¼Æ (SQL REPLACE() Function)

REPLACE() ¨ç¼Æ¥Î¨Ó¥H·s¦r¦ê¨ú¥N­ì¦r¦ê¤º®e¡C

REPLACE() »yªk (SQL REPLACE() Syntax)

SELECT REPLACE(str, from_str, to_str) FROM table_name;

¨ç¼Æ·N¸q¬°¡A¦b¦r¦ê str ¤¤¡A±N©Ò¦³¦r¦ê from_str¡A¨ú¥N¬°¦r¦ê to_str¡C

REPLACE() ¨ç¼Æ¬d¸ß¥Îªk (Example)

°²³]§Ú­Ì¦³¤@­Ó customers ¸ê®Æªí¡G

C_Id Name
1 Smith
2 Brad

§Ú­Ì¥i¥H¦p¦¹¡G

SELECT REPLACE(Name, 'Smith', 'ReplaceSmith') FROM customers;

ªð¦^ªºµ²ªG¦p¤U¡G

REPLACE(Name, 'Smith', 'ReplaceSmith')
ReplaceSmith
Brad

¡@

¡@

UCASE() ¨ç¼Æ (SQL UCASE() Function)

UCASE() ¨ç¼Æ¥Î¨Ó±N­^¤å¦r¥ÀÂà´«¬°¤j¼g¡C

UCASE() »yªk (SQL UCASE() Syntax)

SELECT UCASE(column_name) FROM table_name;

UCASE() ¨ç¼Æ¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤¬d¸ßÅU«È©m¦W¨Ã¥H¤j¼gªð¦^µ²ªG¡G

C_Id Name
1 Smith
2 Brad

§Ú­Ì¥i¥H³o¼Ë¼g¡G

SELECT UCASE(Name) FROM customers;

ªð¦^ªºµ²ªG¦p¤U¡G

UCASE(Name)
SMITH
BRAD

¡@

¡@

¡@

LCASE() ¨ç¼Æ (SQL LCASE() Function)

LCASE() ¨ç¼Æ¥Î¨Ó±N­^¤å¦r¥ÀÂà´«¬°¤p¼g¡C

LCASE() »yªk (SQL LCASE() Syntax)

SELECT LCASE(column_name) FROM table_name;

LCASE() ¨ç¼Æ¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤¬d¸ßÅU«È©m¦W¨Ã¥H¤p¼gªð¦^µ²ªG¡G

C_Id Name
1 Smith
2 Brad

§Ú­Ì¥i¥H³o¼Ë¼g¡G

SELECT LCASE(Name) FROM customers;

ªð¦^ªºµ²ªG¦p¤U¡G

LCASE(Name)
smith
brad

¡@

¡@

MID() ¨ç¼Æ (SQL MID() Function)

MID() ¨ç¼Æ¥Î¨ÓºI¨ú¦r¦êÄæ¦ì­È¡C

MID() »yªk (SQL MID() Syntax)

SELECT MID(column_name, start [,length]) FROM table_name;

±q¦r¦ê¯Á¤Þ­È start ¶}©lºI¨ú¦@ length ªø«×ªº¦r¦ê¡C¨S«ü©w length «h¨ú¨ì§À¡C¦Ó start ¬O±q 1 ¶}©l¡C

MID() ¨ç¼Æ¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº customers ¸ê®Æªí¤¤¨ú±oÅU«È©m¦Wªº«e¨â­Ó¦r¡G

C_Id Name
1 Smith
2 Brad

§Ú­Ì¥i¥H³o¼Ë¼g¡G

SELECT MID(Name, 1, 2) FROM customers;

ªð¦^ªºµ²ªG¦p¤U¡G

MID(Name, 1, 2)
Sm
Br

¡@

¡@

ABS() ¨ç¼Æ (SQL ABS() Function)

ABS() ¨ç¼Æ¥Î¨Ó¨ú±oµ´¹ï­È¡C

ABS() »yªk (Syntax)

ABS(¼Æ­È);

ABS() ¨ç¼Æ¥Îªk (Example)

§Ú­Ì¦p¦¹ SQL¡G

SELECT ABS(-20), ABS(20);

±o¨ìµ²ªG¡G

ABS(-20) ABS(20)
20 20

¡@

¡@

¡@

CEIL()¡BCEILING() ¨ç¼Æ (SQL CEIL()¡BCEILING() Function)

CEIL()¡BCEILING() ¨ç¼Æ¬O¥Î¨ÓµL±ø¥ó¶i¤J¼Æ­È¡C

CEIL()¡BCEILING() »yªk (Syntax)

CEIL(¼Æ­È);

MySQL ¨ç¼Æ¦WºÙ¬O¥Î CEIL()¡FOracle ¤@¼Ë¥Î CEIL()¡FSQL Server «h¬O¥Î CEILING()¡C

CEIL()¡BCEILING() ¨ç¼Æ¥Îªk (Example)

§Ú­Ì³o¼Ë¼g¡G

SELECT CEIL(-2.1), CEIL(2.1);

¥i¥H±o¨ìµ²ªG¡G

CEIL(-2.1) CEIL(2.1)
-2 3

¡@

¡@

¡@

FLOOR() ¨ç¼Æ (SQL FLOOR() Function)

FLOOR() ¨ç¼Æ¥Î¨ÓµL±ø¥ó±Ë¥h¼Æ­È¡C

FLOOR() »yªk (Syntax)

FLOOR(¼Æ­È);

FLOOR() ¨ç¼Æ¥Îªk (Example)

§Ú­Ì³o¼Ë SQL¡G

SELECT FLOOR(-10.3), FLOOR(10.3);

¥i¥H±o¨ìµ²ªG¡G

FLOOR(-10.3) FLOOR(10.3)
-11 10

¡@

¡@

POWER() ¨ç¼Æ (SQL POWER() Function)

POWER() ¨ç¼Æ¬O¥Î¨Ó­pºâ¨Ã¨ú±o¼Æ­Èªº N ¦¸¤è­È¡C

POWER() »yªk (Syntax)

POWER(¼Æ­È, N¦¸¤è);

POWER() ¨ç¼Æ¥Îªk (Example)

§Ú­Ì¦p¦¹ SQL¡G

SELECT POWER(10, 2);

¥i¥H±o¨ìµ²ªG¡G

POWER(10, 2)
100

¡@

¡@

¡@

ROUND() ¨ç¼Æ (SQL ROUND() Function)

ROUND() ¨ç¼Æ¥Î¨Ó¹ï¼Æ­ÈÄæ¦ì­È¶i¦æ¥|±Ë¤­¤J­pºâ¡C

ROUND() »yªk (SQL ROUND() Syntax)

SELECT ROUND(column_name, decimals) FROM table_name;

decimals ¥Î¨Ó³]©w­n¥|±Ë¤­¤J¨ì¤p¼ÆÂI²Ä´X¦ì¡A0 ªí¥Ü­Ó¦ì¼Æ¡C

ROUND() ¨ç¼Æ¥Îªk (Example)

°²³]§Ú­Ì·Q±q¤U­±ªº orders ¸ê®Æªí¤¤¬d¸ß¦Uµ§­q³æªºª÷ÃB¡A¨Ã¥|±Ë¤­¤J¥h°£¤p¼ÆÂI¦ì¼Æ¡G

O_Id Price
1 1000.45
2 2000.14
3 500.68

§Ú­Ì¥i¥H¤U³o­Ó SELECT ¬d¸ß±Ô­z¥y¡G

SELECT ROUND(Price, 0) FROM orders;

ªð¦^ªºµ²ªG¦p¤U¡G

ROUND(Price, 0)
1000
2000
501

¡@

¡@

SQRT() ¨ç¼Æ (SQL SQRT() Function)

SQRT() ¨ç¼Æ¬O¥Î¨Ó­pºâ¨Ã¨ú±o¼Æ­Èªº¥­¤è®Ú¡C

SQRT() »yªk (Syntax)

SQRT(¼Æ­È);

SQRT() ¨ç¼Æ¥Îªk (Example)

§Ú­Ì³o¼Ë¤U SQL¡G

SELECT SQRT(100);

¥i¥H±o¨ìµ²ªG¡G

SQRT(100)
10

¡@

¡@

PI() ¨ç¼Æ (SQL PI() Function)

PI() ¨ç¼Æ¬O¥Î¨Ó¨ú±o¶ê©P²v¡C

PI() »yªk (Syntax)

PI();

PI() ¨ç¼Æ¥Îªk (Example)

§Ú­Ì³o¼Ë¤U SQL¡G

SELECT PI();

¥i¥H±o¨ì¡G

PI()
3.141593

¡@

¡@

EXP() ¨ç¼Æ (SQL EXP() Function)

EXP() ¨ç¼Æ¥i¥H¥Î¨Ó¨D±o¥H¦ÛµM¹ï¼Æ e ¬°°ò©³ªº«ü¼Æ­È¡C

EXP() »yªk (Syntax)

EXP(¦¸¤è);

EXP() ¨ç¼Æ¥Îªk (Example)

§Ú­Ì¦p¦¹ SQL¡G

SELECT EXP(1), EXP(10);

¥i¥H±o¨ì¡G

EXP(1) EXP(10)
2.718281828459 22026.465794807

¡@

¡@

¡@

LOG() ¨ç¼Æ (SQL LOG() Function)

LOG() ¨ç¼Æ¥i¥H¥Î¨Ó§@¹ï¼Æ¹Bºâ¡C

LOG() »yªk (Syntax)

LOG(°ò©³, ¼Æ­È);

¥t¥~¡AÁÙ¦³³o¨ÇÃþ¦üªº¨ç¼Æ¥i¥H¥Î¡G

LN(¼Æ­È);    -- ¥H¦ÛµM¹ï¼Æ e ¬°©³¼Æ
LOG2(¼Æ­È);  -- ¥H 2 ¬°©³¼Æ
LOG10(¼Æ­È); -- ¥H 10 ¬°©³¼Æ

LOG() ¨ç¼Æ¥Îªk (Example)

§Ú­Ì¦p¦¹ SQL¡G

SELECT LOG(10, 100),
LOG2(4),   
LOG10(1000),   
LN(1);

¥i¥H±o¨ì¡G

LOG(10, 100) LOG2(4) LOG10(1000) LN(1)
2 2 3 0

¡@

¡@

¤T¨¤¨ç¼Æ (SQL Trigonometric Function)

SQL ¤º«Ø³\¦h¨ç¼Æ¨ÓÀ°§U±z¶i¦æ¤T¨¤¹Bºâ¡C

SIN() & ASIN() »yªk (Syntax)

SIN(©·«×); -- ­pºâ¥¿©¶­È
ASIN(¥¿©¶­È); -- ­pºâ¤Ï¥¿©¶­È (¥H©·«×ªí¥Ü)

§Ú­Ì³o¼Ë¤U SQL¡G

SELECT SIN(1.2), ASIN(-0.6);

¥i¥H±o¨ì¡G

SIN(1.2) ASIN(-0.6)
0.93203908596723 -0.64350110879328

COS() & ACOS() »yªk (Syntax)

COS(©·«×); -- ­pºâ¾l©¶­È
ACOS(¾l©¶­È); -- ­pºâ¤Ï¾l©¶­È (¥H©·«×ªí¥Ü)

§Ú­Ì¦p¦¹ SQL¡G

SELECT COS(1.2), ACOS(-0.6);

¥i¥H±o¨ì¡G

COS(1.2) ACOS(-0.6)
0.36235775447667 2.2142974355882

TAN() & ATAN() & COT() »yªk (Syntax)

TAN(©·«×); -- ­pºâ¥¿¤Á­È
ATAN(¥¿¤Á­È); -- ­pºâ¤Ï¥¿¤Á­È (¥H©·«×ªí¥Ü)
COT(©·«×); -- ­pºâ¾l¤Á­È

§Ú­Ì³o¼Ë¤U SQL¡G

SELECT TAN(1.2), ATAN(-0.6), COT(1);

¥i¥H±o¨ì¡G

TAN(1.2) ATAN(-0.6) cot(1)
2.5721516221263 -0.54041950027058 0.64209261593433

DEGREE() & RADIANS() »yªk (Syntax)

DEGREE(©·«×); -- ±N©·«×Âର¨¤«×
RADIANS(¨¤«×); -- ±N¨¤«×Âର©·«×

§Ú­Ì³o¼Ë¤U SQL¡G

SELECT RADIANS(180), DEGREES(PI());

¥i¥H±o¨ì¡G

RADIANS(180) DEGREES(PI())
3.1415926535898 180

¡@

¡@

NOW() ¨ç¼Æ (SQL NOW() Function)

NOW() ¨ç¼Æ¥Î¨Ó¨ú±o¥Ø«eªº¤é´Á®É¶¡¡C

NOW() »yªk (SQL NOW() Syntax)

SELECT NOW() FROM table_name;

NOW() ¨ç¼Æ¥Îªk (Example)

§Ú­Ì¤U³o­Ó SQL ¬d¸ß¡G

SELECT NOW();

¥i¥H±o¨ìµ²ªG¡G

Now()
2016-06-08 12:32:53

¡@

¡@

¡@

¡@

¡@

¡@

¸ê®Æ±±¨î DCL

SQL CREATE USER «Ø¥ß¸ê®Æ®w¨Ï¥ÎªÌ±b¸¹

­n³s½u¨Ãµn¤J¨ì¸ê®Æ®w¡A§Ú­Ì¥²»Ý­n¦³¤@²Õ±b¸¹©M±K½X¡C

«Ø¥ß¸ê®Æ®w¨Ï¥ÎªÌ»yªk (Syntax)

MySQL

CREATE USER 'username'@'hostname' IDENTIFIED BY '±K½X';

hostname ªí¥Ü¤¹³\³o±b¸¹¯à±q¤°»ò¦a¤è³s½u¨ì¸ê®Æ®w¡Clocalhost ªí¥Ü¥u¤¹³\±q¥»¦aºÝµn¤J¡F% ¬O¸U¥Î¦r¤¸¡Aªí¥Ü¤¹³\±q¥ô¦ó¦a¤èµn¤J¡C

¨Ò¦p¡G

CREATE USER 'mike'@'%' IDENTIFIED BY 'hello123';

«Ø¥ß·s±b¸¹ mike ¨Ã³]©w¨ä±K½X¬° hello123¡A¥B¤¹³\±q¥ô¦ó¦a¤èµn¤J (±q¥»¦aºÝ©Î»·ºÝ³s½u³£¥i)¡C

ª`·N¡I«Ø¥ß·s±b¸¹«á¡A³o±b¸¹¹w³]¨S¦³Åv­­¥i¥H¹ï¸ê®Æ®w°µ¥ô¦ó¨Æ¡A±µµÛ§A¥²¶·±Â»P¸ê®Æ®w¨Ï¥ÎÅv­­µ¹³o±b¸¹¡C

¡@

¡@

SQL DROP USER §R°£¸ê®Æ®w¨Ï¥ÎªÌ±b¸¹

¦pªG§A·Q§R°£¤@­Ó¨Ï¥ÎªÌ±b¸¹¡A¸T¤î¦A¥Î³o±b¸¹µn¤J¨ì¸ê®Æ®w¡C

»yªk (Syntax)

MySQL

DROP USER 'username'@'hostname';

¥Îªk¨Ò¦p¡G

DROP USER 'mike'@'%';

¡@

¡@

SQL GRANT ±Â»P¸ê®Æ®w¨Ï¥ÎÅv­­

«Ø¥ß¤@­Ó·s±b¸¹«á¡A§A­n±Â»P¸ê®Æ®w¨Ï¥ÎÅv­­µ¹³o¦ì¨Ï¥ÎªÌ¡A³o±b¸¹¤~¯à¶}©l³s½u¶i¥h¸ê®Æ®w¾Þ§@¡C

GRANT »yªk (Syntax)

MySQL

GRANT type_of_permission ON database_name.table_name TO 'username'@'hostname';

hostname ªí¥Ü¤¹³\³o±b¸¹¯à±q¤°»ò¦a¤è³s½u¨ì¸ê®Æ®w¡Clocalhost ªí¥Ü¥u¤¹³\±q¥»¦aºÝµn¤J¡F% ¬O¸U¥Î¦r¤¸¡Aªí¥Ü¤¹³\±q¥ô¦ó¦a¤èµn¤J¡C

¨Ò¦p¡A±Â»P mike ©Ò¦³¸ê®Æ®w©M©Ò¦³¸ê®Æªíªº©Ò¦³¾Þ§@Åv­­¡G

GRANT ALL PRIVILEGES ON *.* TO 'mike'@'%';

¥Î¤W­±«ü¥O±Â»PÅv­­«á¡A­n°O±o¤U³o­Ó«ü¥OÅýÅv­­¶}©l¥Í®Ä¡G

FLUSH PRIVILEGES;

MySQL ¦³³o¨Ç±`¨£ªºÅv­­Ãþ«¬¡G

§A¥i¥H¥Î³rÂI¤À¹j¦h­ÓÅv­­¡G

GRANT SELECT,INSERT ON customers.* TO 'mike'@'%';

¡@

¡@

¡@

SQL REVOKE ºM¾P¸ê®Æ®w¨Ï¥ÎÅv­­

§AÀH®É¥i¥HºM¾P¨Ï¥ÎªÌªº¸ê®Æ®w¨Ï¥ÎÅv­­¡C

REVOKE »yªk (Syntax)

MySQL

REVOKE type_of_permission ON database_name.table_name FROM 'username'@'hostname';

¥Îªk¨Ò¦p¡G

REVOKE ALL PRIVILEGES FROM 'mike'@'%';

¥Î¤W­±«ü¥OºM¾PÅv­­«á¡A­n°O±o¤U³o­Ó«ü¥OÅýºM¾PÅv­­¶}©l¥Í®Ä¡G

FLUSH PRIVILEGES;

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

§Ú­Ì¨Ã¤¶²Ð¦p¦ó¥Î SQL ¨Ó°µ¥X¥H¤Uªº¹Bºâ¡G

±Æ¦W (Rank)
¤¤¦ì¼Æ (Median)
²Ö¿nÁ`­p (Running Total)
Á`¦X¦Ê¤À¤ñ (Percent to Total)
²Ö¿nÁ`¦X¦Ê¤À¤ñ (Cumulative Percent to Total)

¡@

±Æ¦W (Rank)

¦C¥X¨C¤@¦æªº±Æ¦W¬O¤@­Ó±`¨£ªº»Ý¨D¡A¥i±¤ SQL ¨Ã¨S¦³¤@­Ó«Üª½±µªº¤è¦¡¹F¨ì³o­Ó»Ý¨D¡C­n¥H SQL ¦C¥X±Æ¦W¡A°ò¥»ªº·§©À¬O­n°µ¤@­Óªí®æ¦Û§Ú³sµ² (Self Join)¡A±Nµ²ªG¨Ì§Ç¦C¥X¡AµM«áºâ¥X¨C¤@¦æ¤§«e (¥]§t¨º¤@¦æ¥»¨­) ¦³¦h¤Ö¦æ¼Æ¡C³o¼ËÁ¿ÅªªÌÅ¥±o¥i¯à¦³ÂI§x´b¡A©Ò¥H³Ì¦nªº¤è¦¡¬O¥Î¤@­Ó¹ê¨Ò¨Ó¤¶²Ð¡C°²³]§Ú­Ì¦³¥H¤Uªºªí®æ¡G

Total_Sales ªí®æ
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20

­n§ä¥X¨C¤@¦æªº±Æ¦W¡A§Ú­Ì´N¥´¤J¥H¤Uªº SQL »y¥y¡G

¡@

SELECT a1.Name, a1.Sales, COUNT(a2.Sales) Sales_Rank
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.Sales OR (a1.Sales=a2.Sales AND a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;

µ²ªG:

¡@

Name Sales Sales_Rank
Greg 50 1
Sophia 40 2
Stella 20 3
Jeff 20 3
Jennifer 15 5
John 10 6

§Ú­Ì¥ý¨Ó¬Ý WHERE ¤l¥y¡C¦b¦r¥yªº²Ä¤@³¡¤À (a1.Sales <= a2.Sales)¡A§Ú­Ìºâ¥X¦³¦h¤Öµ§¸ê®Æ Sales Äæ¦ìªº­È¬O¤ñ¦Û¤v¥»¨­ªº­È¤p©Î¬O¬Ûµ¥¡C¦pªG¦b Sales Äæ¦ì¤¤¨S¦³¦P¼Ë¤j¤pªº¸ê®Æ¡A¨º³o³¡¤Àªº WHERE ¤l¥y¥»¨­´N¥i¥H²£¥Í¥X¥¿½Tªº±Æ¦W¡C

¤l¥yªº²Ä¤G³¡¤À¡A(a1.Sales=a2.Sales AND a1.Name = a2.Name)¡A«h¬OÅý§Ú­Ì¦b Sales Äæ¦ì¤¤¦³¦P¼Ë¤j¤pªº¸ê®Æ®É (¹³ Stella ¤Î Jeff ³o¨âµ§¸ê®Æ)¡A¤´µM¯à°÷²£¥Í¥¿½Tªº±Æ¦W¡C

¡@

¡@

¡@

¤¤¦ì¼Æ (Median)

­nºâ¥X¤¤¦ì¼Æ¡A§Ú­Ì¥²¶·­n¯à°÷¹F¦¨¥H¤U´X­Ó¥Ø¼Ð¡G

¨Ó¬Ý¬Ý¥H¤Uªº¨Ò¤l¡C°²³]§Ú­Ì¦³¥H¤Uªºªí®æ¡G

Total_Sales ªí®æ
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20

­n§ä¥X¤¤¦ì¼Æ¡A§Ú­Ì´NÁä¤J¡G

¡@

SELECT Sales Median FROM
(SELECT a1.Name, a1.Sales, COUNT(a1.Sales) Rank
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales < a2.Sales OR (a1.Sales=a2.Sales AND a1.Name <= a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales desc) a3
WHERE Rank = (SELECT (COUNT(*)+1) DIV 2 FROM Total_Sales);

µ²ªG:

¡@

Median
20

ŪªÌ±N·|µo²{¡A²Ä 2 ¦æ¨ì²Ä 6 ¦æ¬O¸ò²£¥Í ±Æ¦W ªº»y¥y§¹¥þ¤@¼Ë¡C²Ä 7 ¦æ«h¬Oºâ¥X¤¤¶¡ªº±Æ¦W¡CDIV ¬O¦b MySQL ¤¤ºâ¥X°Óªº¤è¦¡¡C¦b¤£¦Pªº¸ê®Æ®w¤¤·|¦³¤£¦Pªº¤è¦¡¨D°Ó¡C²Ä 1 ¦æ«h¬O¦C¥X±Æ¦W¤¤¶¡ªº¸ê®Æ­È¡C

¡@

¡@

²Ö¿nÁ`­p (Running Total)

ºâ¥X²Ö¿nÁ`­p¬O¤@­Ó±`¨£ªº»Ý¨D¡A¥i±¤¥H SQL ¨Ã¨S¦³¤@­Ó«Üª½±µªº¤è¦¡¹F¨ì³o­Ó»Ý¨D¡C­n¥H SQL ºâ¥X²Ö¿nÁ`­p¡A°ò¥»¤Wªº·§©À»P¦C¥X±Æ¦WÃþ¦ü¡G²Ä¤@¬O¥ý°µ­Óªí®æ¦Û§Ú³sµ² (Self Join)¡AµM«á±Nµ²ªG¨Ì§Ç¦C¥X¡C¦b°µ¦C¥X±Æ¦W®É¡A§Ú­Ìºâ¥X¨C¤@¦æ¤§«e (¥]§t¨º¤@¦æ¥»¨­) ¦³¦h¤Ö¦æ¼Æ¡F¦Ó¦b°µ²Ö¿nÁ`­p®É¡A§Ú­Ì«h¬Oºâ¥X¨C¤@¦æ¤§«e (¥]§t¨º¤@¦æ¥»¨­) ªºÁ`¦X¡C

¨Ó¬Ý¬Ý¥H¤Uªº¨Ò¤l¡C°²³]§Ú­Ì¦³¥H¤Uªºªí®æ¡G

Total_Sales ªí®æ
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20

­nºâ¥X²Ö¿nÁ`­p¡A§Ú­Ì´NÁä¤J¥H¤Uªº SQL »y¥y¡G

¡@

SELECT a1.Name, a1.Sales, SUM(a2.Sales) Running_Total
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.Sales OR (a1.Sales=a2.Sales AND a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;

µ²ªG:

¡@

Name Sales Running_Total
Greg 50 50
Sophia 40 90
Stella 20 110
Jeff 20 130
Jennifer 15 145
John 10 155

¦b¥H¤Wªº SQL »y¥y¤¤¡A WHERE ¤l¥y©M ORDER BY ¤l¥yÅý§Ú­Ì¯à°÷¦b¦³­«½Æ­È®É¯à°÷ºâ¥X¥¿½Tªº²Ö¿nÁ`­p¡C

¡@

¡@

¡@

¡@

Á`¦X¦Ê¤À¤ñ (Percent to Total)

­n¥Î SQL ºâ¥XÁ`¦X¦Ê¤À¤ñ¡A§Ú­Ì»Ý­n¥Î¨ìºâ±Æ¦W©M²Ö¿nÁ`­pªº·§©À¡A¥H¤Î¹B¥Î¤l¬d¸ßªº°µªk¡C¦b³o¸Ì¡A§Ú­Ì§â¤l¬d¸ß©ñ¦b¥~³¡¬d¸ßªº SELECT ¤l¥y¤¤¡CÅý§Ú­Ì¨Ó¬Ý¥H¤Uªº¨Ò¤l¡G

Total_Sales ªí®æ
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20

­nºâ¥XÁ`¦X¦Ê¤À¤ñ¡A§Ú­ÌÁä¤J¥H¤Uªº SQL »y¥y¡G

¡@

SELECT a1.Name, a1.Sales, a1.Sales/(SELECT SUM(Sales) FROM Total_Sales) Pct_To_Total
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.Sales OR (a1.Sales=a2.Sales AND a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;

µ²ªG:

¡@

Name Sales Pct_To_Total
Greg 50 0.3226
Sophia 40 0.2581
Stella 20 0.1290
Jeff 20 0.1290
Jennifer 15 0.0968
John 10 0.0645

SELECT SUM(Sales) FROM Total_Sales ³o¤@¬q¤l¬d¸ß¬O¥Î¨Óºâ¥XÁ`¦X¡CÁ`¦Xºâ¥X«á¡A§Ú­Ì´N¯à°÷±N¨C¤@¦æ¤@¤@°£¥HÁ`¦X¨Ó¨D¥X¨C¤@¦æªºÁ`¦X¦Ê¤À¤ñ¡C

¡@

¡@

¡@

¡@

¿nÁ`¦X¦Ê¤À¤ñ (Cumulative Percent to Total)

­n¥Î SQL ²Ö¿nÁ`¦X¦Ê¤À¤ñºâ¥X¡A§Ú­Ì¹B¥ÎÃþ¦üÁ`¦X¦Ê¤À¤ñªº·§©À¡C¨âªÌªº¤£¦P³B¦b©ó¦b³o­Ó±¡ªp¤U¡A§Ú­Ì­nºâ¥X¨ì¥Ø«e¬°¤îªº²Ö¿nÁ`¦X¬O©Ò¦³Á`¦Xªº¦Ê¤À¤§´X¡A¦Ó¤£¬O¥ú¬Ý¨C¤@µ§¸ê®Æ¬O©Ò¦³Á`¦Xªº¦Ê¤À¤§´X¡CÅý§Ú­Ì¨Ó¬Ý¬Ý¥H¤Uªº¨Ò¤l¡G

Total_Sales ªí®æ
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20

­nºâ¥X²Ö¿nÁ`¦X¦Ê¤À¤ñ¡A§Ú­ÌÁä¤J¡G

¡@

SELECT a1.Name, a1.Sales, SUM(a2.Sales)/(SELECT SUM(Sales) FROM Total_Sales) Pct_To_Total
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales <= a2.Sales or (a1.Sales=a2.Sales and a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;

µ²ªG:

¡@

Name Sales Pct_To_Total
Greg 50 0.3226
Sophia 40 0.5806
Stella 20 0.7097
Jeff 20 0.8387
Jennifer 15 0.9355
John 10 1.0000

SELECT SUM(Sales) FROM Total_Sales ³o¤@¬q¤l¬d¸ß¬O¥Î¨Óºâ¥XÁ`¦X¡C§Ú­Ì±µ¤U¨Ó¥Î²Ö¿nÁ`­p SUM(a2.Sales) °£¥HÁ`¦X¨Ó¨D¥X¨C¤@¦æªº²Ö¿nÁ`¦X¦Ê¤À¤ñ¡C

¡@

¡@

¡@

MySQL »yªk¶×¾ã

±µÄ² MySQL ¦h¦~ªº­á¤¯¤@ª½¥H¨Ó³£¨S¦³¦n¦nªº«ü¥O°O§¹¡A¤@¯ë³£·|¨Ï¥Î phpMyAdmin ³oÃþªº GUI ¨Ó¾Þ§@ MySQL¡A©Ò¥H§â±`¥Îªº select, insert, update, drop ­I¼ô´N°½¯º¤F¡I¥i¥u¦³³o¨Ç¹ï­á¤¯¦Ó¨¥¬O¤£°÷ªº¡A·í GUI ¥¢ÆF®ÉÁÙ¬O±o¨Ì¿à«ü¥O¨Ó§¹¦¨¥ô°È¡A¦AÁÙ¥¼ºë³q«e´NÅý­á¤¯¦b¦Û®a°ò¦a©ñ¥»¤p§Û¤F¡C

1. °ò¦»yªk

¾Þ§@¥\¯à SQL »yªk
»¡©ú
«Ø¥ß¸ê®Æ®w create database ¸ê®Æ®w¦WºÙ;
¦C¥X©Ò¦³¸ê®Æ®w show databases;
§R°£¸ê®Æ®w drop database ¸ê®Æ®w¦WºÙ;
¨Ï¥Î¸ê®Æ®w use ¸ê®Æ®w¦WºÙ;
«Ø¥ß¸ê®Æªí create table ¸ê®Æªí¦WºÙ(
sn integer auto_increment primary key,
name char(20),
mail char(50),
home char(50),
messages char(50)
);
±`¥Î¸ê®Æ®w¸ê®Æ«¬ºA
1. INT (¾ã¼Æ)
2. CHAR (1~255¦r¤¸¦r¦ê)
3. VARCHAR (¤£¶W¹L255¦r¤¸¤£©wªø«×¦r¦ê)
4. TEXT (¤£©wªø«×¦r¦ê³Ì¦h65535¦r¤¸)

¦C¥X¸ê®ÆªíÄæ¦ì¸ê°T describe ¸ê®Æªí¦WºÙ;
­×§ï¸ê®ÆªíÄæ¦ì alter table ¸ê®Æªí¦WºÙ
change column ­ì¨ÓÄæ¦ì¦WºÙ
·sÄæ¦ì¦WºÙ¸ê®Æ«¬ºA;
·s¼W¸ê®ÆªíÄæ¦ì alter table ¸ê®Æªí¦WºÙ add column Äæ¦ì¦WºÙ ¸ê®Æ«¬ºA;
§R°£¸ê®ÆªíÄæ¦ì alter table ¸ê®Æªí¦WºÙ drop column Äæ¦ì¦WºÙ;
§R°£¸ê®Æªí drop table ¸ê®Æªí¦WºÙ;
²MªÅ¸ê®Æªí truncate table ¸ê®Æªí¦WºÙ; ¥u²M°£¸ê®Æ¨Ã«O¯dµ²ºc¡BÄæ¦ì¡B¯Á¤Þ ¡K
´¡¤JÄæ¦ì¸ê®Æ insert into ¸ê®Æªí¦WºÙ(Äæ¦ì1,Äæ¦ì2,Äæ¦ì3,Äæ¦ì4, ...... Äæ¦ìN)
values('­È1','­È2','­È3','­È4', ...... '­ÈN');
§ó·s­×§ïÄæ¦ì¸ê®Æ update ¸ê®Æªí¦WºÙ set Äæ¦ì1='­È1',Äæ¦ì2='­È2',Äæ¦ì3='­È3',... Äæ¦ìN='­ÈN'
where ±ø¥ó¦¡ (¨Ò¦p sn='5' ©Î name='¶ð¥q¥§' );
¬d¸ß³æ¤@Äæ¦ì¸ê®Æ select Äæ¦ì¦W from ¸ê®Æªí¦WºÙ;
¬d¸ß¦h­ÓÄæ¦ì¸ê®Æ select Äæ¦ì¦W, Äæ¦ì¦W, Äæ¦ì¦W from ¸ê®Æªí¦WºÙ;
¬d¸ßÄæ¦ì¸ê®Æªº°ß¤@­È select distinct Äæ¦ì¦W from ¸ê®Æªí¦WºÙ; ­«½Æ­È¥u¦C¤@¦¸
¬d¸ß©Ò¦³Äæ¦ì¸ê®Æ select * from ¸ê®Æªí¦WºÙ;
±ø¥ó¦¡¬d¸ß select * from ¸ê®Æªí¦WºÙ where ±ø¥ó¦¡ (¨Ò¦p sn='5'); ¡]=, <, >, !=¡^
±ø¥ó¦¡¬d¸ß and select * from ¸ê®Æªí¦WºÙ where ±ø¥ó¦¡1 and ±ø¥ó¦¡2;
±ø¥ó¦¡¬d¸ß or select * from ¸ê®Æªí¦WºÙ where ±ø¥ó¦¡1 or ±ø¥ó¦¡2;
¬d¸ß¬Y¤@½d³ò between select * from ¸ê®Æªí¦WºÙ where Äæ¦ì¦W between ­È1 and ­È2; ­È¬°¼Æ¦r
¬d¸ßªÅ­ÈÄæ¦ìªº¸ê®Æ select * from ¸ê®Æªí¦WºÙ where Äæ¦ì¦W is null not null;
¬d¸ß¯S©wµ§¼Æ¸ê®Æ select * from ¸ê®Æªí¦WºÙ limit 8, 10; ²Ä9µ§¶}©l¿ï¨ú10µ§
¬d¸ßµ²ªG»¼¼W±Æ§Ç select * from ¸ê®Æªí¦WºÙ order by Äæ¦ì¦W;
¬d¸ßµ²ªG»¼´î±Æ§Ç select * from ¸ê®Æªí¦WºÙ order by Äæ¦ì¦W desc ;
¬d¸ß¤ñ¹ï¦r¦ê¦C¥X³æ¤@Äæ¦ì select Äæ¦ì¦W from ¸ê®Æªí¦WºÙ where Äæ¦ì¦W like '%¦r¦ê%';
¬d¸ß¤ñ¹ï¦r¦ê¦C¥X©Ò¦³Äæ¦ì select * from ¸ê®Æªí¦WºÙ where Äæ¦ì¦W like '%¦r¦ê%';
§R°£±ø¥ó­È¸ê®Æ delete from ¸ê®Æªí¦WºÙ where ±ø¥ó¦¡ (¨Ò¦p sn='5' ©Î id='91001' );
§R°£±ø¥ó­È¸ê®Æ delete from ¸ê®Æªí¦WºÙ where ±ø¥ó¦¡1
and
±ø¥ó¦¡2;
§R°£±ø¥ó­È¸ê®Æ delete from ¸ê®Æªí¦WºÙ where ±ø¥ó¦¡1 or ±ø¥ó¦¡2;
¤ñ¹ï§R°£±ø¥ó­È¸ê®Æ delete from ¸ê®Æªí¦WºÙ where Äæ¦ì¦W like '%¦r¦ê%';

2. ¶i¶¥»yªk

¾Þ§@¥\¯à SQL »yªk
»¡©ú
¬d¬Ý¥¿¦b°õ¦æªº¦æµ{ show processlist;
¬d¬Ý Master ª¬ºA show master status;
¬d¬Ý Slave ª¬ºA show slave status\G;
¬d¬Ý binlog ª¬ºA show binary logs;
²M°£¹L®Éªº binlog purge binary logs to 'mysql-bin.000006'; ±N§R°£ mysql-bin.000001 ~ 5 ªº binlog

3. mysqldump ¡Ð MySQL ¶×¥X»yªk

mysqldump ¬O­Ó¶×¥X Database (¸ê®Æ®w) ªº«ü¥O¡A¤]¬O IT ¤H­û³Ì±`¥Î¨ìªº MySQL ¸ê®Æ®w¶×¥X¤èªk¤§¤@¡C
[ jonny@trusty ~ ]
$ mysqldump -u root -p ¸ê®Æ®w¦WºÙ > database_name.sql [Enter]

¯¸¤º³sµ²¡G
¡¹ MySQL ¤j¾Çµ§°O on Windows XP

¬ÛÃö³sµ²¡G
¡¹ §R°£¾ã­Ó¸ê®Æªí¡A¨Ï¥Î Delete, Truncate »P Drop ªº®t²§ @ ¤ë¯«ªº©@°ØÀ]
¡¹ How to setup MySQL Master/Slave Replication with existing data ¡X The WP Guru
¡¹ MySQL 5.1 Reference Manual

¸ê®Æ¨Ó·½¡G
¡¹ MySQL °ò¦»yªk¤â¥U | InspireGate ¬£§JªÅ¶¡
¡¹ MySQL 5.1 mysqldump ¾É¥X³Æ¥÷ | IT «È
¡¹ Jax ªº¤u§@¬ö¿ý: (Âà¸ü)mysqldump 5.1 ¸ê®Æ³Æ¥÷¸Ô²Ó«ü¥O [MySQL]
¡¹ mysqldump ³Æ¥÷ÁÙ­ì©M¾É¤J¶×¥X»y¥y¤j¥þ¸Ô¸Ñ @ ¸ê°T¶é

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@

¡@