MySQL 基本使用教程

這里介紹下 基本的MySQL 使用教程

所有操作 都是在mac終端sql 環(huán)境下操作


  • 進(jìn)入 MySQL mysql -u root -p

        // 進(jìn)入MySQl 命令
        // mysql -u 數(shù)據(jù)庫用戶名  -p 密碼(明碼)  
        // 也可以不寫密碼, 回車后 再輸入密碼 可以讓密碼不回顯
        yulilideMacBook-Pro:~ sky-fish$ mysql -u root -p
        // 輸入 MySQL 密碼
        Enter password: 
        Welcome to the MySQL monitor.  Commands end with ; or \g.
        Your MySQL connection id is 5
        Server version: 5.7.18 Homebrew
    
        Copyright (c) 2000, 2017, 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.
    
        // \h 獲取幫助信息
        // \c 清除輸入信息
        Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
  • 顯示數(shù)據(jù)庫列表

    mysql> show databases;
            +--------------------+
            | Database           |
            +--------------------+
            | information_schema |
            | mysql              |
            | performance_schema |
            | sys                |
            +--------------------+
            4 rows in set (0.01 sec)
    
  • 使用指定數(shù)據(jù)庫 mysql>use mysql;(指定mysql庫)

        mysql> use mysql;
        Reading table information for completion of table and column names
        You can turn off this feature to get a quicker startup with -A

        Database changed
    ```
- 顯示庫中的數(shù)據(jù)表 `mysql>show tables;`
    

    ```
        mysql> show tables
            -> ;
        +---------------------------+
        | Tables_in_mysql           |
        +---------------------------+
        | columns_priv              |
        | db                        |
        | engine_cost               |
        | event                     |
        | func                      |
        | general_log               |
        | gtid_executed             |
        | help_category             |
        | help_keyword              |
        | help_relation             |
        | help_topic                |
        | innodb_index_stats        |
        | innodb_table_stats        |
        | ndb_binlog_index          |
        | plugin                    |
        | proc                      |
        | procs_priv                |
        | proxies_priv              |
        | server_cost               |
        | servers                   |
        | slave_master_info         |
        | slave_relay_log_info      |
        | slave_worker_info         |
        | slow_log                  |
        | tables_priv               |
        | time_zone                 |
        | time_zone_leap_second     |
        | time_zone_name            |
        | time_zone_transition      |
        | time_zone_transition_type |
        | user                      |
        +---------------------------+
        31 rows in set (0.01 sec)
    ```
- 顯示數(shù)據(jù)表結(jié)構(gòu)  `mysql>describe yourtablename; `
    ```
        mysql> describe INNODB_SYS_TABLESTATS;
        +-------------------+---------------------+------+-----+---------+-------+
        | Field             | Type                | Null | Key | Default | Extra |
        +-------------------+---------------------+------+-----+---------+-------+
        | TABLE_ID          | bigint(21) unsigned | NO   |     | 0       |       |
        | NAME              | varchar(193)        | NO   |     |         |       |
        | STATS_INITIALIZED | varchar(193)        | NO   |     |         |       |
        | NUM_ROWS          | bigint(21) unsigned | NO   |     | 0       |       |
        | CLUST_INDEX_SIZE  | bigint(21) unsigned | NO   |     | 0       |       |
        | OTHER_INDEX_SIZE  | bigint(21) unsigned | NO   |     | 0       |       |
        | MODIFIED_COUNTER  | bigint(21) unsigned | NO   |     | 0       |       |
        | AUTOINC           | bigint(21) unsigned | NO   |     | 0       |       |
        | REF_COUNT         | int(11)             | NO   |     | 0       |       |
        +-------------------+---------------------+------+-----+---------+-------+
        9 rows in set (0.01 sec)
    ```
-  建庫 `mysql>create database yourdbname;`
    ```
    mysql> create database binbinDatabases;
    Query OK, 1 row affected (0.00 sec)
    ```
- 建表 `mysql>create table yourtablename (columnname colunmtype, ...);`
    ```
    mysql> create table binbinFirstTable(Sno CHAR(9) PRIMARY KEY,
    -> Sname CHAR(20) UNIQUE,
    -> Ssex CHAR(2),
    -> Sage SMALLINT,
    -> Sdept CHAR(20));
    // 創(chuàng)建成功
    Query OK, 0 rows affected (0.01 sec)
    // 查看有沒有我們創(chuàng)建的表 `binbinFirstTable`
    mysql> show tables
    -> ;
    +--------------------+
    | Tables_in_binbindb |
    +--------------------+
    | binbinFirstTable   |
    +--------------------+
    1 row in set (0.00 sec)

    ```
-  表中插入數(shù)據(jù) `INSERT INTO yourtablename(屬性型1, 屬性型2, 屬性型3) VALUES(屬性值1, 屬性值2, 屬性值3) `
    ```
    // 注意: 這里的 `李華`, `Physics`
    mysql> INSERT INTO binbinFirstTable(Sno,Sname,Ssex,Sage,Sdept) VALUES(100,'李華',1,28,'Physics');
    Query OK, 1 row affected (0.01 sec)

    // 查看剛剛插入的表
    mysql> select *from binbinFirstTable;
    +-----+--------+------+------+---------+
    | Sno | Sname  | Ssex | Sage | Sdept   |
    +-----+--------+------+------+---------+
    | 100 | 李華   | 1    |   28 | Physics |
    +-----+--------+------+------+---------+

    //再次插入, 這里可以省略表屬性型, 不過要按照建表的屬性順序輸入值
    mysql> INSERT INTO binbinFirstTable VALUES(101,'韓梅梅',1,27,'Art');
           Query OK, 1 row affected (0.00 sec)
    //查看表內(nèi)容
    mysql> select * from binbinFirstTable;  
    +-----+-----------+------+------+---------+
    | Sno | Sname     | Ssex | Sage | Sdept   |
    +-----+-----------+------+------+---------+
    | 100 | 李華      | 1    |   28 | Physics |
    | 101 | 韓梅梅    | 1    |   27 | Art     |
    +-----+-----------+------+------+---------+
    2 rows in set (0.00 sec)

    ```
----
    
// 創(chuàng)建一個課程 Course 表
mysql> CREATE TABLE Course(
-> Cno CHAR(4) PRIMARY KEY,
-> Cname CHAR(40),
-> Cpno CHAR(4),
-> Ceredit SMALLINT
-> );
// 創(chuàng)建成功
Query OK, 0 rows affected (0.01 sec)
// 查看數(shù)據(jù)庫中的表 發(fā)現(xiàn)里面有 課程表 `Course` 但是沒有學(xué)生表 
mysql> SHOW TABLES;
+--------------------+
| Tables_in_binbindb |
+--------------------+
| Course             |
| binbinFirstTable   |
+--------------------+
2 rows in set (0.00 sec)

- 改表名 `rename table table1 to table2`

mysql> rename table binbinFirstTable to Student;
Query OK, 0 rows affected (0.01 sec)
// 可以看到 表 binbinFirstTable 已經(jīng)改名為 Student 了
mysql> show tables;
+--------------------+
| Tables_in_binbindb |
+--------------------+
| Course |
| Student |
+--------------------+
2 rows in set (0.00 sec)

- 添加列 `添加列:alter table 表名 add column 列名 varchar(30);`, column 可以省略
// 添加了S_score(分?jǐn)?shù))列
mysql> alter table Student add S_score SMALLINT;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0
// 查看添加結(jié)果
mysql> select * from Student;
+-----+-----------+------+------+---------+------------+---------+
| Sno | Sname     | Ssex | Sage | Sdept   | S_entrance | S_score |
+-----+-----------+------+------+---------+------------+---------+
| 100 | 李華      | 1    |   28 | Physics | NULL       |    NULL |
| 101 | 韓梅梅    | 1    |   27 | Art     | NULL       |    NULL |
+-----+-----------+------+------+---------+------------+---------+

2 rows in set (0.00 sec)


- 修改列 數(shù)據(jù)類型 `alter table 表名 modify 屬性名 數(shù)據(jù)類型;`

// 可以看到 Sage 的數(shù)據(jù)類型是 samllint(6), 我們要改為 char(4) 
mysql> DESC Student;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Sno        | char(9)     | NO   | PRI | NULL    |       |
| Sname      | char(20)    | YES  | UNI | NULL    |       |
| Ssex       | char(2)     | YES  |     | NULL    |       |
| Sage       | smallint(6) | YES  |     | NULL    |       |
| Sdept      | char(20)    | YES  |     | NULL    |       |
| S_entrance | date        | YES  |     | NULL    |       |
| S_score    | smallint(6) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

// 把 Sage 的數(shù)據(jù)類型改為 char(4)
mysql> alter table Student modify Sage varchar(4);
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

// 查看更改結(jié)果, 我們成功把 Sage 數(shù)據(jù)類型由 smallint(6) 改為了 char(4)
mysql> desc Student;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Sno        | char(9)     | NO   | PRI | NULL    |       |
| Sname      | char(20)    | YES  | UNI | NULL    |       |
| Ssex       | char(2)     | YES  |     | NULL    |       |
| Sage       | varchar(4)  | YES  |     | NULL    |       |
| Sdept      | char(20)    | YES  |     | NULL    |       |
| S_entrance | date        | YES  |     | NULL    |       |
| S_score    | smallint(6) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

- 添加取唯一值約束 `alter table 表名 add UNIQUE(屬性);`
// 我們給 表 Course 的 Cname (課程名) 添加唯一值約束

mysql> desc Course;
// 可以看到, 現(xiàn)在 Cname 是沒有約束的
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Cno | char(4) | NO | PRI | NULL | |
| Cname | char(40) | YES | | NULL | |
| Ceredit | smallint(6) | YES | | NULL | |
| Cpno | char(4) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

// 添加唯一值約束

mysql> alter table Course add UNIQUE(Cname);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

// 查看結(jié)果, 添加成功

mysql> desc Course;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Cno | char(4) | NO | PRI | NULL | |
| Cname | char(40) | YES | UNI | NULL | |
| Ceredit | smallint(6) | YES | | NULL | |
| Cpno | char(4) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Student 中忘記添加 下面的完整性約束條件, 百度下 給加上
FOREIGN KEY (Cno) REFERENCES

- 刪除列  `alter 表名 drop column 屬性名;`
// 我們要刪除 列 Cpno
mysql> desc Course;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Cno     | char(4)     | NO   | PRI | NULL    |       |
| Cname   | char(40)    | YES  | UNI | NULL    |       |
| Ceredit | smallint(6) | YES  |     | NULL    |       |
| Cpno    | char(4)     | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

// 刪除成功
mysql> alter table Course drop column Cpno;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

// Cpno 已經(jīng)被刪除
mysql> desc Course;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Cno     | char(4)     | NO   | PRI | NULL    |       |
| Cname   | char(40)    | YES  | UNI | NULL    |       |
| Ceredit | smallint(6) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)



- 修改列名字 `alter table 表名 change 舊屬性名 新屬性名字 數(shù)據(jù)類型;`
// 查看 Ceredit 狀態(tài)
mysql> desc Course;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Cno     | char(4)     | NO   | PRI | NULL    |       |
| Cname   | char(40)    | YES  | UNI | NULL    |       |
| Ceredit | smallint(6) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

// 修改 Ceredit 
mysql> alter table Course change Ceredit New_Ceredit int;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

// 查看修改結(jié)果
mysql> desc Course;
+-------------+----------+------+-----+---------+-------+
| Field       | Type     | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| Cno         | char(4)  | NO   | PRI | NULL    |       |
| Cname       | char(40) | YES  | UNI | NULL    |       |
| New_Ceredit | int(11)  | YES  |     | NULL    |       |
+-------------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

- 表記錄清空 `delete from 表名;`
// 刪除 表 Student

mysql> delete from Student;
Query OK, 2 rows affected (0.00 sec)
// 查看 表 Student 發(fā)現(xiàn)表已經(jīng)空了
mysql> select * from Student;
Empty set (0.00 sec)
// 查看表存不存在, 表 Student 還是存在的
mysql> desc Student;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Sno | char(9) | NO | PRI | NULL | |
| Sname | char(20) | YES | UNI | NULL | |
| Ssex | char(2) | YES | | NULL | |
| Sage | varchar(4) | YES | | NULL | |
| Sdept | char(20) | YES | | NULL | |
| S_entrance | date | YES | | NULL | |
| S_score | smallint(6) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

> 表記錄清空 `delete from 表名;` 和 刪表 `DROP TABLE Student [RESTRICT | CASCADE]` 是有區(qū)別的.
表記錄清空, 單純的清除表的內(nèi)容, 但是表還在.
但是 刪表, 是整個表都被刪除, 即 數(shù)據(jù)庫中不再存在這個表
----
- 刪表 `DROP TABLE Student [RESTRICT | CASCADE]`
    - RESTRICT 弱刪除, 當(dāng)表被其他表的約束引用(如: CHECK, FOREIGN KEY 等), 而且不能有(視圖,觸發(fā)器,存儲過程, 函數(shù)), 在這些條件下, 才能被刪除. 
    - CASCADE: 強(qiáng)刪除, 刪表的同時 會刪除相關(guān)的依賴對象, 如 視圖,會一起刪除
    ```
    // 刪除 Student 表
    mysql> Drop TALE Student CASCADE;
    // 這里報錯了, 是因為 Student 是 選課表 SC 的參照表, 里面的Sno 屬性是 SC 的外鍵, 所以無法刪除
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TALE Student CASCADE' at line 1
    ```
- 刪庫
`mysql>drop database yourdbname;`

----

- [MySQL 教程](http://www.runoob.com/mysql/mysql-tutorial.html)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 什么是數(shù)據(jù)庫? 數(shù)據(jù)庫是存儲數(shù)據(jù)的集合的單獨的應(yīng)用程序。每個數(shù)據(jù)庫具有一個或多個不同的API,用于創(chuàng)建,訪問,管理...
    chen_000閱讀 4,057評論 0 19
  • 1. 連接MySQL數(shù)據(jù)庫 打開命令行終端程序,windows CMD(首先要在系統(tǒng)環(huán)境變量中配置好MySQL的路...
    米酒真香閱讀 4,351評論 0 50
  • 當(dāng)前備份數(shù)據(jù)庫:http://pan.baidu.com/s/1nvzA68p 一、登錄數(shù)據(jù)庫 在此就不多敘述了 ...
    流水潺湲閱讀 7,296評論 1 26
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,767評論 18 399
  • 早上打了輛快車出門。司機(jī)打電話過來確認(rèn)具體位置時,給我種他很著急的感覺,因為他連說了兩遍,“我知道在哪里,我轉(zhuǎn)彎就...
    落在枝頭閱讀 441評論 0 1