PostgreSQL Documentation

Return posted @ Mar 18, 2018 03:59:13 AM in OI with tags PostgreSQL Documentation , 1742 阅读

这篇PostgreSQL教程将提供SQL的基本和高级概念。
对于使用MySQL等其他SQL软件的用户,其之间的区别仅仅在于SQL UI的不同,所以下文所述的SQL基本语句同样适用于这些SQL。

Conventions

Brackets ([ and ]) indicate optional parts.
Braces ({ and }) and vertical lines (l) indecate that you must choose one alternative. 
Dots (...) mean that the preceding element can be repeated.

Install

 

安装显然不应该存在问题 : )

After your installation

在安装之后你可以得到两个程序

 

 

 

pgAdmin4 & SQL shell

SQL shell,是PostgreSQL的交互式终端程序,它被称为psql, 它允许你交互地输入、编辑和执行SQL命令。

你也可以使用一种已有的图形化前端工具,比如pgAdmin来创建和管理数据库。

同时使用有助于你清晰的看到即时修改

pgAdmin 4 System

在输入密码后, 界面应如图所示

 

 

SQL Shell (Highly Recommended)

一路敲回车(表示进入默认数据库),输入口令后回车

如果需要进入新建的数据库,请在Database之后输入你需要访问的数据库名字。

你可以通过输入 \c database_name 以在不同的数据库之间切换

在输入密码后,界面应如图所示

 

 

psql程序有一些不属于SQL命令的内部命令。它们以反斜线 \ 开头。 欢迎信息中列出了一些这种命令。

比如你可以用 \h 获取各种PostgreSQL的SQL命令的帮助语法

或者要退出psql,输入 \q

Console command
\q: Exit.
\h: Help.
\?: View the list of psql commands.
\l: List all databases.
\c [database_name]: Connect to other databases.
\d: List all tables in the current databases.
\d [table_name]: List the structure of a table.

//丑的一无是处

Creating a Database

要创建一个新的数据库,你可以在SQL Shell中输入如下命令:

create database database_name;

这里, database_name 是指定要创建的数据库的名称。

例如要创建一个名为 Test_1 的数据库,一个正确的实例应该如图所示:

 

 

当然你也可以使用UI来引导创建数据库,这里就不赘述了。

你有两种方法可以查看数据库,一种是在pgAdmin4的UI下直观的查看,另一种为使用Console command 中的 \l 语句来查看所有数据库

 

 

 

 

如果你并没有在pgAdmin4里看到你所新建的数据库,那么请右击 Servers 并 Refresh

当然如果你不会,我建议你使用 Ctrl + W 来查看进一步的教程

Deleting a Database

强烈建议使用UI界面删除数据库,当然你也可以使用如下命令:

drop database database_name;

Creating a New Table

你可以通过指定表的名字和所有列的名字及其类型来创建表:

create table table_name(  
    column1 datatype,
    column2 datatype,
    column3 datatype,
    .....  
    columnN datatype,  
    PRIMARY KEY( one or more columns )  
);

在 演习II 的例子中,我们可以这样创建:

create table Table (
    LastName  varchar(255),
    FirstName varchar(255),           
    Address   varchar(255),           
    City      varchar(255)
);

你可以在psql输入这些命令以及换行符,psql可以识别该命令直到分号才结束。

你可以在 SQL 命令中自由使用空白(即空格、制表符和换行符)。 这就意味着你可以用和上面不同的对齐方式键入命令,或者将命令全部放在一行中。

两个划线("--")引入注释。 任何跟在它后面直到行尾的东西都会被忽略。SQL 是对关键字和标识符大小写不敏感的语言,只有在标识符用双引号包围时才能保留它们的大小写(上例没有这么做)。

PostgreSQL支持标准的SQL类型 int、 smallint、 real、 double precision、 char(N) 、 varchar(N)date、 time、 timestamp 和 interval

创建成功之后SQL Shell会反馈 CREATE TABLE

 

 

值得注意的是,你必须为每一列指定其数据类型,否则将为视为语法错误,同样你也必须输入行末的 ; ,这是很多人往往会忽略的。

最后我们还要提到如何删除一个表,这和删除数据库的语句是类似的:

drop table table_name;

Populating a Table With Rows

Insert 语句用于向表中添加行:

insert into table_name (column1, column2, column3,...columnN)  
    values (value1, value2, value3,...valueN),
    (value1, value2, value3,...valueN),
    .....
    (value1, value2, value3,...valueN);

或是:
insert into table_name values (value1, value2, value3,...valueN);

注意 column1column2column3, ..., columnN是要插入数据的表中的列的名称。(你可以通过省略某个colum的名字来忽略该未知列)

插入成功后SQL Shell会反馈 INSERT O 1

 

 

请注意所有数据类型都应使用相当明了的输入格式。那些不是简单数字值的常量通常必需用单引号 ' 包围

Querying a Table

在PostgreSQL中,SELECT 语句用于从数据库表中检索数据。 数据以结果表格的形式返回。 这些结果表称为结果集。

select column1, column2..column from table_name;

如果要从表中检索所有字段,则可以使用以下语法:

select * from table_name;

检索之后的结果应如图所示

 

 

Continuing...

Netpnb Login 说:
Aug 07, 2022 06:12:50 PM

Punjab National Bank is a popular yet most preferred National Bank in India, and the Net Banking service for the bank allows its customers to utilize different services online without investing in the branch. Netpnb Login As the modern days are invoking with the technology, the services from Banks through their Net Banking feature are in good spike, and there are multiple benefits for the customer who does get the access to the PNB Net Banking service.This makes them comfortable in using bank transactions at their fingertips, and yet the online banking is secure which guarded with unique credentials that only allow you to access the online portal.

walden university st 说:
Aug 20, 2022 12:35:30 AM

Welcome to the myWalden student portal. Click below to log in with your Walden University e-mail address and password. walden university student portal How do I reset my Walden portal password? You can reset ... What do I do if I am missing a class in my student portal? Contact your ... Where do I login walden university student portal

AP SSC Gk Question P 说:
Sep 10, 2022 11:33:23 PM

General Knowlorge is most important subject to all Class 10 students studying in English Medium, AP SSC Gk Question Paper Telugu Medium & Urdu Medium of the State Board. So every student who is studying in the state Government & Private Schools can download the AP 10th GK Model Paper 2023 Pdf with answer solutions designed and suggested by subject experts. General Knowlorge is most important subject to all Class 10 students studying in English Medium.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter