SQL CheatSheet (Alter Table Example)


SUBMITTED BY: Guest

DATE: July 21, 2014, 11:38 p.m.

FORMAT: Text only

SIZE: 3.8 kB

HITS: 1188

  1. SQL: ALTER TABLE Statement
  2. Learn how to use the SQL ALTER TABLE statement to add a column, modify a column, drop a column, rename a column or rename a table (with lots of clear, concise examples). We've also added some practice exercises that you can try for yourself.
  3. Description
  4. The SQL ALTER TABLE statement is used to add, modify, or drop/delete columns in a table. The SQL ALTER TABLE statement is also used to rename a table.
  5. Add column in table
  6. Syntax
  7. To add a column in a table, the SQL ALTER TABLE syntax is:
  8. ALTER TABLE table_name
  9. ADD column_name column-definition;
  10. Example
  11. Let's look at a SQL ALTER TABLE example that adds a column.
  12. For example:
  13. ALTER TABLE supplier
  14. ADD supplier_name varchar2(50);
  15. This SQL ALTER TABLE example will add a column called supplier_name to the supplier table.
  16. Add multiple columns in table
  17. Syntax
  18. To add multiple columns to an existing table, the SQL ALTER TABLE syntax is:
  19. ALTER TABLE table_name
  20. ADD (column_1 column-definition,
  21. column_2 column-definition,
  22. ...
  23. column_n column_definition);
  24. Example
  25. Let's look at SQL ALTER TABLE example that adds more than one column.
  26. For example:
  27. ALTER TABLE supplier
  28. ADD (supplier_name varchar2(50),
  29. city varchar2(45));
  30. This SQL ALTER TABLE example will add two columns, supplier_name as a varchar2(50) field and city as a varchar2(45) field to the supplier table.
  31. Modify column in table
  32. Syntax
  33. To modify a column in an existing table, the SQL ALTER TABLE syntax is:
  34. ALTER TABLE table_name
  35. MODIFY column_name column_type;
  36. Example
  37. Let's look at SQL ALTER TABLE example that modifies a column.
  38. For example:
  39. ALTER TABLE supplier
  40. MODIFY supplier_name varchar2(100) not null;
  41. This SQL ALTER TABLE example will modify the column called supplier_name to be a data type of varchar2(100) and force the column to not allow null values.
  42. Modify multiple columns in table
  43. Syntax
  44. To modify multiple columns in an existing table, the SQL ALTER TABLE syntax is:
  45. ALTER TABLE table_name
  46. MODIFY (column_1 column_type,
  47. column_2 column_type,
  48. ...
  49. column_n column_type);
  50. Example
  51. Let's look at SQL ALTER TABLE example that modifies more than one column.
  52. For example:
  53. ALTER TABLE supplier
  54. MODIFY (supplier_name varchar2(100) not null,
  55. city varchar2(75));
  56. This SQL ALTER TABLE example will modify both the supplier_name and city columns.
  57. Drop column in table
  58. Syntax
  59. To drop a column in an existing table, the SQL ALTER TABLE syntax is:
  60. ALTER TABLE table_name
  61. DROP COLUMN column_name;
  62. Example
  63. Let's look at SQL ALTER TABLE example that drops (ie: deletes) a column from a table.
  64. For example:
  65. ALTER TABLE supplier
  66. DROP COLUMN supplier_name;
  67. This SQL ALTER TABLE example will drop the column called supplier_name from the table called supplier.
  68. Rename column in table
  69. Syntax
  70. To rename a column in an existing table, the SQL ALTER TABLE syntax is:
  71. ALTER TABLE table_name
  72. RENAME COLUMN old_name to new_name;
  73. Example
  74. Let's look at SQL ALTER TABLE example that renames a column in a table.
  75. For example:
  76. ALTER TABLE supplier
  77. RENAME COLUMN supplier_name to sname;
  78. This SQL ALTER TABLE example will rename the column called supplier_name to sname.
  79. Rename table
  80. Syntax
  81. To rename a table, the SQL ALTER TABLE syntax is:
  82. ALTER TABLE table_name
  83. RENAME TO new_table_name;

comments powered by Disqus