Advanced PostgreSQL Flashcards
How Do I Make Sure My Database Stays Intact?
Data Types As Constraints
Columns of a PostgreSQL database table must have a data type, which constrains the type of information that can be entered into that column. This is important in order to ensure data integrity and consistency over time. Some common PostgreSQL types are integer, decimal, varchar, and boolean. Data types are defined in a CREATE TABLE statement by indicating the data type after each column name.
How Do I Make Sure My Database Stays Intact?
Check Constraints
When using PostgreSQL, it can be important to enforce check constraints on columns of a database table in order to ensure data integrity and consistency over time. Check constraints can be enforced on a single column, multiple columns, or on all columns. They are implemented within a CREATE TABLE statement using the CHECK keyword.
How Do I Make Sure My Database Stays Intact?
Multiple Constraints
Columns in a database table can have multiple constraints. Multiple constraints can be implemented by listing them in a row following the relevant column name and data type in a CREATE TABLE statement. The order of the constraints does not matter.
How Do I Make Sure My Database Stays Intact?
NOT NULL
In PostgreSQL, NOT NULL constraints can be used to ensure that particular columns of a database table do not contain missing data. This is important for ensuring database integrity and consistency over time. NOT NULL constraints can be enforced within a CREATE TABLE statement using NOT NULL.
How Do I Make Sure My Database Stays Intact?
UNIQUE
In PostgreSQL, UNIQUE constraints can be used to ensure that elements of a particular column (or group of columns) are unique (i.e., no two rows have the same value or combination of values). This is important for ensuring database integrity and consistency over time. UNIQUE constraints can be enforced within a CREATE TABLE statement using the UNIQUE keyword.
How Do I Make Sure My Database Stays Intact?
Primary Key Constraint
In PostgreSQL, a primary key constraint indicates that a particular column (or group of columns) in a database table can be used to identify a unique row in that table. In terms of restrictions, this is equivalent to a UNIQUE NOT NULL constraint; however, a table may only have one primary key, whereas multiple columns can be constrained as UNIQUE NOT NULL. A primary key constraint can be enforced within a CREATE TABLE statement using PRIMARY KEY
How Do I Make Sure My Database Stays Intact?
Foreign Key Constraint
In PostgreSQL, a foreign key constraint ensures that the values in a particular column of a database table exactly match values in another database table. This is important to ensure the “referential integrity” of the database. A foreign key constraint can be enforced within a CREATE TABLE statement either by adding REFERENCES other_table_name (other_table_primary_key) after the relevant column name and type or using FOREIGN KEY (column_1, column_2) REFERENCES other_table_name (other_key1, other_key2) to indicate a link between groups of columns.
How Do I Make Sure My Database Stays Intact?
Cascade and Restrict
In PostgreSQL, when implementing foreign key constraints in a database table, it is possible to preemptively specify database behavior when values in a referenced column are updated or deleted. Specifying ON DELETE RESTRICT or ON UPDATE RESTRICT after a foreign key constraint ensures that referenced values/rows cannot be deleted/updated.
Specifying ON DELETE CASCADE or ON UPDATE CASCADE ensures that updated/deleted values/rows in the referenced table are automatically updated/deleted in the referencing table.
How Do I Make Sure My Database Stays Intact?
Updating A Table With Constraints
In PostgreSQL, when implementing a constraint on an existing table, the table must already be consistent with the constraint or PostgreSQL will reject the new constraint. A DB user may backfill the table using UPDATE or ALTER TABLE statements to make the table consistent with the constraint.
How Do I Make Sure My Database Stays Intact?
PostgreSQL’s default Superuser
PostgreSQL database clusters are created with a default role named Postgres. This is a special role that has SUPERUSER privileges and can modify all access restrictions within the database.
How Do I Make Sure My Database Stays Intact?
Principle of Least Privilege
The principle of least privilege says that applications and users should have the minimum permissions required for their function. In PostgreSQL, this means superusers should not be performing routine database tasks and specialized roles should be created for each user or application.
How Do I Make Sure My Database Stays Intact?
Checking User Permissions
In PostgreSQL, a user may check the system table, pg_catalog.pg_roles to understand what permissions users in the DB have. They may also check the information_schema.table_privileges table for more granular description of permissions at a table level.
How Do I Make Sure My Database Stays Intact?
PostgreSQL Roles
In PostgreSQL, roles are a collection of privileges that can be granted to one or more users.
How Do I Make Sure My Database Stays Intact?
SET into other roles in PostgreSQL
In PostgreSQL, a superuser can use SET ROLE to modify the current session to test the permissions of another user.
How Do I Make Sure My Database Stays Intact?
Creating Roles in PostgreSQL
In PostgreSQL, CREATE ROLE statements allow a user to create a new role. Keywords passed to CREATE ROLE like VALID UNTIL, LOGIN, IN GROUP, CREATEUSER, CREATEDB and PASSWORD allow for customization of the new user’s privileges.
– Create a role named miriam
with NOSUPERUSER
(is not a superuser) and LOGIN
(can log in to the DB)
CREATE ROLE miriam WITH NOSUPERUSER LOGIN;
How Do I Make Sure My Database Stays Intact?
GRANTING and REVOKING Database Privileges
In PostgreSQL, GRANT and REVOKE statements can be used to modify what privileges users have on an existing database object (e.g. schemas, databases, sequences, functions, etc.)
How Do I Make Sure My Database Stays Intact?
ALTER DEFAULT Permissions and Privilege
In PostgreSQL, ALTER DEFAULT statements can be used to set privileges on all new objects in a database or schema, this statement does not affect existing objects.
– Alter the default permissions to a schema, combine ALTER DEFAULT
with a GRANT
statement
ALTER DEFAULT PRIVILEGES IN SCHEMA finance GRANT SELECT ON TABLES TO analyst;
How Do I Make Sure My Database Stays Intact?
Creating a Group Role
In PostgreSQL, group roles are used to distribute permissions to sets of users. A CREATE ROLE statement that includes WITH ROLE name name name…. can be used to create a group comprised of a set of existing roles.
– Create a group role named marketing
with alice
and bob
as members.
CREATE ROLE marketing WITH NOLOGIN ROLE alice, bob;
How Do I Make Sure My Database Stays Intact?
Groups and Roles in PostgreSQL
In PostgreSQL, because groups are roles themselves, you can create a role and GRANT access to that group. The privileges granted to that group will also apply to the group’s members.
How Do I Make Sure My Database Stays Intact?
GRANT Permissions for subsets of columns
In PostgreSQL, GRANT statements can be applied to specific columns in a PostgreSQL table to limit the permissions a given user can access or modify.
How Do I Make Sure My Database Stays Intact?
PostgreSQL Row Level Security
In PostgreSQL, row level security is a pattern that only grants access to certain rows of a relation to a given role/group. Row level security can be implemented with CREATE POLICY and ENABLE ROW LEVEL SECURITY statements.
How Do I Make Sure My Database Stays Intact?
Database Transaction with ACID Properties
When working with a database, a single unit of work is defined as a transaction. While this unit of work can vary between databases in terms of its complexity, every transaction must maintain ACID properties. This allows for concurrent use of the database and helps with recovery in the case of a system failure.
How Do I Make Sure My Database Stays Intact?
Atomicity in ACID
When working with a database transaction, the ACID property Atomic means that if any part of the transaction fails, then the entire transaction will fail as well. This means that the database would remain unchanged once the transaction has finished if any one part fails.
How Do I Make Sure My Database Stays Intact?
Consistency in ACID
In most databases, certain constraints and triggers exist within it to ensure that all of the data is consistent. Transaction within the database must also follow these rules to maintain Consistency in the ACID properties.
How Do I Make Sure My Database Stays Intact?
Isolation in ACID
When using multiple database transactions at once, the ACID property of Isolation ensures that no two transactions interact with each other at the same time. Should two transactions end up interacting, they will be performed sequentially as to maintain isolation.
How Do I Make Sure My Database Stays Intact?
Durability in ACID
Once a database transaction has finished its operations, it must ensure that its operations were fully committed. This act is the Durability part of ACID properties, and makes sure the data can be recovered should anything go wrong.
How Do I Make Sure My Database Stays Fast?
A Downside of Indexes: Space
One of the downsides of creating an index in PostgreSQL is that indexes take up space. The index data structures can sometimes take up as much space as the database itself.
How Do I Make Sure My Database Stays Fast?
A Downside of Indexes: Data Entry and Updates
One of the downsides of creating an index in PostgreSQL is that indexes slow down data entry or modification. Whenever a new row is added that contains a column with an index, that index is modified as well. If you are adding a large amount of data to an existing table, it may be better to drop the index, add the data, and then recreate the index rather than having to update the index on each insertion.