數(shù)據(jù)庫的管理
連接數(shù)據(jù)庫
mysql -u root -p? ? (連接數(shù)據(jù)庫)? ? ? ? ? ? ? exit (斷開連接數(shù)據(jù)庫)
123456? ? ? ? ? ? ? ? (密碼)
show?databases;? ? ? ? ? (查看數(shù)據(jù)庫)?
use?數(shù)據(jù)庫的名字皇钞;? ? ? ? ?(選擇數(shù)據(jù)庫)
show?tables;? ? ? (查看表)
增
insert?into?表名(字段1括蝠,字段2晰搀,字段3)?values(值1呀舔,值2场斑,值3)糠睡;
eg:insert into t_class (id挽鞠,class,teacher) values (5狈孔,'一班'滞谢,'阿華');
刪
delete?from?表名?where?條件;
eg:?delete from t_class where class is null;? (刪除班級表里班級欄null)
改
update 表名 set 字段1 = 值 除抛,字段2 =?值 where 條件;
例:update t_student set sname = '阿華',age = 21 where id = 1;
查
1, select *from 表名 where 條件(?字段1 = 值? and 字段2 =?值);?
?(*號代表所有,也可以改成條件:字段1母截,字段2 )
例:select * from t_student;
select * from t_student where age= 21;
多表聯(lián)查
select * from 表1 join 表2 on 表1.字段1 = 表2.字段2 where 表1.字段1 = 值;
例:select?t_student.id,t_student.sname,t_class.class,t_results.chinese?from?t_student? join?t_class? on?t_student.classname?=?t_class.id?join?t_results? on?t_student.id?=?t_results.sid?where?t_results.chinese?>100;
count 計(jì)數(shù)
max?最大值
min?最小值
avg?平均值
sum?求和
取別名?as (可以省略)
到忽!=? 不等于
in?在什么里面
is?is?null;
between?在清寇、喘漏、、之間(只用于int數(shù)字)?where?age?between 18?and 20华烟;
like? 字符串的查詢?包含
? ? ?where?name?like ‘王%’ 翩迈;? ? 姓王
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ‘%為’;? ? ?最后是為
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?‘%子%’盔夜;? ?帶子的
小練習(xí)
建立表