cmdmysql.exe/mysql -h主機(jī)地址 -P端口 -u用戶名 -p密碼 //注意端口號的P要大寫cmdmysql.exe/mysql -hlocalhost -P3306 -uroot -proot
退出數(shù)據(jù)庫
Exit; //注意加分號\q; // 分號可加可不加
基本語法:create database 數(shù)據(jù)庫名字[庫選項(xiàng)];
庫選項(xiàng):數(shù)據(jù)庫的相關(guān)屬性
create database mydatabase charset gbk ;
語法:use 數(shù)據(jù)名稱;
修改數(shù)據(jù)庫庫選項(xiàng):字符集和校對集
基本語法:alter database 數(shù)據(jù)名 charset=字符集;
基本語法:drop database 數(shù)據(jù)庫名;
創(chuàng)建表必須要掛在某個數(shù)據(jù)庫上:
// 數(shù)據(jù)庫名.表名 用點(diǎn)來鏈接數(shù)據(jù)庫和表create table mydatabase.mytable( name varchar(10));
使用表選項(xiàng):
create table student(name varchar(10))engine[=]innodb/myisam charset utf8;
顯示所有表:show tables;
顯示部分表:show tables like ‘匹配模式’;
顯示表結(jié)構(gòu):
describe 表名;
desc 表名;
show columns from 表名
顯示表創(chuàng)建語句
show create table 表名;
設(shè)置表屬性
基本語法:alter table 表名 表選項(xiàng) 值;
修改表名:rename 舊表名 to 新表名;
新增字段:
alter table 表名 add [column] 新字段名 列類型 [列屬性] [位置first/after字段名]
修改字段:
alter table 表名 change 舊字段名 新字段名 字段類型 [列屬性] [新位置]
修改字段類型(屬性):
alter table 表名 modify 字段名 新類型 [新屬性] [新位置]
刪除字段:
alter table 表名 drop 字段名
刪除表結(jié)構(gòu):
drop table 表名 [,表名2…]
插入操作:
基本語法:
insert into 表名 [ (字段列表)] values(對應(yīng)字段列表) //指定字段插入值,不要求順序,但是要求對應(yīng)
insert into 表名 values(對應(yīng)表結(jié)構(gòu)) //所有字段插入數(shù)據(jù),要對應(yīng)字段順序
insert into my_teacher (name,age) values('jack',30);
查詢操作:
查詢?nèi)繑?shù)據(jù): select * from 表名 ;
查詢部分?jǐn)?shù)據(jù):select 字段列表 from 表名; // 字段列表用逗號隔開
條件查詢數(shù)據(jù):select 字段列表 / * from 表名 where 字段名=值;
刪除操作
delete from 表名 where 條件;
更新操作
update 表名 set 字段名=新值 [where 條件];
處理中文數(shù)據(jù)
set names gbk;
等同于:
set character_set_client=gbk;
set character_set_connection=gbk;
set character_set_results=gbk;