ªð¦^¦Cªí ¤W¤@¥DÃD µo©«

9-2-2 «Ø¥ß»P§R°£¸ê®Æªí

9-2-2 «Ø¥ß»P§R°£¸ê®Æªí

9-2-2   «Ø¥ß»P§R°£¸ê®Æªí
¦pªG§Ú­Ì­n§Q¥Î«eºÝªºPHPµ{¦¡«Ø¥ß¸ê®Æ®w¤¤ªº¸ê®Æªí¡A«h¥i¨Ï¥ÎDDL«ü¥O¸s¤¤ªº¡uCreate Table¡v«ü¥O¡A«ü¥O®æ¦¡¦p¤U¡G
«Ø¥ß¸ê®Æªí
  1. CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name [(create_definition,...)]
  2. [table_options] [select_statement]
½Æ»s¥N½X
create_definition°Ñ¼Æ¥i¬°¡G

  • col_name type [NOT NULL | NULL] [DEFAULT default_value] [AUTO_INCREMENT]
  • [PRIMARY KEY] [reference_definition]
  • PRIMARY KEY (index_col_name,...)
  • KEY [index_name] (index_col_name,...)
  • INDEX [index_name] (index_col_name,...)
  • UNIQUE [INDEX] [index_name] (index_col_name,...)
  • [CONSTRAINT symbol] FOREIGN KEY index_name (index_col_name,...) [reference_definition]
  • CHECK (expr)

Äæ¦ì¸ê®Æ«¬ºA¥i¬°¡G

  • TINYINT[(length)] [UNSIGNED] [ZEROFILL]
  • SMALLINT[(length)] [UNSIGNED] [ZEROFILL]
  • MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL]
  • INT[(length)] [UNSIGNED] [ZEROFILL]
  • INTEGER[(length)] [UNSIGNED] [ZEROFILL]
  • BIGINT[(length)] [UNSIGNED] [ZEROFILL]
  • REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
  • DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]
  • FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]
  • DECIMAL(length,decimals) [UNSIGNED] [ZEROFILL]
  • NUMERIC(length,decimals) [UNSIGNED] [ZEROFILL]
  • CHAR(length) [BINARY]
  • VARCHAR(length) [BINARY]
  • DATE¡BTIME¡BTIMESTAMP¡BDATETIME
  • TINYBLOB¡BBLOB¡BMEDIUMBLOB¡BLONGBLOB
  • TINYTEXT¡BTEXT¡BMEDIUMTEXT¡BLONGTEXT
  • ENUM(value1,value2,value3,...)¡BSET(value1,value2,value3,...)

table_options°Ñ¼Æ¥i¬°¡G

  • TYPE = {ISAM | MYISAM | HEAP}
  • AUTO_INCREMENT = #
  • AVG_ROW_LENGTH = #
  • CHECKSUM = {0 | 1}
  • COMMENT = "string"
  • MAX_ROWS = #
  • MIN_ROWS = #
  • PACK_KEYS = {0 | 1}
  • PASSWORD = "string"
  • DELAY_KEY_WRITE = {0 | 1}
  • ROW_FORMAT= { default | dynamic | static | compressed }

select_statement°Ñ¼Æ¥i¬°¡G

  • [IGNORE | REPLACE] SELECT ...  (Some legal select statement)
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

¬Ý¤£À´¡H¨SÃö«Y¡Aµ§ªÌ§â¥¦¥Õ¸ÜªºÂ²³æ»¡©ú¦p¤Uªº®æ¦¡¡G
«Ø¥ß¸ê®Æªí
  1. CREATE TABLE
  2.     ­n·s¼Wªº¸ê®Æªí¦WºÙ
  3.        (
  4.         Äæ¦ì¦W1 ¸ê®Æ«¬ºA Default ¹w³]­È ¬O§_¤¹³\NULL,
  5. Äæ¦ì¦W2 ¸ê®Æ«¬ºA Default ¹w³]­È ¬O§_¤¹³\NULL,
  6. ¡K
  7.     )
½Æ»s¥N½X
¨Ò¦p¡G
  1. Create Table
  2. PHP´ú¸Õ¸ê®Æªí
  3. (
  4. PersonID char(10) Not NULL,
  5. Name varChar(10),_
  6. Email varChar(100) Default 'µL' Not NULL
  7. )
