PostgreSQL入门之CRUD(二)

Postgresql

    • 1.前提
      • 1.GUI Clients:
      • 2. 数据库文件在线生成(生成的数据用于后面的练习)
    • 2.语句

1.前提

1.GUI Clients:

  • DaraGrip 工具,JetBrain公司
  • PgAdmin

2. 数据库文件在线生成(生成的数据用于后面的练习)

数据库文件在线生成网站:www.mockaroo.com
截图:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4L8kMPMD-1588943492657)(C:\Users\lee\Desktop\1.png)]
点击Download Data会下载.sql文件
下载后的.sql文件如下所示(插入语句没有全部显示):

create table person (
	id INT,
	first_name VARCHAR(50),
	last_name VARCHAR(50),
	email VARCHAR(50),
	gender VARCHAR(50),
	date_of_brith DATE,
	country_of_birth VARCHAR(50)
);
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (1, 'Andrus', 'Persse', '[email protected]', 'Male', '11/23/2012', 'Lebanon');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (2, 'Jabez', 'Lydall', '[email protected]', 'Male', '2/28/2018', 'United States');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (3, 'Waldon', 'Atherton', null, null, '8/12/2017', 'Portugal');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (4, 'Jonas', 'Alfuso', '[email protected]', 'Male', '4/30/2017', 'China');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (5, 'Major', 'Gentry', '[email protected]', 'Male', '11/26/2010', 'Vietnam');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (6, 'Gregory', 'Lanchberry', null, null, '2/11/1991', 'Peru');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (7, 'Christabel', 'Fountian', '[email protected]', 'Female', '5/8/2006', 'Venezuela');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (8, 'Betteann', 'McOwen', '[email protected]', 'Female', '10/14/2007', 'Philippines');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (9, 'Teodoro', 'Coleiro', '[email protected]', 'Male', '3/18/2000', 'Ukraine');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (10, 'Aeriela', 'Bonicelli', '[email protected]', 'Female', '12/13/2017', 'China');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (11, 'Fredrika', 'Morin', '[email protected]', 'Female', '11/19/2018', 'Russia');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (12, 'Perren', 'Osmant', '[email protected]', 'Male', '6/29/1991', 'Albania');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (13, 'Rudolfo', 'Dumper', '[email protected]', 'Male', '3/5/2014', 'Brunei');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (14, 'Garnette', 'Peiro', '[email protected]', 'Female', '10/2/2019', 'China');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (15, 'Ibby', 'Korneichuk', '[email protected]', 'Female', '10/28/2016', 'Nigeria');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (16, 'Myrlene', 'Padwick', '[email protected]', 'Female', '10/16/1995', 'Israel');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (17, 'Boniface', 'Espadero', '[email protected]', 'Male', '7/31/2019', 'Uganda');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (18, 'Zebulen', 'Condell', '[email protected]', 'Male', '3/11/2001', 'Poland');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (19, 'Euell', 'Semmence', '[email protected]', 'Male', '3/26/2013', 'Afghanistan');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (20, 'Thorn', 'Klesse', '[email protected]', 'Male', '8/29/2000', 'Tajikistan');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (21, 'Kirsti', 'Jozsa', '[email protected]', 'Female', '6/1/2014', 'Israel');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (22, 'Jessee', 'Grundey', null, null, '5/9/2008', 'France');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (23, 'Sammy', 'Deschelle', null, null, '4/12/2008', 'China');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (24, 'Jewell', 'Audritt', '[email protected]', 'Female', '10/20/2015', 'Philippines');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (25, 'Nananne', 'Henke', null, null, '12/9/2012', 'South Africa');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (26, 'Langsdon', 'Syddie', null, null, '6/29/2003', 'Indonesia');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (27, 'Estella', 'Klezmski', '[email protected]', 'Female', '7/15/2019', 'Haiti');
insert into person (id, first_name, last_name, email, gender, date_of_brith, country_of_birth) values (28, 'Kristofer', 'Bolzmann', '[email protected]', 'Male', '9/23/2008', 'Ethiopia');

如下图所示
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-b7cWsmOy-1588943492661)(C:\Users\lee\Desktop\2.png)]

# 将上面的.sql文件复制到CentOS系统
[root@kfk-pro03 datas]# vim person.sql
[root@kfk-pro03 datas]# pwd
/opt/datas
# 进入数据库,创建person表
[root@kfk-pro03 datas]# su postgres
bash-4.1$ psql leedb
psql (10.12)
Type "help" for help.
# 将.sql导入到名为leedb的数据库
leedb=# \i /opt/datas/person.sql

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9k1BVp9M-1588943492664)(C:\Users\lee\Desktop\3.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9YaRjA8i-1588943492670)(C:\Users\lee\Desktop\4.png)]

