Mac上如何實現(xiàn)終端管理MySQL數(shù)據(jù)庫

這篇文章主要介紹了Mac上如何實現(xiàn)終端管理MySQL數(shù)據(jù)庫,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的確山網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

打開終端輸入如下命令:

/usr/local/MySQL/bin/mysql -u root -p

其中root為用戶名。

這時會出現(xiàn)如下命令:

Enter password:

此時如果你沒有改密碼,直接敲回車。否則,輸入你的密碼。
這樣就可以訪問你的數(shù)據(jù)庫服務(wù)器了。


1、的操作及管理

數(shù)據(jù)表的基

數(shù)據(jù)庫(database)管理

1.1 create 創(chuàng)建數(shù)據(jù)庫

create database firstDB;

1.2 show 查看所有數(shù)據(jù)庫

mysql> show databases;+--------------------+| Database           |
+--------------------+| information_schema |
| firstDB            |
| mysql              |
| performance_schema |+--------------------+rows in set (0.00 sec)

1.3 alter 修改數(shù)據(jù)庫

alter 命令修改數(shù)據(jù)庫編碼:

默認創(chuàng)建的數(shù)據(jù)庫默認不支持中文字符,如果我們需要它支持中文字符,則將它的編碼設(shè)置為utf8格式:

mysql> ALTER DATABASE testDB CHARACTER SET UTF8;Query OK, 1 row affected (0.00 sec)

1.4 use 使用數(shù)據(jù)庫

mysql> use firstDB;
Database changed

1.5 查看當前使用的數(shù)據(jù)庫

mysql> select database();
+------------+| database() |
+------------+| firstdb    |
+------------+1 row in set (0.00 sec)

1.6 drop 刪除數(shù)據(jù)庫

mysql> drop database firstDB;
Query OK, 0 rows affected (0.00 sec)

2、數(shù)據(jù)表(table)管理

我們首先創(chuàng)建一個數(shù)據(jù)庫,提供我們往后的使用:

mysql> create database testDB;Query OK, 1 row affected (0.00 sec)

創(chuàng)建后記得用use命令進入(使用)數(shù)據(jù)庫,不然后面的操作都會不成功的。

2.1 create 創(chuàng)建表

mysql> create table PEOPLE (
    -> ID int AUTO_INCREMENT PRIMARY KEY,    -> NAME varchar(20) not null,    -> AGE int not null,    -> BIRTHDAY datetime);                                                     
Query OK, 0 rows affected (0.01 sec)

2.2 show 顯示表

顯示當前數(shù)據(jù)庫所有的數(shù)據(jù)表

mysql> show tables;
+------------------+| Tables_in_testdb |
+------------------+| PEOPLE           |
+------------------+1 row in set (0.00 sec)

2.3 desc 查看表結(jié)構(gòu)

mysql> desc PEOPLE    -> ;
+----------+-------------+------+-----+---------+----------------+| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+| ID       | int(11)     | NO   | PRI | NULL    | auto_increment |
| NAME     | varchar(20) | NO   |     | NULL    |                |
| AGE      | int(11)     | NO   |     | NULL    |                |
| BIRTHDAY | datetime    | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

2.4 alter 修改表結(jié)構(gòu)(增、刪、改)
默認創(chuàng)建的表不支持中文字符,所以需將表編碼設(shè)置為utf8:

mysql> ALTER TABLE KEYCHAIN CONVERT TO CHARACTER SET UTF8;
Query OK, 1 row affected (0.02 sec)
Records: 1  Duplicates: 0  Warnings: 0

2.4.1 insert 在表中添加列(字段)

mysql> alter table PEOPLE add star BOOL;Query OK, 0 rows affected (0.02 sec)Records: 0  Duplicates: 0  Warnings: 0

提示:在MySQL里,布爾類型會自動轉(zhuǎn)換為tinyint(1)類型。

我們不妨使用desc去查看一下PEOPLE表結(jié)構(gòu):

mysql> desc PEOPLE;
+----------+-------------+------+-----+---------+----------------+| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+| ID       | int(11)     | NO   | PRI | NULL    | auto_increment |
| NAME     | varchar(20) | NO   |     | NULL    |                |
| AGE      | int(11)     | NO   |     | NULL    |                |
| BIRTHDAY | datetime    | YES  |     | NULL    |                |
| star     | tinyint(1)  | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

現(xiàn)在,你該相信我了吧?

2.4.2 alter 修改表(列)字段

mysql> alter table PEOPLE MODIFY star int;Query OK, 0 rows affected (0.01 sec)Records: 0  Duplicates: 0  Warnings: 0

也可以指定 int(n) 的長度,比如 int(2)。

我們再次使用desc查看PEOPLE表結(jié)構(gòu):

mysql> desc PEOPLE;
+----------+-------------+------+-----+---------+----------------+| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+| ID       | int(11)     | NO   | PRI | NULL    | auto_increment |
| NAME     | varchar(20) | NO   |     | NULL    |                |
| AGE      | int(11)     | NO   |     | NULL    |                |
| BIRTHDAY | datetime    | YES  |     | NULL    |                |
| star     | int(11)     | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

2.4.3 delete 刪除表(列)字段

mysql> alter table PEOPLE DROP column star;Query OK, 0 rows affected (0.02 sec)Records: 0  Duplicates: 0  Warnings: 0

刪除后,再次查看PEOPLE表結(jié)構(gòu):

mysql> desc PEOPLE;
+----------+-------------+------+-----+---------+----------------+| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+| ID       | int(11)     | NO   | PRI | NULL    | auto_increment |
| NAME     | varchar(20) | NO   |     | NULL    |                |
| AGE      | int(11)     | NO   |     | NULL    |                |
| BIRTHDAY | datetime    | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

刪除字段成功,現(xiàn)在我們已經(jīng)不能看到star的字段了。

2.4.4 rename 重命名表名

mysql> RENAME TABLE PEOPLE TO NEW_PEOPLE;
Query OK, 0 rows affected (0.00 sec)

2.4.5 null or not null
修改表字段允許為空或不允許為空:

mysql> ALTER TABLE PEOPLE MODIFY AGE INT(3) NULL;
Query OK, 0 rows affected (0.01 sec)Records: 0  Duplicates: 0  Warnings: 0

把 PEOPLE 表的 AGE 字段設(shè)置成“允許為空”,即插入記錄時這個字段可以不錄入。否則相反。
它的格式為:ALTER TABLE  MODIFY

mysql> create table newTable select * from PEOPLE;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

我們查看一下目前數(shù)據(jù)庫存在的表:

mysql> show tables;
+------------------+| Tables_in_testdb |
+------------------+| PEOPLE           || newTable         |
+------------------+2 rows in set (0.00 sec)

3、數(shù)據(jù)的操作及管理

本操作,包含增、刪、改、查數(shù)據(jù)。
以下命令均在PEOPLE表上操作。

3.1 增加數(shù)據(jù)(增)
PEOPLE表目前是沒有數(shù)據(jù)的,它是空的數(shù)據(jù)表,我們現(xiàn)在先添加一些數(shù)據(jù)。
insert into 命令添加數(shù)據(jù):

mysql> insert into PEOPLE VALUES (null, 'Anny', 22, '1992-05-22');
Query OK, 1 row affected (0.00 sec)

使用select命令查看表(會在后面介紹),現(xiàn)在我們查看PEOPLE數(shù)據(jù)表的數(shù)據(jù):

mysql> select * from PEOPLE;
+----+------+-----+---------------------+| ID | NAME | AGE | BIRTHDAY            |
+----+------+-----+---------------------+|  1 | Anny |  22 | 1992-05-22 00:00:00 |
+----+------+-----+---------------------+1 row in set (0.00 sec)

數(shù)據(jù)表現(xiàn)在有一條數(shù)據(jù)。

我們多添加幾條數(shù)據(jù),如:

mysql> select * from PEOPLE;
+----+--------+-----+---------------------+| ID | NAME   | AGE | BIRTHDAY            |
+----+--------+-----+---------------------+|  1 | Anny   |  22 | 1992-05-22 00:00:00 |
|  2 | Garvey |  23 | 1991-05-22 00:00:00 |
|  3 | Lisa   |  25 | 1989-05-22 00:00:00 |
|  4 | Nick   |  24 | 1990-05-22 00:00:00 ||  5 | Rick   |  24 | 1991-05-22 00:00:00 |
+----+--------+-----+---------------------+5 rows in set (0.00 sec)

3.2 刪除數(shù)據(jù)(刪)

delete 命令刪除數(shù)據(jù):

mysql> delete from PEOPLE where name = 'Lisa';
Query OK, 1 row affected (0.01 sec)

再次查詢PEOPLE表:

mysql> select * from PEOPLE;
+----+--------+-----+---------------------+| ID | NAME   | AGE | BIRTHDAY            |
+----+--------+-----+---------------------+|  1 | Anny   |  22 | 1992-05-22 00:00:00 |
|  2 | Garvey |  23 | 1991-05-22 00:00:00 |
|  4 | Nick   |  24 | 1990-05-22 00:00:00 ||  5 | Rick   |  24 | 1991-05-22 00:00:00 |
+----+--------+-----+---------------------+4 rows in set (0.00 sec)

已經(jīng)看不到名為“Lisa”的數(shù)據(jù)了。

3.3 修改數(shù)據(jù)(改)

update 命令修改數(shù)據(jù):

mysql> update PEOPLE set name='Calvin' where name = 'Garvey';
Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0

查詢PEOPLE表內(nèi)容:

mysql> select * from PEOPLE;
+----+--------+-----+---------------------+| ID | NAME   | AGE | BIRTHDAY            |
+----+--------+-----+---------------------+|  1 | Anny   |  22 | 1992-05-22 00:00:00 |
|  2 | Calvin |  23 | 1991-05-22 00:00:00 |
|  4 | Nick   |  24 | 1990-05-22 00:00:00 ||  5 | Rick   |  24 | 1991-05-22 00:00:00 |
+----+--------+-----+---------------------+4 rows in set (0.00 sec)

名為“Garvey”的記錄已經(jīng)修改為“Calvin”。

3.4 查詢數(shù)據(jù)(查)

select 命令查詢數(shù)據(jù),最簡單的就是查詢表的所有數(shù)據(jù),也就是我們最初使用到的那條命令:

mysql> select * from PEOPLE;
+----+--------+-----+---------------------+| ID | NAME   | AGE | BIRTHDAY            |
+----+--------+-----+---------------------+|  1 | Anny   |  22 | 1992-05-22 00:00:00 |
|  2 | Calvin |  23 | 1991-05-22 00:00:00 |
|  4 | Nick   |  24 | 1990-05-22 00:00:00 ||  5 | Rick   |  24 | 1991-05-22 00:00:00 |
+----+--------+-----+---------------------+4 rows in set (0.00 sec)

格式:select * from <表名>,*代表所有字段。

查詢數(shù)據(jù)時也可指定顯示的(列)字段:

mysql> select NAME, AGE, BIRTHDAY from PEOPLE;
+--------+-----+---------------------+| NAME   | AGE | BIRTHDAY            |
+--------+-----+---------------------+| Anny   |  22 | 1992-05-22 00:00:00 |
| Calvin |  23 | 1991-05-22 00:00:00 |
| Nick   |  24 | 1990-05-22 00:00:00 || Rick   |  24 | 1991-05-22 00:00:00 |
+--------+-----+---------------------+4 rows in set (0.00 sec)

格式:select <字段名,字段名,…> from <表名>。

select查詢命令還有很多的高級用法,比如用來查找不重復(distinct)的數(shù)據(jù),使數(shù)據(jù)按條件排序(order by),按查詢條件顯示數(shù)據(jù)(where)等等。這些都會在下一篇文章作重點介紹,請大家繼續(xù)留意我的博客,謝謝。

4、管理視圖

4.1 創(chuàng)建視圖

視圖是從數(shù)據(jù)庫里導出一個或多個表的虛擬表,是用來方便用戶對數(shù)據(jù)的操作。

mysql> CREATE VIEW PEOPLE_VIEW (
    -> NAME, AGE)
    -> AS SELECT NAME, AGE FROM PEOPLE;

創(chuàng)建成功后查看視圖。

PEOPLE          PEOPLE.AGE      PEOPLE.BIRTHDAY PEOPLE.ID       PEOPLE.NAME    mysql> SELECT * FROM PEOPLE_VIEW
    -> ;+--------+-----+
| NAME   | AGE |
+--------+-----+
| Anny   |  22 |
| Calvin |  23 |
| Nick   |  24 |
| Rick   |  24 |
+--------+-----+4 rows in set (0.00 sec)

我們也可以使用 DESC 命令查看視圖的結(jié)構(gòu)。

mysql> DESC PEOPLE_VIEW;
+-------+---------+------+-----+---------+-------+| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+| ID    | int(11) | NO   |     | 0       |       |
+-------+---------+------+-----+---------+-------+1 row in set (0.01 sec)

4.2 替換視圖

創(chuàng)建或替換原有視圖。

mysql> CREATE OR REPLACE VIEW PEOPLE_VIEW(PEOPLE_ID,PEOPLE_NAME,PEOPLE_AGE) AS SELECT ID,NAME,AGE FROM PEOPLE;
Query OK, 0 rows affected (0.00 sec)

創(chuàng)建或替換后查看視圖。

mysql> SELECT * FROM PEOPLE_VIEW;
+-----------+-------------+------------+| PEOPLE_ID | PEOPLE_NAME | PEOPLE_AGE |
+-----------+-------------+------------+|         1 | Anny        |         22 |
|         2 | Calvin      |         23 |
|         4 | Nick        |         24 ||         5 | Rick        |         24 |
+-----------+-------------+------------+4 rows in set (0.00 sec)

4.3 操作視圖

當視圖數(shù)據(jù)有變化時(增、刪、改),真實的表數(shù)據(jù)也會隨著改變。也就是說,對視圖的操作就是對表的數(shù)據(jù),所以我們可以把視圖當作表。

例:往視圖插入一條數(shù)據(jù)。

mysql> INSERT INTO PEOPLE_VIEW VALUES(NULL, 'Kerry', '33');
Query OK, 1 row affected (0.00 sec)

插入數(shù)據(jù)成功后查看視圖。

mysql> SELECT * FROM PEOPLE_VIEW ;
+-----------+-------------+------------+| PEOPLE_ID | PEOPLE_NAME | PEOPLE_AGE |
+-----------+-------------+------------+|         1 | Anny        |         22 |
|         2 | Calvin      |         23 |
|         4 | Nick        |         24 |
|         5 | Rick        |         24 ||         6 | Kerry       |         33 |
+-----------+-------------+------------+5 rows in set (0.00 sec)

可以在視圖上看到我們剛剛插入的數(shù)據(jù),現(xiàn)在我們就來驗證一下真實的表是否也會作出變化。

mysql> SELECT * FROM PEOPLE;
+----+--------+-----+---------------------+| ID | NAME   | AGE | BIRTHDAY            |
+----+--------+-----+---------------------+|  1 | Anny   |  22 | 1992-05-22 00:00:00 |
|  2 | Calvin |  23 | 1991-05-22 00:00:00 |
|  4 | Nick   |  24 | 1990-05-22 00:00:00 |
|  5 | Rick   |  24 | 1991-05-22 00:00:00 ||  6 | Kerry  |  33 | NULL                |
+----+--------+-----+---------------------+5 rows in set (0.00 sec)

可見,真實的表數(shù)據(jù)也已經(jīng)有所改變,剛剛往視圖里插入的那一條數(shù)據(jù)存在于真實表中,真理便是:對視圖的操作就是對表的數(shù)據(jù)。

4.4 刪除視圖

mysql> DROP VIEW PEOPLE_VIEW;Query OK, 0 rows affected (0.00 sec)

感謝你能夠認真閱讀完這篇文章,希望小編分享Mac上如何實現(xiàn)終端管理MySQL數(shù)據(jù)庫內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細的解決方法等著你來學習!

分享文章:Mac上如何實現(xiàn)終端管理MySQL數(shù)據(jù)庫
當前地址:http://muchs.cn/article28/pjjjcp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導航、靜態(tài)網(wǎng)站外貿(mào)網(wǎng)站建設(shè)、域名注冊營銷型網(wǎng)站建設(shè)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)