½Æ»s¥N½X
µ§ªÌ®³¤@¥yÄæ¦ì©w¸q±Ô­z¨Ó»¡©ú¡G
  1. Email varChar(100) Default 'µL' Not NULL_
½Æ»s¥N½X

  • Email¡iÄæ¦ì¦WºÙ¡j¡G«Ø¥ß¸ê®Æªí¤¤ªº¬Y­ÓÄæ¦ì©ïÀY¡C
  • varChar(100)¡i¸ê®Æ«¬ºA¡j¡G©w¸q¦s©ñ©óÄæ¦ì¤º¸ê®Æªº¸ê®Æ«¬ºA»P¦s©ñ¸ê®Æªºªø«×¡C
  • Default ¡¥µL¡¦¡iÄæ¦ì¹w³]­È¡j¡G·í·s¼W¸ê®Æ¿ý®É¡A­Y¸ÓÄæ¦ì¨S¦³¸ê®Æ¿é¤J¡A«h¥H¡uDefault¡vÃöÁä«áªº¸ê®Æ­È¶ñ¤JÄæ¦ì¤º¡C
  • Not NULL¡i¬O§_¤¹³\µL­È¿é¤J¡j¡G­Y©óÄæ¦ì©w¸q¤¤¥[¤J¡uNot NULL¡v¡A«h·s¼W¸ê®Æ¿ý®É¡A¸ÓÄæ¦ì¤@©w­n¿é¤J¡C
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

¹ê°µ¾Ç²ß¡G
©ó½d¨Òex09_10©Ò«Ø¥ßªº¡uPHP´ú¸Õ¡v¸ê®Æ®w¤¤·s¼W¤@­Ó¦W¬°¡uPHPTable¡vªº¸ê®Æªí¡A¸ê®Æªí¤¤ªºÄæ¦ì¦p¤Uªí©Ò¥Ü¡G

¦WºÙ

¸ê®Æ«¬ºA

¸ê®Æªø«×

¬O§_¤¹³\µL­È

¹w³]­È

PersonID

Char

10

§_

Name

varChar

10

¬O

¡¥n/a¡¦

Email

varchar

100

§_



ª`·N¡G¥²¶·¥ý«ü©ú¹w³]ªº¾Þ§@¸ê®Æ®w¬°¡uPHP´ú¸Õ¡v¡A·í§Ú­Ì¥H¡yCreate Table¡z«ü¥O«Ø¥ß·s¸ê®Æªí®É¡A³o·s¼Wªº¸ê®Æªí¤~·|«Ø¥ß¨ì¡uPHP´ú¸Õ¡v¸ê®Æ®w¤¤¡A­Y¥¼¥[¥H«ü©w¸ê®Æ®w¡A«h·s¼Wªº¸ê®Æªí±N·|«Ø¥ß©ómysql_connect()¨ç¼Æ³sµ²µn¤J±b¸¹©Ò¹w³]ªº¸ê®Æ®w¤¤¡C

¹Ï9-22  ¥HphpMyAdmin¬d¬Ý¸ê®Æªí³]­p¤º®e¡C
ex09_13.php
  1. <?
  2.     $conn = mysql_connect("localhost", "root", "12345a");
  3.       //«ü©w®Õ¹ï¦r¤¸ªí
  4.       mysql_query("set character set big5");
  5.       //«Ø¥ßSQL©R¥O¦r¦ê
  6.     if(mysql_select_db("php´ú¸Õ", $conn))
  7.      {
  8.       //¸ê®Æ®w¦s¦b,«Ø¥ßSQL©R¥O¦r¦ê
  9.       $SQL="Create Table PHPTable(
  10.            PersonID char(10) Not NULL,
  11.            Name varChar(10),
  12.            Email varChar(100) Default 'n/a' not NULL)
  13.            CHARACTER SET big5 COLLATE big5_chinese_ci ";

  14.      //«Ø¥ß¸ê®Æªí
  15.       if (mysql_query($SQL))
  16.         {
  17.           echo "¸ê®Æªí«Ø¥ß¦¨¥\\" ;
  18.         }
  19.       else
  20.         {
  21.           echo "¸ê®Æªí«Ø¥ß¥¢±Ñ©Î¸ê®Æªí¤w¸g¦s¦b";
  22.         }
  23.      }
  24.     else
  25.      {
  26.       //«Ø¥ßSQL©R¥O¦r¦ê
  27.       $SQL="CREATE DataBase PHP´ú¸Õ DEFAULT CHARACTER SET big5 COLLATE big5_chinese_ci";
  28.       //°õ¦æ¸ê®Æ®w«Ø¥ß
  29.      mysql_query($SQL);
  30.       //¸ê®Æ®w¤w«Ø¥ß«á¦s¦b,«Ø¥ß·s¼W¸ê®Æªí©R¥O¦r¦ê
  31.       $SQL="Create Table PHPTable(PersonID char(10) Not NULL,
  32.            Name varChar(10),
  33.            Email varChar(100) Default 'n/a' not NULL)
  34.            CHARACTER SET big5 COLLATE big5_chinese_ci ";

  35.        //¤Á´«¸ê®Æ®w
  36.        mysql_select_db("PHP´ú¸Õ", $conn);

  37.       //«Ø¥ß¸ê®Æªí
  38.       if (mysql_query($SQL))
  39.         {
  40.           echo "¸ê®Æªí«Ø¥ß¦¨¥\\" ;
  41.         }
  42.       else
  43.         {
  44.           echo "¸ê®Æªí«Ø¥ß¥¢±Ñ©Î¸ê®Æªí¤w¸g¦s¦b";
  45.         }
  46.      }
  47. ?>
½Æ»s¥N½X
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

¹ê°µ½m²ß1¡G
©ó½d¨Òex09_10©Ò«Ø¥ßªº¡uPHP´ú¸Õ¡v¸ê®Æ®w¤¤·s¼W¤@­Ó¦W¬°¡upeoplelist¡vªº¸ê®Æªí¡A¸ê®Æªí¤¤ªºÄæ¦ì¦p¤Uªí©Ò¥Ü¡G

¦WºÙ

¸ê®Æ«¬ºA

¸ê®Æªø«×

¬O§_¤¹³\Null

©m¦W

varchar

(8)

«D Null (ªÅ­È)

²{¥ô¾ºÙ

varchar

(10)

NULL (ªÅ­È)

³¡ªù¥N¸¹

varchar

(10)

«D Null (ªÅ­È)

¿¤¥«

varchar

(10)

NULL (ªÅ­È)

¦a§}

varchar

(50)

NULL (ªÅ­È)

¹q¸Ü

varchar

(10)

NULL (ªÅ­È)

¶l»¼°Ï¸¹

varchar

(5)

NULL (ªÅ­È)

¥Ø«e¤ëÁ~¸ê

smallint

NULL (ªÅ­È)

¦~°²¤Ñ¼Æ

smallint

NULL (ªÅ­È)



§¹¦¨«áªº¸ê®Æªí³W¹ºÀ³¸Ó¦p¤U¹Ï©Ò¥Ü¡G

¹Ï9-23   Äæ¦ì³]­p¤º®e¡C

¡uCreate Table¡v«ü¥O±Ô­z´£¥Ü¡G
  1. CREATE TABLE peoplelist (
  2.         ©m¦W varchar (8) NOT NULL ,
  3.         ²{¥ô¾ºÙ varchar (10) NULL ,
  4.         ³¡ªù¥N¸¹ varchar (10) NOT NULL ,
  5.         ¿¤¥« varchar (10) NULL ,
  6.         ¦a§} varchar (50) NULL ,
  7.         ¹q¸Ü varchar (10) NULL ,
  8.         ¶l»¼°Ï¸¹ varchar (5) NULL ,
  9.         ¥Ø«e¤ëÁ~¸ê smallint NULL ,
  10.         ¦~°²¤Ñ¼Æ smallint NULL
  11. )
½Æ»s¥N½X
­n³z¹L«eºÝPHPµ{¦¡¤¤ªºmysql_query()¨ç¼Æ§R°£«áºÝSQL Server¤¤ªº¸ê®Æ®w¥²¶·¨Ï¥Î¨Ï¥ÎDDL«ü¥O¸s¤¤ªº¡uDrop Table¡v«ü¥O¡G
§R°£¸ê®Æªí
  1. Drop Table ¸ê®Æªí¦W
½Æ»s¥N½X
¨Ò¦p¡G§R°£½d¨Òex09_13.php©ó«áºÝSQL Server¡uPHP´ú¸Õ¡v¸ê®Æ®w¤¤·s¼Wªº¡uPHPTable¡v¡G
ex09_14.php
  1. <?
  2.     $conn = mysql_connect("localhost", "root", "12345a");
  3.       //«ü©w®Õ¹ï¦r¤¸ªí
  4.       mysql_query("set character set big5");
  5.       //¤Á´«¸ê®Æ®w
  6.       mysql_select_db("php´ú¸Õ", $conn);
  7.       //«Ø¥ßSQL©R¥O¦r¦ê
  8.       $SQL="Drop Table PHPTable";
  9.       //°õ¦æ¸ê®Æ®w«Ø¥ß
  10.      mysql_query($SQL);
  11. ?>
½Æ»s¥N½X
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

¹ê°µ½m²ß1¸Ñµª¡G¡]test09_01.php¡^
  1. <?
  2.     $conn = mysql_connect("localhost", "root", "12345a");
  3.       //«ü©w®Õ¹ï¦r¤¸ªí
  4.       mysql_query("set character set big5");
  5. if(@mysql_select_db("PHP´ú¸Õ", $conn))
  6.      {
  7.       //¸ê®Æ®w¦s¦b,«Ø¥ßSQL©R¥O¦r¦ê
  8.       $SQL="CREATE TABLE peoplelist (" .
  9.            "©m¦W varchar (8) NOT NULL ," .
  10.            "²{¥ô¾ºÙ varchar (10) NULL ," .
  11.            "³¡ªù¥N¸¹ varchar (10) NOT NULL ," .
  12.            "¿¤¥« varchar (10) NULL ," .
  13.            "¦a§} varchar (50) NULL ," .
  14.            "¹q¸Ü varchar (10) NULL ," .
  15.            "¶l»¼°Ï¸¹ varchar (5) NULL ," .
  16.            "¥Ø«e¤ëÁ~¸ê smallint NULL ," .
  17.            "¦~°²¤Ñ¼Æ smallint NULL " .
  18.            ")";

  19.      //«Ø¥ß¸ê®Æªí
  20.       if (@mysql_query($SQL))
  21.         {
  22.           echo "¸ê®Æªí«Ø¥ß¦¨¥\\" ;
  23.         }
  24.       else
  25.         {
  26.           echo "¸ê®Æªí«Ø¥ß¥¢±Ñ©Î¸ê®Æªí¤w¸g¦s¦b";
  27.         }
  28.      }
  29.     else
  30.      {
  31.       //«Ø¥ßSQL©R¥O¦r¦ê
  32.       $SQL="CREATE DataBase PHP´ú¸Õ";
  33.       //°õ¦æ¸ê®Æ®w«Ø¥ß
  34.      mysql_query($SQL);
  35.       //¸ê®Æ®w¤w«Ø¥ß«á¦s¦b,«Ø¥ß·s¼W¸ê®Æªí©R¥O¦r¦ê
  36.       $SQL="CREATE TABLE peoplelist (" .
  37.            "©m¦W varchar (8) NOT NULL ," .
  38.            "²{¥ô¾ºÙ varchar (10) NULL ," .
  39.            "³¡ªù¥N¸¹ varchar (10) NOT NULL ," .
  40.            "¿¤¥« varchar (10) NULL ," .
  41.            "¦a§} varchar (50) NULL ," .
  42.            "¹q¸Ü varchar (10) NULL ," .
  43.            "¶l»¼°Ï¸¹ varchar (5) NULL ," .
  44.            "¥Ø«e¤ëÁ~¸ê smallint NULL ," .
  45.            "¦~°²¤Ñ¼Æ smallint NULL " .
  46.            ")";

  47.        //¤Á´«¸ê®Æ®w
  48.        mysql_select_db("PHP´ú¸Õ", $conn);

  49.       //«Ø¥ß¸ê®Æªí
  50.       if (@mysql_query($SQL))
  51.         {
  52.           echo "¸ê®Æªí«Ø¥ß¦¨¥\\" ;
  53.         }
  54.       else
  55.         {
  56.           echo "¸ê®Æªí«Ø¥ß¥¢±Ñ©Î¸ê®Æªí¤w¸g¦s¦b";
  57.         }
  58.      }
  59. ?>
½Æ»s¥N½X
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

        ÀR«ä¦Û¦b : ·R¤£¬O­n¨D¹ï¤è¡A¦Ó¬O­n¥Ñ¦Û¨­ªº¥I¥X¡C
ªð¦^¦Cªí ¤W¤@¥DÃD