1 Introduction to Experiments VietTrung Tran is.hust.edu.vn~trungtv trungtvsoict.hust.edu.vn Department of Information Systems School of Information and Communication Technology Course page • moodletrungtv.rhcloud.com – homework – course schedule – etc Objective • Providing practical skills implement and exploit database systems with a relational database management system (RDBMS) supporting SQL language • Using PostGreSQL, opensource DBMS Evaluation Method • Continuous Assessment: 20% • Project: 80% – Implementation: 50% – Writen report: 20% – Presentation: 10% 2 Check for psql • Install Vagrant – http:www.vagrantup.comdownloads.html • root password: hut123 – su – hut123 • Open terminal – type: psql Install postgresdb • https:fedoraproject.orgwikiPostgreSQL • sudo yum install postgresqlserver postgresqlcontrib • https:www.digitalocean.comcommunity tutorialshowtoinstallandusepostgresqlonubuntu1204 • sudo aptget install postgresql postgresqlcontrib • sudo aptget install postgresqlclient Postgres on windows • PgAdmin Postgres Interface • http:wiki.postgresql.orgwiki Community_Guide_to_PostgreSQL_GUI_Tool spgAdmin_III – PgAdmin – phpPgMyAdmin 3 Resources • http:www.postgresql.orgdocs8.3 interactiveindex.html • Tutorial – http:www.tutorialspoint.compostgresql postgresql_insert_query.htm Creating a Database • CREATEDB • CREATE TABLE table_name (attr_name1 datatype, attr_name2 datatype, ….. ) Database Access • Managing database access permissions using the concept of roles. • Depending on how the role is set up, a role can be – 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. Database Roles • 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 http:www.postgresql.orgdocs8.3interactivesqlcreaterole.html CREATE ROLE name WITH option ... 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 4 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 5 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 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=> PSQL commands • d: display list of tables • d : display object structure object~ table, index, sequence, view • System tables – Pg_user: list of users; – Pg_group: list of groups To do • Studying Dellstore database – http:pgfoundry.orgprojectsdbsamples • Discussing project organization 6 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 322004 • 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 SQL queries Data types • http:www.postgresql.orgdocs9.3static datatype.html 7 Data types cont’d Create table 8 Foreign key Alter table 9 Drop table Insert into 10
Course page • moodle-trungtv.rhcloud.com Introduction to Experiments – homework – course schedule – etc Viet-Trung Tran is.hust.edu.vn/~trungtv/ trungtv@soict.hust.edu.vn 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% Check for psql • Install Vagrant – http://www.vagrantup.com/downloads.html • root password: hut@123 – su – hut@123 • Open terminal – type: psql Install postgresdb • https://fedoraproject.org/wiki/PostgreSQL • sudo yum install postgresql-server postgresql-contrib • https://www.digitalocean.com/community/ 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 • http://wiki.postgresql.org/wiki/ Community_Guide_to_PostgreSQL_GUI_Tool s#pgAdmin_III – PgAdmin – phpPgMyAdmin Resources • http://www.postgresql.org/docs/8.3/ interactive/index.html • Tutorial – http://www.tutorialspoint.com/postgresql/ postgresql_insert_query.htm Creating a Database • CREATEDB • 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 [http://www.postgresql.org/docs/8.3/interactive/sql-createrole.html] !!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 ! 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 ] 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 : 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 • Studying Dellstore database – http://pgfoundry.org/projects/dbsamples/ • Discussing project organization – Pg_user: list of users; – Pg_group: list of groups Lược đồ sở 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 country có nhiều customer Đưa danh sách phân loại sản phẩm (categories) Đưa danh sách sản phẩm thuộc loại “Documentary” Đưa danh sách loại sản phẩm số lượng sản phẩm thuộc loại Đưa danh sách sản phẩm chưa đặt hàng Đưa danh sách nước có khách hàng Đưa danh sách nước có khách hàng số lượng khách hàng nước Đưa danh sách khách hàng chưa đặ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 ngày 3/2/2004 Thống kê giá trị trung bình đơn đặt hàng Đưa danh sách sản phẩm đặt nhiều Đưa danh sách khách hàng thường xuyên Data types • http://www.postgresql.org/docs/9.3/static/ datatype.html SQL queries Data types [cont’d] Create table Foreign key Alter table Drop table Insert into 10