Creating PostgreSQL Databases
postgresql
themore you do it, the better you get out of it
Course content
Structure of postgreSQL Databases
Postresql Data Types
Database Normalization
Access control
PostreSQL is object-relational database mangement system
system components are considered objects
the top-level object is the database
CREATE DATABASE sba;
-- Define the business_type table below
CREATE TABLE business_type (
id serial PRIMARY KEY,
description TEXT NOT NULL
);
-- Define the applicant table below
CREATE TABLE applicant (
id serial PRIMARY KEY,
name text NOT NULL,
zip_code CHAR(5) NOT NULL,
business_type_id INTEGER references business_type(id)
);
ALTER TABLE account ADD column date_opened DATE;
ALTER TABLE account RENAME column short_name To nick_name;
ALTER TABLE account DROP Column date_opened;what fields should i use?
how many tables should i add?
which data types are best to use for the fields of my table
Schema
Three Schema Architecture



why we have three schemas?
to acheive data independece, that meaning if i do some changes to a certain schema, the higher ones still not affected
for example
if i add specific allocation to a physical schema, should the conceptual schema be affected?
no
If i add new table to the conceptual schema, should external schema affected?
it dependes, meaning that the external schema which allowed to see the table will be affected xD
Data model
[1] physical
[2] logical

Mapping
the request goes from external schema to the physical back and forth
the transferring of request between different schema is called "Mapping"

An important use-case for PostgreSQL schemas is the ability to provide database users with their own group of tables that are only accessible to each individual user, such that users' database access does not interfere with others. In the name of security, this can be taken one step further to separate any production tables from being manipulated by unauthorized users. Schemas allow these divisions to be created without the use of multiple databases which can reduce maintenance requirements for database administrators.
schema contains collection of tables, schema can also contain data types and functions
schema uses cases:
provide database users with seperate enviroment, for example giving each user his own set of tables to use without intefering with other users


The default schema
the public schema is the default schema in postreSQL
The public schema of a PostgreSQL database is created by default when a new database is created. All users by default have access to this schema unless this access is explicitly restricted. When a database is going to be used by a single user and does not have complex groupings of data objects beyond what can naturally be supported by an object-relational database, the public schema will usually suffice. No additional schemas need to be added to such a database.
Create schema
an exercise: creating 2 schemas each one has it's own table
Data types
using data types wisely is computer sciene man :D
Text datatype:
TEXT, VARCHAR, CHAR(N)
TEXT:
strings of unlimited length
strings of variable length
Good for text-based values of unknown length
VARCHAR:
strings of unlimited length
strings of variable length
Restrictions can be imposed on columns values
VARCHAR(N)
without specificying the N, it's equivalent to use
TEXTallow strings that are less than N characters to be stored in the column without any error
CHAR:
`CHAR(N) values consist of exactly N characters
strings are right-padded with spaces if we insert less than N
CHARonly is equivalent toCHAR(1)

Numeric types
SMALLINT -> small-range integer (age)
INTEGER
BIGINT -> visa or master card
SERIAL (incremetal)
BIGSERIAL
DECIMAL(precision, scale), and NUMERIC (user-specificed precision)
REAL -> 6 decimal of precision
DOUBLE PRECISION -. 15 decimal precision
Boolean and temporal
Boolean or bool
Temporal: when representing a date and/or a time related to a table record
TIMESTAMP -> date and time
DATE -> date
TIME --> time
gluing everything together till now
Datebase normalization
normalization is applied to tables to ensure:
no anomalies
the integrity of data.
Access control
fdefault: postgres superuser role is used for adminstrations purposes:
creating databases
dropping databases
inserting records
deleting records
dropping tables
it should be used with care
Creatig new Users
we create user for that specific database
the olduser has no access to tables created by other users
olduser account does not have password by default
Acess privileges
Users are a type of fole
Group roles can aslo be defined
Database objects access given to roles (database itself, tables as well as schemas);
when creating a database object, the user that created the object owns it
other roles can access th object is granted privileges to access the object by it's owner
GRANT:
SELECTUPDATEDELETE
when this table is created, it was created by superuser (postres), therefore when a new user account is created yousef for example will not have the privileges to access or modify the account table
we give privileges to users based on their scope of actions
though we have some limitation of the GRANT command
while many of the privileges on a table can be granted directly to a role the onwer, certain commands can only be executed by the table's owner
for example
modifiying the table structure:
renaming the table or columns
adding new column
only this can be done if we altered the table and give the ownership to yousef
Hierarchical access control
Hierarchical privileges are done using groups and schemas
schema-based access
schema - names container for database objects such as tables and other things
schemas can be used for access control
granting privileges specicif to different schemas in a databas
can be used to easily control access to multiple objects simulataneously
look for this scenario to understand better
group-based
Group - a type of role that indentifies one or more users
Access control can be applied at group level
with such a setup, the family group can be given access to all tables in the public schema, each family member could have then a schema of a their own for maintaing their individua financial date.
another case study:
3 developer are hired into a new company, before starting to work, each one is given a new account
in this case, the software development team will make a development enviroment seperated from the production enviroment so the team lead decided to make a development schema seperated from production (public) one
Removing access
using REVOKE
suppose you have this scenairo where you give your cousin all the privileges on the database schema and after then you found he mistakenly deleted it, so you decided to revoke the delete or truncate statement from here
REVOKE can remove users from groups
use case
Summing all up to this moment
Next Steps
Database objects (e.g views and functions)
Data types (e.g geometric and array-based)
Normalization (e.g 4NF)
Access control (further)
Last updated