数据库连接
//Mysql加载驱动
driver:com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/[database(数据库名字)]
username:root
password:root
//启动数据库
运行cmd命令页面 输入sqlplus
DML(数据库操作语言):insert、update、delete(添加、修改和删除表中的行)
DDL(数据库定义语言):create、drop(添加新表和删除表)
DCL(数据库控制语言):grant、revoke(授权和撤销)
单表查询
//emp员工表
//查询表
select * form emp;
//查询列
select empo,ename,job from emp;
//别名(可以用as 和 空格)
select empno as 员工编号 , ename as 员工姓名 from emp;
//去除重复的数据 distinct
select distinct jod from emp;
//字符串连接查询 用||符号
select '员工编号是' || empno || '姓名是' || ename || '的工作是' || job from emp;
//四则运算(+ - * /)
//每个员工的年薪
select ename,sal*12 from emp;
限定查询
//where条件查询
//查询员工工资大于1500
select * from emp where sal>1500;
//非空和空
//每个月能得到奖金的员工 is null(为空) is not null(不为空)
select * from emp where comm is not null;
//查询员工既有奖金并且工资大于1500的信息 AND(并且)
select * from where sal<1500 and comm is not null;
//查询工资大于1500或者有奖金的员工 OR(或者)
select * from emp where sal>1500 or comm is not null;
//查询工资不大于1500和没有奖金的人
select * from emp where sal<=1500 and comm is not null;
select * from emp where not(sal>1500 or comm is not null);
//查询员工工资大于1500并且小于3000的信息 between and
select * from emp sal>1500 and sal<3000;
select * from emp where sal between 1500 and 3000;
// 查询日期之间的数值
select * from emp where hiredate between to_date('1981/1/1','yyyy/mm/dd') and to_date('1981-12-31','yyyy-mm-dd');
//查询员工名字为SMITH的
//sql语句不区分大小写 但是表中的值是区分的
select * from emp where ename = 'SMITH';
// 查询员工编号为 7369 7499 7521的员工信息 in(包含)
select * from emp where empno = 7369 or empno=7499 or empno=7521
select * from emp where empno in(7369,7499,7521);
//查询员工编号不包含 7369 7499 7521 的员工信息 not in(不包含)
select * from emp where empno not in(7369,7499,7521);
//LIKE模糊查询 %可以匹配任意长度 “_” 可以匹配一个长度的内容
//查询名字中第二个字包含“M”的员工
select * from emp where ename like '_M%';
//查询名字中倒数第三个字母是M的员工名字
select * from emp where ename like '%M__';
//查询名字中包含M的名字员工
select * from emp where ename like '%M%';
//查询员工编号不是7369的员工信息 不等于(<> !=)
select * from emp where empno <> 7369;
select * from emp where empno !=7369;
对结果集排序
//查询员工工资从高到低排序 order by ASC(升序) DESC(降序)
select * from emp oder by sal asc;
//倒序 从低到高
select * from emp Oder by sal desc;
单行函数
1.字符函数
//把小写的字符转换成大写的字符 dual是伪装 upper() 转换大写
select upper ('smith') from dual;
//转换小写 lower() 转换小写
select lower('smith') from dual;