当前位置:网站首页>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;
原网站

版权声明
本文为[Zhu Weijun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061703164869.html