MySQL基本資料庫管理命令

2022-01-25 入門小站
安裝 MySQL 資料庫

用 yum 或者 apt 安裝包管理器 MySQL 資料庫。

# yum install mysql mysql-client mysql-server  (on `Yum` based Systems)

# apt-get install mysql mysql-client mysql-server (on `Apt` based Systems)

啟動 MySQL

啟動 MySQL 資料庫服務

# service mysqld start
or
# service mysql start

安裝好一個 MySQL 資料庫,啟動後,下一步就可以進入MySQL資料庫了

# mysql -u root -p
Welcome to the MySQL monitor.  Commands end with ; or \\g.

Your MySQL connection id is 195 

Server version: 31-0+wheezy1 (Debian) 

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. 
Other names may be trademarks of their respective owners. 

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql>

創建資料庫
mysql> create database rumenz ;
Query OK, 1 row affected (02 sec) 
mysql>

Note: Query OK表示資料庫已創建。

mysql> show databases; 
++
| Database           | 
++ 
| information_schema | 
| mysql              | 
| performance_schema | 
| `rumenz`            | 
| test               | 
++ 
9 rows in set (00 sec) 
mysql>

選擇資料庫

現在你需要選擇要處理的資料庫。

mysql> use rumenz;
Database changed
mysql>

在 MySQL 中創建表

在這裡,我們將創建一個表test_table三個欄位為:

mysql> CREATE TABLE test_table (
    -> id Int(3), 
    -> first_name Varchar (15), 
    -> email Varchar(20) 
    -> ); 
Query OK, 0 rows affected (08 sec) 
mysql>

Note: 上面的查詢 OK這意味著表的創建沒有任何錯誤。要查看該表,請運行以下查詢。

mysql> show tables; 
+----+ 
| Tables_in_rumenz | 
+----+ 
| test_table           | 
+----+ 

1 row in set (00 sec) 

mysql>

mysql> show columns from test_table; 

+--+---+-++----+--+ 
| Field      | Type        | Null | Key | Default | Extra | 
+--+---+-++----+--+ 
| id         | int(3)      | YES  |     | NULL    |       | 
| first_name | varchar(15) | YES  |     | NULL    |       | 
| email      | varchar(20) | YES  |     | NULL    |       | 
+--+---+-++----+--+ 
3 rows in set (00 sec)

mysql>

varchar是具有定義的可變長度的字符。Type 後面的值是它可以存儲數據的欄位長度。

現在我們需要添加一列 last_name在first_name列之後。

