PgSQL - Study Notes 18: AUTO INCREMENT (automatic growth) & PRIVILEGES (permissions)

Table of contents

AUTO INCREMENT: Generates a unique number when a new record is inserted into the table.

PRIVILEGES (privileges)

GRANT command: assign permissions to users

REVOKE command: cancel permissions 


AUTO INCREMENT: Generates a unique number when a new record is inserted into the table.

PostgreSQL uses sequences to identify the auto-increment of fields, and the data types are smallserial, serial and bigserial.

These properties are similar to the AUTO_INCREMENT properties supported by the MySQL database.

The statement to set up automatic growth using MySQL is as follows:

CREATE TABLE IF NOT EXISTS `runoob_tbl`(
   `runoob_id` INT UNSIGNED AUTO_INCREMENT,
   `runoob_title` VARCHAR(100) NOT NULL,
   `runoob_author` VARCHAR(40) NOT NULL,
   `submission_date` DATE,
   PRIMARY KEY ( `runoob_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

MySQL uses the AUTO_INCREMENT attribute to identify the auto-increment of a field.

PostgreSQL uses sequences to identify the auto-increment of fields:

CREATE TABLE runoob
(
    id serial NOT NULL,
    alttext text,
    imgurl text
)

SMALLSERIAL, SERIAL and BIGSERIAL range:

 Example:

mydb=# CREATE TABLE COMPANY_3(
mydb(#    ID  SERIAL PRIMARY KEY,
mydb(#    NAME           TEXT      NOT NULL,
mydb(#    AGE            INT       NOT NULL,
mydb(#    ADDRESS        CHAR(50),
mydb(#    SALARY         REAL
mydb(# );
CREATE TABLE
mydb=# select * from company_3;
                mydb
 id | name | age | address | salary
----+------+-----+---------+--------
(0 行记录)


mydb=# INSERT INTO COMPANY_3 (NAME,AGE,ADDRESS,SALARY) VALUES
mydb-# ( 'Paul', 32, 'California', 20000.00 ),
mydb-# ('Allen', 25, 'Texas', 15000.00 ),
mydb-# ('Teddy', 23, 'Norway', 20000.00 ),
mydb-# ( 'Mark', 25, 'Rich-Mond ', 65000.00 ),
mydb-# ( 'David', 27, 'Texas', 85000.00 ),
mydb-# ( 'Kim', 22, 'South-Hall', 45000.00 ),
mydb-# ( 'James', 24, 'Houston', 10000.00 );
INSERT 0 7
mydb=# select * from company_3;
                                      mydb
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  1 | Paul  |  32 | California                                         |  20000
  2 | Allen |  25 | Texas                                              |  15000
  3 | Teddy |  23 | Norway                                             |  20000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  5 | David |  27 | Texas                                              |  85000
  6 | Kim   |  22 | South-Hall                                         |  45000
  7 | James |  24 | Houston                                            |  10000
(7 行记录)

PRIVILEGES (privileges)

Whenever a database object is created, it is assigned an owner, usually the person who executed the create statement.

For most types of objects, the initial state is that only the owner (or superuser) can modify or delete the object. To allow other roles or users to use it, permissions must be set for the user.

In PostgreSQL, permissions are divided into the following types:

  • SELECT
  • INSERT
  • UPDATE
  • DELETE
  • TRUNCATE
  • REFERENCES
  • TRIGGER
  • CREATE
  • CONNECT
  • TEMPORARY
  • EXECUTE
  • USAGE

Depending on the type of object (table, function, etc.), the specified permissions are applied to the object.

To assign permissions to users, use the GRANT command.

GRANT command: assign permissions to users

/*GRANT 命令的基本语法如下:*/
GRANT privilege [, ...]
ON object [, ...]
TO { PUBLIC | GROUP group | username }
  • privilege − value can be: SELECT, INSERT, UPDATE, DELETE, RULE, ALL.
  • object − The name of the object to which access is to be granted. Possible objects are: table, view, sequence.
  • PUBLIC − means all users.
  • GROUP group − Grant permissions to user groups.
  • username − The username to which permissions are to be granted. PUBLIC is the short form for all users.

REVOKE command: cancel permissions 

/*REVOKE 语法:*/
REVOKE privilege [, ...]
ON object [, ...]
FROM { PUBLIC | GROUP groupname | username }

 Example:

/*为了理解权限,创建一个用户:【用户为mydb_user】*/
mydb=# CREATE USER mydb_user WITH PASSWORD 'password';
CREATE ROLE
mydb=# select * from company_3;
                                      mydb
 id | name  | age |                      address                       | salary
----+-------+-----+----------------------------------------------------+--------
  1 | Paul  |  32 | California                                         |  20000
  2 | Allen |  25 | Texas                                              |  15000
  3 | Teddy |  23 | Norway                                             |  20000
  4 | Mark  |  25 | Rich-Mond                                          |  65000
  5 | David |  27 | Texas                                              |  85000
  6 | Kim   |  22 | South-Hall                                         |  45000
  7 | James |  24 | Houston                                            |  10000
(7 行记录)

/*给用户 "mydb_user" 分配所有权限:*/
mydb=# GRANT ALL ON company_3 TO mydb_user;
GRANT
/*撤销用户 "mydb_user" 的权限:*/
mydb=# REVOKE ALL ON company_3 FROM mydb_user;
REVOKE
/*删除用户:*/
mydb=# DROP USER mydb_user;
DROP ROLE

Guess you like

Origin blog.csdn.net/qq_41361442/article/details/124878871