MySQL有許多的視覺化管理工具,像是「mysql-workbench」和「sequel-pro-」。 現在我寫MySQL的終端指令操作的文章,是想強化一下自己對於MySQL的理解,總是會比使用圖形化的理解透徹,因為我本來就比較喜歡寫程式碼。同時寫出來這些文章,是想要給大家當個參考,希望也能對大家有幫助,有所提升,這就是我為什麼要寫終端操作MySQL的文章了。
注意:MySQL資料庫指令不區分大小寫。但在MAC的終端,如果你想使用tab自動補全指令,那麼你就必須使用大寫,這樣MAC的終端才會幫你補全指令,否則你按N遍tab都不會回應。
1.1 create 建立資料庫
create database firstDB;
#1.2 show 檢視所有資料庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | firstDB | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec)
1.3 alter 修改資料庫
alter 指令修改資料庫編碼:
預設建立的資料庫預設不支援中文字符,如果我們需要它支援中文字符,則將它的編碼設定為utf8格式:
mysql> ALTER DATABASE testDB CHARACTER SET UTF8; Query OK, 1 row affected (0.00 sec)
1.4 use 使用資料庫
mysql> use firstDB; Database changed
#1.5 查看目前使用的資料庫
mysql> select database(); +------------+ | database() | +------------+ | firstdb | +------------+ 1 row in set (0.00 sec)
1.6 drop 刪除資料庫
mysql> drop database firstDB; Query OK, 0 rows affected (0.00 sec)
我們先建立一個資料庫,提供我們往後的使用:
mysql> create database testDB; Query OK, 1 row affected (0.00 sec)
建立後記得用use指令進入(使用)資料庫,不然後面的操作都會不成功的。
2.1 create 建立表格
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 顯示表
顯示目前資料庫所有的資料表mysql> show tables; +------------------+ | Tables_in_testdb | +------------------+ | PEOPLE | +------------------+ 1 row in set (0.00 sec)
2.3 desc 檢視表結構
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 修改表結構(增、刪、改)##默認創建的表不支援中文字符,所以需要將表編碼設為utf8:
#
mysql> ALTER TABLE KEYCHAIN CONVERT TO CHARACTER SET UTF8; Query OK, 1 row affected (0.02 sec) Records: 1 Duplicates: 0 Warnings: 0
mysql> alter table PEOPLE add star BOOL; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
#提示:在MySQL裡,布林類型會自動轉換為tinyint(1 )類型。
我們不妨使用desc去查看PEOPLE表結構:
#
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)
現在,你該相信我了吧?
#
mysql> alter table PEOPLE MODIFY star int; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
我們再次使用desc查看PEOPLE表結構:
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)
#
mysql> alter table PEOPLE DROP column star; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
#刪除後,再查看PEOPLE表結構:
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)
#刪除欄位成功,現在我們已經不能看到star的欄位了。
#
mysql> RENAME TABLE PEOPLE TO NEW_PEOPLE; Query OK, 0 rows affected (0.00 sec)
mysql> create table newTable select * from PEOPLE; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
#我們來看看目前資料庫存在的資料表:
mysql> show tables; +------------------+ | Tables_in_testdb | +------------------+ | PEOPLE | | newTable | +------------------+ 2 rows in set (0.00 sec)
3、資料的操作及管理
以下指令均在PEOPLE表上操作。
3.1 增加資料(增)PEOPLE表目前是沒有資料的,它是空的資料表,我們現在先加入一些資料。
insert into 指令新增資料:
mysql> insert into PEOPLE VALUES (null, 'Anny', 22, '1992-05-22'); Query OK, 1 row affected (0.00 sec)
使用select指令查看表格(會在後面介紹),現在我們查看PEOPLE資料表的資料:
mysql> select * from PEOPLE; +----+------+-----+---------------------+ | ID | NAME | AGE | BIRTHDAY | +----+------+-----+---------------------+ | 1 | Anny | 22 | 1992-05-22 00:00:00 | +----+------+-----+---------------------+ 1 row in set (0.00 sec)
資料表現在有一條資料。
我們多加入幾個數據,如:
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)
delete 指令刪除資料:
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)
已經看不到名為「Lisa」的資料了。
update 指令修改資料:
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表格內容:
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」的記錄已修改為「Calvin」。
3.4 查詢數據(查)
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 a26d98d33123a70024fa8ba5642906c6,*代表所有欄位。 ######查詢資料時也可指定顯示的(列)欄位:#############
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 b1fbf6fe5b2012d82009bbcec12b9c40 from a26d98d33123a70024fa8ba5642906c6。 ###
select查詢命令還有很多的高級用法,例如用來查找不重複(distinct)的數據,使數據按條件排序(order by),按查詢條件顯示數據(where)等等。這些都會在下一篇文章作重點介紹,請大家繼續留意我的博客,謝謝。
以上就是MySQL終端機管理資料庫操作指南的內容,更多相關內容請關注PHP中文網(www.php.cn)!