用PostgreSQL运行文件中的SQL程序

首先文件内容如下:

$ ls
barcode.sql                 drop_tables.sql  orderline.sql
create_tables-bpsimple.sql  item.sql         PostgreSQL.md
customer.sql                orderinfo.sql    stock.sql

然后创建数据库bpsimple

$ su
密码:
# su - postgres
$ createdb bpsimple
Password:

然后可以两次退出exit,返回原来的用户进行操作,比较安全。

首先在数据库bpsimple创建中创建各种表:

$ psql -U postgres -d bpsimple -f create_tables-bpsimple.sql
Password for user postgres:
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
CREATE TABLE
  • 其中create_tables-bpsimple.sql中的内容如下:
CREATE TABLE customer ( customer_id serial , title char(4) ,fname varchar(32) , lname varchar(32) NOT NULL, addressline varchar(64) , town varchar(32) , zipcode char(10) NOT NULL, phone varchar(16) , CONSTRAINT customer_pk PRIMARY KEY(customer_id)
);CREATE TABLE item ( item_id serial , description varchar(64) NOT NULL, cost_price numeric(7,2) , sell_price numeric(7,2) , CONSTRAINT item_pk PRIMARY KEY(item_id)
);CREATE TABLE orderinfo ( orderinfo_id serial , customer_id integer NOT NULL, date_placed date NOT NULL, date_shipped date , shipping numeric(7,2) , CONSTRAINT orderinfo_pk PRIMARY KEY(orderinfo_id)
); CREATE TABLE stock ( item_id integer NOT NULL, quantity integer NOT NULL, CONSTRAINT stock_pk PRIMARY KEY(item_id)
);CREATE TABLE orderline ( orderinfo_id integer NOT NULL,item_id integer NOT NULL, quantity integer NOT NULL, CONSTRAINT orderline_pk PRIMARY KEY(orderinfo_id, item_id)
); CREATE TABLE barcode ( barcode_ean char(13) NOT NULL, item_id integer NOT NULL, CONSTRAINT barcode_pk PRIMARY KEY(barcode_ean)
);
  • 其他文件也按照此方法即可创建这个数据库。下面见结果(此处注意语句末尾的“;”):
$ psql -U postgres -d bpsimple
Password for user postgres:
psql.bin (10.4)
Type "help" for help.bpsimple=# table item
bpsimple-# ;item_id |  description  | cost_price | sell_price
---------+---------------+------------+------------1 | Wood Puzzle   |      15.23 |      21.952 | Rubik Cube    |       7.45 |      11.493 | Linux CD      |       1.99 |       2.494 | Tissues       |       2.11 |       3.995 | Picture Frame |       7.54 |       9.956 | Fan Small     |       9.23 |      15.757 | Fan Large     |      13.36 |      19.958 | Toothbrush    |       0.75 |       1.459 | Roman Coin    |       2.34 |       2.4510 | Carrier Bag   |       0.01 |       0.0011 | Speakers      |      19.73 |      25.32
(11 rows)
bpsimple=# \dtList of relationsSchema |   Name    | Type  |  Owner
--------+-----------+-------+----------public | barcode   | table | postgrespublic | customer  | table | postgrespublic | item      | table | postgrespublic | orderinfo | table | postgrespublic | orderline | table | postgrespublic | stock     | table | postgres
(6 rows)
bpsimple=# table customer;customer_id | title |   fname   |  lname  |   addressline    |   town    |  zip
code   |  phone
-------------+-------+-----------+---------+------------------+-----------+-----
-------+----------1 | Miss  | Jenny     | Stones  | 27 Rowan Avenue  | Hightown  | NT2
1AQ    | 023 98762 | Mr    | Andrew    | Stones  | 52 The Willows   | Lowtown   | LT5
7RA    | 876 35273 | Miss  | Alex      | Matthew | 4 The Street     | Nicetown  | NT2
2TX    | 010 45674 | Mr    | Adrian    | Matthew | The Barn         | Yuleville | YV672WR   | 487 38715 | Mr    | Simon     | Cozens  | 7 Shady Lane     | Oakenham  | OA3
6QW    | 514 59266 | Mr    | Neil      | Matthew | 5 Pasture Lane   | Nicetown  | NT3
7RT    | 267 12327 | Mr    | Richard   | Stones  | 34 Holly Way     | Bingham   | BG4
2WE    | 342 59828 | Mrs   | Ann       | Stones  | 34 Holly Way     | Bingham   | BG4
2WE    | 342 59829 | Mrs   | Christine | Hickman | 36 Queen Street  | Histon    | HT3
5EM    | 342 543210 | Mr    | Mike      | Howard  | 86 Dysart Street | Tibsville | TB3
7FG    | 505 548211 | Mr    | Dave      | Jones   | 54 Vale Rise     | Bingham   | BG3
8GD    | 342 826412 | Mr    | Richard   | Neill   | 42 Thatched Way  | Winersby  | WB3
6GQ    | 505 648213 | Mrs   | Laura     | Hardy   | 73 Margarita Way | Oxbridge  | OX2
3HX    | 821 233514 | Mr    | Bill      | Neill   | 2 Beamer Street  | Welltown  | WT3
8GM    | 435 123415 | Mr    | David     | Hudson  | 4 The Square     | Milltown  | MT2
6RT    | 961 4526
(15 rows)

完整的SQL代码:https://blog.csdn.net/Rong_Toa/article/details/80644443

用PostgreSQL运行文件中的SQL程序相关推荐

  1. IDEA去除mapper.xml文件中的sql语句的背景色

    2019独角兽企业重金招聘Python工程师标准>>> IDEA版本 2017.3 mapper.xml文件中的sql语句,总是黄色一大片,看起来不舒服. 按如下设置进行设置即可 此 ...

  2. idea查看项目pid_intellij idea 插件开发--快速定位到mybatis mapper文件中的sql

    intellij idea 提供了openApi,通过openApi我们可以自己开发插件,提高工作效率.这边直接贴个链接,可以搭个入门的demo:http://www.jianshu.com/p/24 ...

  3. 开机后自动执行bat文件中的python程序,自动登录软件进行功能配置

    文章目录 1.bat文件 2.使用pyautogui进行自动点击 1.bat文件 bat文件的作用是可以和cmd命令行一样自动执行,将其放置到开机自启动文件夹中,便可以实现开机后的自动执行. 开机自启 ...

  4. 记事本打开 .exe 运行文件,发现所有程序文件都默认记事本打开了怎么解决问题

    无意间用记事本打开.exe 文件,想查看其内部的实际信息,却发现所有程序文件(例 如浏览器,dos界面等)都变成了只能由记事本打开.是不是内心有种混乱加恐惧,dos界面打不开,注册表打不开,浏览器打不 ...

  5. 从sql文件中读取sql语句执行后报语法错误问题的解决办法

    一般的sql文件保存时都为UTF-8编码,默认(如果sql文件中有中文字符)会有一个BOM头,读取并执行的时候会报syntax error语法错误(这个错误搞了好几天,郁闷),这个时候需要将sql文件 ...

  6. mybatis的mapper文件中,sql插入语句

    sql语句没有对字段判断 <insert id="insert">insert into student (id, name, age,money, cla_code, ...

  7. mysql 生明变量_在 MySQL 的 SQL 文件中,定义变量与使用变量

    1.现阶段存在一个新需求,需要在部署产品时,针对产品做一些初始化数据的工作.预先准备好相应的 SQL 文件.由于需要执行多条 SQL 语句,且后续的 SQL 语句依赖于之前的 SQL 语句生成的主键 ...

  8. if mybatis tk 多个_面试题:mybatis 中的 DAO 接口和 XML 文件里的 SQL 是如何建立关系的?...

    前言 这是 mybatis 比较常问到的面试题,我自己在以前的面试过程中被问到了2次,2次都是非常重要的面试环节,因此自己印象很深刻. 这个题目我很早就深入学习了,但是一直没有整理出来,刚好最近一段时 ...

  9. (转)MyBatis框架的学习(四)——Mapper.xml文件中的输入和输出映射以及动态sql

    http://blog.csdn.net/yerenyuan_pku/article/details/71893689 前面对MyBatis框架的学习中,我们对Mapper.xml映射文件多少有些了解 ...

最新文章

  1. es类比关系型数据库
  2. 学习进度条(第十五周)
  3. redis设置开机自启动
  4. jQuery.ajaxPrefilter()函数的使用
  5. 微软web服务器组件,iis8.0安装包微软Web服务器组件 官方版
  6. phpfpm内存越来越高_DDR5内存规范发布
  7. 深入浅出理解 COOKIE MAPPING
  8. C语言知识点总结(三)
  9. Hybrid APP的离线存储总结
  10. 计算机类毕业设计选题推荐 springboot+vue宠物医院管理系统 java宠物预约挂号系统 微服务 宠物挂号系统 宠物医院预约挂号系统 springboot宠物诊所
  11. element中Tree组件更新后不自动收起
  12. Gateway网关- 微服务(十五)
  13. gc buffer busy acquire 、gc buffer busy release
  14. Android Room的使用
  15. C/C++动态申请空间方式
  16. 量子计算机不是永动机,几种人类设计的永动机,最后一个是谁设计的!彻底服了!...
  17. 微信公众号都有哪些营销技巧
  18. Excel如何对比两个单元格是否完全一致
  19. GP2Y1010AU0F使用心得
  20. ”未获得合适的许可,你可能是盗版软件的受害者相关解决办法“

热门文章

  1. leetcode题解279-完全平方数
  2. 深度优先搜索之城堡问题
  3. Java volatile关键字详解
  4. some interview question
  5. solidity合约详解
  6. [one day one question] GIF动画为什么只动一次不能循环
  7. Java作业-多线程
  8. 什么是urlencode,什么时候能用到
  9. 【VS开发】VS2010中导入ActiveX控件
  10. 创建线程的两种方式:继承Thread类和实现Runnable接口