MySQL之終端Terminal(dos界面)治理數據庫、數據表、數據的根本操作。本站提示廣大學習愛好者:(MySQL之終端Terminal(dos界面)治理數據庫、數據表、數據的根本操作)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL之終端Terminal(dos界面)治理數據庫、數據表、數據的根本操作正文
MySQL有許多的可視化治理對象,好比“mysql-workbench”和“sequel-pro-”。 如今我寫MySQL的終端敕令操作的文章,是想強化一下本身關於MySQL的懂得,總會比應用圖形化的懂得透辟,由於我原來就比擬愛好寫代碼。同時寫出來這些文章,是想要給年夜家當個參考,願望也能對年夜家有所贊助,有所晉升,這就是我為何要寫終端操作MySQL的文章了。
留意:MySQL數據庫敕令不辨別年夜小寫。但在MAC的終端,假如你想應用tab主動補全敕令,那末你就必需應用年夜寫,如許MAC的終端才會幫你補全敕令,不然你按N遍tab都不會有呼應。
1、數據庫(database)治理
1.1 create 創立數據庫
1.2 show 檢查一切數據庫
1.3 alter 修正數據庫
1.4 use 應用數據庫
1.5 檢查以後應用的數據庫
1.6 drop 刪除數據庫
2、數據表(table)治理
2.1 create 創立表
2.2 show 顯示表
2.3 desc 檢查表構造
2.4 alter 修正表構造(增、刪、改)
2.4.1 insert 在表中添加列(字段)
2.4.2 alter 修正表(列)字段
2.4.3 delete 刪除表(列)字段
2.4.4 rename 重定名表名
2.5 create 應用已稀有據創立新表
3、數據的操作及治理
3.1 增長數據(增)
3.2 刪除數據(刪)
3.3 修正數據(改)
3.4 查詢數據(查)
1、數據庫(database)治理
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)
2、數據表(table)治理
我們起首創立一個數據庫,供給我們往後的應用:
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
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裡,布爾類型會主動轉換為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)
如今,你該信任我了吧?
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
我們再次應用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)
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表構造:
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的字段了。
2.4.4 rename 重定名表名
mysql> RENAME TABLE PEOPLE TO NEW_PEOPLE; Query OK, 0 rows affected (0.00 sec)
2.5 create 應用已稀有據創立新表
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)
3.2 刪除數據(刪)
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”的數據了。
3.3 修正數據(改)
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 查詢數據(查)
select 敕令查詢數據,最簡略的就是查詢表的一切數據,也就是我們最後應用到的那條敕令:
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 <表名>,*代表一切字段。
查詢數據時也可指定顯示的(列)字段:
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)的數據,使數據按前提排序(order by),按查詢前提顯示數據(where)等等。這些都邑鄙人一篇文章作重點引見,請年夜家持續留心我的博客,感謝。