mySql 常用命令
1 //自定义 结束符号 $ 2 delimiter $ 3 4 //创建表(库存表) 5 create table a2( 6 xuhao int auto_increment, 7 primary key(xuhao), 8 name varchar(10) charset utf8 collate utf8_general_ci not null, 9 bianhao int(10), 10 danjia int(6), 11 kucun int(10) 12 ) 13 engine=innodb charset utf8 collate utf8_general_ci$ 14 15 //创建订单表 16 create table b2( 17 xuhao int auto_increment, 18 primary key(xuhao), 19 name varchar(10) charset utf8 collate utf8_general_ci, 20 bianhao int(10), 21 number int(10) 22 ) 23 engine=innodb charset utf8 collate utf8_general_ci$ 24 25 insert into a2(name,bianhao,danjia,kucun)values('苹果',100001,15,90000), 26 ('草莓',100002,18,5000),('香蕉',100003,22,4000), 27 ('葡萄',100004,19,2000)$ 28 29 30 insert into b2(name,bianhao,number)values('苹果',100001,234); 31 32 //触发器添加 33 create trigger cfq after insert on dd for each row begin update kc set 34 kucun=kucun-new.number where bianhao=new.bianhao; end$ 35 36 37 //触发器删除 38 create trigger t2 after delete on dingdan for each row begin update kucun set 39 kucun=kucun+old.number where bianhao=old.bianhao; end$ 40 41 //触发器修改 42 create trigger t3 after update on dingdan for each row begin update kucun set 43 kc=kc+old.dingnum-new.dingnum where number=old.number; end$ 44 45 内容增加 46 insert into 表名()values(); 47 48 内容删除 49 delete from 表名 where 字段名='值'; 50 51 内容修改 52 update 表名 set kucun=9000; 53 54 55 删除数据库 56 mysql> drop database php29; 57 58 59 60 61 62 63 64 65 66 67 68 69 70 mysql 71 72 73 关键词的顺序 74 75 where-group by-having-order by -limit 76 77 78 数据库 and 表: 79 80 \c //去除报错声音 81 82 show databases; //查看数据库 83 show tables //查看表 84 create database //创建数据库 85 86 rename table 原表名 to 新表名; //表重命名 87 drop database 库名; //删除数据库 88 89 创建表的常用类型 90 int 都是正数 0-2的32次方 有负值 -?~? 91 92 char(length) 固定长度 93 varchar(length) 不固定长度 94 95 auto_increment //自增(只有int型) 96 notnull //该字段内容不能为空 97 default '默认值' //该字段如果不添加内容执行默认值 98 comment '值' //该字段名的备注 99 primary key(id) //设置主键 (自增字段自带主键) 100 101 102 103 104 内容 105 insert into 表名(字段)value(值),(值); //添加内容 106 107 select * from 表名; //查询内容 108 select 字段名 from 表名; 109 select 字段1,字段2 from 表名; 110 111 update 表名 set 字段名='值' where 字段名='值'; //修改内容 112 113 delete from 表名 where 字段名='值'; //删除内容 114 115 116 字段结构 117 (after 后的...) 118 alter table 表名 add 字段 类型; //添加字段 119 120 alter table 表名 drop 字段名; //删除字段 121 122 alter table 表名 change 原字段 新字段 类型; //修改字段 123 124 125 126 127 128 where条件 查询(<,>,<=,>=,!=) 129 select * from 表名 where name="王二"; 130 select * from 表名 id > 2; 131 select * from 表名 where id in (3,5,7); 132 133 select * from 表名 where name like '_小%'; //模糊查询 134 135 select * from 表名 where name like '_小%' and id=9;//多条件查询 136 137 select * from 表名 order by 字段名 asc/desc; //升/降排列 138 139 select * from class1 order by cj desc,name limit 2,5;//分页查询 140 141 142 143 144 145 146 max() 最大值 147 min() 最小值 148 sum() 求和 149 avg() 平均数 150 count()统计记录数 151 例:select name,sum(cj) from 表名 group by name; 152 153 通过生日计算年龄 154 select now() //获取当前时间 155 select year(now()) //获取当前年份 156 select name,sex,birth,year(now())-year(birth) 157 as age from class where id > 1; 158 159 160 161 162 163 合并 164 select * from a union select * from b; //a,b表合并 165 166 select name,sum(cj) from class1 group by name having sum(cj)>380 order by sum(cj) desc limit 0,3; //虚拟字段查询 167 168 where-group by-having-order by -limit //关键词的顺序 169 例: 170 select km as 科目,sum(cj) as 总成绩 from (select a.km,a.cj from a union all select b.km,b.cj from b) c group by c.km; 171 //按照成绩查询合并后的表 (不覆盖相同项) 172 173 174 外联(left join)(right join) 175 例: 176 select gril.name,gril.phone,boy.name,boy.phone from 177 gril left join boy on gril.phone=boy.phone; 178 179 内联 180 例: 181 select gril.name,gril.phone,boy.name,boy.phone from 182 gril inner join boy on gril.phone=boy.phone; 183 184 185 186 187 188 选择判断 189 case 字段名 when 原值1 then 新值1 190 when 原值2 then 新值2 191 ……………… 192 end 193 194 195 196 197 198 199 触发器 trigger 200 201 ① 监视操作 insert / update / delete 202 ② 触发时间 after / before 203 ③ 触发操作 insert / update / delete 204 ④ 监视表 表的名称 205 206 207 例: 208 增加 209 create trigger t1 after insert on dingdan for each row begin update kucun set kc=kc-new.dingnum where number=new.number; end$ 210 211 create trigger t1 after insert on hzy for each row begin update chengchi set tong=tong+new.tong where name=new.name; end$ 212 213 create trigger t_up after update on hzy for each row begin update chengchi set liang=liang-old.liang+new.liang where name=old.name; end$ 214 delimiter $ 215 //insert 216 t1 217 hzy 218 AFTER 219 INSERT 220 begin update chengchi set tong=tong+new.tong where name=new.name; end 221 root@localhost 222 //update 223 t3 224 hzy 225 AFTER 226 UPDATE 227 begin update chengchi set tong=tong-old.tong+new.tong where name=old.name; end 228 root@localhost 229 230 231 删除 232 例: 233 create trigger t2 after delete on dingdan for each row begin update kucun set kc=kc+old.dingnum where number=old.number; end$ 234 235 修改 236 create trigger t3 after update on dingdan for each row begin update kucun set kc=kc+old.dingnum-new.dingnum where number=old.number; end$ 237 238 239 //root设置密码 ,密码为空和修改密码 240 mysql> use mysql; 241 mysql> UPDATE user SET password=PASSWORD("123456") WHERE user='root'; 242 mysql> FLUSH PRIVILEGES; 243 244 245 select name,href from rtfz where id=1 order by id desc limit 0,4; //倒序排列