1.show databases; 查看所有的數(shù)據(jù)庫(kù)
2.create database test; 創(chuàng)建一個(gè)叫test的數(shù)據(jù)庫(kù)
3.drop database test;刪除一個(gè)叫test的數(shù)據(jù)庫(kù)
4.use test;選中庫(kù) ,在建表之前必須要選擇數(shù)據(jù)庫(kù)
5.show tables; 在選中的數(shù)據(jù)庫(kù)之中查看所有的表
6.create table 表名 (字段1 類型, 字段2 類型);
7.desc 表名;查看所在的表的字段
8.drop table 表名; 刪除表
9.show create databases 庫(kù)名;查看創(chuàng)建庫(kù)的詳細(xì)信息
10.show create table 表名; 查看創(chuàng)建表的詳細(xì)信息
1.修改字段類型 alter table 表名 modify 字段 字段類型;
2.添加新的字段 alter table 表名 add 字段 字段類型
3.添加字段并指定位置 alter table 表名 add 字段 字段類型 after 字段;
4.刪除表字段 alter table 表名 drop 字段名;
5.修改指定的字段 alter table 表名 change 原字段名字 新的字段名字 字段類型
1.增加數(shù)據(jù)(insert)3種方式
1.1 insert into 表名 values(值1,值2,...)(很少用)
1.2 insert into 表名(字段1米丘,字段2...) values(值1,值2喧锦,....);(較常用)
1.3 insert into 表名(字段1,字段2...) values(值1,值2菱阵,....)罩缴,(值1蚊逢,值2,....)箫章,(值1时捌,值2,....);
2.刪除數(shù)據(jù)(delete) delete from 表名 where 條件 注意:where 條件必須加炉抒,否則數(shù)據(jù)會(huì)被全部刪除
3.更新數(shù)據(jù)(update) update 表名 set字段1 = 值1, 字段2 = 值2 where 條件
4.查詢數(shù)據(jù)(select)
4.1 查詢表中的所有數(shù)據(jù) select * from 表名
4.2 指定數(shù)據(jù)查詢 select 字段 from 表名
根據(jù)條件查詢出來(lái)的數(shù)據(jù) select 字段 from 表名 where 條件 (最常用的)
where 條件后面跟的條件
關(guān)系:>,<,>=,<=,!=
邏輯:or, and
區(qū)間:id between 4 and 6 ;閉區(qū)間奢讨,包含邊界
5.排序
select 字段 from 表 order by 字段 排序關(guān)鍵詞(desc | asc)
排序關(guān)鍵詞 desc 降序 asc 升序(默認(rèn))
5.1 通過(guò)字段來(lái)排序
例如 :select * from star orser by money desc, age asc;
5.2 多字段排序
select 字段 from 表 order by 字段1 desc |asc,...字段n desc| asc;
6.常用的統(tǒng)計(jì)函數(shù) sum,avg焰薄,count拿诸,max,min
只分組:select * from 表 group by 字段
例子: select count(sex) as re,sex from star group by sex having re > 3;
分組統(tǒng)計(jì): select count(sex) from star group by sex;
7.分組 select * from 表名 limit 偏移量,數(shù)量
說(shuō)明:
8.1.不寫偏移量的話就是默認(rèn)的為0
8.2.實(shí)現(xiàn)分頁(yè)的時(shí)候必須寫偏移量
偏移量怎么計(jì)算?:
limit (n-1)*數(shù)量 ,數(shù)量
1.內(nèi)連接
隱式內(nèi)連接 select username,name from user,goods where user,gid=gods,gid;
顯示內(nèi)連接
select username,from user inner join goods on user.gid=goods.gid;
select * from user left join goods on user.gid=goods.gid;
2.外鏈接
左連接 包含所有的左邊表中的記錄以及右邊表中沒(méi)有和他匹配的記錄
右連接
select * from user where gid in(select gid from goods);
select * from user right jOin goods on user.gid=goods.gid;
子嵌套查詢
數(shù)據(jù)聯(lián)合查詢
select * from user left join goods on user.gid=goods.gid union select * from user right join goods on user.gid=goods.gid;
兩個(gè)表同時(shí)更新
update user u, goods g set u.gid=12,g.price=1 where u.id=2 and u.gid=g.gid;
五塞茅、DCL 數(shù)據(jù)控制語(yǔ)言
1.創(chuàng)建用戶:create user'xiaoming'@'localhost' identified by '666666';
2.授權(quán)用戶:grant all on test.*to'xiaoming'@'localhost';
3.刷新權(quán)限:flush privileges;
4.取消授權(quán):revoke all on test.* from 'xiaoming'@'localhost';
5.刪除用戶: drop user'xiaoming'@'localhost';
-
DTL 數(shù)據(jù)事務(wù)語(yǔ)言
開(kāi)啟事務(wù):set autocommit=0;
操作回滾:rollback;
提交事務(wù):commit;