A.建立資料庫、資料表
root登入MySQL
建立一個名稱為demo的資料庫
1
<code>CREATE SCHEMA demo;</code>
使用USE指令指定使用demo資料庫
<code>USE demo;</code>
建立一個t_message表格,其中有id,name,email,msg等字段
2
3
4
5
6
<code>CREATE TABLE t_message(</code>
<code>id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,</code>
<code>name CHAR(</code><code>20</code><code>) NOT NULL,</code>
<code>email CHAR(</code><code>40</code><code>),</code>
<code>msg TEXT NOT NULL</code>
<code>);</code>
設定id為AUTO_INCREMENT,表示由MySQL來管理字段的值,如果沒有指定id的值,則會根據上一筆資料的id值自動遞增;指定PRIMARY KEY可以指定字段設定主鍵。也可以像下面這樣設定主鍵:
7
<code>id INT NOT NULL AUTO_INCREMENT,</code>
<code>msg TEXT NOT NULL,</code>
<code>PRIMARY KEY(id)</code>
删除資料表
<code>DROP TABLE t_message;</code>
删除整個資料庫
<code>DROP SCHEMA demo;</code>
B.CRUD操作
資料庫4個基本操作:CRUD也就是Create、Read、Update、Delete
對應的4個SQL語句:INSERT、SELECT、UPDATE、DELECT
1.INSERT(增)
為t_message資料表插入一筆新資料
<code>INSERT INTO t_message VALUES(</code><code>1</code><code>,</code><code>'caterpillar'</code><code>,</code><code>'[email protected]'</code><code>,</code><code>'This is a new message'</code><code>);</code>
如果想用不同的插入順序
<code>INSERT INTO t_message(name,msg)VALUES(</code><code>'caterpillar'</code><code>,</code><code>'this is a new message'</code><code>)</code>
添加多筆資料
<code>INSERT INTO t_message VALUES</code>
<code>(</code><code>1</code><code>,</code><code>'bush'</code><code>,</code><code>'[email protected]'</code><code>,</code><code>'a new message1'</code><code>),</code>
<code>(</code><code>2</code><code>,</code><code>'justin'</code><code>,</code><code>'[email protected]'</code><code>,</code><code>'a new message2'</code><code>),</code>
<code>(</code><code>3</code><code>,</code><code>'momor'</code><code>,</code><code>'[email protected]'</code><code>,</code><code>'a new message3'</code><code>);</code>
2.SELECT(查)
将資料表所有資料查詢出來
<code>SELECT * FROM t_message;</code>
使用指定條件查詢
<code>SELECT * FROM t_message WHERE name=</code><code>'justin'</code><code>;</code>
兩個以上條件可以使用AND或者OR來加以組合
<code>SELECT * FROM t_message WHERE name=</code><code>'justin'</code> <code>AND </code><code>'[email protected]'</code><code>;</code>
同時取得兩個字段的資料
<code>SELECT name,msg FROM t_message;</code>
根據name字段的值排序(預設是順序)
<code>SELECT * FROM t_message ORDER BY name;</code>
反序排列,加入DESC
<code>SELECT * FROM t_message ORDER BY name DESC;</code>
3.UPDATE(改)
更新資料表中已登入的資料
<code>UPDATE t_message SET name=</code><code>'caterpillar'</code> <code>WHERE id=</code><code>1</code><code>;</code>
如果沒有使用WHERE限定更新條件,将對所有資料生效
UPDATE也可以用時更新多個字段
<code>UPDATE t_message</code>
<code>SET name=</code><code>'caterpillar'</code><code>,email=</code><code>'[email protected]'</code>
<code>WHERE id=</code><code>1</code><code>;</code>
4.DELETE(删)
删除資料表中的資料
<code>DELETE FROM t_message WHERE id=</code><code>1</code><code>;</code>
如果不使用WHERE指定條件,則會删除資料表中所有的資料
<code>DELETE FROM t_message;</code>
C.其它一些常用指令
1.顯示所有資料庫資訊
<code>SHOW DATABASES;</code>
2.顯示所有表資訊
<code>SHOW TABLES;</code>
3.顯示指定表資訊
<code>DESCRIBE tablename;</code>
- - - - - - - - - - - - - - - - -
資料庫檔案的導入與導出操作
a.設定MySQL的指令行模式
我的電腦->屬性->進階->環境變量->->建立(如果原來就有path變量的不用建立,直接編輯)->
PATH=“;path\MySQL\bin;”其中path為MySQL的安裝路徑。比如我安裝的路徑在H盤的SQL檔案夾中,是以我path:H:\SQL\MySQL
導出資料庫檔案
b.01将資料庫db_database01導出到e:\other\db_database01.sql檔案
b.02将資料庫db_database01中的表格tb_user導出到e:\other\tb_user.sql檔案中
b.03将資料庫db_db_base01的結構導出到e:\other\mydb_stru.sql檔案中:
導入資料庫檔案
從e:\other\mydb2.sql中将檔案中的SQL語句導入資料庫中:
1.從指令行進入MySQL,然後用指令CREATE DATABASE mydb2;建立資料庫mydb2。
2.退出MySQL 可以輸入指令exit;或者quit;
3.在CMD中輸入下列指令:
還有一種方法導入:
(1)将檔案夾中的xxxx.sql檔案拷貝到本地機器中,如拷貝到C槽根目錄。
(2)打開MySQL指令行工具,輸入密碼進入用戶端,在指令行輸入“\. C:\xxxx.sql”回車,完成MySQL資料庫的還原操作。
mysql修改資料庫字段長度
alter table 表名 modify column 字段名 類型;
舉例子:
資料庫中tb_user表 email字段是varchar(50)
可以用
<code>alter table user modify column name </code><code>var</code><code>char(</code><code>100</code><code>) not </code><code>null</code> <code>;</code>
本文轉自lixiyu 51CTO部落格,原文連結:http://blog.51cto.com/lixiyu/1319536,如需轉載請自行聯系原作者