当前位置:网站首页>How do novices get started and learn PostgreSQL?
How do novices get started and learn PostgreSQL?
2022-07-07 00:53:00 【Zhu Weijun】
In the recruitment of various data Posts ,SQL It has almost become a necessary skill . Where there is a company, there will be data , Where there is data, there will be a database , Where there is a database, there will be SQL.
SQL How important is it in data analysis ? Let me put it this way , except Excel Outside ,SQL It is the most common tool for data work .
Although in essence SQL and Excel Are used to process and analyze data , But the difference is ,SQL It's a query language , Not visual software , So the learning threshold is higher , It requires users to have more logical thinking .
Of course learning SQL It's not hard , contrast Java、C++、Python This kind of programming language ,SQL The language logic of is simpler , You can start your SQL Of 3 There are four functions to get a preliminary understanding of it .
(1) SQL Data definition function : Responsible for creating 、 modify 、 Delete 、 Indexes 、 View 、 function 、 Objects such as stored procedures and triggers .
(2)SQL Data manipulation function : Be responsible for adding, deleting, modifying and checking the table , Especially user-defined queries , It is the most commonly used scenario in work .
(3)SQL Data control function of : Be responsible for controlling the access rights of users , To ensure the safety of the system .
For this 3 Features , With PostgreSQL For example , We just need to start from the following 4 Just learn from three aspects .
1、 Database and data table
Understanding databases , There are several important concepts that need to be clarified .
The first is the database management system , Database for short , That is to say DBMS, This is the database management software .
There are many choices in the market , such as MySQL、Oracle、PostgreSQL etc. , Different software features are also different , You try to choose the mainstream to learn .
PostgreSQL It is an object of free software with very complete features - Relational database management system (ORDBMS), It supports most SQL Standards and many other modern features , Such as complex queries 、 Foreign keys 、 trigger 、 View 、 Transaction integrity 、 Multi version concurrent control, etc .
Again ,PostgreSQL It can also be extended in many ways , For example, by adding new data types 、 function 、 The operator 、 Aggregation function 、 Index method 、 Process language, etc .
in addition , Because of the flexibility of the license , Anyone can use it for free for any purpose 、 Modify and distribute PostgreSQL.
Let's talk about databases , This is the set of data tables we created in the database software , You can create multiple databases , To store data tables for different purposes , Convenient retrieval .
have access to SQL Code or database tools to create a database , Pay attention to setting database permissions when creating .
stay SQL In the sentence , Use the following statement to create a table :
CREATE DATABASE database_name
Finally, the data table , This is the most basic unit in the database , The data table is stored in the database .
In a relational database , Data is stored in the data table in the form of rows and columns , Each column has a corresponding data type , This is set when creating tables .
stay SQL In the sentence , Use the following statement to create a table :
CREATE TABLE <table_name> (columns_name datatype , ...)
There are some constraints in the data table that need attention , For example, primary key constraints 、 Foreign key constraints 、 Non empty constraint 、 Uniqueness constraint 、 Default constraints, etc , Set according to specific business .
2、 Data types and functions
Like other programming languages ,SQL There are fixed data types and various functions in , In different database software , Data types and functions also differ . But all relational databases will follow SQL Basic grammar rules ,
With PostgreSQL For example , The main data types include 「 Integer types 、 Floating point type 、 Date time type 、 String type 、 Binary type 、 Boolean type 、 An array type 、 Geometric type 、 Network address type 、JSON type 」 etc. , There are many sub types under each type . See the following figure for details :
SQL There are many functions in , Used to calculate and modify data , Based on the data table, you can get any analysis results you want . Generally put SQL Functions are mainly divided into the following 6 Categories: :「 Aggregate functions 、 Conversion function 、 Date time function 、 Mathematical functions 、 String function 、 System processing function .」
In addition to the above functions ,PostgreSQL The system has customized many functions for dealing with special scenes , For example, geometric functions 、 Text search function, etc . Let's say PostgreSQL For example , List the usage of common functions :
Aggregate functions :
Conversion function :
Date time function :
Mathematical functions :
String function :
System function :
There are other things like geometric functions :
3、 Data query
Data query is learning SQL The core purpose , It is also the most daily work of data analysts .
select It's you who study SQL Basic statements of data query , Almost all queries need to use select To achieve .
select Query is also divided into single table query 、 Aggregate query 、 Link query 、 Subquery 、 Merge query .
Single table query ( Query a table ):
SELECT ... FROM table_name
Aggregate query ( Calculate according to a field aggregation ):
SELECT count(column_1) FROM table_name GROUP BY column_2
Link query ( adopt JOIN Connect multiple tables ):
SELECT ... FROM table_name_1 LEFTJOIN tablea_name_2 ON...
Subquery ( Perform nested queries on multiple tables )
SELECT ... FROM table_name_1
WHERE column_1 IN (SELECT column_2 FROM table_name_2)
Merge query ( Merge the query results )
SELECT ... FROM table_name_1
UNION
SELECT ... FROM table_name_2
Several simple examples of common queries are listed above , In daily data analysis , Various query clauses will also be used .
WHERE Clause ( Filter query operations )
SELECT ... FROM fdt WHERE c1 > 5
SELECT ... FROM fdt WHERE c1 IN (1, 2, 3)
SELECT ... FROM fdt WHERE c1 IN (SELECT c1 FROM t2)
SELECT ... FROM fdt WHERE c1 IN (SELECT c3 FROM t2 WHERE c2 = fdt.c1 + 10)
SELECT ... FROM fdt WHERE c1 BETWEEN (SELECT c3 FROM t2 WHERE c2 = fdt.c1 + 10) AND 100
SELECT ... FROM fdt WHERE EXISTS (SELECT c1 FROM t2 WHERE c2 > fdt.c1)
GROUP BY and HAVING Clause
In the past WHERE After filter , The generated input table can use GROUP BY Clause to group , And then use HAVING Clause delete some grouped rows .
SELECT select_list FROM ... [WHERE ...] GROUP BY ... HAVING boolean_expression
ORDER BY Clause
Sort the query results .
SELECT ... FROM table_name
ORDER BY ...
LIMIT and OFFSET
Limit the number of rows returned by the query .
SELECT ...FROM table_name
[ LIMIT { number | ALL } ] [ OFFSET number ]
DISTINCT
Remove duplicates .
SELECT DISTINCT column_name FROM table_name
WITH Inquire about
Define the query statement as a temporary table .
WITH table_name AS (select ...)
4、 data IO And addition, deletion and modification
In addition to querying the data , You also need to learn how to insert data 、 Derived data 、 Update data 、 Delete data .
These belong to the scope of data operation and definition , It is very useful for Data Administrators .
insert data ( Use INSERT sentence )
INSERT INTO tableau_name VALUES (1, 'Cheese', 9.99);
Update data ( Use UPDATE sentence )
UPDATE table_name SET price = 10 WHERE price = 5;
Delete data ( Use DELETE sentence )
Delete table
DELETE FROM table_name
Delete specified row
DELETE FROM table_name WHERE price = 10;
Add column ( Use ALTER sentence )
ALTER TABLE table_name ADD COLUMN description text;
Remove column ( Use ALTER DROP sentence )
ALTER TABLE table_name DROP COLUMN description;
Add constraints ( Use ALTER... sentence )
ALTER TABLE table_name ADD CHECK (name <> '');
ALTER TABLE table_name ADD CONSTRAINT some_name UNIQUE (product_no);
ALTER TABLE table_name ADD FOREIGN KEY (product_group_id) REFERENCES product_groups;
Change the data type of the column (ALTER TYPE sentence )
ALTER TABLE table_name ALTER COLUMN price TYPE numeric(10,2);
To be ranked high (ALTER COLUMN sentence )
ALTER TABLE table_name RENAME COLUMN product_no TO product_number;
rename table (ALTER RENAME)
ALTER TABLE old_table_name RENAME TO new_table_name;
边栏推荐
- New feature of Oracle 19C: automatic DML redirection of ADG, enhanced read-write separation -- ADG_ REDIRECT_ DML
- Uniapp uploads and displays avatars locally, and converts avatars into Base64 format and stores them in MySQL database
- Advanced learning of MySQL -- Fundamentals -- four characteristics of transactions
- Advanced learning of MySQL -- basics -- multi table query -- joint query
- Attention SLAM:一種從人類注意中學習的視覺單目SLAM
- [user defined type] structure, union, enumeration
- 深度学习之环境配置 jupyter notebook
- QT tutorial: creating the first QT program
- VTK volume rendering program design of 3D scanned volume data
- Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
猜你喜欢
做微服务研发工程师的一年来的总结
【软件逆向-求解flag】内存获取、逆变换操作、线性变换、约束求解
Threejs image deformation enlarge full screen animation JS special effect
Mujoco second order simple pendulum modeling and control
[C language] dynamic address book
【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
New feature of Oracle 19C: automatic DML redirection of ADG, enhanced read-write separation -- ADG_ REDIRECT_ DML
Deep understanding of distributed cache design
Deep learning environment configuration jupyter notebook
@TableId can‘t more than one in Class: “com.example.CloseContactSearcher.entity.Activity“.
随机推荐
Mujoco Jacobi - inverse motion - sensor
How to get started and improve test development?
Equals() and hashcode()
Quaternion attitude calculation of madgwick
uniapp中redirectTo和navigateTo的区别
C Primer Plus Chapter 14 (structure and other data forms)
Chapter 5 DML data operation
If the college entrance examination goes well, I'm already graying out at the construction site at the moment
Chapter II proxy and cookies of urllib Library
Attention SLAM:一种从人类注意中学习的视觉单目SLAM
Trace tool for MySQL further implementation plan
Model-Free Prediction
48 page digital government smart government all in one solution
集合(泛型 & List & Set & 自定义排序)
一行代码实现地址信息解析
Service asynchronous communication
【批处理DOS-CMD命令-汇总和小结】-查看或修改文件属性(ATTRIB),查看、修改文件关联类型(assoc、ftype)
build. How to configure the dependent version number in the gradle file
Leetcode (547) - number of provinces
Interface master v3.9, API low code development tool, build your interface service platform immediately