Windows操作系統(tǒng)進(jìn)入CMD命令行,進(jìn)入mysql.exe所在目錄,運(yùn)行命令
mysql.exe -h主機(jī)名 -u用戶名 -p密碼
注意:參數(shù)名與值之間沒(méi)有空格 , 如:-h127.0.0.1
二、數(shù)據(jù)庫(kù)命令
1. 創(chuàng)建數(shù)據(jù)庫(kù)
create database 數(shù)據(jù)庫(kù)名;
如:
create database itsource;
2. 切換到指定數(shù)據(jù)庫(kù)
use 數(shù)據(jù)庫(kù)名;
如:
use itsource;
3. 顯示數(shù)據(jù)庫(kù)列表
show databases;
4. 顯示數(shù)據(jù)庫(kù)建立語(yǔ)句
show create database 數(shù)據(jù)庫(kù)名;
如:
show create database itsource;
5. 修改數(shù)據(jù)庫(kù)
alter database 數(shù)據(jù)庫(kù)名 選項(xiàng);
如:
alter database itsource charset=gbk;
6. 刪除數(shù)據(jù)庫(kù)
drop database 數(shù)據(jù)庫(kù)名;
如:
drop database itsource;
三、數(shù)據(jù)表命令
1. 創(chuàng)建數(shù)據(jù)表
create table 數(shù)據(jù)表名(
字段名1 類型 修飾符,
字段名2 類型 修飾符,
字段名n 類型 修飾符
);
如:
create table news(
id int primary key auto_increment,
title varchar(50),
author varchar(20),
content text );
2. 查看數(shù)據(jù)表列表
show tables;
3. 查看數(shù)據(jù)表結(jié)構(gòu)
desc 數(shù)據(jù)表名;
例如:
desc news;
4. 查看數(shù)據(jù)表建表語(yǔ)句
show create table 數(shù)據(jù)表名;
如:
show create table news;
5. 刪除數(shù)據(jù)表
drop table 數(shù)據(jù)表名;
如:
drop table news;
6. 修改數(shù)據(jù)表
alter table 數(shù)據(jù)表名 選項(xiàng);
如:
alter table news charset=utf8;
7. 新增字段
alter table 數(shù)據(jù)表名 add column 字段名 類型 修飾語(yǔ)句 位置
如:
alter table news add column newstime timestamp default current_timestamp after content;
8. 修改字段定義
alter table 數(shù)據(jù)表名 modify column 字段名 新的定義
如:
alter table news modify column content longtext;
9. 修改字段名及定義
alter table 數(shù)據(jù)表名 change column 舊字段名 新字段名 新的定義;
10. 刪除字段
alter table 數(shù)據(jù)表名 drop column 字段名;
如:
alter table news drop column title;
四、記錄操作命令
1. 新增記錄
insert into 數(shù)據(jù)表名(字段1,字段2,字段n) values(值1,值2,值n);
如:
insert into news(title,author,content) values('新聞標(biāo)題','作者','新聞詳細(xì)內(nèi)容');
注意:值的數(shù)量與類型必須與字段列表數(shù)量與類型定義一致
2. 查看記錄
select 字段列表 from 數(shù)據(jù)表名 where 條件 order by 字段名 desc limit m,n
如:
select * from news;
select * from news where id=10;
select * from news order by id desc limit 10;
注意:select語(yǔ)句是SQL中最為強(qiáng)大與復(fù)雜的查詢語(yǔ)句,有七大子句,每段子句都可以省略,如果出現(xiàn)必須在正確的位置順序上,不可調(diào)換先后位置。
3. 修改記錄
update 數(shù)據(jù)表名 set 字段1=值1 and 字段2=值2 where 條件;
如:
update news set title='新的新聞標(biāo)題' where id=1;
4. 刪除記錄
delete from 數(shù)據(jù)表名 where 條件;
如:
delete from news where id=10;
五、其它常用命令
set names gbk
由于CMD命令行只支持系統(tǒng)當(dāng)前編碼,所以一般需要將CMD與MYSQL服務(wù)器的交互編碼設(shè)置為gbk才能正常顯示utf8的數(shù)據(jù)。