PostgreSQL Alter Table: Alter a table to change the name of the column, keeping the same size and data type
5. Write a SQL statement to change the name of the column state_province to state, keeping the same size and data type and size.
Here is the structure of the table locations.
postgres=# \d locations
Column | Type | Modifiers
----------------+-----------------------+-----------
location_id | numeric(4,0) |
street_address | character varying(40) |
postal_code | character varying(12) |
city | character varying(30) |
state_province | character varying(25) |
country_id | character varying(2) |
region_id | text |
Sample Solution:
Code:
ALTER TABLE locations
RENAME COLUMN state_province TO state;
Output:
Now see the structure of the table locations after alteration.
postgres=# \d locations
Column | Type | Modifiers
----------------+-----------------------+-----------
location_id | numeric(4,0) |
street_address | character varying(40) |
postal_code | character varying(12) |
country_id | character varying(2) |
region_id | text |
state | character varying(25) |
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a SQL statement to drop the column city from the table locations.
Next: Write a SQL statement to add a primary key to the columns location_id in the locations table.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework