It's not supported to alter table add serial column
时间: 2023-12-03 15:46:31 浏览: 118
Actually, it is possible to add a serial column to an existing table in PostgreSQL using the ALTER TABLE statement. The syntax for adding a serial column is as follows:
```
ALTER TABLE table_name ADD COLUMN column_name SERIAL;
```
This will create a new column called "column_name" that will automatically generate a unique integer value for each new row that is inserted into the table. You can also specify additional options for the serial column, such as the starting value and the increment value, by using the following syntax:
```
ALTER TABLE table_name ADD COLUMN column_name SERIAL [OPTIONS];
```
For example, to set the starting value for the serial column to 100 and the increment value to 10, you can use the following command:
```
ALTER TABLE my_table ADD COLUMN serial_id SERIAL START WITH 100 INCREMENT BY 10;
```
Note that if you are using an older version of PostgreSQL (prior to version 10), you may need to use a different syntax to add a serial column to an existing table. In that case, you can use the following command:
```
ALTER TABLE table_name ADD COLUMN column_name SERIAL PRIMARY KEY;
```
This will create a new serial column and set it as the primary key for the table.
阅读全文