数据库文件在线生成网站: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)]
下面开始练习
SELECT * FROM person ORDER BY country_of_birth DESC;
SELECT DISTINCT country_of_birth FROM person ORDER BY country_of_birth;
SELECT * FROM person LIMIT 10;
SELECT * FROM person LIMIT 5;
SELECT * FROM person OFFSET 5 LIMIT 5;
SELECT * FROM person OFFSET 5;
SELECT * FROM person OFFSET 5 FETCH FIRST 5 ROW ONLY;
SELECT * FROM person OFFSET 5 FETCH FIRST 1 ROW ONLY;
select * from person WHERE country_of_birth IN('China','Mexico','France');
SELECT country_of_birth,COUNT(*) FROM person GROUP BY country_of_birth ORDER BY country_of_birth;
SELECT country_of_birth,COUNT(*) FROM person GROUP BY country_of_birth HAVING COUNT(*)>5 ORDER BY country_of_birth;
SELECT COALESCE(email,'Email not provided') FROM person;
SELECT first_name,last_name,gender,country_of_birth,date_of_birth,AGE(NOW(),date_of_birth) AS age FROM person;
ALTER TABLE person ADD PRIMARY KEY (id);
ALTER TABLE person ADD CONSTRAINT unique_eamil_add UNIQUE(email);
ALTER TABLE person ADD CONSTRAINT gender_constraint CHECK (gender='Male' OR gender='Female');
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');