首页 > 数据库 >PostgreSQL用psql导入sql文件

PostgreSQL用psql导入sql文件

时间:2022-08-30 16:33:49浏览次数:51  
标签:psql city PostgreSQL temp -- lo weather sql SELECT

一、sql文件

---------------------------------------------------------------------------
--
-- basics.sql-
--    Tutorial on the basics (table creation and data manipulation)
--
--
-- src/tutorial/basics.source
--
---------------------------------------------------------------------------

-----------------------------
-- Creating a New Table:
--      A CREATE TABLE is used to create base tables.  PostgreSQL has
--      its own set of built-in types.  (Note that SQL is case-
--      insensitive.)
-----------------------------

CREATE TABLE weather (
        city            varchar(80),
        temp_lo         int,            -- low temperature
        temp_hi         int,            -- high temperature
        prcp            real,           -- precipitation
        date            date
);

CREATE TABLE cities (
        name            varchar(80),
        location        point
);


-----------------------------
-- Populating a Table With Rows:
--      An INSERT statement is used to insert a new row into a table.  There
--      are several ways you can specify what columns the data should go to.
-----------------------------

-- 1. The simplest case is when the list of value correspond to the order of
--    the columns specified in CREATE TABLE.

INSERT INTO weather
    VALUES ('San Francisco', 46, 50, 0.25, '1994-11-27');

INSERT INTO cities
    VALUES ('San Francisco', '(-194.0, 53.0)');

-- 2. You can also specify what column the values correspond to.  (The columns
--    can be specified in any order.  You may also omit any number of columns,
--    e.g., unknown precipitation below.

INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)
    VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');

INSERT INTO weather (date, city, temp_hi, temp_lo)
    VALUES ('1994-11-29', 'Hayward', 54, 37);


-----------------------------
-- Querying a Table:
--      A SELECT statement is used for retrieving data.  The basic syntax is
--      SELECT columns FROM tables WHERE predicates.
-----------------------------

-- A simple one would be:

SELECT * FROM weather;

-- You may also specify expressions in the target list.  (The 'AS column'
-- specifies the column name of the result.  It is optional.)

SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;

-- If you want to retrieve rows that satisfy certain condition (i.e., a
-- restriction), specify the condition in WHERE.  The following retrieves
-- the weather of San Francisco on rainy days.

SELECT *
    FROM weather
    WHERE city = 'San Francisco'
        AND prcp > 0.0;

