DQL表的准备

DQL:Data Query Language  数据查询语言

DQL主要用于数据的查询,其基本结构是使用select子句,from子句和where子句的组合来检查一条或多条数据。

首先准备四张表:dept(department部门表)emp(employee员工表)salgrade(工资等级表)bonus(奖金表)

创建部门表:

create table dept( -- 创建一个部门表
deptno int(2) not null,   -- 部门编号 不为空
dname varchar(14),     -- 部门姓名
loc varchar(13)      -- location地点
);

添加主键:

alter table dept -- 给部门表添加一个主键
add constraint pk_dept primary key (deptno);

添加数据:

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','bostno');

查看部门表:

select * from dept;

运行结果:

 

 

 

创建员工表:

create table emp( -- 创建一个员工表
empno int(4) primary key,  -- 员工编号 设置主键
ename varchar(10),           -- 员工名字
job varchar(9),                   -- 员工职位
mgr int(4),                          -- mgr(manager上级领导编号)
hiredate date,
sal double(7,2),               -- sal(salary公资)
comm double(7,2),          -- comm(common补助)
deptno int(2)                    -- 部门编号
);

添加外键:

alter table emp -- 给员工表添加一个外键 关联deptno
add constraint fk_deptno foreign key (deptno)    

-- deptno 外键 参考 dept-deptno字段
-- mgr 外键 参考 自身表emp-empno 产生了自关联


references dept(deptno);

添加数据:

insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno)
values (7369,'smith','clerk',7902,'1980-12-12',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,'clake','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,'1981-01-23',1300,null,10);

查看员工表:

select * from emp;

运行结果:

 

 

 

创建工资等级表:

create table salgrade( -- 工资等级表
grade int primary key,   -- grade等级 设置主键
losal double(7,2),          -- losal最低公资
hisal double(7,2)           -- hisal最高公资
);

添加数据:

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);

查看工资等级表:

select * from salgrade;

运行结果:

 

 

 

创建奖金表:

create table bonus( -- 奖金表
ename varchar(10),     -- 员工名字
job varchar(9),             -- 员工职位
sal double(7,2),             -- sal(salary公资)
comm double(7,2)        --  comm(common补助)
);

查看奖金表:

select * from bonus;

运行结果:

 

 

注:因为没有添加数据,所以为空

posted @ 2021-12-18 18:26  努力学爪哇  阅读(86)  评论(0编辑  收藏  举报