ALTER TABLE table_name ADD new_column_name DATA TYPE ĭon’t need that column so bad after all? Not to worry. ALTER TABLE current_table_name RENAME TO new_table_name Not satisfied with your table name? This syntax will handle that for you. For the curious….Ī few of the more common uses of the ALTER TABLE command that may interest you are listed below: The important thing to take away from the above code snippet is the REAL data type now shown for the galaxy_coins column where before it was of type INTEGER. I can verify the new table schema using the psql command ‘\d galaxy_user’( ‘\d’ means describe table where galaxy_user is the table name) in the Linux terminal: The above snippet displays the ALTER TABLE and ALTER COLUMN commands grouped into one command to change (alter) the galaxy_coins column to type REAL. The syntax to change an existing columns’ data type appears below: ALTER COLUMN column_name TYPE new_type īoth statements can be used together in a single command. I will use the REAL data type for this hypothetical example.įirst, you must specify which table you are changing using the following syntax: ALTER TABLE table_name The ALTER TABLE command enables you to change the galaxy_coins column’s data type from INTEGER to a decimal value type. Suppose the galaxy_coins column now needs to represent a sort of ‘game currency’ and must support decimal values instead of whole numbers. The galaxy_user table created in the previous post of the series. In the previous post of the series, I demonstrated creating a table named galaxy_user shown below: To edit or change an existing table, you use the ALTER TABLE command. I encourage you to refer to it for any questions and researching other available options. Again, I cannot emphasize enough just how marvelous the PostgreSQL documentation is. The plethora of options available for the ALTER TABLE command is too exhaustive to be covered in one post. ![]() Regarding ALTER TABLE, I will touch base on a couple of the more commonly used commands to get you going. I will be using Xubuntu Linux 16.04.2 LTS (Xenial Xerus), PostgreSQL 9.6.2 and pdAdmin for these exercises. It by no means depicts actual data belonging to or being used by any party or organization. Note: All data, names or naming found within the database presented in this post, are strictly used for practice, instruction and testing purposes. Visit Part 1 of this series for a refresher of the material covered up to this point. INSERT statements will be introduced as well to populate an empty table with mock data. This blog post will demonstrate basic concepts of the ALTER TABLE command for changing an existing PostgreSQL table.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |