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

[±Ð¾Ç] (PHP+IIS+MsSQL±Ð¾Ç²Ä33½g) ¨Ï¥ÎDDL«ü¥O

[±Ð¾Ç] (PHP+IIS+MsSQL±Ð¾Ç²Ä33½g) ¨Ï¥ÎDDL«ü¥O

DDL¡G¸ê®Æ©w¸q»y¨¥¡]Data Definition Language¡^¡A¥Î¨Ó«Ø¥ß»P§R°£¸ê®Æ®w¡B¸ê®Æªí¡Kµ¥ªºSQL«ü¥O¸s¡C

«Ø¥ß»P¨ø°£¸ê®Æ®w
­n¦b«eºÝªºPHPµ{¦¡¤¤«Ø¥ß«áºÝSQL Server±±ºÞªº·s¸ê®Æ®w¡A¥²¶·¨Ï¥ÎDDL«ü¥O¸s¤¤ªº¡uCreate¡v«ü¥O¡G
³Ð«Ø¸ê®Æ®w
  1. Create DataBase ·s¸ê®Æ®w¦W
½Æ»s¥N½X
¨Ò¦p¡G§Q¥Î«eºÝPHPµ{¦¡¤¤ªºCommandª«¥ó°õ¦æCreate«ü¥O¡A©ó«áºÝSQL Server·s¼W¤@­Ó¦W¬°¡uPHP´ú¸Õ¡vªº·s¸ê®Æ®w¡G
  1. <?
  2.     $conn = mssql_connect("127.0.0.1", "sa", "12345");
  3.       //«Ø¥ßSQL©R¥O¦r¦ê
  4.       $SQL="Create DataBase PHP´ú¸Õ";
  5.       //°õ¦æ¸ê®Æ®w«Ø¥ß
  6.      mssql_query($SQL);
  7. ?>
½Æ»s¥N½X
­n¦b«áºÝSQL Server¤¤«Ø¥ß·s¸ê®Æ®w¡A¤]´N¬O¨Ï¥ÎCREATE DATABASE«ü¥O¡]¡^¡AÅv­­¬O¹w³]µ¹sysadmin©Mdbcreator©T©w¦øªA¾¹¨¤¦âªº¦¨­û¡A¦]¦¹¡A¨Ã«D¨C­Óµn¤JSQL Serveªº±b¸¹³£¥i¥H«Ø¥ß·s¸ê®Æ®w¡A¤]´N¬O¥Î¤£¦Pªºµn¤J±b¸¹´N¦³¤£¦P¦s¨ú¸ê®Æ©M°õ¦æ¥\¯àªºÅv­­¡C

¦b¤W¨Ò¤¤¡A§Ú­Ì¨Ï¥Îªºµn¤J±b¸¹¬°¡usa¡]¨t²ÎºÞ²zªÌ¡^¡v¡A¦]¦¹¾Ö¦³³Ì¤jªºSQL Server¨Ï¥ÎÅv­­¡A©Ò¥H¯à¦b«áºÝSQL Server¤¤«Ø¥ß·s¸ê®Æ®w¡A¤p»x«Øij±z¡A¦pªG­nÅý§O¤H¨Ï¥Î±zªºSQL Server¾÷¾¹¡A³Ì¦n«Ø¥ß¤@­Ó¦³Åv­­­­¨îªº±b¸¹¡A¤£­n±N¨t²ÎºÞ²zªÌ±b¸¹»P±K½XÀH«K¥æµ¹§O¤H¨Ï¥Î¡I


