0% found this document useful (0 votes)
23 views44 pages

Day+15+ +Course+Slides

Uploaded by

harleen dhingra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views44 pages

Day+15+ +Course+Slides

Uploaded by

harleen dhingra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Day 15

User management

superuser All privileges

Create new users

New users All privileges

Create new object


Existing object ✓ SELECT,
✓ INSERT,
Owner Privileges are needed ✓ UPDATE,
✓ DELETE,
✓ TRUNCATE,
Modify and delete ✓ USAGE
User management
✓ SELECT,
✓ INSERT,
Assign
✓ UPDATE,
Create new users New users ✓ DELETE,
✓ TRUNCATE,
✓ USAGE

Assign

✓ SELECT,
✓ INSERT,
Assign
✓ UPDATE,
Create new roles role ✓ DELETE,
✓ TRUNCATE,
✓ USAGE
User management

Create new users Sarah


Frank

Assign

Assign ✓ SELECT,
Create new roles Analyst ✓ USAGE
CREATE USER

CREATE USER <user_name>


WITH PASSWORD 'pwd123'

Create new users


✓ Create objects
✓ Use objects

Create database public


CREATE USER

CREATE USER <user_name>


WITH PASSWORD 'pwd123'

Role = User + Login


CREATE USER

CREATE USER <user_name> CREATE ROLE <role_name>


WITH PASSWORD 'pwd123' WITH LOGIN PASSWORD 'pwd123'

Role = User + Login


CREATE USER

DROP USER <user_name> DROP ROLE <role_name>


Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE,
✓ DELETE, Priviliges
✓ TRUNCATE,
✓ USAGE,
✓ ALL

GRANT privilege
ON database_object
TO USER | ROLE | PUBLIC
Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE,
✓ DELETE, Priviliges
✓ TRUNCATE,
✓ USAGE,
✓ ALL

GRANT SELECT
ON customer
TO nikolai
Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE, TABLES
✓ DELETE,
✓ TRUNCATE,
✓ USAGE,
✓ ALL

GRANT SELECT
ON customer
TO nikolai
Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE, TABLES
✓ DELETE,
✓ TRUNCATE,
✓ USAGE,
✓ ALL

GRANT SELECT
ON ALL TABLES IN SCHEMA schema_name
TO nikolai
Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE, TABLES
✓ DELETE,
✓ TRUNCATE,
✓ USAGE,
✓ ALL

GRANT ALL
ON ALL TABLES IN SCHEMA schema_name
TO nikolai
Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE, TABLES
✓ DELETE,
✓ TRUNCATE,
✓ USAGE,
✓ ALL

superuser
GRANT ALL
ON ALL TABLES IN SCHEMA schema_name
TO nikolai
owner
Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE, TABLES
✓ DELETE,
✓ TRUNCATE,
✓ USAGE,
✓ ALL

superuser
GRANT SELECT
ON ALL TABLES IN SCHEMA schema_name
TO nikolai WITH GRANT OPTION
owner
Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE,
✓ DELETE, Priviliges
✓ TRUNCATE,
✓ USAGE,
✓ ALL

REVOKE privilege
ON database_object
FROM USER | ROLE | PUBLIC
Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE,
✓ DELETE, Priviliges
✓ TRUNCATE,
✓ USAGE,
✓ ALL

REVOKE privilege
ON database_object
FROM USER | ROLE | PUBLIC
GRANTED BY USER | ROLE
Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE,
✓ DELETE, Priviliges
✓ TRUNCATE,
✓ USAGE,
✓ ALL

REVOKE GRANT OPTION FOR privilege


ON database_object
FROM USER | ROLE | PUBLIC
GRANTED BY USER | ROLE
Privileges

Privilege Applicable Object Types


SELECT TABLE (and table-like objects), table column

INSERT TABLE, table column

UPDATE TABLE, table column

DELETE TABLE

TRUNCATE TABLE

CREATE DATABASE, SCHEMA

CONNECT DATABASE

EXECUTE FUNCTION, PROCEDURE

USAGE SCHEMA
Privileges
How to grant acces?
Typical statements
CREATE USER

CREATE USER amar


WITH PASSWORD 'amar1234';

GRANT USAGE on schema GRANT SELECT & UPDATE

GRANT USAGE GRANT SELECT, UPDATE


ON SCHEMA name ON customer
TO amar; TO amar;
Privileges
How to grant acces?
Typical statements

GRANT all privileges on schema

GRANT ALL
ON ALL TABLES IN SCHEMA public
TO amar ;

GRANT all privileges on database

GRANT ALL
ON DATABASE greencycles
TO amar ;
Privileges
How to grant acces?
Typical statements
GRANT createdb

