database-tools
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDatabase Tools Skill
数据库工具技能
Database operations for SQLite and PostgreSQL.
针对SQLite和PostgreSQL的数据库操作。
When to Use
适用场景
- Query databases
- Run migrations
- Backup and restore
- Check database health
- Analyze queries
- 查询数据库
- 执行迁移
- 备份与恢复
- 检查数据库健康状态
- 分析查询语句
SQLite Operations
SQLite 操作
Connect & Query
连接与查询
bash
undefinedbash
undefinedConnect to database
Connect to database
sqlite3 my.db
sqlite3 my.db
List tables
List tables
sqlite3 my.db ".tables"
sqlite3 my.db ".tables"
Schema of table
Schema of table
sqlite3 my.db ".schema users"
sqlite3 my.db ".schema users"
List indexes
List indexes
sqlite3 my.db ".indexes"
undefinedsqlite3 my.db ".indexes"
undefinedQuery Examples
查询示例
bash
undefinedbash
undefinedSelect all from table
Select all from table
sqlite3 my.db "SELECT * FROM users;"
sqlite3 my.db "SELECT * FROM users;"
Select with where
Select with where
sqlite3 my.db "SELECT * FROM users WHERE id = 1;"
sqlite3 my.db "SELECT * FROM users WHERE id = 1;"
Count rows
Count rows
sqlite3 my.db "SELECT COUNT(*) FROM users;"
sqlite3 my.db "SELECT COUNT(*) FROM users;"
Insert row
Insert row
sqlite3 my.db "INSERT INTO users (name, email) VALUES ('John', 'john@example.com');"
sqlite3 my.db "INSERT INTO users (name, email) VALUES ('John', 'john@example.com');"
Update row
Update row
sqlite3 my.db "UPDATE users SET name = 'Jane' WHERE id = 1;"
sqlite3 my.db "UPDATE users SET name = 'Jane' WHERE id = 1;"
Delete row
Delete row
sqlite3 my.db "DELETE FROM users WHERE id = 1;"
undefinedsqlite3 my.db "DELETE FROM users WHERE id = 1;"
undefinedExport/Import
导出/导入
bash
undefinedbash
undefinedExport to CSV
Export to CSV
sqlite3 my.db -header -csv "SELECT * FROM users;" > users.csv
sqlite3 my.db -header -csv "SELECT * FROM users;" > users.csv
Import from CSV
Import from CSV
sqlite3 my.db ".import users.csv users"
sqlite3 my.db ".import users.csv users"
Export schema
Export schema
sqlite3 my.db ".schema" > schema.sql
sqlite3 my.db ".schema" > schema.sql
Full database dump
Full database dump
sqlite3 my.db ".dump" > backup.sql
undefinedsqlite3 my.db ".dump" > backup.sql
undefinedVacuum & Optimize
清理与优化
bash
undefinedbash
undefinedCheck database size
Check database size
ls -lh my.db
ls -lh my.db
Vacuum (reclaim space)
Vacuum (reclaim space)
sqlite3 my.db "VACUUM;"
sqlite3 my.db "VACUUM;"
Analyze (update statistics)
Analyze (update statistics)
sqlite3 my.db "ANALYZE;"
undefinedsqlite3 my.db "ANALYZE;"
undefinedPostgreSQL Operations
PostgreSQL 操作
Connect & Query
连接与查询
bash
undefinedbash
undefinedConnect
Connect
psql -U user -d database
psql -U user -d database
Connect to remote
Connect to remote
psql -h hostname -U user -d database
psql -h hostname -U user -d database
With password
With password
PGPASSWORD=secret psql -U user -d database
undefinedPGPASSWORD=secret psql -U user -d database
undefinedCommon Commands
常用命令
bash
undefinedbash
undefinedList tables
List tables
\dt
\dt
Describe table
Describe table
\d users
\d users
List indexes
List indexes
\di
\di
List sequences
List sequences
\ds
\ds
List views
List views
\dv
\dv
Quit
Quit
\q
undefined\q
undefinedQuery Examples
查询示例
bash
undefinedbash
undefinedSelect all
Select all
SELECT * FROM users;
SELECT * FROM users;
Select with limit
Select with limit
SELECT * FROM users LIMIT 10;
SELECT * FROM users LIMIT 10;
Join example
Join example
SELECT u.name, o.total FROM users u
JOIN orders o ON u.id = o.user_id;
SELECT u.name, o.total FROM users u
JOIN orders o ON u.id = o.user_id;
Aggregate
Aggregate
SELECT status, COUNT(*) FROM orders GROUP BY status;
SELECT status, COUNT(*) FROM orders GROUP BY status;
Subquery
Subquery
SELECT * FROM users WHERE id IN (
SELECT user_id FROM orders WHERE total > 100
);
undefinedSELECT * FROM users WHERE id IN (
SELECT user_id FROM orders WHERE total > 100
);
undefinedUser Management
用户管理
bash
undefinedbash
undefinedCreate user
Create user
CREATE USER newuser WITH PASSWORD 'secret';
CREATE USER newuser WITH PASSWORD 'secret';
Grant privileges
Grant privileges
GRANT ALL PRIVILEGES ON DATABASE mydb TO newuser;
GRANT ALL PRIVILEGES ON DATABASE mydb TO newuser;
Grant table access
Grant table access
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO newuser;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO newuser;
Revoke access
Revoke access
REVOKE ALL PRIVILEGES ON DATABASE mydb FROM newuser;
undefinedREVOKE ALL PRIVILEGES ON DATABASE mydb FROM newuser;
undefinedBackup & Restore
备份与恢复
bash
undefinedbash
undefinedDump single database
Dump single database
pg_dump -U user database > backup.sql
pg_dump -U user database > backup.sql
Dump with compression
Dump with compression
pg_dump -U user database | gzip > backup.sql.gz
pg_dump -U user database | gzip > backup.sql.gz
Restore
Restore
psql -U user database < backup.sql
psql -U user database < backup.sql
Restore from gzipped
Restore from gzipped
gunzip -c backup.sql.gz | psql -U user database
gunzip -c backup.sql.gz | psql -U user database
Dump all databases
Dump all databases
pg_dumpall -U user > all_databases.sql
undefinedpg_dumpall -U user > all_databases.sql
undefinedMigration Examples
迁移示例
Create Table Migration
创建表迁移
sql
-- migrations/001_create_users.sql
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_created_at ON users(created_at);sql
-- migrations/001_create_users.sql
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_created_at ON users(created_at);Add Column Migration
添加列迁移
sql
-- migrations/002_add_status.sql
ALTER TABLE users ADD COLUMN status VARCHAR(50) DEFAULT 'active';
-- Rollback
ALTER TABLE users DROP COLUMN status;sql
-- migrations/002_add_status.sql
ALTER TABLE users ADD COLUMN status VARCHAR(50) DEFAULT 'active';
-- Rollback
ALTER TABLE users DROP COLUMN status;Rename/Migrate
重命名/迁移
sql
-- Rename table
ALTER TABLE users RENAME TO accounts;
-- Rename column
ALTER TABLE users RENAME COLUMN name TO full_name;sql
-- Rename table
ALTER TABLE users RENAME TO accounts;
-- Rename column
ALTER TABLE users RENAME COLUMN name TO full_name;Performance Analysis
性能分析
Explain Queries
解释查询语句
sql
-- Explain query plan
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'test@example.com';
-- Explain without running
EXPLAIN SELECT * FROM users;sql
-- Explain query plan
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'test@example.com';
-- Explain without running
EXPLAIN SELECT * FROM users;Find Slow Queries
查找慢查询
bash
undefinedbash
undefinedPostgreSQL: Enable slow query log
PostgreSQL: Enable slow query log
In postgresql.conf:
In postgresql.conf:
log_min_duration_statement = 1000
log_min_duration_statement = 1000
View logs
View logs
tail -f /var/log/postgresql/postgresql.log
tail -f /var/log/postgresql/postgresql.log
Using pg_stat_statements
Using pg_stat_statements
SELECT query, calls, mean_time, total_time
FROM pg_stat_statements
ORDER BY total_time DESC
LIMIT 10;
undefinedSELECT query, calls, mean_time, total_time
FROM pg_stat_statements
ORDER BY total_time DESC
LIMIT 10;
undefinedIndex Recommendations
索引建议
sql
-- Find missing indexes from queries
SELECT
schemaname,
relname,
seq_scan,
seq_tup_read,
idx_scan,
idx_tup_fetch
FROM pg_stat_user_tables
WHERE seq_scan > 100
ORDER BY seq_scan DESC;sql
-- Find missing indexes from queries
SELECT
schemaname,
relname,
seq_scan,
seq_tup_read,
idx_scan,
idx_tup_fetch
FROM pg_stat_user_tables
WHERE seq_scan > 100
ORDER BY seq_scan DESC;Database Health Check
数据库健康检查
SQLite Health
SQLite 健康检查
bash
#!/bin/bashbash
#!/bin/bashSQLite health check
SQLite health check
DB="$1"
echo "=== SQLite Health Check ==="
echo "Database: $DB"
echo ""
echo "Size:"
ls -lh "$DB"
echo ""
echo "Table count:"
sqlite3 "$DB" "SELECT COUNT(*) FROM sqlite_master WHERE type='table';"
echo ""
echo "Index count:"
sqlite3 "$DB" "SELECT COUNT(*) FROM sqlite_master WHERE type='index';"
echo ""
echo "Integrity check:"
sqlite3 "$DB" "PRAGMA integrity_check;"
undefinedDB="$1"
echo "=== SQLite Health Check ==="
echo "Database: $DB"
echo ""
echo "Size:"
ls -lh "$DB"
echo ""
echo "Table count:"
sqlite3 "$DB" "SELECT COUNT(*) FROM sqlite_master WHERE type='table';"
echo ""
echo "Index count:"
sqlite3 "$DB" "SELECT COUNT(*) FROM sqlite_master WHERE type='index';"
echo ""
echo "Integrity check:"
sqlite3 "$DB" "PRAGMA integrity_check;"
undefinedPostgreSQL Health
PostgreSQL 健康检查
bash
#!/bin/bashbash
#!/bin/bashPostgreSQL health check
PostgreSQL health check
echo "=== PostgreSQL Health ==="
echo ""
echo "Database size:"
psql -U user -d db -c "SELECT pg_database_size(current_database());"
echo "Table sizes:"
psql -U user -d db -c "SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 5;"
echo "Active connections:"
psql -U user -d db -c "SELECT count(*) FROM pg_stat_activity;"
echo "Long running queries:"
psql -U user -d db -c "SELECT pid, now() - pg_stat_activity.query_start AS duration, query FROM pg_stat_activity WHERE state = 'active' AND query NOT ILIKE '%pg_stat_activity%' ORDER BY duration DESC LIMIT 5;"
undefinedecho "=== PostgreSQL Health ==="
echo ""
echo "Database size:"
psql -U user -d db -c "SELECT pg_database_size(current_database());"
echo "Table sizes:"
psql -U user -d db -c "SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 5;"
echo "Active connections:"
psql -U user -d db -c "SELECT count(*) FROM pg_stat_activity;"
echo "Long running queries:"
psql -U user -d db -c "SELECT pid, now() - pg_stat_activity.query_start AS duration, query FROM pg_stat_activity WHERE state = 'active' AND query NOT ILIKE '%pg_stat_activity%' ORDER BY duration DESC LIMIT 5;"
undefinedNotes
注意事项
- Always backup before migrations
- Use transactions for multi-step changes
- Test migrations on staging first
- Use parameterized queries to prevent SQL injection
- Thepopebot stores data in SQLite at
data/thepopebot.sqlite
- 迁移前务必备份数据
- 多步骤变更使用事务
- 先在测试环境测试迁移
- 使用参数化查询防止SQL注入
- Thepopebot 将数据存储在SQLite数据库 中
data/thepopebot.sqlite