MySQL資料庫常用命令集

來源:互聯網
上載者:User

標籤:des   http   os   ar   使用   sp   strong   資料   div   

MySQL是世界上最流行的開來源資料庫。目前很多人都在使用資料庫管理工具,協助自己開發程式。像Navicat和phpadmin都是非常熱門的MySQL資料庫管理工具。當作為初學者,知道必要的MySQL資料庫常用命令是非常重要的,不僅可以更好的理解管理工具的執行模式,而且可以大型項目整合中,體現出更佳的操作資料庫優勢。

MySQL資料庫常用命令包括對資料庫及其資料庫中的表進行操作,其中對錶的操作,對初學者來說非常關鍵。

處理資料庫

查看資料庫

擷取伺服器上的資料庫列表通常很有用。執行show databases;命令就可以搞定。

mysql>show databases;

建立資料庫

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

使用資料庫

資料庫一旦建立,就可以通過“使用”(use命令)資料庫,將其指定為預設的工作資料庫。

mysql> use db_test;
Database changed

刪除資料庫

刪除資料庫的方式與建立的方式很相似。可以在mysql用戶端中使用drop命令刪除資料庫,如下:

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

處理表

這裡將對如何建立、列出、查看、刪除和修改MySQL資料庫表。

建立表

表通過create table語句來建立。建立表的過程中會使用非常多的選項和子句,在這裡完全總結一遍也是不現實的,這裡只是總結最普遍的,以後遇到別的,再單個總結。建立表的一般用法如下:

1 2 3 4 5 6 7 mysql> create table tb_test(      -> id int unsigned not null auto_increment,      -> firstname var char( 25 ) not null ,      -> lastname var char( 25 ) not null ,      -> email var char( 45 ) not null ,      -> phone var char( 10 ) not null ,      -> primary key(id));

記住,表至少包含一列。另外,建立表之後總是可以再回過頭來修改表的結構。無論當前是否在使用目標資料庫,都可以建立表,只要在表名前面加上目標資料庫即可。例如:

1 2 3 4 5 6 7 mysql> create table db_test.tb_test(      -> id int unsigned not null auto_increment,      -> firstname var char( 25 ) not null ,      -> lastname var char( 25 ) not null ,      -> email var char( 45 ) not null ,      -> phone var char( 10 ) not null ,      -> primary key(id));

Query OK, 0 rows affected (0.03 sec)

有條件的建立表

在預設情況下,如果試圖建立一個已經存在的表,MySQL會產生一個錯誤。為了避免這個錯誤,create table語句提供了一個子句,如果你希望在目標表已經存在的情況下簡單地退出表建立,就可以使用這個子句。例如:

1 2 3 4 5 6 7 mysql> create table if not exists db_test.tb_test(      -> id int unsigned not null auto_increment,      -> firstname varchar (25) not null ,      -> lastname varchar (25) not null ,      -> email varchar (45) not null ,      -> phone varchar (10) not null ,      -> primary key (id));

Query OK, 0 rows affected, 1 warning (0.00 sec)

基於現有的表建立新表是一項很容易的任務。以下代碼將得到tb_test表的一個副本,名為tb_test2:

1 mysql> create table tb_test2 select * from db_test.tb_test;

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0

將向資料庫增加一個相同的表tb_test2。而有的時候,可能希望只基於現有表的幾個列建立一個表。通過create select語句中指定列就可以實現:

mysql> describe tb_test;

+-----------+------------------+------+-----+---------+----------------+

| Field     | Type             | Null | Key | Default | Extra          |

+-----------+------------------+------+-----+---------+----------------+

| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| firstname | varchar(25)      | NO   |     | NULL    |                |

| lastname  | varchar(25)      | NO   |     | NULL    |                |

| email     | varchar(45)      | NO   |     | NULL    |                |

| phone     | varchar(10)      | NO   |     | NULL    |                |

+-----------+------------------+------+-----+---------+----------------+

5 rows in set (0.01 sec)

mysql> create table tb_test2 select id, firstname,lastname, email from tb_test;

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> describe tb_test2;

+-----------+------------------+------+-----+---------+-------+

| Field     | Type             | Null | Key | Default | Extra |

+-----------+------------------+------+-----+---------+-------+

| id        | int(10) unsigned | NO   |     | 0       |       |

