一、啟動與退出
1、進入MySQL:啟動MySQL Command Line Client(MySQL的DOS介面),直接輸入安裝時的密碼即可。此時的提示符是:mysql>
2、退出MySQL:quit或exit
二、庫操作
1、、建立資料庫
命令:create database <資料庫名>
例如:建立一個名為xhkdb的資料庫
mysql> create database xhkdb;
2、顯示所有的資料庫
命令:show databases (注意:最後有個s)
mysql> show databases;
3、刪除資料庫
命令:drop database <資料庫名>
例如:刪除名為 xhkdb的資料庫
mysql> drop database xhkdb;
4、串連資料庫
命令: use <資料庫名>
例如:如果xhkdb資料庫存在,嘗試存取它:
mysql> use xhkdb;
工具提示:Database changed
5、查看當前使用的資料庫
mysql> select database();
6、當前資料庫包含的表資訊:
mysql> show tables; (注意:最後有個s)
三、表操作,操作之前應串連某個資料庫
1、建表
命令:create table <表名> ( <欄位名1> <類型1> [,..<欄位名n> <類型n>]);
mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default '0',
> degree double(16,2));
2、擷取表結構
命令: desc 表名,或者show columns from 表名
mysql>DESCRIBE MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;
3、刪除表
命令:drop table <表名>
例如:刪除表名為 MyClass 的表
mysql> drop table MyClass;
4、插入資料
命令:insert into <表名> [( <欄位名1>[,..<欄位名n > ])] values ( 值1 )[, ( 值n )]
例如,往表 MyClass中插入二條記錄, 這二條記錄表示:編號為1的名為Tom的成績為96.45, 編號為2 的名為Joan 的成績為82.99,編號為3 的名為Wang 的成績為96.5.
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
5、查詢表中的資料
1)、查詢所有行
命令: select <欄位1,欄位2,...> from < 表名 > where < 運算式 >
例如:查看錶 MyClass 中所有資料
mysql> select * from MyClass;
2)、查詢前幾行資料
例如:查看錶 MyClass 中前2行資料
mysql> select * from MyClass order by id limit 0,2;
或者:
mysql> select * from MyClass limit 0,2;
6、刪除表中資料
命令:delete from 表名 where 運算式
例如:刪除表 MyClass中編號為1 的記錄
mysql> delete from MyClass where id=1;
7、修改表中資料:update 表名 set 欄位=新值,… where 條件
mysql> update MyClass set name='Mary' where id=1;
7、在表中增加欄位: