DROP TABLE IF EXISTS EMP;
DROP TABLE IF EXISTS DEPT;
DROP TABLE IF EXISTS SALGRADE;
CREATE TABLE DEPT
(DEPTNO int(2) not null ,
DNAME VARCHAR(14) ,
LOC VARCHAR(13),
primary key (DEPTNO)
);
CREATE TABLE EMP
(EMPNO int(4) not null ,
ENAME VARCHAR(10),
JOB VARCHAR(9),
MGR INT(4),
HIREDATE DATE DEFAULT NULL,
SAL DOUBLE(7,2),
COMM DOUBLE(7,2),
primary key (EMPNO),
DEPTNO INT(2)
)
;
CREATE TABLE SALGRADE
( GRADE INT,
LOSAL INT,
HISAL INT );
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES (
10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES (
20, 'RESEARCH', 'DALLAS');
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES (
30, 'SALES', 'CHICAGO');
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES (
40, 'OPERATIONS', 'BOSTON');
commit;
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7369, 'SMITH', 'CLERK', 7902, '1980-12-17'
, 800, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7499, 'ALLEN', 'SALESMAN', 7698, '1981-02-20'
, 1600, 300, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7521, 'WARD', 'SALESMAN', 7698, '1981-02-22'
, 1250, 500, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7566, 'JONES', 'MANAGER', 7839, '1981-04-02'
, 2975, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7654, 'MARTIN', 'SALESMAN', 7698, '1981-09-28'
, 1250, 1400, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7698, 'BLAKE', 'MANAGER', 7839, '1981-05-01'
, 2850, NULL, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7782, 'CLARK', 'MANAGER', 7839, '1981-06-09'
, 2450, NULL, 10);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7788, 'SCOTT', 'ANALYST', 7566, '1987-04-19'
, 3000, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7839, 'KING', 'PRESIDENT', NULL, '1981-11-17'
, 5000, NULL, 10);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7844, 'TURNER', 'SALESMAN', 7698, '1981-09-08'
, 1500, 0, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7876, 'ADAMS', 'CLERK', 7788, '1987-05-23'
, 1100, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7900, 'JAMES', 'CLERK', 7698, '1981-12-03'
, 950, NULL, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7902, 'FORD', 'ANALYST', 7566, '1981-12-03'
, 3000, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7934, 'MILLER', 'CLERK', 7782, '1982-01-23'
, 1300, NULL, 10);
commit;
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES (
1, 700, 1200);
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES (
2, 1201, 1400);
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES (
3, 1401, 2000);
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES (
4, 2001, 3000);
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES (
5, 3001, 9999);
commit;
where和having都是为了完成数据的过滤,他们后面都是添加条件
where是在group by之前完成过滤
having是在group by之后完成过滤
内连接:取出两张表中匹配到的数据,匹配不到的不保留
外连接:取出连接表中匹配到的数据,匹配不到的也会保留,其值为NULL
左外连接:以左边的表为主表
右外连接:以右边的表为主表
查询每一个员工所在的部门名称,要求最终显示员工姓名和对应的部门名称(内连接:等值)
select e.ename,d.dname from emp e join dept d on e.deptno = d.deptno
找出每一个员工对应的部门名称,要求部门名称全部显示(左/右连接)
select e.ename,d.dname from emp e right outer join dept d on e.deptno = d.deptno;
select e.ename,d.dname from dept d left outer join emp e on e.deptno = d.deptno;
注意:任何一个右外连接都可写成左外连接,任何一个左外连接都可写成右外连接;
(where后)找出薪水比公司平均薪水高的员工,要求显示员工名和薪水
select ename,sal from emp where sal > (select avg(sal) from emp);
注意:合并结果集的时候,查询字段个数必须相同; 查询出job为MANAGER和SALESMAN的员工
select empno,ename,job from emp where job = 'MANAGER'
union
select empno,ename,job from emp where job = 'SALESMAN'
用法:limit起始下标m,长度n 找出工资排名前5的员工
select ename,sal from emp order by sal desc limit 5;
varchar比较智能,可以根据实际的数据长度分配空间,比较节省空间,但在分配的时候需要相关判断
char不需要动态分配空间,所以执行效率高,但是可能会导致空间浪费
若字段中的数据不具备伸缩性,建议采用char类型存储
若字段中的数据具有很强的伸缩性,建议采用varchar类型存储
字段包括: 学号:no INT(10)
姓名:name varchar(32)
性别:sex char(1)
出生日期:birth date
邮箱:email varchar(128)
建表语句:
create table t_student(
no int(10),
name varchar(32),
sex char(1),
birth date,
email varchar(128)
)
drop table t_student;
drop table if exists t_student;(推荐)
插入数据insert语法
insert into 表名(字段名1,字段名2,....) values(值1,值2); 注意:字段和数值必须一一对应,字段与数据个数必须相同,数据类型必须一致;
第一种方式:向表格中所有字段插入数据
insert into t_student(no,name,sex,birth,email) values(1,'zz','m','1970-01-01','zz');
第二种方式:向t_student表格中部分字段插入数据
insert into t_student(name,email) values('wangwu','[email protected]');
新增:alter table 表名 add 字段名 字段类型(长度);
修改:alter table 表名 modify 字段名 字段类型(长度);
删除:alter table 表名 drop 字段名; 创建表 编号:no int(10) 姓名:name varchar(32)
drop table if exists t_student;
create table t_student(
no int(10),
name varchar(32)
);
给表添加一个电话字段
alter table t_student add tel varchar(10);
将字段tel长度扩展到20个长度
alter table t_student modify tel varchar(20);
将t_student表中的tel字段删除
alter table t_student drop tel;
insert添加数据
语法格式:insert into 表名(字段名1,字段名2....) values(值1,值 2);
update修改数据
语法格式:update 表名 set 字段名 = 字段值,字段名 = 字段值 where 条件; 注意:update如果没有条件限制,将把整张表的数据全部更新;
update t_student set name = 'zhangsan',email = '[email protected]' where no = 3;
delete from 表名 where 条件限制
注意:若没有条件限制,会将表中所有记录全部删除;
delete from t_student where no = 3;