http://techmaster.vn
CƠ SỞ DỮ LIỆU
PostgreSQL
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
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
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.
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
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
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
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
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
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/
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)
http://techmaster.vn
Giới thiệu pgAdmin
Download pgAdmin tại:
http://www.pgadmin.org/download/
http://techmaster.vn
Giao diện pgAdmin
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
http://techmaster.vn
Tạo database
CREATE DATABASE name
http://techmaster.vn
Xóa database
DROP DATABASE [ IF EXISTS ] name
http://techmaster.vn
Tạo bảng
CREATE TABLE [ IF NOT EXISTS ] table_name (
column_name data_type [ column_constraint ]
| table_constraint
)
http://techmaster.vn
Xóa bảng
DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
http://techmaster.vn
Constraint trong Postgres
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT
http://techmaster.vn
Insert dữ liệu
INSERT INTO table_name [ ( column_name [, ...] ) ]
VALUES ( [, ...] )
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 ]
http://techmaster.vn
Xóa dữ liệu
DELETE FROM [ ONLY ] table_name [ * ] [ [ AS ] alias ]
[ USING using_list ]
[ WHERE condition ]
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 ] ]
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
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)
http://techmaster.vn
Backup và restore database
• Sử dụng pgAdmin để backup và restore database

Cơ sở dữ liệu postgres

Editor's Notes

  • #4 Database là một cơ s