Course page
• moodle-trungtv.rhcloud.com
Introduction to Experiments
– homework
– course schedule
– etc
Viet-Trung Tran
is.hust.edu.vn/~trungtv/
Department of Information Systems
School of Information and Communication Technology
Objective
• Providing practical skills implement and
exploit database systems with a relational
database management system (RDBMS)
supporting SQL language
• Using PostGreSQL, open-source DBMS
Evaluation Method
• Continuous Assessment: 20%
• Project: 80%
– Implementation:
– Writen report:
– Presentation:
50%
20%
10%
1
Check for psql
• Install Vagrant
– />
• root password: hut@123
– su
– hut@123
• Open terminal
– type: psql
Install postgresdb
• />
• sudo yum install postgresql-server
postgresql-contrib
• />tutorials/how-to-install-and-use-postgresqlon-ubuntu-12-04
• sudo apt-get install postgresql postgresqlcontrib
• sudo apt-get install postgresql-client
Postgres on windows
• PgAdmin
Postgres Interface
• />Community_Guide_to_PostgreSQL_GUI_Tool
s#pgAdmin_III
– PgAdmin
– phpPgMyAdmin
2
Resources
• />interactive/index.html
• Tutorial
– />postgresql_insert_query.htm
Creating a Database
• CREATEDB <database_name>
• CREATE TABLE table_name
(attr_name1 datatype,
attr_name2 datatype,
….. )
Database Access
Database Roles
• Managing database access permissions using the concept of
roles.
• Depending on how the role is set up, a role can be
• Separate from operating system users but it might be convenient to
maintain a correspondence, but this is not required.
• Database roles are global across a database cluster installation (and not
per individual database).
• Syntax [ />!!CREATE ROLE name [ [ WITH ] option [ ... ] ] !
– database user, or
– a group of database users
• Roles can
– own database objects (for example, tables) or
– assign privileges on those objects to other roles to control who has
access to which objects.
• Grant membership in a role to another role, thus allowing the
member role use of privileges assigned to the role it is a
member of.
where option can be:
SUPERUSER | NOSUPERUSER | CREATEDB | !
!!NOCREATEDB | CREATEROLE | NOCREATEROLE | !
!!CREATEUSER | NOCREATEUSER | INHERIT | !
!!NOINHERIT | LOGIN | NOLOGIN | !
!!CONNECTION LIMIT connlimit | [ ENCRYPTED | UNENCRYPTED ] !
!!PASSWORD 'password' | VALID UNTIL 'timestamp' | !
!!IN ROLE rolename [, ...] | IN GROUP rolename [, ...] | !
!!ROLE rolename [, ...] | ADMIN rolename [, ...] | !
!!USER rolename [, ...] | SYSID uid !
3
Database Roles (2)
•
•
ALTER ROLE name [ [ WITH ] option [ ... ] ] !
DROP ROLE [ IF EXISTS ] name [, ...] !
•
•
•
CREATE USER name [ [ WITH ] option [ ... ] ] !
ALTER USER username [ [ WITH ] option [ ... ] ]!
DROP USER [option...] [username]!
•
•
•
CREATE GROUP [option...] [groupname]!
ALTER GROUP groupname [ [ WITH ] option [ ... ] ]!
DROP GROUP [option...] [groupname]!
Privileges
• Several different kinds of privilege:
SELECT, INSERT, UPDATE, DELETE, REFERENCES,
TRIGGER, CREATE, CONNECT, TEMPORARY, EXECUTE,
and USAGE.
• GRANT !
• REVOKE!
• ALTER!
GRANT
•
GRANT { { SELECT | INSERT | UPDATE | DELETE | REFERENCES |
TRIGGER } [,...] | ALL [ PRIVILEGES ] } !
!ON [ TABLE ] tablename [, ...] TO { [ GROUP ] rolename |
PUBLIC } [, ...] !
![ WITH GRANT OPTION ] !
• GRANT { { USAGE | SELECT | UPDATE } [,...] | ALL
[ PRIVILEGES ] } !
!ON SEQUENCE sequencename [, ...] TO { [ GROUP ] rolename |
PUBLIC } [, ...] !
![ WITH GRANT OPTION ] !
• GRANT { { CREATE | CONNECT | TEMPORARY | TEMP } [,...] | ALL
[ PRIVILEGES ] } !
!ON DATABASE dbname [, ...] TO { [ GROUP ] rolename |
PUBLIC } [, ...] !
![ WITH GRANT OPTION ] !
GRANT
•
GRANT { EXECUTE | ALL [ PRIVILEGES ] }
ON FUNCTION funcname ( [ [ argmode ] [ argname ] argtype [, ...] ] ) [, ...] TO { [ GROUP ]
rolename | PUBLIC } [, ...]
[ WITH GRANT OPTION ]
•
•
GRANT { USAGE | ALL [ PRIVILEGES ] }
ON LANGUAGE langname [, ...] TO { [ GROUP ] rolename | PUBLIC } [, ...]
[ WITH GRANT OPTION ]
GRANT { { CREATE | USAGE } [,...] | ALL [ PRIVILEGES ] }
ON SCHEMA schemaname [, ...] TO { [ GROUP ] rolename | PUBLIC } [, ...]
[ WITH GRANT OPTION ]
•
GRANT { CREATE | ALL [ PRIVILEGES ] }
ON TABLESPACE tablespacename [, ...] TO { [ GROUP ] rolename | PUBLIC } [, ...]
[ WITH GRANT OPTION ]
•
GRANT role [, ...] TO rolename [, ...] [ WITH ADMIN OPTION ]
4
Accessing a Database
• Running the PostgreSQL interactive terminal
program, called psql, which allows you to
interactively enter, edit, and execute SQL
commands.
• Using an existing graphical frontend tool like
pgAdmin or an office suite with ODBC
support to create and manipulate a database.
These possibilities are not covered in this
tutorial.
• Writing a custom application, using one of the
several available language bindings
PSQL commands
• \d: display list of tables
• \d <object_name> : display object
structure
object~ table, index, sequence, view
• System tables
PSQL
$ psql mysql!
Welcome to psql 8.3.3, the PostgreSQL interactive terminal. !
Type: !\copyright for distribution terms !
!!\h for help with SQL commands !
!!\? for help with psql commands !
!!\g or terminate with semicolon to execute query !
!!\q to quit !
mydb=> !
To do
• Studying Dellstore database
– />
• Discussing project organization
– Pg_user: list of users;
– Pg_group: list of groups
5
Lược đồ cơ sở dữ liệu
• Categories(category, categoryname)!
• cust_hist(customerid, orderid, prod_id)!
• Customers(customerid, firstname, lastname,
address1, address2, city, state, zip, country,
region, email, phone, creditcardtype, creditcard,
creditcardexpiration, username, password, age,
income, gender)!
• Inventory(prod_id, quan_in_stock,sales)!
• Orderlines(orderlineid, orderid, prod_id,
quantity, orderdate)!
• Orders(orderid, orderdate, customerid, netamount,
tax, totalamount)!
• Products(prod_id, category, title, actor, price,
special, common_prod_id)!
Viết câu lệnh SQL
•
•
•
•
•
•
•
•
•
•
•
•
•
Đưa ra country có nhiều customer nhất
Đưa ra danh sách phân loại sản phẩm (categories)
Đưa ra danh sách các sản phẩm thuộc loại “Documentary”
Đưa ra danh sách các loại sản phẩm và số lượng sản phẩm thuộc từng
loại
Đưa ra danh sách các sản phẩm chưa được đặt hàng
Đưa ra danh sách các nước có khách hàng
Đưa ra danh sách các nước có khách hàng và số lượng khách hàng của
từng nước
Đưa ra danh sách khách hàng chưa từng đặt hàng
Thống kê số đơn đặt hàng theo ngày
Thống kê số lượng sản phẩm đặt trong ngày 3/2/2004
Thống kê giá trị trung bình các đơn đặt hàng
Đưa ra danh sách các sản phẩm được đặt nhiều nhất
Đưa ra danh sách khách hàng thường xuyên
Data types
• />datatype.html
SQL queries
6
Data types [cont’d]
Create table
7
Foreign key
Alter table
8
Drop table
Insert into
9
10