SQL 快速参考

  1. SQL 语句 语法
  2. -- AND / OR
  3. SELECT column_name(s)
  4. FROM table_name
  5. WHERE condition
  6. AND|OR condition
  7. -- ALTER TABLE
  8. ALTER TABLE table_name
  9. ADD column_name datatype
  10. or
  11. ALTER TABLE table_name
  12. DROP COLUMN column_name
  13. -- AS (alias)
  14. SELECT column_name AS column_alias
  15. FROM table_name
  16. or
  17. SELECT column_name
  18. FROM table_name AS table_alias
  19. BETWEEN SELECT column_name(s)
  20. FROM table_name
  21. WHERE column_name
  22. BETWEEN value1 AND value2
  23. CREATE DATABASE CREATE DATABASE database_name
  24. CREATE TABLE CREATE TABLE table_name
  25. (
  26. column_name1 data_type,
  27. column_name2 data_type,
  28. column_name2 data_type,
  29. ...
  30. )
  31. CREATE INDEX CREATE INDEX index_name
  32. ON table_name (column_name)
  33. or
  34. CREATE UNIQUE INDEX index_name
  35. ON table_name (column_name)
  36. CREATE VIEW CREATE VIEW view_name AS
  37. SELECT column_name(s)
  38. FROM table_name
  39. WHERE condition
  40. DELETE DELETE FROM table_name
  41. WHERE some_column=some_value
  42. or
  43. DELETE FROM table_name
  44. (Note: Deletes the entire table!!)
  45. DELETE * FROM table_name
  46. (Note: Deletes the entire table!!)
  47. DROP DATABASE DROP DATABASE database_name
  48. DROP INDEX DROP INDEX table_name.index_name (SQL Server)
  49. DROP INDEX index_name ON table_name (MS Access)
  50. DROP INDEX index_name (DB2/Oracle)
  51. ALTER TABLE table_name
  52. DROP INDEX index_name (MySQL)
  53. DROP TABLE DROP TABLE table_name
  54. GROUP BY SELECT column_name, aggregate_function(column_name)
  55. FROM table_name
  56. WHERE column_name operator value
  57. GROUP BY column_name
  58. HAVING SELECT column_name, aggregate_function(column_name)
  59. FROM table_name
  60. WHERE column_name operator value
  61. GROUP BY column_name
  62. HAVING aggregate_function(column_name) operator value
  63. IN SELECT column_name(s)
  64. FROM table_name
  65. WHERE column_name
  66. IN (value1,value2,..)
  67. INSERT INTO INSERT INTO table_name
  68. VALUES (value1, value2, value3,....)
  69. or
  70. INSERT INTO table_name
  71. (column1, column2, column3,...)
  72. VALUES (value1, value2, value3,....)
  73. INNER JOIN SELECT column_name(s)
  74. FROM table_name1
  75. INNER JOIN table_name2
  76. ON table_name1.column_name=table_name2.column_name
  77. LEFT JOIN SELECT column_name(s)
  78. FROM table_name1
  79. LEFT JOIN table_name2
  80. ON table_name1.column_name=table_name2.column_name
  81. RIGHT JOIN SELECT column_name(s)
  82. FROM table_name1
  83. RIGHT JOIN table_name2
  84. ON table_name1.column_name=table_name2.column_name
  85. FULL JOIN SELECT column_name(s)
  86. FROM table_name1
  87. FULL JOIN table_name2
  88. ON table_name1.column_name=table_name2.column_name
  89. LIKE SELECT column_name(s)
  90. FROM table_name
  91. WHERE column_name LIKE pattern
  92. ORDER BY SELECT column_name(s)
  93. FROM table_name
  94. ORDER BY column_name [ASC|DESC]
  95. SELECT SELECT column_name(s)
  96. FROM table_name
  97. SELECT * SELECT *
  98. FROM table_name
  99. SELECT DISTINCT SELECT DISTINCT column_name(s)
  100. FROM table_name
  101. SELECT INTO SELECT *
  102. INTO new_table_name [IN externaldatabase]
  103. FROM old_table_name
  104. or
  105. SELECT column_name(s)
  106. INTO new_table_name [IN externaldatabase]
  107. FROM old_table_name
  108. SELECT TOP SELECT TOP number|percent column_name(s)
  109. FROM table_name
  110. TRUNCATE TABLE TRUNCATE TABLE table_name
  111. UNION SELECT column_name(s) FROM table_name1
  112. UNION
  113. SELECT column_name(s) FROM table_name2
  114. UNION ALL SELECT column_name(s) FROM table_name1
  115. UNION ALL
  116. SELECT column_name(s) FROM table_name2
  117. UPDATE UPDATE table_name
  118. SET column1=value, column2=value,...
  119. WHERE some_column=some_value
  120. WHERE SELECT column_name(s)
  121. FROM table_name
  122. WHERE column_name operator value