Skip to content

Latest commit

 

History

History
90 lines (76 loc) · 2.57 KB

index.md

File metadata and controls

90 lines (76 loc) · 2.57 KB

PostgreSQL

Introduction

PostgreSQL is a powerful, open-source object-relational database system with a strong reputation for reliability, feature robustness, and performance.

Basic Syntax

-- Create a new database
CREATE DATABASE mydatabase;

-- Create a new table
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100) UNIQUE NOT NULL
);

-- Insert data into the table
INSERT INTO users (name, email) VALUES ('John Doe', 'john.doe@example.com');

-- Query data from the table
SELECT * FROM users;

Common Use Cases

  • Storing relational data
  • Complex queries and transactions
  • Data integrity and constraints
  • Full-text search

Advanced Features

  • Indexes: Improve the speed of data retrieval.
  • Views: Create virtual tables based on the result of a query.
  • Stored Procedures: Write functions in SQL or other languages.
  • Triggers: Execute a function in response to an event on a table.

Code Snippets

Creating an Index

-- Create an index on the email column
CREATE INDEX idx_users_email ON users(email);

Creating a View

-- Create a view for active users
CREATE VIEW active_users AS
SELECT * FROM users WHERE active = true;

Creating a Stored Procedure

-- Create a stored procedure to update user email
CREATE OR REPLACE FUNCTION update_user_email(user_id INT, new_email VARCHAR)
RETURNS VOID AS $$
BEGIN
    UPDATE users SET email = new_email WHERE id = user_id;
END;
$$ LANGUAGE plpgsql;

Creating a Trigger

-- Create a trigger to log changes to the users table
CREATE OR REPLACE FUNCTION log_user_changes()
RETURNS TRIGGER AS $$
BEGIN
    INSERT INTO user_changes(user_id, change_time, change_type)
    VALUES (NEW.id, NOW(), TG_OP);
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER user_changes_trigger
AFTER INSERT OR UPDATE OR DELETE ON users
FOR EACH ROW EXECUTE FUNCTION log_user_changes();

Tips & Best Practices

  • Use Indexes: Use indexes to speed up data retrieval.
  • Normalize Data: Normalize your data to reduce redundancy and improve data integrity.
  • Backup Regularly: Regularly backup your database to prevent data loss.
  • Monitor Performance: Use tools to monitor and optimize database performance.
  • Security: Use roles and permissions to secure your database.

External Resources