当前位置:网站首页>PgSQL learning notes
PgSQL learning notes
2022-07-02 06:42:00 【Thank you】
PgSQL course
value type
Create database
-- Create a runoobdb The database of :
create database runoobdb;
- Delete database
-- Delete database
DROP DATABASE xyx;
- Create a table
-- Create a table
CREATE TABLE table_01(
id INT PRIMARY KEY NOT NULL,
name VARCHAR(50),
age INT
);
- Delete table
-- Delete table
DROP TABLE table_01;
- PostgreSQL Pattern (SCHEMA)
-- Create mode
CREATE SCHEMA test01
-- Delete the pattern
DROP SCHEMA test01
- INSERT INTO ( Insert )
-- Insert table-01 surface Insert only id,name
INSERT INTO table_01 ( ID, NAME )
VALUES
( 15, ' Li Si ' );
-- Insert all
INSERT INTO table_01
VALUES
( 45, ' Zhang San ', 25 );
Return result description :
INSERT oid 1
Insert only one row and the target table has OID The return information of , that oid Is assigned to the inserted row OID.
INSERT 0 #
Insert multiple rows of returned information , # Is the number of rows inserted .
The following insert statement JOIN_DATE Fields use DEFAULT Clause to set the default value , Instead of specifying a value :
-- The following insert statement JOIN_DATE Fields use DEFAULT Clause to set the default value , Instead of specifying a value :
INSERT INTO table_01 ( ID, NAME ,age ,JOIN_DATE )
VALUES
(78,' Aton ',28,DEFAULT);
- SELECT( Inquire about )
-- Inquire about
SELECT * FROM table_01;
- Operator
A simple example :
SELECT 2+3;
SELECT 2*3;
SELECT 10/5;
SELECT 2-3;
-- Remainder
SELECT 12%5;
-- Index
SELECT 2^3;
-- square root
SELECT |/ 25.0;
-- Cube root
SELECT ||/ 27.0;
- expression
-- Read SALARY Field is greater than the 50000 The data of
SELECT * FROM COMPANY WHERE salary>5000;
-- Read SALARY Field equals 20000 The data of
SELECT * FROM COMPANY WHERE salary=20000;
-- Read SALARY The field is not equal to 20000 The data of
SELECT * FROM COMPANY WHERE salary>65000;
-- Read SALARY Field is greater than or equal to 65000 The data of
SELECT * FROM COMPANY WHERE salary>=65000;
Logical operators
PostgreSQL There are several logical operators :
SQL Use a three valued logic system , Include true、false and null,null Express " Unknown ".
-- Read AGE Field is greater than or equal to 25 And SALARY Field is greater than or equal to 6500 The data of :
SELECT * FROM company WHERE age>=25 and salary>=6500;
-- Read AGE Field is greater than or equal to 25 or SALARY Field is greater than the 6500 The data of :
SELECT * FROM company WHERE age >=25 or salary>6500;
-- Read SALARY Field is not NULL The data of :
SELECT * FROM company WHERE salary is NOT NULL;
- WHERE Clause
A Boolean expression reads data according to a specified condition :
SELECT * FROM COMPANY WHERE salary=20000;
Numeric expressions are often used in mathematical operations in query statements :
SELECT (17 + 6) AS ADDITION ;
Besides PostgreSQL There are also built-in mathematical functions , Such as :
avg() : Returns the average value of an expression
sum() : Returns the sum of the specified fields
count() : Returns the total number of records queried
-- Total number of records queried
SELECT COUNT(*) AS "RECORDS" FROM COMPANY;
-- Query the current time current_timestamp
SELECT CURRENT_TIMESTAMP;
- AND & OR Operator
WHERE Clause using a comparison operator or a logical operator , for example >, <, =, LIKE, NOT wait
IN following SELECT Statement lists AGE( Age ) Field is 25 or 27 The data of :
SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 );
NOT IN
following SELECT Statement lists AGE( Age ) Field is not 25 or 27 The data of :
SELECT * FROM COMPANY WHERE AGE NOT IN ( 25, 27 );
BETWEEN
following SELECT Statement lists AGE( Age ) Field in 25 To 27 The data of :
SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27;
Subquery
Following SELECT The sentence uses SQL Subquery of , Read... In subquery statement SALARY( Salary ) Field is greater than the 65000 The data of , And then through EXISTS Operator to determine whether it returns a line , If there is a return line, read all AGE( Age ) Field .
SELECT age FROM company WHERE EXISTS (SELECT age from
company WHERE salary >6500);
Read... In subquery statement SALARY( Salary ) Field is greater than the 65000 Of AGE( Age ) Field data , And then use > The operator query is greater than this AGE( Age ) Field data :
-- Read... In subquery statement SALARY( Salary ) Field is greater than the 65000 Of AGE( Age ) Field data , And then use > The operator query is greater than this AGE( Age ) Field data
SELECT * FROM company WHERE age>(SELECT age FROM company WHERE salary>65000);
- UPDATE sentence
to update COMPANY In the table id by 3 Of salary field value :
UPDATE company SET salary = '90000' WHERE id=3;
Simultaneous updating salary Fields and address Value of field :
-- Simultaneous updating salary Fields and address Value of field
UPDATE company SET salary = '10000',address=' Beijing China ' WHERE id=3;
Delete delete
-- Delete id=8 The data of
DELETE FROM company WHERE id=8;
- LIKE Clause
-- find SALARY In the field, use 200 Initial data .
WHERE SALARY::text LIKE '200%'
--------------------------------------------------------------------------------------------------------------
-- find SALARY The field contains 200 Character data .
WHERE SALARY::text LIKE '%200%'
--------------------------------------------------------------------------------------------------------------
-- find SALARY There are... In the second and third positions in the field 00 The data of
WHERE SALARY::text LIKE '_00%'
--------------------------------------------------------------------------------------------------------------
-- find SALARY In the field, use 2 The length of the first character is greater than 3 The data of .
WHERE SALARY::text LIKE '2_%_%'
--------------------------------------------------------------------------------------------------------------
-- find SALARY In the field, use 2 Data at the end
WHERE SALARY::text LIKE '%2'
--------------------------------------------------------------------------------------------------------------
-- find SALARY Field 2 In the second position and with 3 Data at the end
WHERE SALARY::text LIKE '_2%3'
--------------------------------------------------------------------------------------------------------------
-- find SALARY In the field, use 2 start ,3 It ends with 5 Digit data
WHERE SALARY::text LIKE '2___3'
--------------------------------------------------------------------------------------------------------------
example , Extract... From the third place 3 A record
SELECT * FROM COMPANY LIMIT 3 OFFSET 2;
边栏推荐
- sprintf_s的使用方法
- Latex在VSCODE中编译中文,使用中文路径问题解决
- Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
- Flask-Migrate 检测不到db.string() 等长度变化
- 构建学习tensorflow
- Fe - eggjs combined with typeorm cannot connect to the database
- Redis——Cluster数据分布算法&哈希槽
- VSCODE 安装LATEX环境,参数配置,常见问题解决
- qq邮箱接收不到jenkins构建后使用email extension 发送的邮件(timestamp 或 auth.......)
- Fe - wechat applet - Bluetooth ble development research and use
猜你喜欢
CTF three count
Blog directory of zzq -- updated on 20210601
Redis - big key problem
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
广告业务Bug复盘总结
查询GPU时无进程运行,但是显存却被占用了
pytest(1) 用例收集规则
Vscode installation, latex environment, parameter configuration, common problem solving
The default Google browser cannot open the link (clicking the hyperlink does not respond)
pytest(2) mark功能
随机推荐
Tensorrt command line program
sprintf_ How to use s
Sentinel rules persist to Nacos
Distributed transactions: the final consistency scheme of reliable messages
华为MindSpore开源实习机试题
记录一次RDS故障排除--RDS容量徒增
selenium+msedgedriver+edge浏览器安装驱动的坑
Linux MySQL 5.6.51 community generic installation tutorial
Vscode installation, latex environment, parameter configuration, common problem solving
Loops in tensorrt
Latex warning: citation "*****" on page y undefined on input line*
浏览器滚动加载更多实现
The default Google browser cannot open the link (clicking the hyperlink does not respond)
table 组件指定列合并行方法
Automation - when Jenkins pipline executes the nodejs command, it prompts node: command not found
Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决
Blog directory of zzq -- updated on 20210601
Warp matrix functions in CUDA
MySql索引
20210306 reprint how to make TextEdit have background pictures