| firstname | varchar(25)      | NO   |     | NULL    |       |

| lastname  | varchar(25)      | NO   |     | NULL    |       |

| email     | varchar(45)      | NO   |     | NULL    |       |

+-----------+------------------+------+-----+---------+-------+

4 rows in set (0.01 sec)

建立暫存資料表

有的時候,當工作在非常大的表上時,可能偶爾需要運行很多查詢獲得一個大量資料的小的子集,不是對整個表運行這些查詢,而是讓MySQL每次找出 所 需的少數記錄,將記錄儲存到一個暫存資料表可能更快一些,然後對這些暫存資料表進行查詢操作。可以通過使用temporary關鍵字和 create table語句來實現。

mysql> create temporary table emp_temp select firstname, lastname from tb_test;

Query OK, 0 rows affected (0.02 sec)

Records: 0  Duplicates: 0  Warnings: 0

暫存資料表的建立與其它表一樣,只是它們儲存在作業系統指定的臨時目錄中。暫存資料表將在你串連MySQL期間存在,當你斷開時,MySQL將自動刪除表並釋放所有的記憶體空間;當然了,你也可以手動的使用drop table命令刪除暫存資料表。
查看資料庫中可用的表

可以使用show tables命令完成。例如:

mysql> show tables;

+-------------------+

| Tables_in_db_test |

+-------------------+

| tb_test           |

| tb_test2          |

+-------------------+

2 rows in set (0.00 sec)

查看錶結構

可以使用describe語句查看錶結構,例如:

mysql> describe tb_test;

+-----------+------------------+------+-----+---------+----------------+

| Field     | Type             | Null | Key | Default | Extra          |

+-----------+------------------+------+-----+---------+----------------+

| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| firstname | varchar(25)      | NO   |     | NULL    |                |

| lastname  | varchar(25)      | NO   |     | NULL    |                |

| email     | varchar(45)      | NO   |     | NULL    |                |

| phone     | varchar(10)      | NO   |     | NULL    |                |

+-----------+------------------+------+-----+---------+----------------+

5 rows in set (0.00 sec)

另外,使用show命令也能得到相同的結果,例如:

mysql> show columns in tb_test;

+-----------+------------------+------+-----+---------+----------------+

| Field     | Type             | Null | Key | Default | Extra          |

+-----------+------------------+------+-----+---------+----------------+

| id        | int(10) unsigned | NO   | PRI | NULL    | auto_increment |

| firstname | varchar(25)      | NO   |     | NULL    |                |

| lastname  | varchar(25)      | NO   |     | NULL    |                |

| email     | varchar(45)      | NO   |     | NULL    |                |

| phone     | varchar(10)      | NO   |     | NULL    |                |

+-----------+------------------+------+-----+---------+----------------+

5 rows in set (0.00 sec)

刪除表

刪除表是使用drop table語句實現的,其文法如下:

drop [temporary] table [if exists] tbl_name [, tbl_name, ...]

更改表結構

我們會發現,我們會經常修改和改進表結構,特別是在開發初期;但是,每次進行修改時不必都先刪除再重新建立表。相反,可以使用alter語句修改 表 的結構。利用這個語句,可以再必要時刪除、修改和增加列。和create table一樣,alter table提供了很多子句、關鍵字和選項。這 裡只是會說一些簡單的使用,比如在表tb_demo表中插入一列,表示email,代碼如下:

mysql> alter table tb_demo add column email varchar(45);

Query OK, 0 rows affected (0.14 sec)

Records: 0  Duplicates: 0  Warnings: 0

新的列放在表的最後位置。不過,還可以使用適當的關鍵字(包括first、after和last)來控制新列的位置。如果想修改表,比如,剛剛加的email,我想加入一個not null控制,代碼可以是這樣的:

mysql> alter table tb_demo change email email varchar(45) not null;

Query OK, 0 rows affected (0.11 sec)

Records: 0  Duplicates: 0  Warnings: 0

如果覺的這個email這列沒有存在的必要了,可以使用下面的代碼刪除它,例如:

mysql> alter table tb_demo drop email;

Query OK, 0 rows affected (0.09 sec)

Records: 0  Duplicates: 0  Warnings: 0


MySQL資料庫常用命令集

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.