MySql資料庫和表的基本操作命令
阿新 • • 發佈:2019-01-01
資料庫和表的基本操作
Mysql注意事項
- MySQL 建議我們關鍵字使用大寫,但不是必須大寫。
- 資料庫名字的反引號``,是為了防止使用的資料庫名剛好是關鍵字。
資料庫的基本操作
-
命令後面一定加分號,表示命令結束
-
顯示當前資料庫:
show databases ;
-
建立資料庫資料庫:
create database hello ;
(這裡的hello為資料庫名,在建立過程中,自擬,下面的例子中,以此為例
-
使用資料庫:
use hello ;
-
顯示建立語句:
show creat database hello ;
-
修改資料庫:
alter database hello ;
(對資料庫的修改主要指的是修改資料庫的字符集,校驗準則) -
刪除資料庫:
drop database hello ;
(不要隨意刪除資料庫:資料庫內部看不到對應的資料庫,對應的資料庫資料夾被刪除,級聯刪除,裡面的資料表全部被刪) -
備份和恢復
-
備份:
# mysqldump -P3306 -u root -p 密碼 -B 資料庫名 > 資料庫備份儲存的檔案路徑
-
同時備份多個數據庫:
# mysqldump -u root -p -B 資料庫名1 資料庫名2 ... > 資料庫存放路徑
-
還原:
mysql> source D:/mysql-5.7.22/hello.sql;
-
檢視連線情況:
show processlist ;
表的基本操作
- 建立表:
CREATE TABLE table_name ( field1 datatype, field2 datatype, field3 datatype ) character set 字符集 collate 校驗規則 engine 儲存引擎; field 表示列名; datatype 表示列的型別; character set 字符集,如果沒有指定字符集,則以所在資料庫的字符集為準; collate 校驗規則,如果沒有指定校驗規則,則以所在資料庫的校驗規則為準;
- 查看錶結構:
desc 表名 ;
- 顯示錶的建立語句:
show create table users;
- 修改表:
在表中新增一個欄位:
alter table 表名 add (column datatype ...)
修改表中某條屬性:
alter table 表名 modify (column datatype ...)
刪除表中的某屬性等:
alter table 表名 drop(column);
- 修改表名:
alter table 舊錶名 rname to 新表名 ; (to 可以省略)
- 修改表中屬性名(新欄位需要完整定意思):
- 刪除表:
drop table 表名 ;