­n³z¹L«eºÝPHPµ{¦¡¤¤ªºCommandª«¥ó¨ø°£«áºÝSQL Server¤¤ªº¸ê®Æ®w¥²¶·¨Ï¥Î¨Ï¥ÎDDL«ü¥O¸s¤¤ªº¡uDrop¡v«ü¥O¡G
¨øÂ÷¸ê®Æ®w
  1. Drop DataBase ¸ê®Æ®w¦W
½Æ»s¥N½X
¨Ò¦p¡G¨ø°£¤W¤@­Ó½d¨Ò©ó«áºÝSQL Server·s¼Wªº¡uPHP´ú¸Õ¡v¸ê®Æ®w¡G
  1. <?
  2.     $conn = mssql_connect("127.0.0.1", "sa", "12345");
  3.       //«Ø¥ßSQL©R¥O¦r¦ê
  4.       $SQL="DROP DataBase PHP´ú¸Õ";
  5.       //°õ¦æ¸ê®Æ®w¨ø°£
  6.      mssql_query($SQL);
  7. ?>
½Æ»s¥N½X
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

«Ø¥ß»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 TABLE
  2.     [ database_name.[ owner ] . | owner. ] table_name
  3.        ( { < column_definition >
  4.         | column_name AS computed_column_expression
  5.         | < table_constraint > ::= [ CONSTRAINT constraint_name ] }
  6.             | [ { PRIMARY KEY | UNIQUE } [ ,...n ]
  7.     )
½Æ»s¥N½X
¬Ý¤£À´¡H¨SÃö«Y¡A¤p»x§â¥¦¥Õ¸ÜªºÂ²³æ»¡©ú¦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 NvarChar(10),_
  6. Email NvarChar(100) Default 'µL' Not NULL
  7. )
½Æ»s¥N½X
¤p»x®³¤@¥yÄæ¦ì©w¸q±Ô­z¨Ó»¡©ú¡G
  1. Email NvarChar(100) Default 'µL' Not NULL_
½Æ»s¥N½X

  • Email¡iÄæ¦ì¦WºÙ¡j¡G«Ø¥ß¸ê®Æªí¤¤ªº¬Y­ÓÄæ¦ì©ïÀY¡C
  • NvarChar(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



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

¦WºÙ

¸ê®Æ«¬ºA

¸ê®Æªø«×

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

¹w³]­È

PersonID

Char

10

§_

Name

NvarChar

10

¬O

¡§µL¡¨

Email

Nvarchar

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·|«Ø¥ß©óConnectionª«¥ó³sµ²µn¤J±b¸¹©Ò¹w³]ªº¸ê®Æ®w¤¤¡C

  1. <?
  2. $conn = mssql_connect("127.0.0.1", "sa", "12345");
  3. if(@mssql_select_db("PHP´ú¸Õ", $conn))
  4. {
  5. //¸ê®Æ®w¦s¦b,«Ø¥ßSQL©R¥O¦r¦ê
  6. $SQL="Create Table " .
  7. "PHP´ú¸Õ¸ê®Æªí " .
  8. "(" .
  9. "PersonID char(10) Not NULL," .
  10. "Name NvarChar(10)," .
  11. "Email NvarChar(100) Default 'µL' not NULL" .
  12. ")";

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

  37. //¤Á´«¸ê®Æ®w
  38. mssql_select_db("PHP´ú¸Õ", $conn);

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

TOP

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

¦WºÙ

¸ê®Æ«¬ºA

¸ê®Æªø«×

¬O§_¤¹³\Null

©m¦W

nvarchar

(8)

«D Null (ªÅ­È)

²{¥ô¾ºÙ

nvarchar

(10)

NULL (ªÅ­È)

³¡ªù¥N¸¹

nvarchar

(10)

«D Null (ªÅ­È)

¿¤¥«

nvarchar

(10)

NULL (ªÅ­È)

¦a§}

nvarchar

(50)

NULL (ªÅ­È)

¹q¸Ü

nvarchar

(10)

NULL (ªÅ­È)

¶l»¼°Ï¸¹

nvarchar

(5)

NULL (ªÅ­È)

¥Ø«e¤ëÁ~¸ê

smallint

NULL (ªÅ­È)

¦~°²¤Ñ¼Æ

smallint

NULL (ªÅ­È)



§¹¦¨«áªº¸ê®Æªí³W¹ºÀ³¸Ó¦p¤U¹Ï©Ò¥Ü¡G
10.png
  1. <?
  2. $conn = mssql_connect("127.0.0.1", "sa", "12345");
  3. if(@mssql_select_db("PHP´ú¸Õ", $conn))
  4. {
  5. //¸ê®Æ®w¦s¦b,«Ø¥ßSQL©R¥O¦r¦ê
  6. $SQL="CREATE TABLE ­û¤u¦W³æ (" .
  7. "©m¦W nvarchar (8) NOT NULL ," .
  8. "²{¥ô¾ºÙ nvarchar (10) NULL ," .
  9. "³¡ªù¥N¸¹ nvarchar (10) NOT NULL ," .
  10. "¿¤¥« nvarchar (10) NULL ," .
  11. "¦a§} nvarchar (50) NULL ," .
  12. "¹q¸Ü nvarchar (10) NULL ," .
  13. "¶l»¼°Ï¸¹ nvarchar (5) NULL ," .
  14. "¥Ø«e¤ëÁ~¸ê smallint NULL ," .
  15. "¦~°²¤Ñ¼Æ smallint NULL " .
  16. ")";

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

  45. //¤Á´«¸ê®Æ®w
  46. mssql_select_db("PHP´ú¸Õ", $conn);

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

TOP

­n³z¹L«eºÝPHPµ{¦¡¤¤ªºCommandª«¥ó§R°£«áºÝSQL Server¤¤ªº¸ê®Æ®w¥²¶·¨Ï¥Î¨Ï¥ÎDDL«ü¥O¸s¤¤ªº¡uDrop Table¡v«ü¥O¡G
§R°£¸ê®Æªí
  1. Drop Table ¸ê®Æªí¦W
½Æ»s¥N½X
¨Ò¦p¡G§R°£¤§«e½d¨Ò©ó«áºÝSQL Server¡uPHP´ú¸Õ¡v¸ê®Æ®w¤¤·s¼Wªº¡uPHP´ú¸Õ¸ê®Æªí¡v¡G
  1. <?
  2.     $conn = mssql_connect("127.0.0.1", "sa", "12345");
  3.       //«Ø¥ßSQL©R¥O¦r¦ê
  4.       $SQL="Drop Table PHP´ú¸Õ¸ê®Æªí";
  5.       //°õ¦æ¸ê®Æ®w«Ø¥ß
  6.      mssql_query($SQL);
  7. ?>
½Æ»s¥N½X
¤ß¦w¡A¥­¦w   
µoªí¥ô¦óµL·N¸qªº·PÁ©ÎÄé¤ô¤å³¹¤@«ß¬å,¬Ã±¤±b¸¹,½Ð¸Ô¾\ª©³W!!

TOP

        ÀR«ä¦Û¦b : ¦³¦h¤Ö¤O¶q´N°µ¦h¤Ö¨Æ¡A¤£­n¤ß¦sµ¥«Ý¡Aµ¥«Ý¤~·|¸¨ªÅ¡C
ªð¦^¦Cªí ¤W¤@¥DÃD