database_notes
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
database_notes [2019/03/24 22:59] – created steve | database_notes [2019/03/24 23:04] – steve | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Database Notes ====== | ====== Database Notes ====== | ||
- | ** This is legacy documentation from old releases.** | ||
- | |||
- | Developer Notes | ||
These are just notes and things I've picked up about SQL and the databases while working with this project. | These are just notes and things I've picked up about SQL and the databases while working with this project. | ||
- | MySQL | + | ===== MySQL ===== |
Copy table: | Copy table: | ||
+ | < | ||
CREATE TABLE new_table SELECT * FROM old_table; | CREATE TABLE new_table SELECT * FROM old_table; | ||
+ | </ | ||
Rename column: | Rename column: | ||
+ | < | ||
ALTER TABLE table_name CHANGE column_old column_new (TYPE) [UNSIGNED]; | ALTER TABLE table_name CHANGE column_old column_new (TYPE) [UNSIGNED]; | ||
+ | </ | ||
Rename value in field: | Rename value in field: | ||
+ | < | ||
UPDATE table_name SET field = REPLACE(field, | UPDATE table_name SET field = REPLACE(field, | ||
+ | </ | ||
Select data into a field: | Select data into a field: | ||
+ | < | ||
INSERT INTO table_name SELECT * FROM table_two; | INSERT INTO table_name SELECT * FROM table_two; | ||
UPDATE table_name SET column = CONCAT(' | UPDATE table_name SET column = CONCAT(' | ||
+ | </ | ||
Rename table: | Rename table: | ||
+ | < | ||
ALTER TABLE table_name RENAME new_table_name; | ALTER TABLE table_name RENAME new_table_name; | ||
+ | </ | ||
- | mysqldump | + | Export specific |
+ | < | ||
mysqldump database_name --tables table1 table2 table3 | mysqldump database_name --tables table1 table2 table3 | ||
+ | </ | ||
Add a new column: | Add a new column: | ||
+ | < | ||
ALTER TABLE table_name ADD COLUMN new_column mediumint(8) not null FIRST / AFTER column1; | ALTER TABLE table_name ADD COLUMN new_column mediumint(8) not null FIRST / AFTER column1; | ||
+ | </ | ||
Add a primary key: | Add a primary key: | ||
+ | < | ||
ALTER TABLE table_name ADD PRIMARY KEY(colname); | ALTER TABLE table_name ADD PRIMARY KEY(colname); | ||
+ | </ | ||
Show indexes: | Show indexes: | ||
+ | < | ||
SHOW INDEX FROM colname; | SHOW INDEX FROM colname; | ||
+ | </ | ||
Add autoincrement: | Add autoincrement: | ||
+ | < | ||
ALTER TABLE tablename ADD PRIMARY KEY(column); | ALTER TABLE tablename ADD PRIMARY KEY(column); | ||
ALTER TABLE tablename CHANGE column column tinyint(3) unsigned auto_increment; | ALTER TABLE tablename CHANGE column column tinyint(3) unsigned auto_increment; | ||
+ | </ | ||
concat: | concat: | ||
+ | < | ||
select concat(lds_scriptures_books.book_title, | select concat(lds_scriptures_books.book_title, | ||
+ | </ | ||
update + concat | update + concat | ||
+ | < | ||
UPDATE `Table A`,`Table B` SET `Table A`.`text`=concat_ws('', | UPDATE `Table A`,`Table B` SET `Table A`.`text`=concat_ws('', | ||
+ | </ | ||
Set the pager (less) so it quits if the display is less than one screen (like psql) | Set the pager (less) so it quits if the display is less than one screen (like psql) | ||
+ | < | ||
\P less -F | \P less -F | ||
+ | </ | ||
- | Annoyances | + | ==== Regular Expressions |
- | + | ||
- | It seems to be purposely trying NOT to be compatible with any other database' | + | |
- | + | ||
- | Regular Expressions | + | |
MySQL' | MySQL' | ||
Line 72: | Line 91: | ||
Create an `lds_scriptures` table in MySQL for CSV | Create an `lds_scriptures` table in MySQL for CSV | ||
+ | < | ||
CREATE TABLE lds_scriptures AS SELECT verses.verse_id, | CREATE TABLE lds_scriptures AS SELECT verses.verse_id, | ||
+ | </ | ||
PostgreSQL-compatible dump | PostgreSQL-compatible dump | ||
+ | < | ||
$ mysqldump < | $ mysqldump < | ||
+ | </ | ||
- | SQLite | + | ===== SQLite |
Create an index: | Create an index: | ||
+ | < | ||
CREATE INDEX index-name ON table-name (column-name[, | CREATE INDEX index-name ON table-name (column-name[, | ||
+ | </ | ||
Convert from sqlite2 to sqlite3: | Convert from sqlite2 to sqlite3: | ||
+ | < | ||
sqlite v2.db .dump | sqlite3 v3.db | sqlite v2.db .dump | sqlite3 v3.db | ||
+ | </ | ||
Copy the database structure: | Copy the database structure: | ||
+ | < | ||
sqlite one.db .schema | sqlite two.db | sqlite one.db .schema | sqlite two.db | ||
+ | </ | ||
PostgreSQL | PostgreSQL | ||
Line 96: | Line 125: | ||
Show databases, tables, table description: | Show databases, tables, table description: | ||
+ | < | ||
\l | \l | ||
\dt | \dt | ||
\d table_name | \d table_name | ||
+ | </ | ||
Rename table: | Rename table: | ||
+ | < | ||
ALTER TABLE table_name RENAME TO new_table_name; | ALTER TABLE table_name RENAME TO new_table_name; | ||
+ | </ | ||
Replace text: | Replace text: | ||
+ | < | ||
UPDATE table_name SET column_name = REPLACE(column_name, | UPDATE table_name SET column_name = REPLACE(column_name, | ||
+ | </ | ||
Create new table from SELECT: | Create new table from SELECT: | ||
+ | < | ||
CREATE TABLE new_table AS SELECT * FROM old_table; | CREATE TABLE new_table AS SELECT * FROM old_table; | ||
+ | </ | ||
Create a (unique) index: | Create a (unique) index: | ||
+ | < | ||
CREATE UNIQUE INDEX index_name ON table_name (column1); | CREATE UNIQUE INDEX index_name ON table_name (column1); | ||
+ | </ | ||
Edit a query with $EDITOR | Edit a query with $EDITOR | ||
+ | < | ||
\e | \e | ||
- | + | </ | |
- | Why I like PostgreSQL better | + | |
- | + | ||
- | When running psql, if you hit Ctrl-C, it doesnt kill the client. Just the last query, or entered string. | + | |
- | It accepts only ' as quotes around vales, instead of both " and '. Plus, " is used to quote table names. | + | |
- | Its a little easier to create users. | + | |
- | It's easy to setup the permissions of who can connect. | + | |
- | It has some really cool data types. :) | + | |
- | Better tab delimited options | + | |
- | The pager by default quits on a screen that isn't one page. | + | |
- | The pager is turned on by default. | + | |
- | Things that bug me | + | |
- | + | ||
- | Can't do an export of JUST THE SQL for some reason. (pg_dump) | + | |
- | Can't export within a transaction (pg_dump) | + | |
- | Can't alter column types, I think :( | + | |
SQLite-compatible pg_dump: | SQLite-compatible pg_dump: | ||
+ | < | ||
pg_dump -t tablename -a -f table.sql -O -x -d database | pg_dump -t tablename -a -f table.sql -O -x -d database | ||
+ | </ | ||
Empty a table, reset primary key: | Empty a table, reset primary key: | ||
+ | < | ||
TRUNCATE table_name; | TRUNCATE table_name; | ||
+ | </ | ||
Copy data from one table into another: | Copy data from one table into another: | ||
+ | < | ||
INSERT INTO table_one SELECT * FROM table_two ORDER BY table_two.id; | INSERT INTO table_one SELECT * FROM table_two ORDER BY table_two.id; | ||
+ | </ | ||
Add column: | Add column: | ||
+ | < | ||
ALTER TABLE table_name ADD col_name integer; | ALTER TABLE table_name ADD col_name integer; | ||
+ | </ | ||
Create database with chosen encoding: | Create database with chosen encoding: | ||
+ | < | ||
CREATE DATABASE < | CREATE DATABASE < | ||
+ | </ | ||
Reset sequential index (auto-increment) | Reset sequential index (auto-increment) | ||
+ | < | ||
ALTER < | ALTER < | ||
+ | </ | ||
Create user (with password, create database | user permissions) | Create user (with password, create database | user permissions) | ||
+ | < | ||
CREATE USER < | CREATE USER < | ||
+ | </ | ||
Load a query and execute it | Load a query and execute it | ||
+ | < | ||
\i < | \i < | ||
+ | </ |
database_notes.txt · Last modified: by steve