lang_altertable.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head>
  3. <title>SQLite Query Language: ALTER TABLE</title>
  4. <style type="text/css">
  5. body {
  6. margin: auto;
  7. font-family: Verdana, sans-serif;
  8. padding: 8px 1%;
  9. }
  10. a { color: #45735f }
  11. a:visited { color: #734559 }
  12. .logo { position:absolute; margin:3px; }
  13. .tagline {
  14. float:right;
  15. text-align:right;
  16. font-style:italic;
  17. width:240px;
  18. margin:12px;
  19. margin-top:58px;
  20. }
  21. .toolbar {
  22. font-variant: small-caps;
  23. text-align: center;
  24. line-height: 1.6em;
  25. margin: 0;
  26. padding:1px 8px;
  27. }
  28. .toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
  29. .toolbar a:visited { color: white; }
  30. .toolbar a:hover { color: #80a796; background: white; }
  31. .content { margin: 5%; }
  32. .content dt { font-weight:bold; }
  33. .content dd { margin-bottom: 25px; margin-left:20%; }
  34. .content ul { padding:0px; padding-left: 15px; margin:0px; }
  35. /* rounded corners */
  36. .se { background: url(images/se.png) 100% 100% no-repeat #80a796}
  37. .sw { background: url(images/sw.png) 0% 100% no-repeat }
  38. .ne { background: url(images/ne.png) 100% 0% no-repeat }
  39. .nw { background: url(images/nw.png) 0% 0% no-repeat }
  40. </style>
  41. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  42. </head>
  43. <body>
  44. <div><!-- container div to satisfy validator -->
  45. <a href="lang.html">
  46. <h2 align="center">SQL As Understood By SQLite</h2></a><h1>ALTER TABLE</h1><h4><a href="syntaxdiagrams.html#alter-table-stmt">alter-table-stmt:</a></h4><blockquote> <img alt="syntax diagram alter-table-stmt" src="images/syntax/alter-table-stmt.gif"></img> </blockquote>
  47. <p>SQLite supports a limited subset of ALTER TABLE.
  48. The ALTER TABLE command in SQLite allows the user to rename a table
  49. or to add a new column to an existing table. It is not possible
  50. to rename a column, remove a column, or add or remove constraints from a table.
  51. </p>
  52. <p>The RENAME TO syntax is used to rename the table identified by
  53. <i>&#91;database-name.&#93;table-name</i> to <i>new-table-name</i>.
  54. This command
  55. cannot be used to move a table between attached databases, only to rename
  56. a table within the same database.</p>
  57. <p>If the table being renamed has triggers or indices, then these remain
  58. attached to the table after it has been renamed. However, if there are
  59. any view definitions, or statements executed by triggers that refer to
  60. the table being renamed, these are not automatically modified to use the new
  61. table name. If this is required, the triggers or view definitions must be
  62. dropped and recreated to use the new table name by hand.
  63. </p>
  64. <p>If <a href="foreignkeys.html">foreign key constraints</a> are
  65. <a href="pragma.html#pragma_foreign_keys">enabled</a> when a table is renamed, then any
  66. <a href="syntaxdiagrams.html#foreign-key-clause">REFERENCES clauses</a> in any table (either the
  67. table being renamed or some other table)
  68. that refer to the table being renamed are modified to refer
  69. to the renamed table by its new name.
  70. <p>The ADD COLUMN syntax
  71. is used to add a new column to an existing table.
  72. The new column is always appended to the end of the list of existing columns.
  73. The <a href="syntaxdiagrams.html#column-def">column-def</a> rule defines the characteristics of the new column.
  74. The new column may take any of the forms permissable in a <a href="lang_createtable.html">CREATE TABLE</a>
  75. statement, with the following restrictions:
  76. <ul>
  77. <li>The column may not have a PRIMARY KEY or UNIQUE constraint.</li>
  78. <li>The column may not have a default value of CURRENT_TIME, CURRENT_DATE
  79. or CURRENT_TIMESTAMP.</li>
  80. <li>If a NOT NULL constraint is specified, then the column must have a
  81. default value other than NULL.
  82. <li>If <a href="foreignkeys.html">foreign key constraints</a> are <a href="pragma.html#pragma_foreign_keys">enabled</a> and
  83. a column with a <a href="syntaxdiagrams.html#foreign-key-clause">REFERENCES clause</a>
  84. is added, the column must have a default value of NULL.
  85. </ul>
  86. <p>Note also that when adding a CHECK constraint, the CHECK constraint
  87. is not tested against preexisting rows of the table.
  88. This can result in a table that contains data that
  89. is in violation of the CHECK constraint. Future versions of SQLite might
  90. change to validate CHECK constraints as they are added.</p>
  91. <p>The execution time of the ALTER TABLE command is independent of
  92. the amount of data in the table. The ALTER TABLE command runs as quickly
  93. on a table with 10 million rows as it does on a table with 1 row.
  94. </p>
  95. <p>After ADD COLUMN has been run on a database, that database will not
  96. be readable by SQLite version 3.1.3 and earlier.</p>