第二個星期的學習內(nèi)容有:
? ? 數(shù)據(jù)操作:
? ? ? ? 查詢所有數(shù)據(jù):select * from 表名
? ? ? ? 插入數(shù)據(jù):
? ??????全列插入:insert into 表名 values(...)
????????缺省插入:insert into 表名(列1,...) values(值1,...)
????????同時插入多條數(shù)據(jù):insert into 表名 values(...),(...)...;
? ??????主鍵列是自動增長,但是在全列插入時需要占位,通常使用0,插入成功后以實際數(shù)據(jù)為準
? ? ? ? 修改數(shù)據(jù):
? ??????update 表名 set 列1=值1,... where 條件
? ? ? ? 刪除數(shù)據(jù):
? ??????delete from 表名 where 條件
? ? ? ? 數(shù)據(jù)備份:
? ??????sudo -s箫措; ??cd /var/lib/mysql; ?Mysqldump –????????uroot –p 數(shù)據(jù)庫名 > ~/Desktop/備份文件.sql瘩将;
? ??????消除重復行:
? ??????select distinct 字段名 from 表名;
? ? ? ? 條件篩選:
? ??????select * from 表名 where 條件;
? ? ? ? 舉例:select * from 表名 where id<=4;
? ? ? ? 模糊查詢;
????????select * from 表名 where 字段名 like '黃_';(_代表一個字符胧弛,*代表所有字符骂维,%代表多個字符)
? ? ? ? 范圍查詢:
? ??????select * from students where id in(1,3,8); ??
? ??????select * from students where id between 3 and 8;
? ??????空判斷:
? ??????select * from students where hometown is null;
? ? ? ? 五個聚合函數(shù):
? ? ? ? max() min() count() sum() ?avg()
????????分組查詢:
? ??????select 列1,列2,聚合... from 表名 group by 列1,列2,列3...
? ? ? ? 舉例:select gender as 性別,count(*) from students group by gender;(顯示男女學生總數(shù))
? ? ? ? 對結果集的數(shù)據(jù)篩選:
? ??????select 列1,列2,聚合... from 表名group by 列1,列2,列3...having 列1,...聚合...
? ? ? ? 舉例:select gender as 性別,count(*)from students group by gender having gender=1;
? ? ? ? where與having的不同:where是對原始數(shù)據(jù)集的篩選蚌父,而having是對結果集進行篩選哮兰。
? ? ? ? 查詢排序:
? ??????select * from 表名order by 列1 asc|desc,列2 asc|desc,...(desc是降序,asc是升序)
? ? ? ? 舉例:select * from students where gender=1 order by id desc;
? ? ? ? 部分查詢:
? ??????select * from 表名 limit start,count
? ? ? ? 對查詢結果進行分頁操作:
? ??????select * from student where isdelete=0 limit (n-1)*m,m
????????字段約束之外鍵:
? ? ? ? 在表中添加外鍵:
?????????alter table 子表的數(shù)據(jù)表名add foreign key(子表的外鍵名稱) references 父表的數(shù)據(jù)表名稱(父表的主鍵名稱);
? ? ? ? 或者在創(chuàng)建表時添加外鍵:
? ??????foreign key(外鍵名) references 父表名(父表中要鏈接的字段名),
? ? ? ? 建好外鍵之后可以進行連接查詢:
? ? ? ? inner join 父表名 on 字表名.外鍵名=父表名.鏈接字段名