mysql> ALTER TABLE test_table ADD last_name varchar (20) AFTER first_name; 
Query OK, 0 rows affected (16 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show columns from test_table; 

+--+---+-++----+--+ 
| Field      | Type        | Null | Key | Default | Extra | 
+--+---+-++----+--+ 
| id         | int(3)      | YES  |     | NULL    |       | 
| first_name | varchar(15) | YES  |     | NULL    |       | 
| last_name  | varchar(20) | YES  |     | NULL    |       | 
| email      | varchar(20) | YES  |     | NULL    |       | 
+--+---+-++----+--+ 

4 rows in set (00 sec) 

mysql>

在 MySQL 中添加列

現在我們將在右側添加一列,比如一列 country 在email 的右邊 .

mysql> ALTER TABLE test_table ADD country varchar (15) AFTER email; 
Query OK, 0 rows affected (16 sec) 
Records: 0  Duplicates: 0  Warnings: 0 

mysql>

驗證

mysql> show columns from test_table; 

+--+---+-++----+--+ 
| Field      | Type        | Null | Key | Default | Extra | 
+--+---+-++----+--+
| id         | int(3)      | YES  |     | NULL    |       | 
| first_name | varchar(15) | YES  |     | NULL    |       | 
| last_name  | varchar(20) | YES  |     | NULL    |       | 
| email      | varchar(20) | YES  |     | NULL    |       | 
| country    | varchar(15) | YES  |     | NULL    |       | 
+--+---+-++----+--+
5 rows in set (00 sec) 

mysql>

在欄位中插入值
mysql> INSERT INTO test_table VALUES ('1' , 'Ravi' , 'Saive' , 'raivsaive@xyz.com' , 'India' );
Query OK, 1 row affected (02 sec) 

mysql>

批量插入值

mysql> INSERT INTO test_table VALUES ('2' , 'Narad' , 'Shrestha' , 'narad@xyz.com' , 'India' ), ('3' , 'user' , 'singh' , 'user@xyz.com' , 'Aus' ), ('4' , 'rumenz' , '[dot]com' , 'rumenz@gmail.com' , 'India' );
Query OK, 3 rows affected (05 sec) 
Records: 3  Duplicates: 0  Warnings: 0

查詢結果。

mysql> select * from test_table; 
+-+--+-+----+----+ 
| id   | first_name | last_name | email             | country | 
+-+--+-+----+----+ 
|    1 | Ravi     | Saive     | raivsaive@xyz.com | India   | 
|    2 | Narad      | Shrestha  | narad@xyz.com     | India   | 
|    3 | user       | singh     | user@xyz.com      | Aus     | 
|    4 | rumenz    | [dot]com  | rumenz@gmail.com | India   | 
+-+--+-+----+----+ 

4 rows in set (00 sec)

mysql>

刪除記錄

假設上面輸出中的第三條數據無效,我們需要刪除第三條數據。

mysql> DELETE FROM test_table WHERE id = 3;

Query OK, 1 row affected (02 sec)

查詢結果

mysql> select * from test_table;

+-+--+-+----+----+ 
| id   | first_name | last_name | email             | country | 
+-+--+-+----+----+
|    1 | Ravi       | Saive     | raivsaive@xyz.com | India   | 
|    2 | Narad      | Shrestha  | narad@xyz.com     | India   | 
|    4 | rumenz    | [dot]com  | rumenz@gmail.com | India   | 
+-+--+-+----+----+
3 rows in set (00 sec)

更新欄位中的值

需要編輯 id (=4)。

mysql> UPDATE test_table SET id = 3 WHERE first_name = 'rumenz'; 
Query OK, 1 row affected (02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>

查詢

mysql> UPDATE test_table SET id = 3 WHERE first_name = 'rumenz'; 
Query OK, 1 row affected (02 sec) 
Rows matched: 1  Changed: 1  Warnings: 0

mysql>

mysql> UPDATE test_table SET id = 6 WHERE first_name = 'rumenz'AND last_name = '[dot]com'; 
Query OK, 1 row affected (03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>

刪除 MySQL 中的列

刪除country欄位

mysql> ALTER TABLE test_table drop country; 
Query OK, 3 rows affected (15 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql>

查詢

mysql> select * from test_table; 

+-+--+-+----+ 
| id   | first_name | last_name | email             | 
+-+--+-+----+ 
|    1 | Ravi       | Saive     | raivsaive@xyz.com | 
|    2 | Narad      | Shrestha  | narad@xyz.com     | 
|    6 | rumenz    | [dot]com  | rumenz@gmail.com | 
+-+--+-+----+
3 rows in set (00 sec) 

mysql>

在 MySQL 修改表名

將test_table改成rumenz_table.

mysql> RENAME TABLE test_table TO rumenz_table; 
Query OK, 0 rows affected (03 sec)

mysql>

列出所有表

查看當前資料庫下的所有表。

mysql> show tables; 

+----+ 
| Tables_in_rumenz | 
+----+ 
| rumenz_table     | 
+----+
1 row in set (00 sec) 

mysql>

該表已重命名。現在備份以上內容MySQL資料庫

# mysqldump -u root -p rumenz > rumenz.sql

check the dumped file on your desktop which would have contents something like
-- MySQL dump 13  Distrib 31, for debian-linux-gnu (i686) --
-- Server version 31-0+wheezy1 -- 
Dump completed on 2013-09-02 12:55:37

維護MySQL 資料庫的備份始終是一個好主意。恢復備份MySQL Data 又是一行簡單的代碼,你需要在終端提示符下運行。

刪除資料庫
mysql> drop database rumenz; 
Query OK, 1 row affected (02 sec)

檢查資料庫伺服器上的資料庫rumenz。

mysql> show databases; 

++ 
| Database           | 
++ 
| information_schema | 
| my_database        | 
| mysql              | 
| performance_schema | 
| phpmyadmin         | 
| sisso              | 
| test               | 
++

7 rows in set (00 sec) 
mysql>

不用擔心,我們有備份。

恢復資料庫

要恢復丟失的資料庫,請運行以下命令。

# mysql -u root -p rumenz < rumenz.sql
Enter password:
ERROR 1049 (42000): Unknown database 'rumenz'

還沒有創建資料庫rumenz. 因此,轉到你的 mysql 提示符並創建一個資料庫 rumenz。

mysql> create database rumenz; 
Query OK, 1 row affected (00 sec) 

mysql>

現在是時候在你的 shell 提示符下運行 restore 命令了

# mysql -u root -p rumenz < rumenz.sql 
Enter password:

驗證你的資料庫。

mysql> show databases; 

++ 
| Database           | 
++ 
| information_schema | 
| mysql              | 
| performance_schema | 
| rumenz            | 
| test               | 
++ 
8 rows in set (00 sec)

驗證資料庫的內容。

mysql> show tables from rumenz;

+----+ 
| Tables_in_rumenz | 
+----+ 
| rumenz_table     | 
+----+ 
1 row in set (00 sec)

mysql>

驗證你恢復的表的內容。

mysql> select * from rumenz_table; 

+-+--+-+----+ 
| id   | first_name | last_name | email             | 
+-+--+-+----+ 
|    1 | Ravi       | Saive     | raivsaive@xyz.com | 
|    2 | Narad      | Shrestha  | narad@xyz.com     | 
|    6 | rumenz    | [dot]com  | rumenz@gmail.com | 
+-+--+-+----+

3 rows in set (00 sec)

相關焦點

  • 資料庫管理的MySQL備份和恢復命令
    mysqldump 是一個命令行客戶端程序,用於轉儲本地或遠程 MySQL 用於備份到單個平面文件中的資料庫或資料庫集合。如何備份和恢復 MySQL 資料庫如何備份 MySQL 資料庫?備份 MySQL資料庫或資料庫,該資料庫必須存在於資料庫伺服器中並且你必須有權訪問它。命令的格式是。
  • MySQL資料庫常用命令詳解
    除了用第三方軟體管理MySQL資料庫外,MySQL本身也提供了管理資料庫的操作命令,可以在CentOS終端直接使用MySQL命令,用於MySQL資料庫的創建、表的管理、SQL查詢等管理操作。(1)登錄MySQL資料庫用SSH客戶端連接CentOS伺服器,打開終端命令輸入窗口,在終端輸入窗口輸入命令:mysql -uroot –p 該命令用root帳號以密碼方式登錄MySQL,回車後提示輸入密碼
  • MySQL 資料庫基本命令匯總整理,需要的趕緊學起來
    中創建資料庫的兩種方法:(mysql_create_db(),mysql_query())$conn = mysql_connect(「localhost」,」username」,」password」) ordie ( 「could not connect to localhost」);1.
  • MySQL基本操作命令(DDL、DML、DQL、DCL)
    一:資料庫基本操作命令1.1:mysql查看資料庫結構1.2:查看資料庫信息mysql> show databases;++| Database |++| information_schema || mysql || performance_schema
  • Mysql資料庫備份和還原常用的命令
    備份MySQL資料庫的命令mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql備份MySQL資料庫為帶刪除表的格式備份MySQL資料庫為帶刪除表的格式,能夠讓該備份覆蓋已有資料庫而不需要手動刪除原有資料庫
  • ubuntu mysql 服務管理以及資料庫操作
    MySQL是一種關係型資料庫管理系統(數據保存在不同的表中而不是將所有數據放在同一個大倉庫內,訪問速度以及靈活性有明顯提高 ),支持標準的sql語言,並且mysql是開源的,不需要支付額外的費用,所以當下非常流行 本文介紹 ubuntu下 mysql 服務的安裝與管理以及mysql資料庫的簡單使用
  • 新手入門MYSQL資料庫命令大全
    一、命令行連接資料庫Windows作業系統進入CMD命令行,進入mysql.exe所在目錄,運行命令mysql.exe -h主機名 -u用戶名 -p密碼注意:參數名與值之間沒有空格 , 如:-h127.0.0.1
  • 一看就會,MySQL資料庫的基本操作(四)
    第二種方法:利用命令打開。打開命令:net start mysql;關閉命令:net stop mysql打開、關閉資料庫命令2、登陸資料庫命令:mysql -h 主機地址 -u 用戶名 -p 用戶密碼。退出命令:exit
  • 考前複習必備MySQL資料庫(關係型資料庫管理系統)
    初始化的過程創建元數據表數據目錄創建root用戶mysql提供資料庫命令:mysql --initialize進行初始化初始化完成後,可以啟動資料庫,啟動資料庫有兩種方式。利用windows伺服器管理界面啟動;利用mysql命令啟動。
  • 詳解MySQL資料庫中Show命令的用法
    MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中對show命令的使用還容易產生混淆,本文主要介紹了show命令的主要用法。 a. show tables或show tables from database_name; -- 顯示當前資料庫中所有表的名稱。
  • Mysql資料庫的使用方法
    你們讚賞就是對小編的鼓勵,都是你們的心意,小編收到了,在這邊感謝你們,謝謝    現在看軟體測試的招聘要求,基本都是要求會資料庫這一塊的,薪水越高所具備的技能也要越多,而資料庫知識則是一項必備技能了,所以今天小編就好好的普及下資料庫知識方面的基本使用方法!
  • MySQL資料庫實例管理器命令行選項詳解
    首頁 > 語言 > 關鍵詞 > 資料庫最新資訊 > 正文 MySQL資料庫實例管理器命令行選項詳解
  • MySQL 刪除資料庫 | Mysql Drop Database
    我們可以在登陸 MySQL 服務後,使用 create 命令創建資料庫,語法如下:使用普通用戶登陸 MySQL 伺服器,你可能需要特定的權限來創建或者刪除
  • 了解資料庫類型及MySQL資料庫常用命令行
    一、資料庫1、資料庫DataBase(DB):存儲數據的倉庫。2、資料庫的分類(1) 關係型資料庫(sql資料庫):中型:mysql(埠號3306)、sql server大型:Oracle(埠號1521)(2) 非關係型資料庫(no-sql資料庫)
  • MySQL教程之MySQL定時備份資料庫
    一、MySQL數據備份1.1、 mysqldump命令備份數據在MySQL中提供了命令行導出資料庫數據以及文件的一種方便的工具mysqldump,我們可以通過命令行直接實現資料庫內容的導出dump,首先我們簡單了解一下mysqldump命令用法:
  • MySQL 常用命令手冊
    」,常用功能命令1mysqldump -u 用戶名 -p –default-character-set=latin1 資料庫名 > 導出的文件名(資料庫默認編碼是latin1)  23mysqldump -u wcnc -p smgp_apps_wcnc >
  • python對mysql資料庫的操作(一)
    本文章介紹python對mysql資料庫的基本操作,以及編寫一個模擬用戶的註冊。
  • 如何使用MySQL資料庫
    如何使用MySQL資料庫前言:前面我們已經了解了如何搭建MySQL資料庫,那麼接下來我們就一起來了解一下,如何使用MySQL資料庫。MySQL資料庫系統也是一個典型的C/S(客戶端/伺服器)架構應用,要訪問MySQL資料庫需要使用專門的客戶端軟體。在linux系統中,最簡單、易用的MySQL客戶端軟體是其自帶的MySQL命令工具。
  • 軟體測試 | Mysql資料庫cmd連結+mysql 資料庫的備份操作
    👇掃我進群,學習交流群👇☝學測試,尋知音,經驗交流,掃碼入群☝這裡再給大家介紹一下如何在 cmd 界面通過命令連接資料庫。如果我們沒有安裝資料庫連接工具,那 麼我們就用得上接下來介紹的知識了。 如果連接的是本機的 Mysql,可以省略輸入主機名,只需輸入 mysql –uroot –p,按下回車鍵,提示輸入密碼。輸入正確的密碼,回車確認,出現下圖消息表明資料庫連接成功。使用 show databases 可以查看已有的資料庫。通過 use 資料庫名,可以切換使用某個資料庫。
  • MySQL資料庫及應用
    查詢答案: A7 單選 資料庫、資料庫管理和資料庫系統之間的關係正確的是A. 資料庫包括了資料庫管理系統和資料庫系統B. 資料庫管理系統包括了資料庫和資料庫系統C.資料庫系統包括資料庫和資料庫管理系統D. 以上都不對答案: C8 單選 目前,商品化的資料庫管理系統以__________型為主。A. 關係B. 層次C.