Difference between revisions of "SQL"
Jump to navigation
Jump to search
(Created page with "Create a new database <syntaxhighlight lang='SQL'> DROP DATABASE test; CREATE DATABASE test CHARACTER SET latin1 COLLATE latin1_bin; </syntaxhighlight>") |
|||
| Line 1: | Line 1: | ||
| − | Create a new database | + | Create a new database: |
<syntaxhighlight lang='SQL'> | <syntaxhighlight lang='SQL'> | ||
DROP DATABASE test; | DROP DATABASE test; | ||
| Line 6: | Line 6: | ||
COLLATE latin1_bin; | COLLATE latin1_bin; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | |||
| + | {| class="wikitable" | ||
| + | |- | ||
| + | ! Action | ||
| + | ! Command | ||
| + | ! Notes | ||
| + | |- | ||
| + | | select a db | ||
| + | | USE database | ||
| + | | no semicolon needed | ||
| + | |- | ||
| + | | primary key column | ||
| + | | blank_id INT AUTO_INCREMENT PRIMARY KEY | ||
| + | | | ||
| + | |- | ||
| + | | display table attributes | ||
| + | | DESCRIBE mytable; | ||
| + | | | ||
| + | |- | ||
| + | | add data to a table | ||
| + | | INSERT INTO table (field1) VALUES ('value1'); | ||
| + | | | ||
| + | |- | ||
| + | | create a db | ||
| + | | CREATE DATABASE mydatabase; | ||
| + | | | ||
| + | |- | ||
| + | | create a table | ||
| + | | CREATE TABLE db.table (col1, col2, col3, ...); | ||
| + | | | ||
| + | |- | ||
| + | | show how a table was made | ||
| + | | SHOW CREATE TABLE table \G | ||
| + | | | ||
| + | |- | ||
| + | | backup a ''table'' (not a db) | ||
| + | | mysqldump -u user -p dbname table > tbl-bak.sql | ||
| + | | | ||
| + | |- | ||
| + | | backup a full db | ||
| + | | mysqldump -u user -p dbname > dbname-bak.sql | ||
| + | | | ||
| + | |- | ||
| + | | restore a full db | ||
| + | | mysql -u user -p dbname < dbname-bak.sql | ||
| + | | | ||
| + | |- | ||
| + | | copy table's form to new db | ||
| + | | CREATE TABLE dbnew.table LIKE table; | ||
| + | | | ||
| + | |- | ||
| + | | populate new table | ||
| + | | INSERT INTO newtable SELECT * FROM oldtable; | ||
| + | | | ||
| + | |- | ||
| + | | add column | ||
| + | | ALTER TABLE table ADD COLUMN col_id INT AFTER col2; | ||
| + | | | ||
| + | |- | ||
| + | | drop column | ||
| + | | ALTER TABLE table DROP COLUMN column; | ||
| + | | | ||
| + | |- | ||
| + | | | ||
| + | | | ||
| + | | | ||
| + | |} | ||
Revision as of 21:14, 3 February 2021
Create a new database:
DROP DATABASE test;
CREATE DATABASE test
CHARACTER SET latin1
COLLATE latin1_bin;
| Action | Command | Notes |
|---|---|---|
| select a db | USE database | no semicolon needed |
| primary key column | blank_id INT AUTO_INCREMENT PRIMARY KEY | |
| display table attributes | DESCRIBE mytable; | |
| add data to a table | INSERT INTO table (field1) VALUES ('value1'); | |
| create a db | CREATE DATABASE mydatabase; | |
| create a table | CREATE TABLE db.table (col1, col2, col3, ...); | |
| show how a table was made | SHOW CREATE TABLE table \G | |
| backup a table (not a db) | mysqldump -u user -p dbname table > tbl-bak.sql | |
| backup a full db | mysqldump -u user -p dbname > dbname-bak.sql | |
| restore a full db | mysql -u user -p dbname < dbname-bak.sql | |
| copy table's form to new db | CREATE TABLE dbnew.table LIKE table; | |
| populate new table | INSERT INTO newtable SELECT * FROM oldtable; | |
| add column | ALTER TABLE table ADD COLUMN col_id INT AFTER col2; | |
| drop column | ALTER TABLE table DROP COLUMN column; | |