ALTER USER amar CREATEDB;

GRANT roles to user

GRANT sarah TO amar;

GRANT roles to user

GRANT analyst TO amar;


Privileges
How to grant acces?
Typical statements
REVOKE INSERT

REVOKE INSERT ON customer FROM amar;

REVOKE ALL PRIVILEGES

REVOKE ALL PRIVILEGES ON customer FROM PUBLIC ;

REVOKE ROLE

REVOKE analyst FROM amar;


Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE,
✓ DELETE, Priviliges
✓ TRUNCATE,
✓ USAGE,
✓ ALL

REVOKE GRANT OPTION FOR privilege


ON database_object
FROM USER | ROLE | PUBLIC
GRANTED BY USER | ROLE
Privileges
✓ SELECT,
✓ INSERT,
✓ UPDATE,
✓ DELETE, Priviliges
✓ TRUNCATE,
✓ USAGE,
✓ ALL

GRANT SELECT
ON ALL TABLES IN SCHEMA
<schema_name>
TO nikolai
Privileges

USAGE Users
Priveleges Users
Users
Users
Roles



SELECT,
INSERT,
CREATE ROLE <user_name>
✓ UPDATE, WITH LOGIN PASSWORD 'pwd123'
✓ DELETE,
✓ TRUNCATE,
✓ USAGE
Using indexes

Read Write
operations operations
Using indexes

Read Write
operations operations

Understanding Types of
Guidelines Demo
indexes indexes
Using indexes

SELECT
product_id
FROM sales
WHERE customer_id = 5

3, P0625, 5, visa
Table scan
4, P0432, 8, mastercard 6, P0058, 5, mastercard

1, P0494, 4, visa 2, P0221, 5, visa Read-inefficient

Data is stored without a particular order


Using indexes

SELECT
product_id
FROM sales
WHERE customer_id = 5

Location Value
1, P0494, 4, visa 2, P0221, 5, visa
1 4
6, P0058, 5, mastercard 4, P0432, 8, mastercard
2 5
3, P0625, 5, visa
5 8
✓ Indexes help to make data reads faster!

❖ Slower data writes

❖ Additional storage

❖ B-tree Indexes ❖ Bitmap Indexes


Using indexes

SELECT
product_id
FROM sales
WHERE customer_id = 5

Location Value
1, P0494, 4, visa 2, P0221, 5, visa
1 4
6, P0058, 5, mastercard 4, P0432, 8, mastercard
2 5
3, P0625, 5, visa
5 8
Using indexes
Location Value ✓ Different types of indexes
1 4 for different situations
2 5

5 8 ❖ B-tree Indexes ❖ Bitmap Indexes

1, P0494, 4, visa 2, P0221, 5, visa

6, P0058, 5, mastercard 4, P0432, 8, mastercard


3, P0625, 5, visa
❖ B-tree Indexes

A ✓ Multi-level tree structure

1
20 ✓ Breaks data down into pages or blocks
AB AD
1 AE
AC ✓ Should be used for high-cardinality
(unique) columns
1 ABA… 15
ABB… ✓ Not entire table (costy in terms of storage)

ACA…
ACB…
❖ Bitmap index

✓ Particularily good for dataware houses

✓ Large amounts of data + low-cardinality

✓ Very storage efficient

✓ More optimized for read &


few DML-operations
❖ Bitmap index
✓ Particularily good for dataware houses

✓ Large amounts of data + low-cardinality

✓ Very storage efficient


Row_id Value Bit

1 visa 11100
Good for many repeating values

4 mastercard 00011
(dimensionality)
❖ Bitmap index
✓ Particularily good for dataware houses

✓ Large amounts of data + low-cardinality

✓ Very storage efficient

Value 1 2 3 4 5 6 7 8
Good for many repeating values
mastercard x x
(dimensionality)
visa x x x
Guidelines

B-tree Index Bitmap Index

Default index Slow to update

Unique columns Storage efficient


(surrogate key, names)
Great read performance
Guidelines

Should we put index on every column?

No! They come with a cost! Storage + Create/Update time

Only when necessary!

Avoid full table reads

Small tables do not require indexes


Guidelines
On which columns? 1. Large tables

2. Columns that are used as filters


Guidelines

Fact tables B-tree on surrogate key Bitmap key on foreign keys

Are they used in searchs a lot?


Dimension table Size of table

Choose based on cardinality


Demo: Creating indexes

CREATE INDEX index_name


ON table_name [USING method]
(
column_name
[,…]
);
Demo: Creating indexes

CREATE INDEX index_name


ON table_name
(
column_name
);
Demo: Creating indexes

CREATE INDEX index_name


ON table_name
(
column_name1,
column_name2
);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy