1.mysql常用命令
1显示当前mysql服务器上的所有数据库 2show databases; 3 4选择要使用的数据库 5use 数据库名; 6 7显示当前数据库中的所有表 8show tables; 9 10显示当前使用的数据库 11select database(); 12 13查看表结构 14desc 表名; 15 16查看表中所有数据 17select * from 表名;
2.数据库操作命令
1创建数据库同时指定默认的字符集 2create database 数据库名 default CHARACTER set utf8; 3 4删除数据库 5drop database 数据库名;
3.mysql常用数据类型
1整数: int bigint 2小数 decimal 长度 精度 decimal(18,2) 3字符 char(长度) varchar(长度) 4 char(8) 固定保存8位长度的字符串 5 varchar(8) 最多保存长度为8的字符串 6日期 date time datetime 7二进制 blob 8枚举 enum 从固定取值范围的数据中取一个 比如性别 9集合 set 从固定取值范围的数据中取一个或多个 比如不定项选择题的答案
4.表操作命令
1建表 2create table 表名 3( 4 列名 数据类型, 5 列名 数据类型, 6 ....... 7 列名 数据类型 8) 9 10删除表 11drop table 表名;
5.数据操作命令
5.1 增加数据
1给表中指定的列赋值 2insert into 表名(列名,列名,列名) values(值,值,值); 3给表中的所有列赋值 4insert into 表名 values(值,值,值,值,值);
5.2 删除数据
1delete from 表名 where 条件 2例子: 3删除所有答案是A的记录 4delete from question where answer='A' 5删除question表中的所有记录 6delete from question; 7删除答案带A的记录 8delete from question where ansswer like '%A%' 9删除答案不带A的记录 10delete from question where ansswer not like '%A%'
5.3 运算符
1关系运算符 2 > < >= <= = != <> 3逻辑运算符 4 and or not 5like 模糊匹配 6% 任意字符 '张%' 所有姓张的 7_ 一个任意字符 ‘张_’ 姓张的姓名只有两个字的 8
5.4修改数据
1update 表名 set 列名=值,列名=值 where 条件 2修改题目位3+2=?的A选项和B选项 3update question set optiona='aaaaaa',optionb='bbbbb' where title='3+2=?'
5.4 查询数据
1 简单筛选
select 列名,列名,列名 from 表名 where 条件
2.排序
select 列名,列名.... from 表名 where 条件 order by 列名 asc/desc;
3.模糊匹配
1like 2 3in 4select * from employees where emp_no in(499999,499572,499525); 5select * from employees where emp_no=499999 or emp_no=499572 or emp_no=499525; 6 7between and 8查询出生日期在1960-1-1 到 1960-7-1 之间的员工信息 9select * from employees where birth_date between '1960-1-1' and '1960-7-1'
4.聚合函数
1max 最大值 max(列名) max(表达式) 2 3min 最小值 4 5sum 总和 6 7avg 平均值 8 9count 计数 10 11 select max(列名) from 表名 where 条件 12 13error: select * from 表名 where max(emp_no); 14error: select * from 表名 where emp_no=max(emp_no) 15 16 select * from employees where emp_no = 17 ( 18 select max(emp_no) from employees 19 )
5. 分组
select count(*),gender from employees group by gender;
6.复杂查询(嵌套)
1select .... 2 from ..... 3 where ..... 在分组之前对数据进行筛选(不能使用聚合函数) 4 group by .... 5 having ..... 对分组后的数据进行筛选(可以使用聚合函数) 6 order by.......
7.联合查询
11. 内部联结 2A 3 select first_name,last_name,dept_name,from_date,to_date 4 from employees,departments,dept_emp 5 where employees.emp_no =dept_emp.emp_no 6 and dept_emp.dept_no=departments.dept_no 7 and employees.emp_no=10003 8 and now() between from_date and to_date; 9 10B 11select first_name,last_name,dept_name,from_date,to_date 12 from employees 13 inner join dept_emp on employees.emp_no=dept_emp.emp_no 14 inner join departments on departments.dept_no=dept_emp.dept_no 15 where employees.emp_no=10003 16 and now() between from_date and to_date; 17 2.左外部联结 18 left outer join 19 3.右外部联结 20 right outer join 21 4 完整外部连接 full join