mysql数据表操作
(1)插入數(shù)據(jù) insert
insert into 表(字段1,字段2,字段3 ... 字段n)values(值1,值2,值3 ... 值n)
注意:
1)如果要插入全部的字段,那么字段列表可以省略不寫,但要保證值的個數(shù)與表中字段的位置一一對應(yīng)
insert into 表 values(值1,值2,值3 ... 值n)
2)如果主鍵字段是自增長的,那么主鍵的值可以使用null表示
3)如果全字段插入數(shù)據(jù)的話,自增長的主鍵位置的值不能省略
4)除了數(shù)字類型外,其他類型的值都需要加單引號
5)插入的值的大小不要超過定義字段的值,不然會報:too long的錯誤
(2)修改數(shù)據(jù)update
update 表 set 字段=值,字段=值 ... 字段=值 [where 條件]
注意:
在修改表中的數(shù)據(jù)時,一般都會寫上where限定條件,如果不寫那么全表的數(shù)據(jù)??? 都會被修改
(3)刪除數(shù)據(jù)delete
delete from 表 [where 條件]
注意:
在刪除表的數(shù)據(jù)時,一般都會寫上where條件,如果不寫會將表中的數(shù)據(jù)全部刪??? 除,但表結(jié)構(gòu)還存在
(4)查詢數(shù)據(jù) select
PS:以上面創(chuàng)建的user表為例 進行查詢
1)查詢?nèi)?select * from user;
2)查詢部分 select name,age from user;
3)運算查詢 select name,age+10 from user;
4)聚合查詢
?? a)計數(shù)查詢:select count(*) from user;
?? b)求和查詢:select sum(salary) from user;
?? c)平均數(shù)查詢:select avg(salary) from user;
?? d)最大查詢:select max(salary) from user;
?? e)最小查詢:select min(salary) from user;
5)排序查詢 select * from user order by age desc/asc;
6)去重查詢 select distinct dept from user;
7)條件查詢
??? a)單條件:select * from user where age>30;
??? b)多條件:select * from user where gender=’女’and age<30or gender=’??? ????? 男’ and salary>=8000
??? c)空查詢:select * from user where salary is null
??? d)枚舉查詢:select * from user where age in(22,18,33)
??? e)范圍查詢:select * from user where registTime between ‘2014-01-01’ ????? ?????????????????? and ‘2014-12-31’
??? f)模糊查詢:
? ? ? ? select * from user where username like ‘張%’;
??????? select * from user where username like ‘張_’;
?
8)分組查詢
elect dept,sum(salary) from user group by dept havingsum(salary)>10000;
?
9)分頁查詢 select * from user limit 0,3;
總結(jié):
??? 之前學習過sqlServer數(shù)據(jù)庫,兩者之間差別不大,因為有比較好的基礎(chǔ),所以學習mysql比較快,關(guān)于庫的操作和表的操作大致相同,思想相同。
總結(jié)
以上是生活随笔為你收集整理的mysql数据表操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最近几天比较烦躁!
- 下一篇: 按某个字段分组并排序后,取每一组的第一条