当前位置:网站首页>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;
边栏推荐
- Latex error: the font size command \normalsize is not defined problem solved
- 20210306转载如何使TextEdit有背景图片
- No process runs when querying GPU, but the video memory is occupied
- 奇葩pip install
- Virtualenv and pipenv installation
- After reading useful blogs
- pytest(3)parametrize参数化
- The intern left a big hole when he ran away and made two online problems, which made me miserable
- 一口气说出 6 种实现延时消息的方案
- Latex warning: citation "*****" on page y undefined on input line*
猜你喜欢
js中map和forEach的用法
Linux MySQL 5.6.51 community generic installation tutorial
qq邮箱接收不到jenkins构建后使用email extension 发送的邮件(timestamp 或 auth.......)
Sublime Text 配置php编译环境
CTF web practice competition
Latex error: the font size command \normalsize is not defined problem solved
代码技巧——Controller参数注解@RequestParam
unittest. Texttestrunner does not generate TXT test reports
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
Redis - hot key issues
随机推荐
实现strStr() II
Find the highest value of the current element Z-index of the page
CTF three count
[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
unittest. Texttestrunner does not generate TXT test reports
Eggjs -typeorm 之 TreeEntity 实战
Storage space modifier in CUDA
Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
Redis——大Key问题
Redis - grande question clé
一口气说出 6 种实现延时消息的方案
Fe - wechat applet - Bluetooth ble development research and use
Detailed definition of tensorrt data format
There are multiple good constructors and room will problem
Warp shuffle in CUDA
ModuleNotFoundError: No module named ‘jieba.analyse‘; ‘jieba‘ is not a package
Alibaba cloud MFA binding Chrome browser
web自动化切换窗口时报错“list“ object is not callable
Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?
如何调试微信内置浏览器应用(企业号、公众号、订阅号)