YOU ARE DOWNLOADING DOCUMENT

Please tick the box to continue:

Transcript
Page 1: Cơ sở dữ liệu postgres

http://techmaster.vn

CƠ SỞ DỮ LIỆU

PostgreSQL

Page 2: Cơ sở dữ liệu postgres

http://techmaster.vn

PostgreSQL là gì?

Hệ thống cơ sở dữ liệu

quan hệ/phi quan hệ (NoSQL) viết tắt là Postgres

Page 3: Cơ sở dữ liệu postgres

http://techmaster.vn

Một số khái niệm cơ bản

• Database – Cơ sở dữ liệu

• Table – Bảng dữ liệu

• SQL (Structured Query Language): Ngôn ngữ truy vấn và thao tác dữ liệu

– Thêm, sửa, xóa dữ liệu

– Truy vấn dữ liệu

Page 4: Cơ sở dữ liệu postgres

http://techmaster.vn

Chuẩn hóa dữ liệu

1. First Normal Form (1NF)

2. Second Normal Form (2NF)

3. Third Normal Form (3NF)

Chuẩn hóa dữ liệu giúp giảm thiểu dữ liệu dư thừa và giúp dễ dàng thao tác với dữ liệu.

Page 5: Cơ sở dữ liệu postgres

http://techmaster.vn

Chuẩn hóa dữ liệu

Ví dụ một bảng dữ liệu chưa chuẩn hóa về thông tin của các sinh viên và môn học

Cột Subject có chứa các nhóm thông tin trên cùng 1 dòng (Biology, Maths)=> Gây khó khăn khi truy vấn, cập nhật CSDL

Page 6: Cơ sở dữ liệu postgres

http://techmaster.vn

First Normal Form (1NF)

Chuẩn hóa lại theo chuẩn 1NF

Sử dụng cả 2 cột Student và Subject để làm khóa chính, tách dữ liệu ra nhiều dòng=> Dễ cập nhật, truy vấn CSDL nhưng làm tăng dữ liệu dư thừa

Page 7: Cơ sở dữ liệu postgres

http://techmaster.vn

Second Normal Form (2NF)

Theo chuẩn 2NF: Tách bảng Student ra làm 2 bảng

Chứa thông tin về tuổi của student

Chứa thông tin về môn học của student

Page 8: Cơ sở dữ liệu postgres

http://techmaster.vn

Third Normal Form (3NF)

Ví dụ một bảng dữ liệu theo chuẩn 2NF, có chứa thông tin về sinh viên và địa chỉ của sinh viên

Bảng có khóa chính là Student_id. Tuy nhiên các thuộc tính Street, City, State lại phụ thuộc vào Zip

Page 9: Cơ sở dữ liệu postgres

http://techmaster.vn

Third Normal Form (3NF)

Theo chuẩn 3NF, các thuộc tính trong bảng đều phải phụ thuộc vào khóa chính => Tách làm 2 bảng

Bảng Student có khóa chính là Student_id

Bảng Address có khóa chính là Zip, liên kết với bảng Student qua trường Zip

Page 10: Cơ sở dữ liệu postgres

http://techmaster.vn

Cài đặt Postgres

• Tải Postgres tại: http://www.postgresql.org/download/

• Với MacOSX có thể sử dụng Postgres App: http://postgresapp.com/

Page 11: Cơ sở dữ liệu postgres

http://techmaster.vn

Postgres App

Open psql command và sử dụng lệnh create user để tạo thêm user và gán quyền truy cập cho user đó

Tạo tài khoản techmaster với mật khẩu là 123456 và có quyền tạo cơ sở dữ liệu (createdb)

Page 12: Cơ sở dữ liệu postgres

http://techmaster.vn

Giới thiệu pgAdmin

Download pgAdmin tại:

http://www.pgadmin.org/download/

Page 13: Cơ sở dữ liệu postgres

http://techmaster.vn

Giao diện pgAdmin

Page 14: Cơ sở dữ liệu postgres

http://techmaster.vn

Kiểu dữ liệu trong Postgres

• http://www.postgresql.org/docs/9.4/static/datatype.html

• Một số kiểu dữ liệu cơ bản thường dùng:

– Kiểu số: integer, smallint, bigint, serial, smallserial, bigserial, double precision

– Kiểu ký tự: character, character varying, text

– Kiểu datetime: date, timestamp with time zone, timestamp without time zone

– Kiểu boolean: boolean

– Kiểu json: json, jsonb

Page 15: Cơ sở dữ liệu postgres

http://techmaster.vn

Tạo database

CREATE DATABASE name

Page 16: Cơ sở dữ liệu postgres

http://techmaster.vn

Xóa database

DROP DATABASE [ IF EXISTS ] name

Page 17: Cơ sở dữ liệu postgres

http://techmaster.vn

Tạo bảng

CREATE TABLE [ IF NOT EXISTS ] table_name (

column_name data_type [ column_constraint ]

| table_constraint

)

Page 18: Cơ sở dữ liệu postgres

http://techmaster.vn

Xóa bảng

DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]

Page 19: Cơ sở dữ liệu postgres

http://techmaster.vn

Constraint trong Postgres

• NOT NULL

• UNIQUE

• PRIMARY KEY

• FOREIGN KEY

• CHECK

• DEFAULT

Page 20: Cơ sở dữ liệu postgres

http://techmaster.vn

Insert dữ liệu

INSERT INTO table_name [ ( column_name [, ...] ) ]

VALUES ( [, ...] )

Page 21: Cơ sở dữ liệu postgres

http://techmaster.vn

Update dữ liệu

UPDATE [ ONLY ] table_name [ * ] [ [ AS ] alias ]

SET { column_name = { expression } |

( column_name [, ...] ) = ( { expression } [, ...] ) } [, ...]

[ FROM from_list ]

[ WHERE condition ]

Page 22: Cơ sở dữ liệu postgres

http://techmaster.vn

Xóa dữ liệu

DELETE FROM [ ONLY ] table_name [ * ] [ [ AS ] alias ]

[ USING using_list ]

[ WHERE condition ]

Page 23: Cơ sở dữ liệu postgres

http://techmaster.vn

Truy vấn dữ liệu

SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ] * | expression [ [ AS ] output_name ] [, ...]

[ FROM from_item [, ...] ]

[ WHERE condition ]

[ GROUP BY expression [, ...] ]

[ HAVING condition [, ...] ]

[ ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...] ]

[ LIMIT { count | ALL } ]

[ OFFSET start [ ROW | ROWS ] ]

Page 24: Cơ sở dữ liệu postgres

http://techmaster.vn

Truy vấn dữ liệu sử dụng JOIN query

• INNER JOIN (Viết tắt là JOIN)

• FULL OUTER JOIN (Viết tắt là OUTER JOIN)

• LEFT OUTER JOIN (Viết tắt là LEFT JOIN)

• RIGHT OUTER JOIN (Viết tắt là RIGHT JOIN)

• CROSS JOIN

Page 25: Cơ sở dữ liệu postgres

http://techmaster.vn

Aggregate Functions

• Danh sách Aggregate functions trong Postgres: http://www.postgresql.org/docs/9.4/static/functions-aggregate.html

• Một số Aggregate functions hay dùng:

– Count(expression)

– Sum(expression)

– Avg(expresssion)

– Min(expression), Max(expression)

Page 26: Cơ sở dữ liệu postgres

http://techmaster.vn

Backup và restore database

• Sử dụng pgAdmin để backup và restore database


Related Documents