net start mysql,net stop mysql 開/關(guān)數(shù)據(jù)庫
\s 查看mysql版本信息
- mysql -uroot -proot 登錄mysql
- show databases 查看數(shù)據(jù)庫
- use databaseName 選擇數(shù)據(jù)庫
- show tables 列出表格
- show columns from tableName 顯示表格列的屬性
- desc tables 查看表字段
字段管理
- 添加字段
alter table user add age tinyint unsigned not null
alter table user add pass varchar(30) not null after user
alter table user add pass varchar(30) not null first - 刪除字段
alter table user drop password - 修改字段
alter table user modify username varchar(30) not null
alter table user change username user varchar(30) not null - 查看表字段
desc user
索引
- 主鍵
- 添加
create table user2(
id int unsigned not null auto_increment,
username varchar(30) not null,
primary key(id)
);
create table user2(
id int unsigned not null auto_increment primary key,
username varchar(30) not null
);
- 刪除
alter table user2 modify id int unsigned not null
alter table user2 drop primary key
- 唯一
- 添加
alter table user2 add unique u_username(username) - 刪除
alter table user2 drop index u_username
- 普通
- 添加
alter table user2 add index i_username(username) - 刪除
alter table user2 drop index i_username
數(shù)據(jù)庫操作
- DCL //數(shù)據(jù)控制語言,grant,commit,rollback
- DDL //數(shù)據(jù)定義語言,create,drop,alter
- DML //數(shù)據(jù)操作語言,insert,update,delete
- DQL // 數(shù)據(jù)查詢語言,select
判斷sql語句的檢索效率
desc select * from 表名 where id=5 (\G);
增-insert
insert into 表名(字段名) values (‘值’);
刪-delete
delete from 表名 where id=5
改-update
update 表名 set 字段名='修改值' where id=2
查-select
select * from 表名
查詢表中所有字段以及每個字段所對應(yīng)的所有記錄
查詢一般優(yōu)化:
- 查單列的速度要優(yōu)于多列
- 查主鍵索引的列中的所有值要比其他列速度快
distinct 取相同值
between and 什么之間
in()、or、and as
like %匹配所有 _匹配一個字符 模糊搜索
order by asc升序(從小到大) dsec降序(從大到小)
limit 分頁
delete與truncate的區(qū)別 - delete 清空表數(shù)據(jù),但不會清除計數(shù)器(自增)
- truncate 清空表數(shù)據(jù)谨胞,同時會清除計數(shù)器(自增)
concat() 連接函數(shù)
rand() 隨機數(shù)函數(shù)
count(*) 表總行數(shù)
sum() 求和函數(shù)
avg() 平均值
max() 最大值
min() 最小值
group by 分組聚合的使用
unix_timestamp() 時間戳
from_unixtime() 轉(zhuǎn)換時間戳
多表查詢
select user.username,user.age,class.name,class.ctime from user,class where user.class_id=class.id
/** 在此感謝 LQQ 老師 **/