下面开始练习

2.语句

  • ORDER BY
SELECT * FROM person ORDER BY country_of_birth DESC;

PostgreSQL入门之CRUD(二)_第1张图片

  • DISTINCT
SELECT DISTINCT country_of_birth FROM person ORDER BY country_of_birth;

PostgreSQL入门之CRUD(二)_第2张图片

  • Limit, Offset & Fetch
SELECT * FROM person LIMIT 10;

PostgreSQL入门之CRUD(二)_第3张图片

SELECT * FROM person LIMIT 5;

PostgreSQL入门之CRUD(二)_第4张图片

SELECT * FROM person OFFSET 5 LIMIT 5;

PostgreSQL入门之CRUD(二)_第5张图片

SELECT * FROM person OFFSET 5;

PostgreSQL入门之CRUD(二)_第6张图片

SELECT * FROM person OFFSET 5 FETCH FIRST 5 ROW ONLY;

PostgreSQL入门之CRUD(二)_第7张图片

SELECT * FROM person OFFSET 5 FETCH FIRST 1 ROW ONLY;

在这里插入图片描述

  • IN
select * from person WHERE country_of_birth IN'China','Mexico','France';

PostgreSQL入门之CRUD(二)_第8张图片

  • GROUP BY
SELECT country_of_birth,COUNT(*) FROM person GROUP BY country_of_birth  ORDER BY country_of_birth;

PostgreSQL入门之CRUD(二)_第9张图片

  • GROUP BY HAVING
SELECT country_of_birth,COUNT(*) FROM person GROUP BY country_of_birth HAVING COUNT(*)>5 ORDER BY country_of_birth;

PostgreSQL入门之CRUD(二)_第10张图片

  • COALESCE
SELECT COALESCE(email,'Email not provided') FROM person;

PostgreSQL入门之CRUD(二)_第11张图片

  • AGE FUNCTION(计算年龄)
SELECT first_name,last_name,gender,country_of_birth,date_of_birth,AGE(NOW(),date_of_birth) AS age FROM person;

PostgreSQL入门之CRUD(二)_第12张图片

  • PRIMARY KEYS
ALTER TABLE person ADD PRIMARY KEY (id);
  • CONSTRAINT
ALTER TABLE person ADD CONSTRAINT unique_eamil_add UNIQUE(email);
  • CHECK CONSTRAINT
ALTER TABLE person ADD CONSTRAINT gender_constraint CHECK (gender='Male' OR gender='Female');

PostgreSQL入门之CRUD(二)_第13张图片

  • FOREIGN KEYS & JOINS
    PostgreSQL入门之CRUD(二)_第14张图片
create table car (
	id BIGSERIAL NOT NULL PRIMARY KEY,
	make VARCHAR(100) NOT NULL,
	model VARCHAR(100) NOT NULL,
	price NUMERIC(19,2) NOT NULL
);

create table person (
	id BIGSERIAL NOT NULL PRIMARY KEY,
	first_name VARCHAR(50) NOT NULL,
	last_name VARCHAR(50) NOT NULL,
	gender VARCHAR(7) NOT NULL,
	email VARCHAR(100),
	date_of_brith DATE NOT NULL,
	country_of_birth VARCHAR(50) NOT NULL,
	car_id BIGINT REFERENCES car (id),
	UNIQUE (car_id)
);

insert into person (first_name, last_name, email, gender, date_of_brith, country_of_birth) values ('Sherwynd', 'Dampier', '[email protected]', 'Male', '12/11/2002', 'China');
insert into person (first_name, last_name, email, gender, date_of_brith, country_of_birth) values ('Brendis', 'Schaffel', '[email protected]', 'Male', '1/17/2017', 'French Polynesia');
insert into person (first_name, last_name, email, gender, date_of_brith, country_of_birth) values ('Phil', 'Bentinck', '[email protected]', 'Female', '10/19/2005', 'Indonesia');

insert into car (id, make, model, price) values (1, 'Dodge', 'Caliber', '6750.45');
insert into car (id, make, model, price) values (2, 'BMW', 'X3', '7071.84');
insert into car (id, make, model, price) values (3, 'Ford', 'F450', '2877.61');

PostgreSQL入门之CRUD(二)_第15张图片
PostgreSQL入门之CRUD(二)_第16张图片

  • INNER JOINS
    PostgreSQL入门之CRUD(二)_第17张图片
    PostgreSQL入门之CRUD(二)_第18张图片

  • LEFT JOIN PostgreSQL入门之CRUD(二)_第19张图片
    PostgreSQL入门之CRUD(二)_第20张图片
    PostgreSQL入门之CRUD(二)_第21张图片
    PostgreSQL入门之CRUD(二)_第22张图片

你可能感兴趣的:(PostgreSQL)