mysql與mongoDB常用命令
阿新 • • 發佈:2020-09-07
操作 | mysql | MongoDB |
連線資料庫 |
mysql -h 地址 -u 使用者名稱 -p密碼 | 開啟mongo自動連線本地 |
檢視主機上的所有資料庫 |
show databases; | show dbs |
使用或轉到新建某資料庫 |
use dbName;/create database dbName; | use dbName |
檢視當前資料庫中的所有表/集合 |
show tables; | db.getCollectionNames() |
檢視某個表/集合中的資料 |
select * from tableName; | db.collectionName.find() |
查看錶結構 |
desc tableName; | |
向表/集合中插入一條或多條資料 |
insert into tableName valuses (屬性值1,屬性值2,屬性值3,....) ,(屬性值1,屬性值2,屬性值3,....) ,(屬性值1,屬性值2,屬性值3,....) ....; | db.collectionName.insert([{},{},{}....]) |
新建資料庫 |
create database databaseName | use dbName (需要建立一個集合,才能show出來) |
刪除資料庫 |
drop database databaseName | db.dropDatabase() |
備份資料庫複製資料庫 |
||
恢復資料庫 |
||
新建表/集合 |
create table tableName ('id' int,'' ,...,PRIMARY KEY (`id`)) | db.createCollection('collectionName') |
刪除表/集合 |
drop table tableName | db.collectionName.drop() |
備份表/集合 |
mysqldump -u root -p databaseName > H:\testbak\test.sql | mongodump -h dbhost -d dbname -o dbdirectory |
恢復表/集合 |
mysql -u root -p [資料庫名稱] < filename.sql | mongorestore -h <hostname><:port> -d dbname <path> |
改資料庫名 |
RENAME database olddbname TO newdbname(最好用指令碼) | db.adminCommand({renameCollection: "db1.test1", to: "db2.test2"}) |
改表/集合名 |
rename olddb.tables to newdb.tables |
db.orders.renameCollection( "orders2014" )或 db.runCommand( { renameCollection: "test.orders", to: "test.orders2014" } |
改表/集合中的資料 |
update tableName set 屬性名=屬性值,屬性名=屬性值,屬性名=屬性值.....where '條件' truncate tabletableName alter |
db.collectionName.update({條件},{修改}) update |
刪除表/集合中的資料 |
delete from tableName where '條件' | db.collectionName.remove({條件}) |
表連線-一張表可能不滿足業務需求 |
select * from TableA cross join TableB select * from TableA left join TableB on TableA.id=TableB.id select * from TableA full join TableB on TableA.id=TableB.id select * from TableA JOIN TableB on TableA.id=TableB.id | 不支援 |
# 假設將sakila資料庫名改為new_sakila # MyISAM直接更改資料庫目錄下的檔案即可 mysql -uroot -p123456 -e 'create database if not exists new_sakila' list_table=$(mysql -uroot -p123456 -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='sakila'") for table in $list_table do mysql -uroot -p123456 -e "rename table sakila.$table to new_sakila.$table"done