[postgresql] Postgresql database operation column addition and id auto-increment setting notes

postgresql database operation

add a column

need:

Add a column is_delete is false by default

Junior Edition

The statement of adding a column to a table and setting the default value in PostgreSQL can be implemented using the ALTER TABLE statement. The specific operation is as follows:

ALTER TABLE table_name ADD COLUMN column_name boolean DEFAULT false;

where table_nameis the name of the table you want to add the column to, column_nameis the name of the column you want to add, booleanis the data type of the column, DEFAULT falseand is the default value you want to set.

For example, suppose you want usersto add a is_deleteBoolean column named to a table named and set its default value to false, you can use the following statement:

ALTER TABLE users ADD COLUMN is_delete boolean DEFAULT false;

After executing this statement, you can use the following statement to verify that the column was added successfully:

SELECT * FROM users;

This will return all the rows in usersthe table , including is_deletethe column you just added.

default padding

If new data is added, the is_delete column is filled with false by default.

If you want usersto automatically is_deleteset the column to when new data is inserted into the table false, you can use the following SQL statement:

INSERT INTO users (column1, column2, is_delete) VALUES (value1, value2, false);

In this statement, column1, column2are the other column names you want to insert, value1and , value2are the corresponding values ​​to insert. is_deleteis the name of the column you want to set and set its value to false.

For example, if you wanted to insert into usersthe table a Johnnew user named with their email address set to [email protected], you would use the following statement:

INSERT INTO users (name, email, is_delete) VALUES ('John', '[email protected]', false);

This will insert a new row into usersthe table with is_deletethe column set to false.

summary

postgresql operation requirements

1: Add a column is_delete to the original table axvd_group_user;

2. The default is false, if you add data later, if you do not pass this parameter, it will be false, and if you pass the parameter, it will be saved according to the passed parameter;

If you want axvd_group_userto automatically is_deleteset the column to when you insert new data into the table false, and can set it to another value if needed, you can use the following SQL statement:

ALTER TABLE axvd_group_user ADD COLUMN is_delete boolean NOT NULL DEFAULT false;

This statement will create a boolean column is_deletenamed and set its default value to false. NOT NULLoption will ensure that a value must be is_deleteprovided .

You can then insert new data into axvd_group_userthe table :

INSERT INTO axvd_group_user (user_id, group_id, is_delete) VALUES (1, 2, false);

In this statement, user_id, group_idare the other column names you want to insert, 1and , 2are the corresponding values ​​to insert. is_deleteis the name of the column you want to set and set its value to false.

If you want to is_deleteset , you can use the following statement:

INSERT INTO axvd_group_user (user_id, group_id, is_delete) VALUES (1, 2, true);

In this statement, trueis the value is_deleteof . You can truereplace with any other boolean value to set is_deletethe value of .

postgresql operation upgrade version requirements

Postgresql adds a column is_delete to the original table axvd_group_user 1: If this column exists, it will be deleted first, if it does not exist, it will be added, 2: The default is false, 3: Add data later, if this parameter is not passed, it will be false, and if the parameter is passed, it will be based on the passed Save the parameters, how to write the sql statement

You can use the following SQL statements axvd_group_userto add is_deletecolumns to the table to meet your needs:

-- 如果 is_delete 列存在,先删除
ALTER TABLE axvd_group_user DROP COLUMN IF EXISTS is_delete;
-- 添加 is_delete 列,默认值为 false
ALTER TABLE axvd_group_user ADD COLUMN is_delete boolean NOT NULL DEFAULT false;

This will first check to axvd_group_usersee if is_deletea column named exists in the table, and if so, will drop it. It will then add a boolean column is_deletecalled and set its default value to false.

Next, you can insert new data into axvd_group_userthe table :

INSERT INTO axvd_group_user (user_id, group_id, is_delete) VALUES (1, 2, false);

In this statement, user_id, group_idare the other column names you want to insert, 1and , 2are the corresponding values ​​to insert. is_deleteis the name of the column you want to set and set its value to false.

If you want to is_deleteset , you can use the following statement:

INSERT INTO axvd_group_user (user_id, group_id, is_delete) VALUES (1, 2, true);

In this statement, trueis the value is_deleteof . You can truereplace with any other boolean value to set is_deletethe value of .

id setting self-increment

need:

In the postgresql database, the axvd_group_user: id column is set to self-increment, and there is no need to give a new value every time

You can use PostgreSQL sequences axvd_group_userto idauto-increment columns in tables. Follow the steps below:

  1. Create a sequence axvd_group_user_id_seqcalled . This sequence can be created using the following SQL statement:

    CREATE SEQUENCE axvd_group_user_id_seq;
    
  2. Use the sequence as the default value for idthe column . The following SQL statement can be used to update axvd_group_userthe column in the idtable:

    ALTER TABLE axvd_group_user ALTER COLUMN id SET DEFAULT nextval('axvd_group_user_id_seq');
    

Now, when you insert new data into axvd_group_userthe table , idthe column will auto-increment. For example:

INSERT INTO axvd_group_user (user_id, group_id, is_delete) VALUES (1, 2, false);

In this statement, you don't need to provide a value for idthe column as it will automatically be assigned a new value.

Guess you like

Origin blog.csdn.net/qq_41604569/article/details/130564317