1. Trang chủ
  2. » Công Nghệ Thông Tin

Session1 trungtv dhbkhn

10 761 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 1 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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

Trang 1

Introduction to Experiments

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%

–   Writen report: 20%

Trang 2

•  

•  

•  

•  

•   sudo yum install postgresql-server

•   https://www.digitalocean.com/community/

tutorials/how-to-install-and-use-postgresql-on-ubuntu-12-04

•   sudo apt-get install postgresql

postgresql-•  

Community_Guide_to_PostgreSQL_GUI_Tool s#pgAdmin_III

– –

Trang 3

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 <database_name>

•   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

•   Roles can

Database Roles

maintain a correspondence, but this is not required

per individual database)

•  Syntax [http://www.postgresql.org/docs/8.3/interactive/sql-createrole.html]

!CREATE ROLE name [ [ WITH ] option [ ] ] ! where option can be:

SUPERUSER | NOSUPERUSER | CREATEDB | !

! !NOCREATEDB | CREATEROLE | NOCREATEROLE | !

! !CREATEUSER | NOCREATEUSER | INHERIT | !

! !NOINHERIT | LOGIN | NOLOGIN | !

Trang 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 ]

Trang 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 <object_name> : display object

structure

object~ table, index, sequence, view

•   System tables

To do

•   Studying Dellstore database

•   Discussing project organization

Trang 6

Lược đồ cơ sở dữ liệu

address1, address2, city, state, zip, country,

region, email, phone, creditcardtype, creditcard,

creditcardexpiration, username, password, age,

income, gender)!

quantity, orderdate)!

tax, totalamount)!

special, common_prod_id)!

Viết câu lệnh SQL

loại

từng nước

•   http://www.postgresql.org/docs/9.3/static/

Ngày đăng: 24/05/2016, 15:19

TỪ KHÓA LIÊN QUAN

w