-- Here is a more complicated one.  Duplicates are removed when DISTINCT is
-- specified. ORDER BY specifies the column to sort on.  (Just to make sure the
-- following won't confuse you, DISTINCT and ORDER BY can be used separately.)

SELECT DISTINCT city
    FROM weather
    ORDER BY city;


-----------------------------
-- Joins Between Tables:
--      queries can access multiple tables at once or access the same table
--      in such a way that multiple instances of the table are being processed
--      at the same time.
-----------------------------

-- The following joins the weather table and the cities table.

SELECT * FROM weather JOIN cities ON city = name;

-- This prevents a duplicate city name column:

SELECT city, temp_lo, temp_hi, prcp, date, location
    FROM weather JOIN cities ON city = name;

-- since the column names are all different, we don't have to specify the
-- table name. If you want to be clear, you can do the following. They give
-- identical results, of course.

SELECT weather.city, weather.temp_lo, weather.temp_hi, weather.prcp, weather.date, cities.location
    FROM weather JOIN cities ON weather.city = cities.name;

-- Old join syntax

SELECT *
    FROM weather, cities
    WHERE city = name;

-- Outer join

SELECT *
    FROM weather LEFT OUTER JOIN cities ON weather.city = cities.name;

-- Suppose we want to find all the records that are in the temperature range
-- of other records.  w1 and w2 are aliases for weather.

SELECT w1.city, w1.temp_lo, w1.temp_hi,
       w2.city, w2.temp_lo, w2.temp_hi
FROM weather w1 JOIN weather w2
    ON w1.temp_lo < w2.temp_lo AND w1.temp_hi > w2.temp_hi;


-----------------------------
-- Aggregate Functions
-----------------------------

SELECT max(temp_lo)
    FROM weather;

SELECT city FROM weather
    WHERE temp_lo = (SELECT max(temp_lo) FROM weather);

-- Aggregate with GROUP BY
SELECT city, max(temp_lo)
    FROM weather
    GROUP BY city;

-- ... and HAVING
SELECT city, max(temp_lo)
    FROM weather
    GROUP BY city
    HAVING max(temp_lo) < 40;


-----------------------------
-- Updates:
--      An UPDATE statement is used for updating data.
-----------------------------

-- Suppose you discover the temperature readings are all off by 2 degrees as
-- of Nov 28, you may update the data as follow:

UPDATE weather
    SET temp_hi = temp_hi - 2,  temp_lo = temp_lo - 2
    WHERE date > '1994-11-28';

SELECT * FROM weather;


-----------------------------
-- Deletions:
--      A DELETE statement is used for deleting rows from a table.
-----------------------------

-- Suppose you are no longer interested in the weather of Hayward, then you can
-- do the following to delete those rows from the table.

DELETE FROM weather WHERE city = 'Hayward';

SELECT * FROM weather;

-- You can also delete all the rows in a table by doing the following.  (This
-- is different from DROP TABLE which removes the table in addition to the
-- removing the rows.)

DELETE FROM weather;

SELECT * FROM weather;


-----------------------------
-- Removing the tables:
--      DROP TABLE is used to remove tables.  After you have done this, you
--      can no longer use those tables.
-----------------------------

DROP TABLE weather, cities;

在psql里面执行 \i basics.sql

标签:psql,city,PostgreSQL,temp,--,lo,weather,sql,SELECT
From: https://www.cnblogs.com/shigongp/p/16639856.html

相关文章

  • PostgeSQL入门
    一、什么是PostgreSQLPostgreSQL是一个基于POSTGRES版本4.2的对象关系数据库管理系统(ORDBMS),由加州大学伯克利分校计算机科学系开发。POSTGRES开创了许多概念,这些......
  • 5分钟搞定MySQL/PostgreSQL/Oracle到StarRocks数据迁移同步-CloudCanal实战
    ##简述CloudCanal2.1.0.x版本开始支持StarRocks作为对端的数据迁移同步能力本文通过MySQL->StarRocks的数据迁移同步案例简要介绍这个源端的能力。链路特点:-结......
  • 2022 IEEE 编程语言榜单发布!Python 又双叒叕霸榜了,学 SQL 工作更吃香!
    哈喽兄弟们!近年来,Python宛如一匹黑马,一骑绝尘,横扫TIOBE、StackOverflow等榜单,如今在IEEESpectrum发布的第九届年度顶级编程语言榜单中,Python依然是C、C++......
  • MySQL 主从复制
    1.MySQL主从复制MySQL数据库默认是支持主从复制的,不需要借助于其他的技术,我们只需要在数据库中简单的配置即可。接下来,我们就从以下的几个方面,来介绍一下主从复制:1.1介......
  • myssql编码格式
    下午有条sql对两个表联查 本来以为小意思 leftjoin轻松秒杀结果写完一直报编码格式不匹配一看表结构 字段排序规则不同 一个是utf-general-ci 一个是utf-......
  • mysql之sql调优
    一、性能分析1.慢查询日志:慢查询日志记录了所有执行时间超过指定时间的所有sql配置方法修改my.cnf文件如下:#开启慢日志查询slow_query_log=1......
  • mysql查询
    目录in和exists的区别结论原理in和exists的区别结论A.idin(B)适合子表b比主表a表数据量小的情况。A.idexists(B)则相反原理in子表驱动主表,是先查出(B)的数据,2个......
  • Sql语句查询慢(持续整理中。。。。。。)
    1.坚决不使用“*”来写查询。建索引,2.减少表之间的关联3.优化sql,尽量让sql很快定位数据,不要让sql做全表查询,应该走索引,把数据量大的表排在前面,简化查询字段,没用的......
  • SQL Server 错误:找不到SQL Server Configuration Manager配置管理工具
    问题描述:SQLServer配置管理器可用来管理与SQLServer相关联的服务、配置SQLServer使用的网络协议以及从SQLServer客户端计算机管理网络连接配置。但是升级到win11......
  • SQL Server 错误:无法连接到本地服务器
    问题描述:在与SQLServer建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且SQLServer已配置为允许远程连接。prov......