DDL:對數(shù)據(jù)庫,表,列進(jìn)行操作DDL:對數(shù)據(jù)庫,表,列進(jìn)行操作
? ? ? ? ? ? ? ? ? ? 關(guān)鍵字:create ? ? ?創(chuàng)建表和數(shù)據(jù)庫
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?alter ? ? ? ? ?修改表的字段
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?alter語句的關(guān)鍵字有 add(增加),drop(刪除),modify(修改字段類型),change(可修改字段名和字段類型),rename to(修改表名)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?drop ? ? ? ? ?刪除表,數(shù)據(jù)庫和一個字段
DML:對表中的數(shù)據(jù)進(jìn)行增,刪,改的操作
? ? ? ? ? ? ? ? ? ? ?關(guān)鍵字: ? ? insert into ? ?向表中插入一條數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?update ? ? ? ? ?修改數(shù)據(jù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? delete ? ? ? ? ? ? 刪除數(shù)據(jù)
DQL:對表中的數(shù)據(jù)進(jìn)行查詢
? ? ? ? ? ? ? ? ? ? ? 關(guān)鍵字: ?select ? ?查詢
查看建表語句: show create table 表名
基本語句:
alter語句:
向表中增加一個字段: ? ? ? ? alter table 表名 add column 字段名 字段類型;
刪除表中一個字段: ? ? ? ? ? ?alter table 表名 drop column 字段名;
修改表中某一個字段類型: alter table 表名 modify column 字段名 ?新字段類型;
可修改的字段名和修改字段類型:
? ? ? ? ? ? ? ? ? ?alter table 表名 change column 字段名 新字段名 ?字段類型(也可寫新字段類型)
修改表名: ? alter table 表名 rename to 表名;
給字段添加主鍵:?
? alter table 表名 change column 字段名 字段名 字段類型 primary key auto_increment
?auto_increment: ? 給主鍵自增 ? ? (一般用于編號)
drop語句
刪除數(shù)據(jù)庫: ?drop 數(shù)據(jù)庫名;
刪除表: ? ? ? ? drop 表名;
刪除字段: ? ? alter table 表名 drop column 字段名
DML語句
插入一條所有字段的數(shù)據(jù):insert into 表名 values(按字段順序?qū)懼?;
插入一條部分字段的數(shù)據(jù):insert into 表名 (字段名)values(值);
修改數(shù)據(jù): ? ?update 表名 set 屬性名=值; ? ? ? 修改的是 屬性名 那一列的數(shù)據(jù)全部為 值
按條件修改數(shù)據(jù): update 表名 set 屬性名=值 where 條件
刪除表所有 數(shù)據(jù) : delete from 表名 ;
按條件刪除數(shù)據(jù): ? ? ? ? ?delete from 表名 where 條件
DQL語句
查詢所有數(shù)據(jù): select * from 表名;
查詢部分字段: select 字段名1,字段名2 from 表名;
查詢?nèi)ブ財?shù)據(jù): select distinct 字段名 ?from 表名;
條件查詢:
1..邏輯:and和or
select * from 表名 where name='jack' and pwd ='123';
select * from 表名 where name=‘jack’or name='rose';
2..比較條件: >? <? >=? <=? =? between and(在某個區(qū)間范圍內(nèi),首位都包含在內(nèi))
select * from 表名 where 字段>1;
select * from 表名 where 字段名 between 數(shù)字 and 數(shù)字;
3..判空條件:? =''? is null(判斷空)? <>''(是不等于) ?is not null(判斷非空)
select * from 表名 where?字段名<>' ';
select * from 表名 where 字段名 is not null;
select * from 表名 where 字段名 is? null;
4..模糊查詢 ?
? ?like關(guān)鍵字 ? ? _(表示一個字符) ? %(表示多個字符 ?包括0個)
想查詢名字包含jack的
select * from表名 where字段名 like '_jack';
匹配名字包含jack的或者包含rose的
select * from 表名 where 字段名 like '%jack%' or 字段名 like '%rose%';
5..聚合函數(shù) max(列) min(列) avg(列) count(列)(不包含null) sum(列)總和
select? max(java) from student ;? ? ? 不包含null
select min(java) from student;? ? ? ? ? 不包含null
select sum(java) from student; ? ? ? ? ?不包含null
select avg(java) from student; ? ? ?不包含null
select count(*) from student; ? ? ? ? ?不包含null
6..分頁查詢
? 關(guān)鍵字: ?limit ? int , int?
select * from 表名 ?limit ?3,4;
3 表示從3的下一條語句開始查詢 ? ? ? ? 4表示查詢出四條語句
7..分組查詢
關(guān)鍵字: group by ? ? ? ??
select * from 表名 group by 分組的列名 ?;
按條件分組 ?
? ?關(guān)鍵字 ?: having ? 分組的的條件 ? ? ?where 分組前的條件
select * from 表名 ?group by 分組的列名 ?having 條件;
select * from 表名 ?where 條件 ? group by 分組的列名 ;
8..排序查詢
? ?關(guān)鍵字:order by ? asc ?升序 ? 默認(rèn) ? ? ?可不許asc
? ? ? ? ? ? ? order ?by ? ?desc ?降序
如果和分組一起用 ? ? 先分組后排序
select * from 表名 group by 分組的列名 order by 排序的列名
9..多表查詢
1) 隱式內(nèi)連接查詢 ? ? (按照條件進(jìn)行顯示)
select * from 表名1,表名2 where 表名1.(點) 主鍵=表名2.(點) ? 和主鍵有關(guān)系的列名
2)顯示內(nèi)連接
select * from 表名1 inner join 表名2 on ?條件 ? ? ? ? ? ? ?inner 可省略
3)外連接
左外連接: ? 左邊的表為主表 ? ? ? 右邊的表為副表
右外連接: ? 右邊的表為主表 ? ? ? 左邊的表為副表
(1)左外連接: ? select * from 表名1 left outer join 表名2 on 條件
(2)右外連接: ?select * from 表名1 rigth outer join 表名2 on 條件
outer可省略
4)自連接: ? (自己連接自己)
? select * from 表 join 表 on 條件
10..子查詢 ? ? ?當(dāng)一個sql語句需要用到另一個sql語句的結(jié)果集
? ? 例: ? ? ?1 ?查看用戶為張三的訂單詳情
? ? ? ? select * from orders where user_id in(select id from user where username='張三')