數(shù)據(jù)庫
- 創(chuàng)建數(shù)據(jù)庫
? ? - create database 數(shù)據(jù)庫名稱 charset=utf8;
- 使用數(shù)據(jù)庫
? ? - use 數(shù)據(jù)庫名
- 查看創(chuàng)建數(shù)據(jù)庫語句
? ? - show create database 數(shù)據(jù)庫名
- 查看所有的數(shù)據(jù)庫
? ? - show databases;
- 刪除數(shù)據(jù)庫
? ? - drop database 數(shù)據(jù)庫名;
- 查看當(dāng)前使用的數(shù)據(jù)庫
? ? - select database()
數(shù)據(jù)表
- 創(chuàng)建數(shù)據(jù)表
? ? - create table 表名(
? ? - id int primary key auto_increment not null,
? ? - name varchar(40) default '',
? ? - price decimal(5,2),
? ? - cate_id int unsigned,
? ? - is_show bit default 1,
? ? - foreign key(cate_id) references goods_cates(id),
? ? - ) select 語句
- 查看表結(jié)構(gòu)
? ? - desc 表名;
- 顯示數(shù)據(jù)庫中所有的表
? ? - show tables;
- 刪除數(shù)據(jù)表
? ? - drop tables 表名
- 查詢數(shù)據(jù)表
? ? - select distinct *
? ? - from 表名
? ? - where ....
? ? - group by 字段名... (having ...|group_concat()|聚合函數(shù)|with rollup)
? ? - order by 字段名 ...
? ? - limit start,count
? ? - 完整的select語句執(zhí)行順序
? ? ? ? - from 表名
? ? ? ? - where ....
? ? ? ? - group by ...
? ? ? ? - select distinct *
? ? ? ? - having ...
? ? ? ? - order by 字段名 ...
? ? ? ? - limit start,count
? ? -? ? 連接查詢
? ? ? ? - select * from 表1? (inner|left|right) join 表2 on 表1.字段 = 表2.字段 (inner|left|right) join 表2 on 表1.字段 = 表2.字段;? 相當(dāng)于一張?zhí)摫?/p>
? ? - 子查詢
? ? ? ? - select 嵌套select? ? 子查詢 為可以返回一行一列,一列,一行,表 ;可以結(jié)合 in ,? is? ,between? 下限? and 上限等使用
? ? - 視圖
? ? ? ? - 創(chuàng)建視圖
? ? ? ? ? ? - create view v_視圖名 as select語句
? ? ? ? - 查看視圖
? ? ? ? ? ? - select * from 視圖名
? ? ? ? - 刪除視圖
? ? ? ? ? ? - drop view 視圖名
? ? ? ? - 查看視圖是否創(chuàng)建成功
? ? ? ? ? ? - show tables;
- 表數(shù)據(jù)修改
? ? - 插入數(shù)據(jù)
? ? ? ? -? insert into 表名(字段,...) values(),()
? ? ? ? - insert? into 表名 (字段,...)? select * from 表名 group
? ? - 修改數(shù)據(jù)
? ? ? ? - update 表名 set 字段1 = 值1, 字段2=值2...? where 條件
? ? - 刪除記錄
? ? ? ? - delete from 表名 where 條件
? ? ? ? - update 表名 set isdelete = 1 where 條件
字段
- 添加字段
? ? - alter table 表名 add 字段 類型 約束條件;
? ? - alter table 表名 add foreign key (字段) references 外鍵表名 (關(guān)聯(lián)字段);
- 修改字段
? ? - alter table 表名 change 舊字段名稱 新字段名稱 類型 約束條件,change 舊字段名稱 新字段名稱 類型 約束條件 ;
? ? - alter table 表名 modify 類型 約束條件;
- 刪除字段
? ? - alter table 表名 drop 字段;
? ? - alter table 表名 drop foreign key 外鍵名稱
聚合函數(shù)
- count 統(tǒng)計(jì)記錄數(shù)量 - 計(jì)數(shù)(不統(tǒng)計(jì)字段中的null項(xiàng))
-? max? 求出集合中的最大值
- min? 求出集合中的最小值(如果其中有空值,最小值不為空,從非空中查找最小值)
- sum 對集合中的某個(gè)字段數(shù)據(jù)求和
-? avg 對集合中的某個(gè)字段數(shù)據(jù)求平均值
- round(小數(shù), 保留的位數(shù),在進(jìn)行取舍的時(shí)候遵循四舍五入的規(guī)則)
事務(wù)
- 具有原子性、一致性、隔離性、持久性
- set autocommit = 0在黑窗口中關(guān)閉自動(dòng)提交 rollback
索引
- 創(chuàng)建索引
? ? - create index 索引名 on 表名(字段名(長度))
- 展示索引
? ? - show index from 表名
- 刪除索引
? ? - drop index 索引名 on 表名
數(shù)據(jù)庫賬戶管理
- 查看所有用戶
? ? - 進(jìn)入mysql系統(tǒng)數(shù)據(jù)庫
? ? - select host,user,authentication_string from user;
- 創(chuàng)建賬戶
? ? - grant 權(quán)限列表 on 數(shù)據(jù)庫 to '用戶名'@'訪問主機(jī)' identified by '密碼';
- 修改權(quán)限
? ? - grant 權(quán)限名稱 on 數(shù)據(jù)庫 to 賬戶@主機(jī) with grant option;
- 修改密碼
? ? - update user set authentication_string=password('新密碼') where user='用戶名';
- 刪除賬戶
? ? - drop user '用戶名'@'主機(jī)';
? ? - delete from user where user='用戶名';
- 刷新權(quán)限
? ? - flush privileges